Skip to content

Commit

Permalink
fix: don't fail getting state on not installed modules
Browse files Browse the repository at this point in the history
  • Loading branch information
halamix2 committed Dec 31, 2024
1 parent 9d16ef4 commit 6bf75e3
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions internal/modules/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strconv"
"strings"

"github.com/kyma-project/cli.v3/internal/kube"
"github.com/kyma-project/cli.v3/internal/kube/kyma"
Expand Down Expand Up @@ -76,11 +77,16 @@ func List(ctx context.Context, client kube.Client) (ModulesList, error) {
),
}

state, err := getModuleState(ctx, client, moduleTemplate, defaultKyma)
if err != nil {
return nil, err
}
moduleInstalled := getModuleInstalledState(defaultKyma, moduleName)

state := ""
if moduleInstalled {
// only get state of installed modules
state, err = getModuleState(ctx, client, moduleTemplate, defaultKyma)
if err != nil {
return nil, err
}
}
if i := getModuleIndex(modulesList, moduleName); i != -1 {
// append version if module with same name is in the list
modulesList[i].Versions = append(modulesList[i].Versions, version)
Expand Down Expand Up @@ -144,7 +150,8 @@ func getStateFromData(ctx context.Context, client kube.Client, data unstructured
unstruct := generateUnstruct(apiVersion, kind, name, namespace)
result, err := client.RootlessDynamic().Get(ctx, &unstruct)
if err != nil {
if errors.IsNotFound(err) {
// ignore if resource or it's kind does not exist, the module may simply not be installed
if errors.IsNotFound(err) || strings.HasPrefix(err.Error(), "failed to discover API resource") {
return "", nil
}
return "", err
Expand All @@ -168,10 +175,10 @@ func getResourceState(ctx context.Context, client kube.Client, manager *kyma.Man
apiVersion := fmt.Sprintf("%s/%s", manager.Group, manager.Version)

unstruct := generateUnstruct(apiVersion, manager.Kind, manager.Name, namespace)

result, err := client.RootlessDynamic().Get(ctx, &unstruct)
if err != nil {
if errors.IsNotFound(err) {
// ignore if resource or it's kind does not exist, the module may simply not be installed
if errors.IsNotFound(err) || strings.HasPrefix(err.Error(), "failed to discover API resource") {
return "", nil
}
return "", err
Expand Down Expand Up @@ -245,6 +252,19 @@ func getStateFromConditions(conditions []interface{}) string {
return ""
}

func getModuleInstalledState(kyma *kyma.Kyma, moduleName string) bool {
if kyma != nil {
for _, module := range kyma.Status.Modules {
if module.Name == moduleName {
return true
}
}
}

// module is not installed
return false
}

func getInstallDetails(kyma *kyma.Kyma, releaseMetas kyma.ModuleReleaseMetaList, moduleName, state string) ModuleInstallDetails {
if kyma != nil {
for _, module := range kyma.Status.Modules {
Expand Down

0 comments on commit 6bf75e3

Please sign in to comment.