From 787e75af369ddc672316129219c83c40b3664226 Mon Sep 17 00:00:00 2001 From: manasachi Date: Mon, 29 Jan 2024 20:03:57 -0500 Subject: [PATCH] working on printing data for list command --- cmd/kperf/commands/virtualcluster/nodepool.go | 24 ++++++++++- helmcli/list.go | 40 +++++++++++++++++++ virtualcluster/node_list.go | 21 ++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 helmcli/list.go create mode 100644 virtualcluster/node_list.go diff --git a/cmd/kperf/commands/virtualcluster/nodepool.go b/cmd/kperf/commands/virtualcluster/nodepool.go index 14af1c9..2e7d91d 100644 --- a/cmd/kperf/commands/virtualcluster/nodepool.go +++ b/cmd/kperf/commands/virtualcluster/nodepool.go @@ -92,6 +92,28 @@ var nodepoolListCommand = cli.Command{ Name: "list", Usage: "List virtual node pools", Action: func(cliCtx *cli.Context) error { - return fmt.Errorf("nodepool list - not implemented") + fmt.Println("In List command") + kubeCfgPath := cliCtx.String("kubeconfig") + nodepools, err := virtualcluster.ListNodepools(context.Background(), kubeCfgPath) + fmt.Println("after getting nodepools") + if err != nil { + return err + } + fmt.Println("No error :)") + fmt.Println("Number of nodepools: \n", len(nodepools)) + fmt.Println() + for _, nodepool := range nodepools { + fmt.Println("Nodepool name: ", nodepool.Name) + + //fmt.Println("Nodepool Nodes: ", len(nodepool.Info.repli)) + + //fmt.Println("Nodepool Memory: ", nodepool.Info.memory) + fmt.Println("Nodepool Status: ", nodepool.Info.Status) + // fmt.Println("-------------------") + // fmt.Println(nodepool) + // fmt.Println("-------------------") + } + fmt.Println("after printing returned nodepools") + return nil }, } diff --git a/helmcli/list.go b/helmcli/list.go new file mode 100644 index 0000000..0ca764b --- /dev/null +++ b/helmcli/list.go @@ -0,0 +1,40 @@ +package helmcli + +import ( + "fmt" + + "helm.sh/helm/v3/pkg/action" + "helm.sh/helm/v3/pkg/release" + "k8s.io/cli-runtime/pkg/genericclioptions" +) + +// ListCli is a client to get helm charts from secret storage. +type ListCli struct { + namespace string + + cfg *action.Configuration +} + +// NewGetCli returns new GetCli instance. +func NewListCli(kubeconfigPath string, namespace string) (*ListCli, error) { + actionCfg := new(action.Configuration) + if err := actionCfg.Init( + &genericclioptions.ConfigFlags{ + KubeConfig: &kubeconfigPath, + }, + namespace, + "secret", + noopLog, + ); err != nil { + return nil, fmt.Errorf("failed to init action config: %w", err) + } + return &ListCli{ + namespace: namespace, + cfg: actionCfg, + }, nil +} + +func (cli *ListCli) List() ([]*release.Release, error) { + listCli := action.NewList(cli.cfg) + return listCli.Run() +} diff --git a/virtualcluster/node_list.go b/virtualcluster/node_list.go new file mode 100644 index 0000000..a50a8a2 --- /dev/null +++ b/virtualcluster/node_list.go @@ -0,0 +1,21 @@ +package virtualcluster + +import ( + "context" + "fmt" + + "helm.sh/helm/v3/pkg/release" + + "github.com/Azure/kperf/helmcli" +) + +// ListNodeppol lists nodepools added by the vc nodeppool add command. +func ListNodepools(_ context.Context, kubeconfigPath string) ([]*release.Release, error) { + listCli, err := helmcli.NewListCli(kubeconfigPath, virtualnodeReleaseNamespace) + if err != nil { + return nil, fmt.Errorf("failed to create helm list client: %w", err) + } + + nodepools, err := listCli.List() + return nodepools, err +}