From 6e05aece3aaf7ab6b4057a1a49e953b4a7f55083 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Thu, 14 Nov 2024 11:36:47 +0100 Subject: [PATCH] Show the kube API server URL, if TLS is checked. #445 Signed-off-by: cmoulliard --- pkg/cmd/get/clusters.go | 32 ++++++++++++++++++++++++++++---- pkg/cmd/helpers/k8s.go | 10 ++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/get/clusters.go b/pkg/cmd/get/clusters.go index 842b349d..f2fa0948 100644 --- a/pkg/cmd/get/clusters.go +++ b/pkg/cmd/get/clusters.go @@ -8,6 +8,7 @@ import ( "github.com/cnoe-io/idpbuilder/pkg/util" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" + "k8s.io/client-go/tools/clientcmd/api" "os" "sigs.k8s.io/kind/pkg/cluster" ) @@ -26,6 +27,12 @@ func preClustersE(cmd *cobra.Command, args []string) error { return helpers.SetLogger() } +// findClusterByName searches for a cluster by name in the kubeconfig +func findClusterByName(config *api.Config, name string) (*api.Cluster, bool) { + cluster, exists := config.Clusters[name] + return cluster, exists +} + func list(cmd *cobra.Command, args []string) error { logger := helpers.CmdLogger @@ -47,19 +54,36 @@ func list(cmd *cobra.Command, args []string) error { os.Exit(1) } + // List the idp builder clusters according to the provider: podman or docker provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt) clusters, err := provider.List() if err != nil { logger.Error(err, "failed to list clusters.") } - for _, c := range clusters { - fmt.Printf("Cluster: %s\n", c) + for _, cluster := range clusters { + fmt.Printf("Cluster: %s\n", cluster) + + config, err := helpers.LoadKubeConfig() + if err != nil { + logger.Error(err, "failed to load the kube config.") + } + + // Search about the idp cluster within the kubeconfig file + cluster, found := findClusterByName(config, "kind-"+cluster) + if !found { + fmt.Printf("Cluster %q not found\n", cluster) + } else { + fmt.Printf("URL of the kube API server: %s\n", cluster.Server) + fmt.Printf("TLS Verify: %t\n", cluster.InsecureSkipTLSVerify) + } + var nodeList corev1.NodeList - err := cli.List(context.TODO(), &nodeList) + err = cli.List(context.TODO(), &nodeList) if err != nil { - logger.Error(err, "failed to list nodes for cluster: %s", c) + logger.Error(err, "failed to list nodes for cluster: %s", cluster) } + for _, node := range nodeList.Items { nodeName := node.Name fmt.Printf(" Node: %s\n", nodeName) diff --git a/pkg/cmd/helpers/k8s.go b/pkg/cmd/helpers/k8s.go index cc6907c5..f371b865 100644 --- a/pkg/cmd/helpers/k8s.go +++ b/pkg/cmd/helpers/k8s.go @@ -5,6 +5,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" + "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/util/homedir" "path/filepath" "sigs.k8s.io/controller-runtime/pkg/client" @@ -23,6 +24,15 @@ func GetKubeConfigPath() string { } } +func LoadKubeConfig() (*api.Config, error) { + config, err := clientcmd.LoadFromFile(GetKubeConfigPath()) + if err != nil { + return nil, fmt.Errorf("Failed to load kubeconfig file: %w", err) + } else { + return config, nil + } +} + func GetKubeConfig() (*rest.Config, error) { kubeConfig, err := clientcmd.BuildConfigFromFlags("", GetKubeConfigPath()) if err != nil {