From 0e716239fb064c394d82b202a55e16b98234ad24 Mon Sep 17 00:00:00 2001 From: Parthvi Vala Date: Thu, 18 Aug 2022 00:51:41 +0530 Subject: [PATCH] odo list: add odo version used to create a component to the output (#6028) Signed-off-by: Parthvi Vala Signed-off-by: Parthvi Vala --- pkg/api/component-abstract.go | 9 ++-- pkg/component/component.go | 9 ++-- pkg/component/component_test.go | 53 ++++++++++++---------- pkg/labels/builder.go | 5 ++ pkg/labels/labels.go | 4 ++ pkg/odo/cli/list/list.go | 5 +- tests/integration/cmd_devfile_list_test.go | 20 ++++++++ 7 files changed, 74 insertions(+), 31 deletions(-) diff --git a/pkg/api/component-abstract.go b/pkg/api/component-abstract.go index 9e2382848b0..716645db896 100644 --- a/pkg/api/component-abstract.go +++ b/pkg/api/component-abstract.go @@ -2,10 +2,11 @@ package api // ComponentAbstract represents a component as part of a list of components type ComponentAbstract struct { - Name string `json:"name"` - ManagedBy string `json:"managedBy"` - RunningIn RunningModeList `json:"runningIn"` - Type string `json:"projectType"` + Name string `json:"name"` + ManagedBy string `json:"managedBy"` + ManagedByVersion string `json:"managedByVersion"` + RunningIn RunningModeList `json:"runningIn"` + Type string `json:"projectType"` } const ( diff --git a/pkg/component/component.go b/pkg/component/component.go index 2ea87742df0..c659886e23c 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -165,11 +165,14 @@ func ListAllClusterComponents(client kclient.ClientInterface, namespace string) continue } + managedByVersion := odolabels.GetManagedByVersion(labels) + // Generate the appropriate "component" with all necessary information component := api.ComponentAbstract{ - Name: name, - ManagedBy: managedBy, - Type: componentType, + Name: name, + ManagedBy: managedBy, + Type: componentType, + ManagedByVersion: managedByVersion, } mode := odolabels.GetMode(labels) componentFound := false diff --git a/pkg/component/component_test.go b/pkg/component/component_test.go index 0b44ada1fac..119b2d10a07 100644 --- a/pkg/component/component_test.go +++ b/pkg/component/component_test.go @@ -18,17 +18,19 @@ import ( ) func TestListAllClusterComponents(t *testing.T) { - res1 := getUnstructured("dep1", "deployment", "v1", "Unknown", "Unknown", "my-ns") - res2 := getUnstructured("svc1", "service", "v1", "odo", "nodejs", "my-ns") - res3 := getUnstructured("dep1", "deployment", "v1", "Unknown", "Unknown", "my-ns") + const odoVersion = "v3.0.0-beta3" + res1 := getUnstructured("dep1", "deployment", "v1", "Unknown", "", "Unknown", "my-ns") + res2 := getUnstructured("svc1", "service", "v1", "odo", odoVersion, "nodejs", "my-ns") + res3 := getUnstructured("dep1", "deployment", "v1", "Unknown", "", "Unknown", "my-ns") res3.SetLabels(map[string]string{}) - commonLabels := labels.Builder().WithComponentName("comp1").WithManager("odo") - resDev := getUnstructured("depDev", "deployment", "v1", "odo", "nodejs", "my-ns") + commonLabels := labels.Builder().WithComponentName("comp1").WithManager("odo").WithManagedByVersion(odoVersion) + + resDev := getUnstructured("depDev", "deployment", "v1", "odo", odoVersion, "nodejs", "my-ns") labelsDev := commonLabels.WithMode("Dev").Labels() resDev.SetLabels(labelsDev) - resDeploy := getUnstructured("depDeploy", "deployment", "v1", "odo", "nodejs", "my-ns") + resDeploy := getUnstructured("depDeploy", "deployment", "v1", "odo", odoVersion, "nodejs", "my-ns") labelsDeploy := commonLabels.WithMode("Deploy").Labels() resDeploy.SetLabels(labelsDeploy) @@ -61,10 +63,11 @@ func TestListAllClusterComponents(t *testing.T) { namespace: "my-ns", }, want: []api.ComponentAbstract{{ - Name: "dep1", - ManagedBy: "Unknown", - RunningIn: nil, - Type: "Unknown", + Name: "dep1", + ManagedBy: "Unknown", + ManagedByVersion: "", + RunningIn: nil, + Type: "Unknown", }}, wantErr: false, }, @@ -100,15 +103,17 @@ func TestListAllClusterComponents(t *testing.T) { namespace: "my-ns", }, want: []api.ComponentAbstract{{ - Name: "dep1", - ManagedBy: "Unknown", - RunningIn: nil, - Type: "Unknown", + Name: "dep1", + ManagedBy: "Unknown", + ManagedByVersion: "", + RunningIn: nil, + Type: "Unknown", }, { - Name: "svc1", - ManagedBy: "odo", - RunningIn: nil, - Type: "nodejs", + Name: "svc1", + ManagedBy: "odo", + ManagedByVersion: "v3.0.0-beta3", + RunningIn: nil, + Type: "nodejs", }}, wantErr: false, }, @@ -127,10 +132,11 @@ func TestListAllClusterComponents(t *testing.T) { namespace: "my-ns", }, want: []api.ComponentAbstract{{ - Name: "comp1", - ManagedBy: "odo", - RunningIn: api.RunningModeList{api.RunningModeDev, api.RunningModeDeploy}, - Type: "nodejs", + Name: "comp1", + ManagedBy: "odo", + ManagedByVersion: "v3.0.0-beta3", + RunningIn: api.RunningModeList{api.RunningModeDev, api.RunningModeDeploy}, + Type: "nodejs", }}, wantErr: false, }, @@ -185,7 +191,7 @@ func TestGetComponentTypeFromDevfileMetadata(t *testing.T) { } // getUnstructured returns an unstructured.Unstructured object -func getUnstructured(name, kind, apiVersion, managed, componentType, namespace string) (u unstructured.Unstructured) { +func getUnstructured(name, kind, apiVersion, managed, managedByVersion, componentType, namespace string) (u unstructured.Unstructured) { u.SetName(name) u.SetKind(kind) u.SetAPIVersion(apiVersion) @@ -193,6 +199,7 @@ func getUnstructured(name, kind, apiVersion, managed, componentType, namespace s u.SetLabels(labels.Builder(). WithComponentName(name). WithManager(managed). + WithManagedByVersion(managedByVersion). Labels()) u.SetAnnotations(labels.Builder(). WithProjectType(componentType). diff --git a/pkg/labels/builder.go b/pkg/labels/builder.go index ca6d9cf9c25..8e098b9a9e5 100644 --- a/pkg/labels/builder.go +++ b/pkg/labels/builder.go @@ -77,3 +77,8 @@ func (o builder) WithComponent(name string) builder { o.m[componentLabel] = name return o } + +func (o builder) WithManagedByVersion(version string) builder { + o.m[kubernetesManagedByVersionLabel] = version + return o +} diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 0704c2d245e..9efd4f8987e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -47,6 +47,10 @@ func GetManagedBy(labels map[string]string) string { return labels[kubernetesManagedByLabel] } +func GetManagedByVersion(labels map[string]string) string { + return labels[kubernetesManagedByVersionLabel] +} + func IsManagedByOdo(labels map[string]string) bool { return labels[kubernetesManagedByLabel] == odoManager } diff --git a/pkg/odo/cli/list/list.go b/pkg/odo/cli/list/list.go index 5d0c6703806..ecc3da53fa1 100644 --- a/pkg/odo/cli/list/list.go +++ b/pkg/odo/cli/list/list.go @@ -258,7 +258,10 @@ func humanReadableOutput(list api.ResourcesList) { // If we are managing that component, output it as blue (our logo colour) to indicate it's used by odo if managedBy == "odo" { - managedBy = text.Colors{text.FgBlue}.Sprint("odo") + managedBy = text.Colors{text.FgBlue}.Sprintf("odo (%s)", comp.ManagedByVersion) + } else if managedBy != "" && comp.ManagedByVersion != "" { + // this is done to maintain the color of the output + managedBy += fmt.Sprintf("(%s)", comp.ManagedByVersion) } t.AppendRow(table.Row{name, componentType, mode, managedBy}) diff --git a/tests/integration/cmd_devfile_list_test.go b/tests/integration/cmd_devfile_list_test.go index d45c1e8e92f..552cd6e62e2 100644 --- a/tests/integration/cmd_devfile_list_test.go +++ b/tests/integration/cmd_devfile_list_test.go @@ -3,6 +3,7 @@ package integration import ( "path" "path/filepath" + "regexp" devfilepkg "github.com/devfile/api/v2/pkg/devfile" "github.com/tidwall/gjson" @@ -112,6 +113,25 @@ var _ = Describe("odo list with devfile", func() { Expect(stdOut).To(ContainSubstring(componentType)) }) } + Context("verifying the managedBy Version in the odo list output", func() { + var version string + BeforeEach(func() { + versionOut := helper.Cmd("odo", "version").ShouldPass().Out() + reOdoVersion := regexp.MustCompile(`v[0-9]+.[0-9]+.[0-9]+(?:-\w+)?`) + version = reOdoVersion.FindString(versionOut) + + }) + It("should show managedBy Version", func() { + By("checking the normal output", func() { + stdout := helper.Cmd("odo", "list").ShouldPass().Out() + Expect(stdout).To(ContainSubstring(version)) + }) + By("checking the JSON output", func() { + stdout := helper.Cmd("odo", "list", "-o", "json").ShouldPass().Out() + helper.JsonPathContentContain(stdout, "components.0.managedByVersion", version) + }) + }) + }) It("show an odo deploy or dev in the list", func() { By("should display the component as 'Dev' in odo list", func() {