Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "managed" flag to "modules" command #2129

Merged
merged 6 commits into from
Jun 6, 2024
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/cmd/kyma.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
102 changes: 89 additions & 13 deletions internal/cmd/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,49 @@ package modules
import (
"encoding/json"
"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"
)

type modulesConfig struct {
*cmdcommon.KymaConfig
cmdcommon.KubeClientConfig

catalog bool
managed bool
installed bool
}

func NewModulesCMD() *cobra.Command {

config := modulesConfig{}
func NewModulesCMD(kymaConfig *cmdcommon.KymaConfig) *cobra.Command {
cfg := 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(cfg.KubeClientConfig.Complete())
},
Run: func(_ *cobra.Command, _ []string) {
clierror.Check(runModules(&config))
clierror.Check(runModules(&cfg))
},
}
cfg.KubeClientConfig.AddFlag(cmd)

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")
Expand All @@ -41,9 +55,9 @@ func NewModulesCMD() *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"))
Expand All @@ -54,19 +68,30 @@ func runModules(config *modulesConfig) clierror.Error {
}
return nil
}
if cfg.managed {
managed, err := listManagedModules(cfg)
if err != nil {
return clierror.WrapE(err, clierror.New("failed to list managed Kyma modules"))
}
fmt.Println("Managed modules:\n")
for _, rec := range managed {
fmt.Println(rec)
}
return nil
}

if config.managed || 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 and managed to implement

return clierror.Wrap(err, clierror.New("failed to get modules", "please use one of: catalog, managed or installed flags"))
}

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()

Expand All @@ -89,3 +114,54 @@ func listAllModules() ([]string, clierror.Error) {
}
return out, nil
}

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
}

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\":\"")
Cortey marked this conversation as resolved.
Show resolved Hide resolved
name = strings.Trim(name, "\"}")
moduleNames = append(moduleNames, name)
}
}
}
return moduleNames, nil
}
Loading