Skip to content

Commit

Permalink
show running pods version (#1835)
Browse files Browse the repository at this point in the history
* show running pods version
  • Loading branch information
nluaces authored Dec 16, 2024
1 parent c4ef199 commit df3380d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 27 deletions.
24 changes: 22 additions & 2 deletions internal/cmd/skupper/version/kube/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kube

import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"os"
"text/tabwriter"

Expand Down Expand Up @@ -38,6 +40,7 @@ func (cmd *CmdVersion) NewClient(cobraCommand *cobra.Command, args []string) {

if err == nil {
cmd.namespace = cli.Namespace
cmd.KubeClient = cli.GetKubeClient()
}
}

Expand All @@ -58,10 +61,27 @@ func (cmd *CmdVersion) ValidateInput(args []string) []error {
}

func (cmd *CmdVersion) InputToOptions() {

mapRunningPods := make(map[string]string)

if cmd.KubeClient != nil {
// search for running pods in the current namespace
runningPodList, err := cmd.KubeClient.CoreV1().Pods(cmd.namespace).List(context.Background(), metav1.ListOptions{LabelSelector: "app.kubernetes.io/part-of in (skupper, skupper-network-observer)"})
if err != nil {
return
}

for _, runningPod := range runningPodList.Items {
for _, container := range runningPod.Status.ContainerStatuses {
mapRunningPods[container.Name] = container.Image
}
}
}

if cmd.output != "" {
cmd.manifest = configs.ManifestManager{Components: images.KubeComponents, EnableSHA: true}
cmd.manifest = configs.ManifestManager{Components: images.KubeComponents, EnableSHA: true, RunningPods: mapRunningPods}
} else {
cmd.manifest = configs.ManifestManager{Components: images.KubeComponents, EnableSHA: false}
cmd.manifest = configs.ManifestManager{Components: images.KubeComponents, EnableSHA: false, RunningPods: mapRunningPods}
}

}
Expand Down
88 changes: 67 additions & 21 deletions pkg/images/image_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ func GetImage(imageNames map[string]string, imageRegistry string, enableSHA bool
var image SkupperImage
var skupperImage []SkupperImage

for key, name := range imageNames {
imageName := os.Getenv(key)
if imageName == "" {
for _, name := range imageNames {
imageName := name
if imageRegistry != "" {
imageName = strings.Join([]string{imageRegistry, name}, "/")
}
image.Name = imageName
Expand All @@ -233,15 +233,43 @@ func GetImages(component string, enableSHA bool) []SkupperImage {
switch component {
case "router":
// skupper router has two components
names[RouterImageEnvKey] = RouterImageName
names[AdaptorImageEnvKey] = AdaptorImageName
registry = GetImageRegistry()
envImage := os.Getenv(RouterImageEnvKey)

if envImage != "" {
names[RouterImageEnvKey] = envImage
} else {
names[RouterImageEnvKey] = RouterImageName
registry = GetImageRegistry()
}

envImage = os.Getenv(AdaptorImageEnvKey)
if envImage != "" {
names[AdaptorImageEnvKey] = envImage
} else {
names[AdaptorImageEnvKey] = AdaptorImageName
registry = GetImageRegistry()
}
case "controller":
names[ControllerImageEnvKey] = ControllerImageName
registry = GetImageRegistry()
envImage := os.Getenv(ControllerImageEnvKey)

if envImage != "" {
names[ControllerImageEnvKey] = envImage
} else {
names[ControllerImageEnvKey] = ControllerImageName
registry = GetImageRegistry()
}

case "network-observer":
names[NetworkObserverImageEnvKey] = NetworkObserverImageName
registry = GetImageRegistry()

envImage := os.Getenv(NetworkObserverImageEnvKey)

if envImage != "" {
names[NetworkObserverImageEnvKey] = envImage
} else {
names[NetworkObserverImageEnvKey] = NetworkObserverImageName
registry = GetImageRegistry()
}

case "cli":
names[CliImageEnvKey] = CliImageName
registry = GetImageRegistry()
Expand All @@ -253,7 +281,7 @@ func GetImages(component string, enableSHA bool) []SkupperImage {
registry = GetOauthProxyImageRegistry()
}

if names != nil && registry != "" {
if names != nil {
return GetImage(names, registry, enableSHA)
} else {
return nil
Expand All @@ -265,18 +293,31 @@ func GetImageVersion(component string) string {

switch component {
case "router":
image = os.Getenv(RouterImageEnvKey)
if image == "" {
envImage := os.Getenv(RouterImageEnvKey)

if envImage != "" {
image = envImage
} else {
image = RouterImageName
}

case "controller":
image = os.Getenv(ControllerImageEnvKey)
if image == "" {

envImage := os.Getenv(ControllerImageEnvKey)

if envImage != "" {
image = envImage
} else {
image = ControllerImageName
}

case "network-observer":
image = os.Getenv(ControllerImageEnvKey)
if image == "" {

envImage := os.Getenv(NetworkObserverImageEnvKey)

if envImage != "" {
image = envImage
} else {
image = NetworkObserverImageName
}
case "cli":
Expand All @@ -296,10 +337,15 @@ func GetImageVersion(component string) string {
}
}
if image != "" {
parts := strings.Split(image, ":")
if len(parts) == 2 {
return parts[1]
}
return GetVersionFromTag(image)
}
return ""
}

func GetVersionFromTag(image string) string {
parts := strings.Split(image, ":")
if len(parts) == 2 {
return parts[1]
}

return ""
Expand Down
39 changes: 35 additions & 4 deletions pkg/utils/configs/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type Manifest struct {
}

type ManifestManager struct {
EnableSHA bool
Components []string
EnableSHA bool
Components []string
RunningPods map[string]string
}

type ManifestGenerator interface {
Expand All @@ -32,7 +33,7 @@ type ManifestGenerator interface {
}

func (manager *ManifestManager) GetConfiguredManifest() SkupperManifest {
return getSkupperConfiguredImages(manager.Components, manager.EnableSHA)
return getSkupperImages(manager.Components, manager.EnableSHA, manager.RunningPods)
}

func (manager *ManifestManager) GetDefaultManifestWithEnv() Manifest {
Expand All @@ -42,14 +43,19 @@ func (manager *ManifestManager) GetDefaultManifestWithEnv() Manifest {
}
}

func getSkupperConfiguredImages(components []string, enableSHA bool) SkupperManifest {
func getSkupperImages(components []string, enableSHA bool, runningPods map[string]string) SkupperManifest {
var manifest SkupperManifest

for _, component := range components {
var image SkupperComponent
image.Component = component
image.Version = images.GetImageVersion(component)
image.Images = images.GetImages(component, enableSHA)
if runningPods[component] != "" {
image.Version = images.GetVersionFromTag(runningPods[component])
image.Images = GetRunningImages(component, enableSHA, runningPods)
}

manifest.Components = append(manifest.Components, image)
}

Expand Down Expand Up @@ -126,3 +132,28 @@ func getEnvironmentVariableMap() *map[string]string {

return &envVariables
}

func GetRunningImages(component string, enableSHA bool, runningPods map[string]string) []images.SkupperImage {

names := make(map[string]string)

switch component {
case "router":
// skupper router has two components
names[images.RouterImageEnvKey] = runningPods["router"]
names[images.AdaptorImageEnvKey] = runningPods["kube-adaptor"]

case "controller":
names[images.ControllerImageEnvKey] = runningPods["controller"]

case "network-observer":

names[images.NetworkObserverImageEnvKey] = runningPods["network-observer"]
}

if names != nil {
return images.GetImage(names, "", enableSHA)
}

return nil
}

0 comments on commit df3380d

Please sign in to comment.