From df3380dcb912293ac470d86ec2ad063b67befaf3 Mon Sep 17 00:00:00 2001 From: Noe Luaces Date: Mon, 16 Dec 2024 15:42:25 +0100 Subject: [PATCH] show running pods version (#1835) * show running pods version --- internal/cmd/skupper/version/kube/version.go | 24 +++++- pkg/images/image_utils.go | 88 +++++++++++++++----- pkg/utils/configs/manifest.go | 39 ++++++++- 3 files changed, 124 insertions(+), 27 deletions(-) diff --git a/internal/cmd/skupper/version/kube/version.go b/internal/cmd/skupper/version/kube/version.go index f120f728b..4af3f6f05 100644 --- a/internal/cmd/skupper/version/kube/version.go +++ b/internal/cmd/skupper/version/kube/version.go @@ -1,7 +1,9 @@ package kube import ( + "context" "fmt" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os" "text/tabwriter" @@ -38,6 +40,7 @@ func (cmd *CmdVersion) NewClient(cobraCommand *cobra.Command, args []string) { if err == nil { cmd.namespace = cli.Namespace + cmd.KubeClient = cli.GetKubeClient() } } @@ -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} } } diff --git a/pkg/images/image_utils.go b/pkg/images/image_utils.go index 7fbcc1471..de3ab7765 100644 --- a/pkg/images/image_utils.go +++ b/pkg/images/image_utils.go @@ -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 @@ -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() @@ -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 @@ -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": @@ -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 "" diff --git a/pkg/utils/configs/manifest.go b/pkg/utils/configs/manifest.go index dd788991f..1af6ce550 100644 --- a/pkg/utils/configs/manifest.go +++ b/pkg/utils/configs/manifest.go @@ -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 { @@ -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 { @@ -42,7 +43,7 @@ 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 { @@ -50,6 +51,11 @@ func getSkupperConfiguredImages(components []string, enableSHA bool) SkupperMani 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) } @@ -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 +}