-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhealth.go
65 lines (57 loc) · 1.47 KB
/
health.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
package kommons
import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// PingMaster attempts to connect to the API server and list nodes and services
// to ensure the API server is ready to accept any traffic
func (c *Client) PingMaster() bool {
client, err := c.GetClientset()
if err != nil {
c.Tracef("pingMaster: Failed to get clientset: %v", err)
return false
}
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
c.Tracef("pingMaster: Failed to get nodes list: %v", err)
return false
}
if nodes == nil && len(nodes.Items) == 0 {
return false
}
_, err = client.CoreV1().ServiceAccounts("kube-system").Get(context.TODO(), "default", metav1.GetOptions{})
if err != nil {
c.Tracef("pingMaster: Failed to get service account: %v", err)
return false
}
return true
}
func (c *Client) GetHealth() Health {
health := Health{}
client, err := c.GetClientset()
if err != nil {
return Health{Error: err}
}
pods, err := client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return Health{Error: err}
}
for _, pod := range pods.Items {
if IsDeleted(&pod) {
continue
}
if pod.Spec.Priority != nil && *pod.Spec.Priority < 0 {
continue
}
if IsPodCrashLoopBackoff(pod) {
health.CrashLoopBackOff++
} else if IsPodHealthy(pod) {
health.RunningPods++
} else if IsPodPending(pod) {
health.PendingPods++
} else {
health.ErrorPods++
}
}
return health
}