Skip to content

Commit

Permalink
Improve the cmd: get clusters to show the cluster and nodes and their…
Browse files Browse the repository at this point in the history
… internal IP. Created a help function to create globally a kube client. cnoe-io#445

Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 14, 2024
1 parent 8c6cd97 commit 7eee957
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
53 changes: 49 additions & 4 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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
}
1 change: 0 additions & 1 deletion pkg/cmd/get/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package get

import (
"fmt"

"github.com/spf13/cobra"
)

Expand Down
40 changes: 40 additions & 0 deletions pkg/cmd/helpers/k8s.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7eee957

Please sign in to comment.