-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
93 lines (81 loc) · 2.81 KB
/
main.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
func main() {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
for {
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Println("------------------- Pods -------------------")
fmt.Println()
fmt.Println("******************* List All ************************")
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
fmt.Println()
for _, pod := range pods.Items {
fmt.Printf("| %6s | %6s |\n", pod.GetName(), pod.GetNamespace())
}
fmt.Println()
fmt.Println("******************* Get By Name *********************")
podName := "etcd-minikube"
namespace := "kube-system"
pod1, _ := clientset.CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{})
fmt.Printf("Found pod %s in namespace %s created at %v\n", podName, namespace, pod1.CreationTimestamp)
pod2, err := clientset.CoreV1().Pods(namespace).Get("notapod", metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found\n", "notapod", namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v\n",
podName, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s created at %v\n", podName, namespace, pod2.CreationTimestamp)
}
fmt.Println()
fmt.Println("------------------- Nodes -------------------")
nodes, err := clientset.CoreV1().Nodes().List(metav1.ListOptions{})
fmt.Printf("There are %d node in the cluster\n", len(nodes.Items))
fmt.Println()
fmt.Println("******************* List All *******************")
fmt.Println()
for _, node := range nodes.Items {
fmt.Printf("| %6s |\n", node.GetName())
}
time.Sleep(2000 * time.Second)
}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}