-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
184 lines (160 loc) · 4.25 KB
/
utility.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package kubernetes
import (
"context"
"errors"
"fmt"
"log"
"time"
)
// PodChecks returns nil if Pod
// 1. exists
// 2. has Owner
// 3. has not been scheduled to be deleted
// 4. and is not in a Healthy state (eg: Pending, Failed or Running with unhealthy containers)
func (c *kubeClient) PodChecks(ctx context.Context, podName, podNamespace string) error {
// verify if Pod exists
podInfo, err := c.GetPodDetails(ctx, podName, podNamespace)
if err != nil {
return err
}
// verify Pod has owner
err = podInfo.verifyPodHasOwner()
if err != nil {
return err
}
// verify Pod is scheduled to be deleted
err = podInfo.verifyPodScheduledToBeDeleted()
if err != nil {
return err
}
// verify Pod is in an Unhealthy state
err = podInfo.verifyPodStatus()
if err != nil {
return nil
} else {
msg := fmt.Sprintf("Pod is in a Healthy State: %s/%s", podNamespace, podName)
return errors.New(msg)
}
}
// verifyPodStatus returns error if Pod is in a Pending, Failed or Running (with unhealthy containers) state
func (p *PodDetails) verifyPodStatus() error {
switch p.Phase {
case "Pending":
msg := fmt.Sprintf(
"Pod is in a %s state: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return errors.New(msg)
case "Running":
if len(p.ContainerStatuses) != 0 {
for _, cst := range p.ContainerStatuses {
if cst.State.Terminated == nil {
continue
}
if cst.State.Terminated.Reason == "Completed" && cst.State.Terminated.ExitCode == 0 {
continue
}
msg := fmt.Sprintf(
"Pod is in a %s state and has issues: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return errors.New(msg)
}
log.Printf(
"Pod is in a %s state and is healthy: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return nil
}
log.Printf(
"Pod is in a %s state and has been evacuated?: %s/%s\n%+v",
p.Phase, p.PodNamespace, p.PodName,
p.ContainerStatuses,
)
return nil
case "Failed":
msg := fmt.Sprintf(
"Pod is in a %s state: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return errors.New(msg)
case "Succeeded":
log.Printf(
"Pod is in a %s state: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return nil
case "Unknown":
msg := fmt.Sprintf(
"Pod is in a %s state: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return errors.New(msg)
}
msg := fmt.Sprintf(
"Pod is in a %s state ????????: %s/%s",
p.Phase, p.PodNamespace, p.PodName,
)
return errors.New(msg)
}
// verify if element in slice
func contains(elems []string, v string) bool {
for _, s := range elems {
if v == s {
return true
}
}
return false
}
// verifyPodHasOwner returns nil if Pod has owner
func (p *PodDetails) verifyPodHasOwner() error {
if len(p.OwnerReferences) > 0 {
return nil
}
msg := fmt.Sprintf(
"Pod does not have owner/controller: %s/%s",
p.PodNamespace, p.PodName,
)
return errors.New(msg)
}
// verifyPodScheduledToBeDeleted returns nil if Pod is not scheduled to be deleted
func (p *PodDetails) verifyPodScheduledToBeDeleted() error {
// verify Pod has not been scheduled to be deleted
if p.DeletionTimestamp != nil {
msg := fmt.Sprintf(
"Pod has already been scheduled to be deleted: %s/%s\n%v",
p.PodNamespace, p.PodName, p.DeletionTimestamp,
)
return errors.New(msg)
}
return nil
}
// getUniqueListOfPods returns a unique list of Pods that have Events that match Reason
func getUniqueListOfPods(events []PodEvent) map[string]string {
var uniquePodList = make(map[string]string)
var uniqueUIDsList []string
for _, event := range events {
if contains(uniqueUIDsList, string(event.UID)) {
continue
}
uniquePodList[event.PodName] = event.PodNamespace
uniqueUIDsList = append(uniqueUIDsList, string(event.UID))
}
return uniquePodList
}
// removeOlderEvents returns a slice of latest Events not older than eventMaxAge
func removeOlderEvents(events []PodEvent, eventMaxAge time.Time) []PodEvent {
var latestEvents []PodEvent
for _, event := range events {
if event.LastTimestamp.Before(eventMaxAge) {
continue
}
latestEvents = append(latestEvents, event)
}
return latestEvents
}
// timeTrack calculates how long it takes to execute a function
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start)
log.Printf("%v ran in %v \n", name, elapsed)
}