From 6b69462e1b3f3b167df9d0268b52f0e6b0564cbe Mon Sep 17 00:00:00 2001 From: Filip Strozik Date: Wed, 10 Jul 2024 21:32:27 +0200 Subject: [PATCH] Replace managed with channel (#2186) --- internal/communitymodules/merge.go | 4 +-- internal/communitymodules/merge_test.go | 15 ++++++------ internal/communitymodules/modules.go | 30 ++++++++++------------- internal/communitymodules/modules_test.go | 25 ++++++++++--------- internal/communitymodules/render.go | 8 +++--- internal/communitymodules/render_test.go | 11 +++++---- 6 files changed, 47 insertions(+), 46 deletions(-) diff --git a/internal/communitymodules/merge.go b/internal/communitymodules/merge.go index 37cc16330..fb5bb00ad 100644 --- a/internal/communitymodules/merge.go +++ b/internal/communitymodules/merge.go @@ -28,8 +28,8 @@ func mergeTwoRows(a row, b row) row { if result.Version == "" { result.Version = b.Version } - if result.Managed == "" { - result.Managed = b.Managed + if result.Channel == "" { + result.Channel = b.Channel } return result } diff --git a/internal/communitymodules/merge_test.go b/internal/communitymodules/merge_test.go index ea213adec..65304d89e 100644 --- a/internal/communitymodules/merge_test.go +++ b/internal/communitymodules/merge_test.go @@ -1,8 +1,9 @@ package communitymodules import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestMergeRowMaps(t *testing.T) { @@ -25,7 +26,7 @@ func TestMergeRowMaps(t *testing.T) { moduleMapManaged := moduleMap{ "serverless": { Name: "serverless", - Managed: "Managed", + Channel: "Managed", }, } moduleMapInstalled := moduleMap{ @@ -38,7 +39,7 @@ func TestMergeRowMaps(t *testing.T) { "serverless": { Name: "serverless", Repository: "github.com/kyma-project/serverless", - Managed: "Managed", + Channel: "Managed", Version: "1.0.0", }, } @@ -54,13 +55,13 @@ func TestMergeRowMaps(t *testing.T) { "serverless": { Name: "serverless", Repository: "github.com/kyma-project/serverless", - Managed: "Managed", + Channel: "Managed", Version: "1.0.0", }, "istio": { Name: "istio", Repository: "github.com/kyma-project/istio", - Managed: "", + Channel: "", Version: "", }, } @@ -86,12 +87,12 @@ func TestMergeTwoRows(t *testing.T) { } var rowB = row{ Name: "serverless", - Managed: "Managed", + Channel: "Managed", } var rowResult = row{ Name: "serverless", Repository: "github.com/kyma-project/serverless", - Managed: "Managed", + Channel: "Managed", } var rowC = row{ Name: "serverless", diff --git a/internal/communitymodules/modules.go b/internal/communitymodules/modules.go index 247bc011c..0ea61b235 100644 --- a/internal/communitymodules/modules.go +++ b/internal/communitymodules/modules.go @@ -26,7 +26,7 @@ type row struct { Repository string LatestVersion string Version string - Managed string + Channel string } type moduleMap map[string]row @@ -116,23 +116,24 @@ func decodeCommunityModulesResponse(err error, resp *http.Response, modules Modu // ManagedModules returns a map of all managed modules from the cluster func ManagedModules(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig) (moduleMap, clierror.Error) { - moduleNames, err := getManagedList(client, cfg) + modules, err := getManagedList(client, cfg) if err != nil { return nil, clierror.WrapE(err, clierror.New("while getting managed modules")) } managed := make(moduleMap) - for _, name := range moduleNames { - managed[name] = row{ - Name: name, - Managed: "True", + for _, module := range modules { + managed[module.Name] = row{ + Name: module.Name, + Channel: module.Channel, + Version: module.Version, } } return managed, nil } // getManagedList gets a list of all managed modules from the Kyma CR -func getManagedList(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig) ([]string, clierror.Error) { +func getManagedList(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig) ([]kyma.ModuleStatus, clierror.Error) { resp, err := kyma.GetDefaultKyma(cfg.Ctx, client.KubeClient) if err != nil && !errors.IsNotFound(err) { return nil, clierror.Wrap(err, clierror.New("while getting Kyma CR")) @@ -141,27 +142,22 @@ func getManagedList(client cmdcommon.KubeClientConfig, cfg cmdcommon.KymaConfig) return nil, nil } - moduleNames, err := decodeKymaCRResponse(resp) + modules, err := decodeKymaCRResponse(resp) if err != nil { return nil, clierror.Wrap(err, clierror.New("while getting module names from CR")) } - return moduleNames, nil + return modules, nil } -// decodeKymaCRResponse interprets the response and returns a list of managed modules names -func decodeKymaCRResponse(unstruct *unstructured.Unstructured) ([]string, error) { - var moduleNames []string - +// decodeKymaCRResponse interprets the response and returns a list of managed modules +func decodeKymaCRResponse(unstruct *unstructured.Unstructured) ([]kyma.ModuleStatus, error) { kyma := &kyma.Kyma{} err := runtime.DefaultUnstructuredConverter.FromUnstructured(unstruct.Object, kyma) if err != nil { return nil, err } - for _, module := range kyma.Spec.Modules { - moduleNames = append(moduleNames, module.Name) - } - return moduleNames, nil + return kyma.Status.Modules, nil } // InstalledModules returns a map of all installed modules from the cluster, regardless whether they are managed or not diff --git a/internal/communitymodules/modules_test.go b/internal/communitymodules/modules_test.go index 2e7998ba6..3b4403c67 100644 --- a/internal/communitymodules/modules_test.go +++ b/internal/communitymodules/modules_test.go @@ -29,14 +29,14 @@ func Test_modulesCatalog(t *testing.T) { Repository: "https://repo2/path/module1.git", LatestVersion: "1.7.0", Version: "", - Managed: "", + Channel: "", }, "module2": row{ Name: "module2", Repository: "https://repo/path/module2.git", LatestVersion: "4.5.6", Version: "", - Managed: "", + Channel: "", }, } @@ -74,14 +74,14 @@ func Test_modulesCatalog(t *testing.T) { Repository: "https://repo2/path/module1.git", LatestVersion: "1.7.0", Version: "", - Managed: "", + Channel: "", }, "module2": row{ Name: "module2", Repository: "https://repo/path/module2.git", LatestVersion: "4.5.6", Version: "", - Managed: "", + Channel: "", }, } @@ -98,15 +98,15 @@ func Test_ManagedModules(t *testing.T) { expectedResult := moduleMap{ "module1": row{ Name: "module1", - Managed: "True", + Channel: "fast", }, "module2": row{ Name: "module2", - Managed: "True", + Channel: "fast", }, "module3": row{ Name: "module3", - Managed: "True", + Channel: "regular", }, } @@ -249,16 +249,19 @@ func fixTestKyma() *unstructured.Unstructured { "name": "default", "namespace": "kyma-system" }, - "spec": { + "status": { "modules": [ { - "name": "module1" + "name": "module1", + "channel": "fast" }, { - "name": "module3" + "name": "module3", + "channel": "regular" }, { - "name": "module2" + "name": "module2", + "channel": "fast" } ] } diff --git a/internal/communitymodules/render.go b/internal/communitymodules/render.go index 0b17665fa..243beb690 100644 --- a/internal/communitymodules/render.go +++ b/internal/communitymodules/render.go @@ -16,16 +16,16 @@ type TableInfo struct { var ( CollectiveTableInfo = TableInfo{ - Header: []string{"NAME", "REPOSITORY", "VERSION INSTALLED", "MANAGED"}, - RowConverter: func(r row) []string { return []string{r.Name, r.Repository, r.Version, r.Managed} }, + Header: []string{"NAME", "REPOSITORY", "VERSION INSTALLED", "CHANNEL"}, + RowConverter: func(r row) []string { return []string{r.Name, r.Repository, r.Version, r.Channel} }, } InstalledTableInfo = TableInfo{ Header: []string{"NAME", "VERSION"}, RowConverter: func(r row) []string { return []string{r.Name, r.Version} }, } ManagedTableInfo = TableInfo{ - Header: []string{"NAME"}, - RowConverter: func(r row) []string { return []string{r.Name} }, + Header: []string{"NAME", "VERSION", "CHANNEL"}, + RowConverter: func(r row) []string { return []string{r.Name, r.Version, r.Channel} }, } CatalogTableInfo = TableInfo{ Header: []string{"NAME", "REPOSITORY", "LATEST VERSION"}, diff --git a/internal/communitymodules/render_test.go b/internal/communitymodules/render_test.go index 9705f237b..9b3280d0f 100644 --- a/internal/communitymodules/render_test.go +++ b/internal/communitymodules/render_test.go @@ -1,8 +1,9 @@ package communitymodules import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestRender(t *testing.T) { @@ -12,7 +13,7 @@ func TestRender(t *testing.T) { Repository: "testRepo", LatestVersion: "testLatest", Version: "testVer", - Managed: "testMan", + Channel: "testMan", }, } var moduleMapEmpty = moduleMap{} @@ -23,14 +24,14 @@ func TestRender(t *testing.T) { Repository: "testRepo1", LatestVersion: "testLatest1", Version: "testVer1", - Managed: "testMan1", + Channel: "testMan1", }, "test2": { Name: "testName2", Repository: "testRepo2", LatestVersion: "testLatest2", Version: "testVer2", - Managed: "testMan2", + Channel: "testMan2", }, } var testMapSort = moduleMap{ @@ -62,7 +63,7 @@ func TestRender(t *testing.T) { require.Equal(t, [][]string{{"testName", "testLatest", "testVer"}}, result) }) t.Run("convertRowToCatalog for map with mutliple entries", func(t *testing.T) { - result := convertModuleMapToTable(testMapLong, func(r row) []string { return []string{r.Repository, r.LatestVersion, r.Managed} }) + result := convertModuleMapToTable(testMapLong, func(r row) []string { return []string{r.Repository, r.LatestVersion, r.Channel} }) require.ElementsMatch(t, [][]string{{"testRepo1", "testLatest1", "testMan1"}, {"testRepo2", "testLatest2", "testMan2"}}, result) }) t.Run("sort names", func(t *testing.T) {