-
Notifications
You must be signed in to change notification settings - Fork 7
/
deployments.go
167 lines (152 loc) · 4.01 KB
/
deployments.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
package main
import (
"bufio"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
// Deployment struct
type Deployment struct {
Namespace string
Name string
Replicas int
ReplicasExpected int
UpToDate int
Avaliable int
Age string
Pods []Pod
Pdb Pdb
}
// GetDeploymentKey should work for most of the cases
func (d Deployment) GetDeploymentKey() string {
return d.Namespace + "|" + d.Name
}
// ContainsPod ..
func (d Deployment) ContainsPod(pod string) bool {
set := make(map[string]bool, len(d.Pods))
for _, pod := range d.Pods {
set[pod.Metadata.Name] = true
}
_, ok := set[pod]
return ok
}
// CountLivenessProbes ..
func (d Deployment) CountLivenessProbes() string {
if len(d.Pods) > 0 {
return d.Pods[0].CountLivenessProbes()
}
return "N/A"
}
// CountReadinessProbes ..
func (d Deployment) CountReadinessProbes() string {
if len(d.Pods) > 0 {
return d.Pods[0].CountReadinessProbes()
}
return "N/A"
}
// CountLifecyclePreStop ..
func (d Deployment) CountLifecyclePreStop() string {
if len(d.Pods) > 0 {
return d.Pods[0].CountLifecyclePreStop()
}
return "N/A"
}
// GetLivenessProbes ..
func (d Deployment) GetLivenessProbes() string {
if len(d.Pods) > 0 {
return d.Pods[0].GetLivenessProbes()
}
return "N/A"
}
// GetReadinessProbes ..
func (d Deployment) GetReadinessProbes() string {
if len(d.Pods) > 0 {
return d.Pods[0].GetReadinessProbes()
}
return "N/A"
}
// GetLifecyclePreStop ..
func (d Deployment) GetLifecyclePreStop() string {
if len(d.Pods) > 0 {
return d.Pods[0].GetLifecyclePreStop()
}
return "N/A"
}
// RetrieveDeployments executes kubectl get deployments command
// if ns is empty, then all namespaces are used
func RetrieveDeployments(nsFilter string, podList []Pod) []Deployment {
cmd := "kubectl get deployments --all-namespaces --no-headers"
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
if err != nil {
log.Fatalf("Failed to execute command: %s", cmd)
}
data := string(out)
deploys := buildDeploymentList(data, nsFilter, podList)
deploys = enrichDeployWithPdb(deploys)
return deploys
}
func enrichDeployWithPdb(deploys []Deployment) (ret []Deployment) {
//TODO: improve performance in this func
pdbs := RetrievePdbs()
for _, deploy := range deploys {
if len(deploy.Pods) > 0 {
for _, pdb := range pdbs {
if pdb.match(deploy.Pods[0].Metadata.Labels) {
deploy.Pdb = pdb
break
}
}
}
ret = append(ret, deploy)
}
return ret
}
const patternOld = `(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*`
const pattern = `(\S*)\s*(\S*)\s*(\S*)\/(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*`
func buildDeploymentList(data string, nsFilter string, podList []Pod) []Deployment {
podsMap := make(map[string][]Pod)
for _, pod := range podList {
deployment := pod.GetDeploymentdKey()
if podsMap[deployment] == nil {
podsMap[deployment] = []Pod{pod}
} else {
podsMap[deployment] = append(podsMap[deployment], pod)
}
}
var deployments []Deployment
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
txt := scanner.Text()
var reg *regexp.Regexp
if match, _ := regexp.MatchString(pattern, txt); match {
reg, _ = regexp.Compile(pattern)
} else {
reg, _ = regexp.Compile(patternOld)
}
groups := reg.FindStringSubmatch(txt)
mamespace := groups[1]
if nsFilter == "" || nsFilter == mamespace {
replicasReady, _ := strconv.Atoi(groups[3])
replicasExpected, _ := strconv.Atoi(groups[4])
upToDate, _ := strconv.Atoi(groups[5])
avaliable, _ := strconv.Atoi(groups[6])
deployment := Deployment{
Namespace: mamespace,
Name: groups[2],
Replicas: replicasReady,
ReplicasExpected: replicasExpected,
UpToDate: upToDate,
Avaliable: avaliable,
Age: groups[7],
}
deployment.Pods = podsMap[deployment.GetDeploymentKey()]
deployments = append(deployments, deployment)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return deployments
}