Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/modules/compability.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func listOldModulesCatalog(moduleTemplates *kyma.ModuleTemplateList) ModulesList
continue
}

if i := getModuleIndex(modulesList, componentInfo.Name); i != -1 {
if i := getModuleIndex(modulesList, componentInfo.Name, false); i != -1 {
// append version if module with same name is in the list
modulesList[i].Versions = append(modulesList[i].Versions, version)
} else {
Expand Down
19 changes: 13 additions & 6 deletions internal/modules/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

type Module struct {
Name string
Versions []ModuleVersion
InstallDetails ModuleInstallDetails
Name string
Versions []ModuleVersion
InstallDetails ModuleInstallDetails
CommunityModule bool
}

type Managed string
Expand Down Expand Up @@ -108,7 +109,7 @@ func ListCatalog(ctx context.Context, client kube.Client) (ModulesList, error) {
),
}

if i := getModuleIndex(modulesList, moduleName); i != -1 {
if i := getModuleIndex(modulesList, moduleName, isCommunityModule(&moduleTemplate)); i != -1 {
// append version if module with same name is in the list
modulesList[i].Versions = append(modulesList[i].Versions, version)
} else {
Expand All @@ -118,13 +119,19 @@ func ListCatalog(ctx context.Context, client kube.Client) (ModulesList, error) {
Versions: []ModuleVersion{
version,
},
CommunityModule: isCommunityModule(&moduleTemplate),
})
}
}

return modulesList, nil
}

func isCommunityModule(moduleTemplate *kyma.ModuleTemplate) bool {
managedBy, exist := moduleTemplate.ObjectMeta.Labels["operator.kyma-project.io/managed-by"]
return !exist || managedBy != "kyma"
}

func getManaged(moduleSpec *kyma.Module) Managed {
if moduleSpec != nil && moduleSpec.Managed != nil {
return Managed(strconv.FormatBool(*moduleSpec.Managed))
Expand Down Expand Up @@ -326,9 +333,9 @@ func getChannelFromAssignments(assignments []kyma.ChannelVersionAssignment, vers
}

// return index of module with given name. if not exists return -1
func getModuleIndex(list ModulesList, name string) int {
func getModuleIndex(list ModulesList, name string, isCommunityModule bool) int {
for i := range list {
if list[i].Name == name {
if list[i].Name == name && list[i].CommunityModule == isCommunityModule {
return i
}
}
Expand Down
17 changes: 17 additions & 0 deletions internal/modules/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var (
"metadata": map[string]interface{}{
"name": "serverless-1",
"namespace": "kyma-system",
"labels": map[string]interface{}{
"operator.kyma-project.io/managed-by": "kyma",
},
},
"spec": map[string]interface{}{
"moduleName": "serverless",
Expand All @@ -44,6 +47,9 @@ var (
"metadata": map[string]interface{}{
"name": "serverless-2",
"namespace": "kyma-system",
"labels": map[string]interface{}{
"operator.kyma-project.io/managed-by": "kyma",
},
},
"spec": map[string]interface{}{
"moduleName": "serverless",
Expand All @@ -62,6 +68,9 @@ var (
"metadata": map[string]interface{}{
"name": "keda-1",
"namespace": "kyma-system",
"labels": map[string]interface{}{
"operator.kyma-project.io/managed-by": "kyma",
},
},
"spec": map[string]interface{}{
"moduleName": "keda",
Expand All @@ -80,6 +89,9 @@ var (
"metadata": map[string]interface{}{
"name": "keda-2",
"namespace": "kyma-system",
"labels": map[string]interface{}{
"operator.kyma-project.io/managed-by": "kyma",
},
},
"spec": map[string]interface{}{
"moduleName": "keda",
Expand All @@ -96,6 +108,9 @@ var (
"metadata": map[string]interface{}{
"name": "keda-3",
"namespace": "kyma-system",
"labels": map[string]interface{}{
"operator.kyma-project.io/managed-by": "kyma",
},
},
},
}
Expand Down Expand Up @@ -223,6 +238,7 @@ var (
Channel: "fast",
},
},
CommunityModule: false,
},
{
Name: "serverless",
Expand All @@ -237,6 +253,7 @@ var (
Version: "0.0.2",
},
},
CommunityModule: false,
},
}

Expand Down
13 changes: 8 additions & 5 deletions internal/modules/render.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package modules

import (
"cmp"
"encoding/json"
"fmt"
"io"
"os"
"slices"
"sort"
"strings"

"github.com/kyma-project/cli.v3/internal/output"
Expand Down Expand Up @@ -36,11 +35,12 @@ var (
}

CatalogTableInfo = TableInfo{
Headers: []interface{}{"NAME", "AVAILABLE VERSIONS"},
Headers: []interface{}{"NAME", "AVAILABLE VERSIONS", "COMMUNITY"},
RowConverter: func(m Module) []interface{} {
return []interface{}{
m.Name,
convertVersions(m.Versions),
m.CommunityModule,
}
},
}
Expand Down Expand Up @@ -101,8 +101,11 @@ func convertToOutputParameters(modulesList ModulesList, tableInfo TableInfo) []m
}

func convertModuleListToRows(modulesList ModulesList, rowConverter RowConverter) [][]interface{} {
slices.SortFunc(modulesList, func(a, b Module) int {
return cmp.Compare(a.Name, b.Name)
sort.Slice(modulesList, func(i, j int) bool {
if modulesList[i].CommunityModule == modulesList[j].CommunityModule {
return modulesList[i].Name < modulesList[j].Name
}
return !modulesList[i].CommunityModule && modulesList[j].CommunityModule
})

var result [][]interface{}
Expand Down
57 changes: 53 additions & 4 deletions internal/modules/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,59 @@ import (
"github.com/stretchr/testify/require"
)

var testModules = []Module{
{
Name: "keda",
Versions: []ModuleVersion{
{
Repository: "url-3",
Version: "0.1",
Channel: "regular",
},
{
Version: "0.2",
Channel: "fast",
},
},
CommunityModule: false,
},
{
Name: "serverless",
Versions: []ModuleVersion{
{
Repository: "url-1",
Version: "0.0.1",
Channel: "fast",
},
{
Repository: "url-2",
Version: "0.0.2",
},
},
CommunityModule: false,
},
{
Name: "cluster-ip",
Versions: []ModuleVersion{
{
Repository: "url-1",
Version: "0.1.1",
Channel: "",
},
{
Repository: "url-2",
Version: "0.1.2",
},
},
CommunityModule: true,
},
}

const (
testCatalogTableView = "NAME AVAILABLE VERSIONS \n" +
"keda 0.1(regular), 0.2(fast) \n" +
"serverless 0.0.1(fast), 0.0.2 \n"
testCatalogTableView = "NAME AVAILABLE VERSIONS COMMUNITY \n" +
"keda 0.1(regular), 0.2(fast) false \n" +
"serverless 0.0.1(fast), 0.0.2 false \n" +
"cluster-ip 0.1.1, 0.1.2 true \n"
testInstalledModulesTableView = "NAME VERSION CR POLICY MANAGED STATUS \n" +
"keda 0.2(fast) CreateAndDelete false Unmanaged \n" +
"serverless 0.0.1(fast) Ignore true Ready \n"
Expand All @@ -21,7 +70,7 @@ func TestRender(t *testing.T) {
t.Run("render table from modules catalog", func(t *testing.T) {
buffer := bytes.NewBuffer([]byte{})

err := renderTable(buffer, testModuleList, CatalogTableInfo)
err := renderTable(buffer, testModules, CatalogTableInfo)
require.NoError(t, err)

tableViewBytes, err := io.ReadAll(buffer)
Expand Down
Loading