From 627741217b67c97de33fd43b6399f13a38c93058 Mon Sep 17 00:00:00 2001 From: manasachi Date: Tue, 30 Jan 2024 22:21:22 -0500 Subject: [PATCH] add list subcmd to vc nodepool --- cmd/kperf/commands/virtualcluster/nodepool.go | 22 +++++++++- helmcli/list.go | 40 +++++++++++++++++++ virtualcluster/node_list.go | 21 ++++++++++ 3 files changed, 82 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..bdeaf70 100644 --- a/cmd/kperf/commands/virtualcluster/nodepool.go +++ b/cmd/kperf/commands/virtualcluster/nodepool.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/Azure/kperf/virtualcluster" + "helm.sh/helm/v3/pkg/release" "github.com/urfave/cli" ) @@ -92,6 +93,25 @@ var nodepoolListCommand = cli.Command{ Name: "list", Usage: "List virtual node pools", Action: func(cliCtx *cli.Context) error { - return fmt.Errorf("nodepool list - not implemented") + kubeCfgPath := cliCtx.String("kubeconfig") + nodepools, err := virtualcluster.ListNodepools(context.Background(), kubeCfgPath) + if err != nil { + return err + } + return renderRunnerGroups(nodepools) + }, } + +func renderRunnerGroups(nodepools []*release.Release) error { + if len(nodepools) > 0 { + fmt.Println("+-------------------+------------+-------------+-------------+------------+") + fmt.Printf("| %-17s | %-10s | %-9s | %-11s | %-9s |\n", "Name", "Nodes", "CPU (cores)", "Memory (GiB)", "Status") + fmt.Println("+-------------------+------------+-------------+-------------+------------+") + } + for _, nodepool := range nodepools { + fmt.Printf("| %-17s | %-10v | %-12v| %-12v| %-10v |\n", nodepool.Name, nodepool.Config["replicas"], nodepool.Config["cpu"], nodepool.Config["memory"], nodepool.Info.Status) + fmt.Println("+-------------------+------------+-------------+-------------+------------+") + } + 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..885a39e --- /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) + } + + return listCli.List() + +}