-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_nodes.go
40 lines (36 loc) · 927 Bytes
/
list_nodes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"github.com/appscode/go/log"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func NewCmdListNodes(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "nodes",
Short: "List nodes",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
namespace, _, err := clientConfig.Namespace()
if err != nil {
log.Fatalln(err)
}
fmt.Println("namespace = ", namespace)
config, err := clientConfig.ClientConfig()
if err != nil {
log.Fatalln(err)
}
client := kubernetes.NewForConfigOrDie(config)
nodes, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
log.Fatalln(err)
}
for _, node := range nodes.Items {
fmt.Println(node.Name)
}
},
}
return cmd
}