/
helper.go
70 lines (66 loc) · 1.67 KB
/
helper.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
package tenancywatcher
import (
"github.com/ica10888/multi-tenancy-operator/pkg/controller/multitenancycontroller"
corev1 "k8s.io/api/core/v1"
"regexp"
)
func getRCKubeapi(objs []multitenancycontroller.KubeObject) (res []multitenancycontroller.Kubeapi) {
for _, obj := range objs {
kind := obj.Kubeapi.Kind
if kind == "Deployment" || kind == "StatefulSet" || kind == "DaemonSet" {
res = append(res, obj.Kubeapi)
}
}
return
}
func podNameMatchesRC(kind, rcName, podName string) bool {
switch kind {
case "Deployment":
p := rcName + "-[a-z0-9]{10}-[a-z0-9]{5}"
m, _ := regexp.MatchString(p, podName)
return m
case "StatefulSet":
p := rcName + "-[0-9]+"
m, _ := regexp.MatchString(p, podName)
return m
case "DaemonSet":
p := rcName + "-[a-z0-9]{5}"
m, _ := regexp.MatchString(p, podName)
return m
}
return false
}
func getPhase(obj *corev1.Pod) string {
csList := obj.Status.ContainerStatuses
if len(csList) > 0 {
if csList[0].LastTerminationState.Waiting != nil && csList[0].LastTerminationState.Waiting.Reason != "" {
return csList[0].LastTerminationState.Waiting.Reason
}
if csList[0].LastTerminationState.Terminated != nil && csList[0].LastTerminationState.Terminated.Reason != "" {
return csList[0].LastTerminationState.Terminated.Reason
}
}
res := ""
switch obj.Status.Phase {
case corev1.PodPending:
res = "Pending"
case corev1.PodRunning:
res = "Running"
case corev1.PodSucceeded:
res = "Succeeded"
case corev1.PodFailed:
res = "Failed"
case corev1.PodUnknown:
res = "Unknown"
}
if res == "Running" {
if len(csList) > 0 {
if csList[0].Ready {
return res
} else {
return "Unavailable"
}
}
}
return res
}