From 2f8a12dd8679ab47cf77bcc13ce5936fc60c0e55 Mon Sep 17 00:00:00 2001 From: MichalKalke Date: Mon, 3 Jun 2024 14:08:13 +0200 Subject: [PATCH 1/6] Add managed flag --- internal/cmd/kyma.go | 2 +- internal/cmd/modules/modules.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/internal/cmd/kyma.go b/internal/cmd/kyma.go index 9444e4672..4793a1872 100644 --- a/internal/cmd/kyma.go +++ b/internal/cmd/kyma.go @@ -40,7 +40,7 @@ func NewKymaCMD() *cobra.Command { cmd.AddCommand(referenceinstance.NewReferenceInstanceCMD(config)) cmd.AddCommand(access.NewAccessCMD(config)) cmd.AddCommand(oidc.NewOIDCCMD(config)) - cmd.AddCommand(modules.NewModulesCMD()) + cmd.AddCommand(modules.NewModulesCMD(config)) return cmd } diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index f1b6bc181..2ab43fabe 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -3,6 +3,7 @@ package modules import ( "encoding/json" "fmt" + "github.com/kyma-project/cli.v3/internal/cmdcommon" "io" "net/http" @@ -11,19 +12,28 @@ import ( ) type modulesConfig struct { + *cmdcommon.KymaConfig + cmdcommon.KubeClientConfig + catalog bool managed bool installed bool } -func NewModulesCMD() *cobra.Command { +func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { - config := modulesConfig{} + config := modulesConfig{ + KymaConfig: kymaConfig, + KubeClientConfig: cmdcommon.KubeClientConfig{}, + } cmd := &cobra.Command{ Use: "modules", Short: "List modules.", Long: `List either installed, managed or available Kyma modules.`, + PreRun: func(_ *cobra.Command, args []string) { + clierror.Check(config.KubeClientConfig.Complete()) + }, Run: func(_ *cobra.Command, _ []string) { clierror.Check(runModules(&config)) }, @@ -55,10 +65,17 @@ func runModules(config *modulesConfig) clierror.Error { return nil } - if config.managed || config.installed { + if config.managed { + _, err := listManagedModules(config) + clierror.WrapE(err, clierror.New("not implemented yet, please use the catalog flag")) + return nil + } + + if config.installed { clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog flag")) + return nil } - //TODO: installed and managed to implement + //TODO: installed to implement return clierror.Wrap(err, clierror.New("failed to get modules", "please use one of: catalog, managed or installed flags")) } @@ -89,3 +106,9 @@ func listAllModules() ([]string, clierror.Error) { } return out, nil } + +func listManagedModules(config *modulesConfig) ([]string, clierror.Error) { + trololo := config.KubeClient.Static().CoreV1().RESTClient().Get().AbsPath("kyma-project.io") + fmt.Println(trololo) + return nil, clierror.New("chleb") +} From ba96111bd3b0400a1250bd782f338b7052cd3b8c Mon Sep 17 00:00:00 2001 From: MichalKalke Date: Mon, 3 Jun 2024 14:42:02 +0200 Subject: [PATCH 2/6] Add new flag managed to modules command --- internal/cmd/modules/modules.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index 2ab43fabe..553d1490d 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -108,7 +108,6 @@ func listAllModules() ([]string, clierror.Error) { } func listManagedModules(config *modulesConfig) ([]string, clierror.Error) { - trololo := config.KubeClient.Static().CoreV1().RESTClient().Get().AbsPath("kyma-project.io") - fmt.Println(trololo) - return nil, clierror.New("chleb") + + return nil, clierror.New("") } From 3a3af6c471b504f72f6cca7cd184b2b68dedd8d6 Mon Sep 17 00:00:00 2001 From: Cortey Date: Wed, 5 Jun 2024 10:18:29 +0200 Subject: [PATCH 3/6] managed_flag --- internal/cmd/modules/modules.go | 86 ++++++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index 553d1490d..3de6b8020 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -5,7 +5,11 @@ import ( "fmt" "github.com/kyma-project/cli.v3/internal/cmdcommon" "io" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" "net/http" + "strings" "github.com/kyma-project/cli.v3/internal/clierror" "github.com/spf13/cobra" @@ -21,8 +25,7 @@ type modulesConfig struct { } func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { - - config := modulesConfig{ + cfg := modulesConfig{ KymaConfig: kymaConfig, KubeClientConfig: cmdcommon.KubeClientConfig{}, } @@ -32,16 +35,16 @@ func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { Short: "List modules.", Long: `List either installed, managed or available Kyma modules.`, PreRun: func(_ *cobra.Command, args []string) { - clierror.Check(config.KubeClientConfig.Complete()) + clierror.Check(cfg.KubeClientConfig.Complete()) }, Run: func(_ *cobra.Command, _ []string) { - clierror.Check(runModules(&config)) + clierror.Check(runModules(&cfg)) }, } - cmd.Flags().BoolVar(&config.catalog, "catalog", false, "List of al available Kyma modules.") - cmd.Flags().BoolVar(&config.managed, "managed", false, "List of all Kyma modules managed by central control-plane.") - cmd.Flags().BoolVar(&config.installed, "installed", false, "List of all currently installed Kyma modules.") + cmd.Flags().BoolVar(&cfg.catalog, "catalog", false, "List of al available Kyma modules.") + cmd.Flags().BoolVar(&cfg.managed, "managed", false, "List of all Kyma modules managed by central control-plane.") + cmd.Flags().BoolVar(&cfg.installed, "installed", false, "List of all currently installed Kyma modules.") cmd.MarkFlagsOneRequired("catalog", "managed", "installed") cmd.MarkFlagsMutuallyExclusive("catalog", "managed") @@ -51,9 +54,9 @@ func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { return cmd } -func runModules(config *modulesConfig) clierror.Error { +func runModules(cfg *modulesConfig) clierror.Error { var err error - if config.catalog { + if cfg.catalog { modules, err := listAllModules() if err != nil { return clierror.WrapE(err, clierror.New("failed to list all Kyma modules")) @@ -64,15 +67,16 @@ func runModules(config *modulesConfig) clierror.Error { } return nil } - - if config.managed { - _, err := listManagedModules(config) - clierror.WrapE(err, clierror.New("not implemented yet, please use the catalog flag")) + if cfg.managed { + _, err := listManagedModules(cfg) + if err != nil { + return clierror.WrapE(err, clierror.New("failed to list managed Kyma modules")) + } return nil } - if config.installed { - clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog flag")) + if cfg.installed { + clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog or managed flag")) return nil } //TODO: installed to implement @@ -83,7 +87,7 @@ func runModules(config *modulesConfig) clierror.Error { func listAllModules() ([]string, clierror.Error) { resp, err := http.Get("https://raw.githubusercontent.com/kyma-project/community-modules/main/model.json") if err != nil { - return nil, clierror.Wrap(err, clierror.New("while getting modules list")) + return nil, clierror.Wrap(err, clierror.New("while getting modules list from github")) } defer resp.Body.Close() @@ -107,7 +111,53 @@ func listAllModules() ([]string, clierror.Error) { return out, nil } -func listManagedModules(config *modulesConfig) ([]string, clierror.Error) { +func listManagedModules(cfg *modulesConfig) ([]string, clierror.Error) { + GVRKyma := schema.GroupVersionResource{ + Group: "operator.kyma-project.io", + Version: "v1beta2", + Resource: "kymas", + } + + unstruct, err := cfg.KubeClient.Dynamic().Resource(GVRKyma).Namespace("kyma-system").Get(cfg.Ctx, "default", metav1.GetOptions{}) + if err != nil { + return nil, clierror.Wrap(err, clierror.New("while getting Kyma CR")) + } + + moduleNames, err := getModuleNames(unstruct) + if err != nil { + return nil, clierror.Wrap(err, clierror.New("while getting module names from CR")) + } + + return moduleNames, nil +} + +func getModuleNames(unstruct *unstructured.Unstructured) ([]string, error) { + var moduleNames []string + managedFields := unstruct.GetManagedFields() + for _, field := range managedFields { + var data map[string]interface{} + err := json.Unmarshal(field.FieldsV1.Raw, &data) + if err != nil { + return nil, err + } - return nil, clierror.New("") + spec, ok := data["f:spec"].(map[string]interface{}) + if !ok { + continue + } + + modules, ok := spec["f:modules"].(map[string]interface{}) + if !ok { + continue + } + + for key := range modules { + if strings.Contains(key, "name") { + name := strings.TrimPrefix(key, "k:{\"name\":\"") + name = strings.Trim(name, "\"}") + moduleNames = append(moduleNames, name) + } + } + } + return moduleNames, nil } From c414e51d24546408ea85d3c8b11e6deeb8a9891e Mon Sep 17 00:00:00 2001 From: Cortey Date: Wed, 5 Jun 2024 10:28:49 +0200 Subject: [PATCH 4/6] kubeconfig_flag --- internal/cmd/modules/modules.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index 3de6b8020..2fb51d615 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -41,6 +41,7 @@ func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command { clierror.Check(runModules(&cfg)) }, } + cfg.KubeClientConfig.AddFlag(cmd) cmd.Flags().BoolVar(&cfg.catalog, "catalog", false, "List of al available Kyma modules.") cmd.Flags().BoolVar(&cfg.managed, "managed", false, "List of all Kyma modules managed by central control-plane.") From 22eab57ff07bcfed7d6cfcfcda6fbaf12bdf0013 Mon Sep 17 00:00:00 2001 From: Cortey Date: Wed, 5 Jun 2024 11:41:34 +0200 Subject: [PATCH 5/6] list_modules --- internal/cmd/modules/modules.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index 2fb51d615..462d68950 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -69,10 +69,11 @@ func runModules(cfg *modulesConfig) clierror.Error { return nil } if cfg.managed { - _, err := listManagedModules(cfg) + managed, err := listManagedModules(cfg) if err != nil { return clierror.WrapE(err, clierror.New("failed to list managed Kyma modules")) } + fmt.Println("Managed modules:\n", managed) return nil } @@ -80,7 +81,6 @@ func runModules(cfg *modulesConfig) clierror.Error { clierror.Wrap(err, clierror.New("not implemented yet, please use the catalog or managed flag")) return nil } - //TODO: installed to implement return clierror.Wrap(err, clierror.New("failed to get modules", "please use one of: catalog, managed or installed flags")) } From ccb3dabe374e4a2faf6b52afad473daddc316307 Mon Sep 17 00:00:00 2001 From: Cortey Date: Wed, 5 Jun 2024 11:45:22 +0200 Subject: [PATCH 6/6] list --- internal/cmd/modules/modules.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/cmd/modules/modules.go b/internal/cmd/modules/modules.go index 462d68950..8013c9405 100644 --- a/internal/cmd/modules/modules.go +++ b/internal/cmd/modules/modules.go @@ -73,7 +73,10 @@ func runModules(cfg *modulesConfig) clierror.Error { if err != nil { return clierror.WrapE(err, clierror.New("failed to list managed Kyma modules")) } - fmt.Println("Managed modules:\n", managed) + fmt.Println("Managed modules:\n") + for _, rec := range managed { + fmt.Println(rec) + } return nil }