Skip to content

Commit

Permalink
Replace managed with channel (kyma-project#2186)
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel authored Jul 10, 2024
1 parent 60738ed commit 6b69462
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 46 deletions.
4 changes: 2 additions & 2 deletions internal/communitymodules/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 8 additions & 7 deletions internal/communitymodules/merge_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package communitymodules

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestMergeRowMaps(t *testing.T) {
Expand All @@ -25,7 +26,7 @@ func TestMergeRowMaps(t *testing.T) {
moduleMapManaged := moduleMap{
"serverless": {
Name: "serverless",
Managed: "Managed",
Channel: "Managed",
},
}
moduleMapInstalled := moduleMap{
Expand All @@ -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",
},
}
Expand All @@ -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: "",
},
}
Expand All @@ -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",
Expand Down
30 changes: 13 additions & 17 deletions internal/communitymodules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type row struct {
Repository string
LatestVersion string
Version string
Managed string
Channel string
}

type moduleMap map[string]row
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand Down
25 changes: 14 additions & 11 deletions internal/communitymodules/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
},
}

Expand Down Expand Up @@ -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: "",
},
}

Expand All @@ -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",
},
}

Expand Down Expand Up @@ -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"
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions internal/communitymodules/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
11 changes: 6 additions & 5 deletions internal/communitymodules/render_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package communitymodules

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestRender(t *testing.T) {
Expand All @@ -12,7 +13,7 @@ func TestRender(t *testing.T) {
Repository: "testRepo",
LatestVersion: "testLatest",
Version: "testVer",
Managed: "testMan",
Channel: "testMan",
},
}
var moduleMapEmpty = moduleMap{}
Expand All @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b69462

Please sign in to comment.