-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
139 lines (124 loc) · 3.95 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
v1 "k8s.io/api/core/v1"
"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"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
func isNodeReady(node v1.Node) bool {
for _, condition := range node.Status.Conditions {
if condition.Type == "Ready" {
return condition.Status == "True"
}
}
return false
}
func main() {
var kubeconfig *string
ctx := context.Background()
if kubeconfigEnvvar := os.Getenv("KUBECONFIG"); kubeconfigEnvvar != "" {
kubeconfigTokenized := strings.Split(kubeconfigEnvvar, ";")
kubeconfig = flag.String("kubeconfig", kubeconfigTokenized[0], "absolute path to the kubeconfig file")
} else 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())
}
pods, err := clientset.CoreV1().Pods("").List(ctx, metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("Number of pods registered: %d\n", len(pods.Items))
nonReadyPods := 0
for index, pod := range pods.Items {
if pod.Status.Phase != v1.PodRunning {
fmt.Printf("tmp")
fmt.Printf(strconv.Itoa(index))
nonReadyPods++
}
}
fmt.Printf("Number of non-running pods: %d\n", nonReadyPods)
deploys, err := clientset.AppsV1().Deployments("").List(ctx, metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("Number of deployments registered: %d\n", len(deploys.Items))
nonReadyDeploys := 0
for _, deploy := range deploys.Items {
if deploy.Status.UnavailableReplicas > 0 {
fmt.Printf("Not Ready Deployment: %s", deploy.GetName())
nonReadyDeploys++
}
}
fmt.Printf("Number of incomplete deployments: %d\n", nonReadyDeploys)
// daemonsets
daemonsets, err := clientset.AppsV1().DaemonSets("").List(ctx, metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("Number of daemonsets registered: %d\n", len(daemonsets.Items))
nonReadyDaemonsets := 0
for _, daemonset := range daemonsets.Items {
if daemonset.Status.NumberUnavailable > 0 {
fmt.Printf("Not Ready DaemonSet: %s", daemonset.GetName())
nonReadyDaemonsets++
}
}
fmt.Printf("Number of incomplete daemonsets: %d\n", nonReadyDaemonsets)
nodes, err := clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("Number of nodes registered: %d\n", len(nodes.Items))
nonReadyNodes := 0
for index, node := range nodes.Items {
if !isNodeReady(node) {
fmt.Printf("tmp")
fmt.Printf("Not Ready Node: %s", node.GetName())
fmt.Printf(strconv.Itoa(index))
nonReadyNodes++
}
}
fmt.Printf("Number of incomplete nodes: %d\n", nonReadyNodes)
// Examples for error handling:
// - Use helper functions like e.g. errors.IsNotFound()
// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
namespace := "default"
pod := "example-xxxxx"
_, err = clientset.CoreV1().Pods(namespace).Get(ctx, pod, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Pod %s in namespace %s not found\n", pod, namespace)
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting pod %s in namespace %s: %v\n", pod, namespace, statusError.ErrStatus.Message)
} else if err != nil {
panic(err.Error())
} else {
fmt.Printf("Found pod %s in namespace %s\n", pod, namespace)
}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}