Skip to content

Commit

Permalink
odo list: add odo version used to create a component to the output (#…
Browse files Browse the repository at this point in the history
…6028)

Signed-off-by: Parthvi Vala <[email protected]>

Signed-off-by: Parthvi Vala <[email protected]>
  • Loading branch information
valaparthvi authored Aug 17, 2022
1 parent 784ad58 commit 0e71623
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 31 deletions.
9 changes: 5 additions & 4 deletions pkg/api/component-abstract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
9 changes: 6 additions & 3 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 30 additions & 23 deletions pkg/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -185,14 +191,15 @@ 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)
u.SetNamespace(namespace)
u.SetLabels(labels.Builder().
WithComponentName(name).
WithManager(managed).
WithManagedByVersion(managedByVersion).
Labels())
u.SetAnnotations(labels.Builder().
WithProjectType(componentType).
Expand Down
5 changes: 5 additions & 0 deletions pkg/labels/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/odo/cli/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
20 changes: 20 additions & 0 deletions tests/integration/cmd_devfile_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package integration
import (
"path"
"path/filepath"
"regexp"

devfilepkg "github.com/devfile/api/v2/pkg/devfile"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 0e71623

Please sign in to comment.