-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck.go
62 lines (51 loc) · 1.17 KB
/
check.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
package main
import (
"time"
pb "gopkg.in/cheggaaa/pb.v1"
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
)
func isRunning(pod api.Pod) bool {
state := pod.Status.ContainerStatuses[0].State
if state.Running != nil {
return true
}
return false
}
func checkRunning(kubeClient *client.Client, targetPod api.Pod, initialRunning bool) bool {
count := 60
bar := pb.StartNew(count)
for s := 0; s < count; s++ {
newPod := getTargetPod(kubeClient, targetPod.Name, targetPod.Namespace)
bar.Increment()
time.Sleep(time.Second)
if !isRunning(newPod) && initialRunning {
return false
}
}
return true
}
func checkHealth(kubeClient *client.Client, targetPod api.Pod) bool {
// health check
if isRunning(targetPod) {
if !checkRunning(kubeClient, targetPod, true) {
return false
}
} else {
if !checkRunning(kubeClient, targetPod, false) {
return false
}
}
// last check
newPod := getTargetPod(kubeClient, targetPod.Name, targetPod.Namespace)
if !isRunning(newPod) {
return false
}
return true
}
func check(kubeClient *client.Client, pod api.Pod) bool {
if checkHealth(kubeClient, pod) {
return true
}
return false
}