Skip to content

Commit

Permalink
Show the kube API server URL, if TLS is checked. #445
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 14, 2024
1 parent 7eee957 commit 6e05aec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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

Expand All @@ -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)

Check failure on line 75 in pkg/cmd/get/clusters.go

View workflow job for this annotation

GitHub Actions / build

fmt.Printf format %q has arg cluster of wrong type *k8s.io/client-go/tools/clientcmd/api.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)
Expand Down
10 changes: 10 additions & 0 deletions pkg/cmd/helpers/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 6e05aec

Please sign in to comment.