-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhpa.go
215 lines (194 loc) · 5.01 KB
/
hpa.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
)
// Hpa struct
type Hpa struct {
Namespace string
Name string
ReferenceKind string
ReferenceName string
UsageCPU int
Target int
MinPods int
MaxPods int
Replicas int
Age string
Pods []Pod
Pdb Pdb
}
// GetDeploymentKey should work for most of the cases
func (h Hpa) GetDeploymentKey() string {
return h.Namespace + "|" + h.GetReference()
}
// GetReference ..
func (h Hpa) GetReference() string {
return h.ReferenceKind + "/" + h.ReferenceName
}
// GetUsageAndTarget ..
func (h Hpa) GetUsageAndTarget() string {
if h.UsageCPU == -1 {
return "<unknown>/" + strconv.Itoa(h.Target) + "%"
}
return strconv.Itoa(h.UsageCPU) + "%/" + strconv.Itoa(h.Target) + "%"
}
// IsDeployment ..
func (h Hpa) IsDeployment() bool {
return h.ReferenceKind == "Deployment"
}
// RefToDeployment ..
func (h Hpa) RefToDeployment(deployment string) bool {
return h.IsDeployment() && h.ReferenceName == deployment
}
// ContainsPod ..
func (h Hpa) ContainsPod(pod string) bool {
set := make(map[string]bool, len(h.Pods))
for _, pod := range h.Pods {
set[pod.Metadata.Name] = true
}
_, ok := set[pod]
return ok
}
// CountLivenessProbes ..
func (h Hpa) CountLivenessProbes() string {
if len(h.Pods) > 0 {
return h.Pods[0].CountLivenessProbes()
}
return "N/A"
}
// CountReadinessProbes ..
func (h Hpa) CountReadinessProbes() string {
if len(h.Pods) > 0 {
return h.Pods[0].CountReadinessProbes()
}
return "N/A"
}
// CountLifecyclePreStop ..
func (h Hpa) CountLifecyclePreStop() string {
if len(h.Pods) > 0 {
return h.Pods[0].CountLifecyclePreStop()
}
return "N/A"
}
// GetLivenessProbes ..
func (h Hpa) GetLivenessProbes() string {
if len(h.Pods) > 0 {
return h.Pods[0].GetLivenessProbes()
}
return "N/A"
}
// GetReadinessProbes ..
func (h Hpa) GetReadinessProbes() string {
if len(h.Pods) > 0 {
return h.Pods[0].GetReadinessProbes()
}
return "N/A"
}
// GetLifecyclePreStop ..
func (h Hpa) GetLifecyclePreStop() string {
if len(h.Pods) > 0 {
return h.Pods[0].GetLifecyclePreStop()
}
return "N/A"
}
// RetrieveHpas executes kubectl get hpas command
// if ns is empty, then all namespaces are used
func RetrieveHpas(nsFilter string, podList []Pod) []Hpa {
cmd := "kubectl get hpa --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)
hpas := buildHpaList(data, nsFilter, podList)
hpas = enrichHpaWithPdb(hpas)
return hpas
}
func enrichHpaWithPdb(hpas []Hpa) (ret []Hpa) {
//TODO: improve performance in this func
pdbs := RetrievePdbs()
for _, hpa := range hpas {
if len(hpa.Pods) > 0 {
for _, pdb := range pdbs {
if pdb.match(hpa.Pods[0].Metadata.Labels) {
hpa.Pdb = pdb
break
}
}
}
ret = append(ret, hpa)
}
return ret
}
func buildHpaList(data string, nsFilter string, podList []Pod) (hpas []Hpa) {
deploymentMap, replicaSetMap := buildPodMaps(podList)
scanner := bufio.NewScanner(strings.NewReader(data))
for scanner.Scan() {
reg, _ := regexp.Compile(`(\S*)\s*(\S*)\s*(\S*)\/(\S*)\s*((\S*)%|(<unknown>))\/(\S*)%\s*(\S*)\s*(\S*)\s*(\S*)\s*(\S*)\s*`)
txt := scanner.Text()
groups := reg.FindStringSubmatch(txt)
if len(groups) > 0 {
mamespace := groups[1]
if nsFilter == "" || nsFilter == mamespace {
usageCPU := -1
if groups[6] != "" {
usageCPU, _ = strconv.Atoi(groups[6])
}
target, _ := strconv.Atoi(groups[8])
minPods, _ := strconv.Atoi(groups[9])
maxPods, _ := strconv.Atoi(groups[10])
replicas, _ := strconv.Atoi(groups[11])
hpa := Hpa{
Namespace: mamespace,
Name: groups[2],
ReferenceKind: groups[3],
ReferenceName: groups[4],
UsageCPU: usageCPU,
Target: target,
MinPods: minPods,
MaxPods: maxPods,
Replicas: replicas,
Age: groups[12],
}
// enrich hpa with pods
key := hpa.Namespace + "|" + hpa.ReferenceName
if hpa.ReferenceKind == "Deployment" {
hpa.Pods = deploymentMap[key]
} else if hpa.ReferenceKind == "ReplicaSet" {
hpa.Pods = replicaSetMap[key]
} else {
// not implemented - return empty pods
}
hpas = append(hpas, hpa)
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return hpas
}
func buildPodMaps(podList []Pod) (map[string][]Pod, map[string][]Pod) {
deploymentMap := make(map[string][]Pod)
replicaSetMap := make(map[string][]Pod)
for _, pod := range podList {
deployment := pod.GetDeploymentdKey()
if deploymentMap[deployment] == nil {
deploymentMap[deployment] = []Pod{pod}
} else {
deploymentMap[deployment] = append(deploymentMap[deployment], pod)
}
replicaset := pod.GetReplicaSetKey()
if replicaSetMap[replicaset] == nil {
replicaSetMap[replicaset] = []Pod{pod}
} else {
replicaSetMap[replicaset] = append(replicaSetMap[replicaset], pod)
}
}
return deploymentMap, replicaSetMap
}