From 7eee9577d99d61ad3a32daf18fd04bb14935b862 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Thu, 14 Nov 2024 11:03:44 +0100 Subject: [PATCH] Improve the cmd: get clusters to show the cluster and nodes and their internal IP. Created a help function to create globally a kube client. #445 Signed-off-by: cmoulliard --- pkg/cmd/get/clusters.go | 53 +++++++++++++++++++++++++++++++++++++---- pkg/cmd/get/root.go | 1 - pkg/cmd/helpers/k8s.go | 40 +++++++++++++++++++++++++++++++ pkg/cmd/root.go | 1 + 4 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 pkg/cmd/helpers/k8s.go diff --git a/pkg/cmd/get/clusters.go b/pkg/cmd/get/clusters.go index 01708c8d..842b349d 100644 --- a/pkg/cmd/get/clusters.go +++ b/pkg/cmd/get/clusters.go @@ -1,10 +1,14 @@ package get import ( + "context" "fmt" - "github.com/cnoe-io/idpbuilder/pkg/cmd/helpers" + "github.com/cnoe-io/idpbuilder/pkg/kind" + "github.com/cnoe-io/idpbuilder/pkg/util" "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" + "os" "sigs.k8s.io/kind/pkg/cluster" ) @@ -16,19 +20,60 @@ var ClustersCmd = &cobra.Command{ PreRunE: preClustersE, } +var kubeCfgPath string + func preClustersE(cmd *cobra.Command, args []string) error { return helpers.SetLogger() } func list(cmd *cobra.Command, args []string) error { - provider := cluster.NewProvider(cluster.ProviderWithDocker()) + logger := helpers.CmdLogger + + detectOpt, err := util.DetectKindNodeProvider() + if err != nil { + logger.Error(err, "failed to detect the provider.") + os.Exit(1) + } + + kubeConfig, err := helpers.GetKubeConfig() + if err != nil { + logger.Error(err, "failed to create the kube config.") + os.Exit(1) + } + + cli, err := helpers.GetKubeClient(kubeConfig) + if err != nil { + logger.Error(err, "failed to create the kube client.") + os.Exit(1) + } + + provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt) clusters, err := provider.List() if err != nil { - return fmt.Errorf("failed to list clusters: %w", err) + logger.Error(err, "failed to list clusters.") } for _, c := range clusters { - fmt.Println(c) + fmt.Printf("Cluster: %s\n", c) + var nodeList corev1.NodeList + err := cli.List(context.TODO(), &nodeList) + if err != nil { + logger.Error(err, "failed to list nodes for cluster: %s", c) + } + for _, node := range nodeList.Items { + nodeName := node.Name + fmt.Printf(" Node: %s\n", nodeName) + + for _, addr := range node.Status.Addresses { + switch addr.Type { + case corev1.NodeInternalIP: + fmt.Printf(" Internal IP: %s\n", addr.Address) + case corev1.NodeExternalIP: + fmt.Printf(" External IP: %s\n", addr.Address) + } + } + fmt.Println("----------") + } } return nil } diff --git a/pkg/cmd/get/root.go b/pkg/cmd/get/root.go index 1e329010..a8aacd9f 100644 --- a/pkg/cmd/get/root.go +++ b/pkg/cmd/get/root.go @@ -2,7 +2,6 @@ package get import ( "fmt" - "github.com/spf13/cobra" ) diff --git a/pkg/cmd/helpers/k8s.go b/pkg/cmd/helpers/k8s.go new file mode 100644 index 00000000..cc6907c5 --- /dev/null +++ b/pkg/cmd/helpers/k8s.go @@ -0,0 +1,40 @@ +package helpers + +import ( + "fmt" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" + "k8s.io/client-go/util/homedir" + "path/filepath" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var ( + KubeConfigPath string + scheme *runtime.Scheme +) + +func GetKubeConfigPath() string { + if KubeConfigPath == "" { + return filepath.Join(homedir.HomeDir(), ".kube", "config") + } else { + return KubeConfigPath + } +} + +func GetKubeConfig() (*rest.Config, error) { + kubeConfig, err := clientcmd.BuildConfigFromFlags("", GetKubeConfigPath()) + if err != nil { + return nil, fmt.Errorf("Error building kubeconfig: %w", err) + } + return kubeConfig, nil +} + +func GetKubeClient(kubeConfig *rest.Config) (client.Client, error) { + kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme}) + if err != nil { + return nil, fmt.Errorf("Error creating kubernetes client: %w", err) + } + return kubeClient, nil +} diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 32d97ab4..12afffc7 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -21,6 +21,7 @@ var rootCmd = &cobra.Command{ func init() { rootCmd.PersistentFlags().StringVarP(&helpers.LogLevel, "log-level", "l", "info", helpers.LogLevelMsg) rootCmd.PersistentFlags().BoolVar(&helpers.ColoredOutput, "color", false, helpers.ColoredOutputMsg) + rootCmd.PersistentFlags().StringVarP(&helpers.KubeConfigPath, "kubePath", "", "", "kube config file Path.") rootCmd.AddCommand(create.CreateCmd) rootCmd.AddCommand(get.GetCmd) rootCmd.AddCommand(delete.DeleteCmd)