Skip to content

Commit 7eee957

Browse files
committed
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. cnoe-io#445
Signed-off-by: cmoulliard <[email protected]>
1 parent 8c6cd97 commit 7eee957

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

pkg/cmd/get/clusters.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package get
22

33
import (
4+
"context"
45
"fmt"
5-
66
"github.com/cnoe-io/idpbuilder/pkg/cmd/helpers"
7+
"github.com/cnoe-io/idpbuilder/pkg/kind"
8+
"github.com/cnoe-io/idpbuilder/pkg/util"
79
"github.com/spf13/cobra"
10+
corev1 "k8s.io/api/core/v1"
11+
"os"
812
"sigs.k8s.io/kind/pkg/cluster"
913
)
1014

@@ -16,19 +20,60 @@ var ClustersCmd = &cobra.Command{
1620
PreRunE: preClustersE,
1721
}
1822

23+
var kubeCfgPath string
24+
1925
func preClustersE(cmd *cobra.Command, args []string) error {
2026
return helpers.SetLogger()
2127
}
2228

2329
func list(cmd *cobra.Command, args []string) error {
24-
provider := cluster.NewProvider(cluster.ProviderWithDocker())
30+
logger := helpers.CmdLogger
31+
32+
detectOpt, err := util.DetectKindNodeProvider()
33+
if err != nil {
34+
logger.Error(err, "failed to detect the provider.")
35+
os.Exit(1)
36+
}
37+
38+
kubeConfig, err := helpers.GetKubeConfig()
39+
if err != nil {
40+
logger.Error(err, "failed to create the kube config.")
41+
os.Exit(1)
42+
}
43+
44+
cli, err := helpers.GetKubeClient(kubeConfig)
45+
if err != nil {
46+
logger.Error(err, "failed to create the kube client.")
47+
os.Exit(1)
48+
}
49+
50+
provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt)
2551
clusters, err := provider.List()
2652
if err != nil {
27-
return fmt.Errorf("failed to list clusters: %w", err)
53+
logger.Error(err, "failed to list clusters.")
2854
}
2955

3056
for _, c := range clusters {
31-
fmt.Println(c)
57+
fmt.Printf("Cluster: %s\n", c)
58+
var nodeList corev1.NodeList
59+
err := cli.List(context.TODO(), &nodeList)
60+
if err != nil {
61+
logger.Error(err, "failed to list nodes for cluster: %s", c)
62+
}
63+
for _, node := range nodeList.Items {
64+
nodeName := node.Name
65+
fmt.Printf(" Node: %s\n", nodeName)
66+
67+
for _, addr := range node.Status.Addresses {
68+
switch addr.Type {
69+
case corev1.NodeInternalIP:
70+
fmt.Printf(" Internal IP: %s\n", addr.Address)
71+
case corev1.NodeExternalIP:
72+
fmt.Printf(" External IP: %s\n", addr.Address)
73+
}
74+
}
75+
fmt.Println("----------")
76+
}
3277
}
3378
return nil
3479
}

pkg/cmd/get/root.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package get
22

33
import (
44
"fmt"
5-
65
"github.com/spf13/cobra"
76
)
87

pkg/cmd/helpers/k8s.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
"k8s.io/apimachinery/pkg/runtime"
6+
"k8s.io/client-go/rest"
7+
"k8s.io/client-go/tools/clientcmd"
8+
"k8s.io/client-go/util/homedir"
9+
"path/filepath"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
)
12+
13+
var (
14+
KubeConfigPath string
15+
scheme *runtime.Scheme
16+
)
17+
18+
func GetKubeConfigPath() string {
19+
if KubeConfigPath == "" {
20+
return filepath.Join(homedir.HomeDir(), ".kube", "config")
21+
} else {
22+
return KubeConfigPath
23+
}
24+
}
25+
26+
func GetKubeConfig() (*rest.Config, error) {
27+
kubeConfig, err := clientcmd.BuildConfigFromFlags("", GetKubeConfigPath())
28+
if err != nil {
29+
return nil, fmt.Errorf("Error building kubeconfig: %w", err)
30+
}
31+
return kubeConfig, nil
32+
}
33+
34+
func GetKubeClient(kubeConfig *rest.Config) (client.Client, error) {
35+
kubeClient, err := client.New(kubeConfig, client.Options{Scheme: scheme})
36+
if err != nil {
37+
return nil, fmt.Errorf("Error creating kubernetes client: %w", err)
38+
}
39+
return kubeClient, nil
40+
}

pkg/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var rootCmd = &cobra.Command{
2121
func init() {
2222
rootCmd.PersistentFlags().StringVarP(&helpers.LogLevel, "log-level", "l", "info", helpers.LogLevelMsg)
2323
rootCmd.PersistentFlags().BoolVar(&helpers.ColoredOutput, "color", false, helpers.ColoredOutputMsg)
24+
rootCmd.PersistentFlags().StringVarP(&helpers.KubeConfigPath, "kubePath", "", "", "kube config file Path.")
2425
rootCmd.AddCommand(create.CreateCmd)
2526
rootCmd.AddCommand(get.GetCmd)
2627
rootCmd.AddCommand(delete.DeleteCmd)

0 commit comments

Comments
 (0)