-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
467 lines (425 loc) · 21.6 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strconv"
"text/tabwriter"
"time"
)
const version = "0.1.3"
const versionDesciption = "Small change to improve get deployment name method"
// TODO: sort-by ? How to handle the below scenarios?
func main() {
p := flag.String("p", "", "Filter by the pod name (default:empty means all pods)")
d := flag.String("d", "", "Filter by the deployment name (default:empty means all deployments)")
n := flag.String("n", "", "Filter by namespace name (default:empty means all namespaces)")
v := flag.Bool("v", false, "Show the plugin version")
show := flag.String("print", "all", "Define what will be printed. Valid values all|pods|hpas|nodes ")
csv := flag.String("csv-output", "", "Save the result to files with format 'kubectl-snapshot-<date>-<csv-output>-<pods|hpas|nohpa|nodes|all>.csv'")
debug := flag.Bool("debug", false, "Show debug info")
flag.Parse()
printFlags(*p, *d, *n, *v, *show, *csv, *debug)
if *v || *debug {
fmt.Printf("Plugin Version: %s (%s)\n", version, versionDesciption)
if *v {
os.Exit(0)
}
}
csvFilePrefix := ""
if *csv != "" {
now := time.Now()
csvFilePrefix = now.Format(fmt.Sprintf("kubectl-snapshot-2006-01-02-1504-%s", *csv))
}
// Pods with resource usage (top) ..
podList := RetrievePods(*n)
if *p != "" {
podList = filterPod(podList, func(pod Pod) bool { return pod.Metadata.Name == *p })
} else if *d != "" {
podList = filterPod(podList, func(pod Pod) bool { return pod.GetDeploymentName() == *d })
}
// Hpas, use podList to confirm resource usgage ..
hpaList := RetrieveHpas(*n, podList)
if *p != "" {
hpaList = filterHpa(hpaList, func(h Hpa) bool { return h.ContainsPod(*p) })
} else if *d != "" {
hpaList = filterHpa(hpaList, func(h Hpa) bool { return h.RefToDeployment(*d) })
}
// Deployments for non-hpas, use podList to confirm resource usgage ..
deploymentList := RetrieveDeployments(*n, podList)
if *p != "" {
deploymentList = filterDeployment(deploymentList, func(deploy Deployment) bool { return deploy.ContainsPod(*p) })
} else if *d != "" {
deploymentList = filterDeployment(deploymentList, func(deploy Deployment) bool { return deploy.Name == *d })
}
hpaMap := make(map[string]Hpa)
for _, hpa := range hpaList {
hpaMap[hpa.Namespace+"|"+hpa.ReferenceName] = hpa
}
deploymentWithoutHpa := []Deployment{}
for _, deploy := range deploymentList {
if _, hasHpa := hpaMap[deploy.GetDeploymentKey()]; !hasHpa {
deploymentWithoutHpa = append(deploymentWithoutHpa, deploy)
}
}
// Nodes, use podList to confirm resource usgage ..
nodeList := RetrieveNodes(podList)
// TODO: filter
// Print standard io or send to csv files ..
switch *show {
case "pod":
case "pods":
printPodsTab(podList, csvFilePrefix, *debug)
case "hpa":
case "hpas":
printHpaTab(hpaList, csvFilePrefix, *debug)
printNoHpaTab(deploymentWithoutHpa, csvFilePrefix, *debug)
case "node":
case "nodes":
printNodesTab(nodeList, csvFilePrefix, *debug)
default:
printPodsTab(podList, csvFilePrefix, *debug)
printHpaTab(hpaList, csvFilePrefix, *debug)
printNoHpaTab(deploymentWithoutHpa, csvFilePrefix, *debug)
printNodesTab(nodeList, csvFilePrefix, *debug)
}
}
func printFlags(p string, d string, n string, v bool, show string, csv string, debug bool) {
if debug {
fmt.Println("---------------------------------------------")
fmt.Println("[debug] FLAGS: ")
fmt.Println(" -p [POD] is: ", p)
fmt.Println(" -d [DEPLOYMENT] is: ", d)
fmt.Println(" -o [NAMESPACE] is: ", n)
fmt.Println(" -v [VERSION] is: ", v)
fmt.Println(" -print [PRINT IN STANDARD OUTPUT] is: ", show)
fmt.Println(" -csv-output [SAVE TO FILES] is: ", csv)
fmt.Println("---------------------------------------------")
fmt.Println()
}
}
func printPodsTab(podList []Pod, csvFilePrefix string, debug bool) {
result := Wrapper{Pods: podList}
if csvFilePrefix == "" || debug {
formatHeader := "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n"
formatValues := "%v\t%v\t%vm\t%vm\t%0.2f%%\t%vMi\t%vMi\t%0.2f%%\t%vm\t%vMi\t%v\n"
fmt.Println("\nPODs SNAPSHOT:")
w := tabwriter.NewWriter(os.Stdout, 0, 1, 2, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, formatHeader, "Namespace", "Pod Name", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)")
fmt.Fprintf(w, formatHeader, "---------", "--------", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------")
for _, pod := range result.Pods {
fmt.Fprintf(w, formatValues, pod.Metadata.Namespace, pod.Metadata.Name, pod.GetRequestsMilliCPU(), pod.GetTopMilliCPU(), pod.GetUsageCPU(), pod.GetRequestsMiMemory(), pod.GetTopMiMemory(), pod.GetUsageMemory(), pod.GetLimitsMilliCPU(), pod.GetLimitsMiMemory(), pod.GetStartupDuration())
}
fmt.Fprintf(w, formatHeader, " ", " ", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------")
fmt.Fprintf(w, formatValues, " ", " ", result.GetRequestsMilliCPU(), result.GetTopMilliCPU(), result.GetUsageCPU(), result.GetRequestsMiMemory(), result.GetTopMiMemory(), result.GetUsageMemory(), result.GetLimitsMilliCPU(), result.GetLimitsMiMemory(), "")
w.Flush()
}
if csvFilePrefix != "" {
file, err := os.Create(csvFilePrefix + "-pods.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"Namespace", "Pod Name", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)"}
err = writer.Write(header)
if err != nil {
log.Fatal(err)
}
for _, pod := range result.Pods {
line := []string{pod.Metadata.Namespace, pod.Metadata.Name, strconv.Itoa(pod.GetRequestsMilliCPU()), strconv.Itoa(pod.GetTopMilliCPU()), fmt.Sprintf("%.2f", pod.GetUsageCPU()), strconv.Itoa(pod.GetRequestsMiMemory()), strconv.Itoa(pod.GetTopMiMemory()), fmt.Sprintf("%.2f", pod.GetUsageMemory()), strconv.Itoa(pod.GetLimitsMilliCPU()), strconv.Itoa(pod.GetLimitsMiMemory()), fmt.Sprintf("%s", pod.GetStartupDuration())}
err := writer.Write(line)
if err != nil {
log.Fatal(err)
}
}
}
}
func printHpaTab(hpaList []Hpa, csvFilePrefix string, debug bool) {
if csvFilePrefix == "" || debug {
formatHeader := "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n"
formatValues := "%v\t%v\t%v\t%v\t%v\t%v\t%vm\t%vm\t%0.2f%%\t%vMi\t%vMi\t%0.2f%%\t%vm\t%vMi\t%v\t%v\t%v\t%v\t%v\t%v\n"
fmt.Println("\nHPAs SNAPSHOT:")
w := tabwriter.NewWriter(os.Stdout, 0, 1, 2, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, formatHeader, "Namespace", "Hpa Name", "Reference", "Target", "Replicas (Min/Max/Actual)", "# Pods ->", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)", "PDB MinAvailable", "PDB MaxUnavailable", "Count Liveness Probe", "Count Readiness Probe", "Count Lifecycle PreStop")
fmt.Fprintf(w, formatHeader, "---------", "--------", "---------", "------", "-------------------------", "---------", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------", "----------------", "------------------", "--------------------", "---------------------", "-----------------------")
for _, hpa := range hpaList {
wp := Wrapper{Pods: hpa.Pods}
replicas := fmt.Sprintf("%d/%d/%d", hpa.MinPods, hpa.MaxPods, hpa.Replicas)
fmt.Fprintf(w, formatValues, hpa.Namespace, hpa.Name, hpa.GetReference(), hpa.GetUsageAndTarget(), replicas, len(hpa.Pods), wp.GetRequestsMilliCPU(), wp.GetTopMilliCPU(), wp.GetUsageCPU(), wp.GetRequestsMiMemory(), wp.GetTopMiMemory(), wp.GetUsageMemory(), wp.GetLimitsMilliCPU(), wp.GetLimitsMiMemory(), wp.GetAvgStartupDuration(), hpa.Pdb.Spec.MinAvailable, hpa.Pdb.Spec.MaxUnavailable, hpa.CountLivenessProbes(), hpa.CountReadinessProbes(), hpa.CountLifecyclePreStop())
}
fmt.Fprintf(w, formatHeader, " ", " ", " ", "------", "-------------------------", "---------", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------", "----------------", "------------------", "--------------------", "---------------------", "-----------------------")
w.Flush()
}
if csvFilePrefix != "" {
file, err := os.Create(csvFilePrefix + "-hpas.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"Namespace", "Hpa Name", "Reference", "Hpa Use(%)", "Hpa Target(%)", "Min Replicas", "Max Replicas", "Actual Replicas", "# Pods ->", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)", "PDB MinAvailable", "PDB MaxUnavailable", "Count Liveness Probe", "Count Readiness Probe", "Count Lifecycle PreStop", "Liveness Probe", "Readiness Probe", "Lifecycle PreStop"}
err = writer.Write(header)
if err != nil {
log.Fatal(err)
}
for _, hpa := range hpaList {
wp := Wrapper{Pods: hpa.Pods}
hpaUse := "<unknown>"
if hpa.UsageCPU != -1 {
hpaUse = strconv.Itoa(hpa.UsageCPU)
}
line := []string{hpa.Namespace, hpa.Name, hpa.GetReference(), hpaUse, strconv.Itoa(hpa.Target), strconv.Itoa(hpa.MinPods), strconv.Itoa(hpa.MaxPods), strconv.Itoa(hpa.Replicas), strconv.Itoa(len(hpa.Pods)), strconv.Itoa(wp.GetRequestsMilliCPU()), strconv.Itoa(wp.GetTopMilliCPU()), fmt.Sprintf("%.2f", wp.GetUsageCPU()), strconv.Itoa(wp.GetRequestsMiMemory()), strconv.Itoa(wp.GetTopMiMemory()), fmt.Sprintf("%.2f", wp.GetUsageMemory()), strconv.Itoa(wp.GetLimitsMilliCPU()), strconv.Itoa(wp.GetLimitsMiMemory()), fmt.Sprintf("%s", wp.GetAvgStartupDuration()), strconv.Itoa(hpa.Pdb.Spec.MinAvailable), strconv.Itoa(hpa.Pdb.Spec.MaxUnavailable), hpa.CountLivenessProbes(), hpa.CountReadinessProbes(), hpa.CountLifecyclePreStop(), hpa.GetLivenessProbes(), hpa.GetReadinessProbes(), hpa.GetLifecyclePreStop()}
err := writer.Write(line)
if err != nil {
log.Fatal(err)
}
}
}
}
func printNoHpaTab(deploymentWithoutHpa []Deployment, csvFilePrefix string, debug bool) {
if csvFilePrefix == "" || debug {
formatHeader := "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n"
formatValues := "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%vm\t%vm\t%0.2f%%\t%vMi\t%vMi\t%0.2f%%\t%vm\t%vMi\t%v\t%v\t%v\t%v\t%v\t%v\n"
fmt.Println("\nNO HPA SNAPSHOT:")
w := tabwriter.NewWriter(os.Stdout, 0, 1, 2, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, formatHeader, "Namespace", "Deployment Name", "Ready", "Up To Date", "Avaliable", "Age", "#Pods ->", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)", "PDB MinAvailable", "PDB MaxUnavailable", "Count Liveness Probe", "Count Readiness Probe", "Count Lifecycle PreStop")
fmt.Fprintf(w, formatHeader, "---------", "---------------", "-----", "----------", "---------", "---", "--------", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------", "----------------", "------------------", "--------------------", "---------------------", "-----------------------")
for _, deploy := range deploymentWithoutHpa {
wp := Wrapper{Pods: deploy.Pods}
ready := fmt.Sprintf("%d/%d", deploy.Replicas, deploy.ReplicasExpected)
fmt.Fprintf(w, formatValues, deploy.Namespace, deploy.Name, ready, deploy.UpToDate, deploy.Avaliable, deploy.Age, len(deploy.Pods), wp.GetRequestsMilliCPU(), wp.GetTopMilliCPU(), wp.GetUsageCPU(), wp.GetRequestsMiMemory(), wp.GetTopMiMemory(), wp.GetUsageMemory(), wp.GetLimitsMilliCPU(), wp.GetLimitsMiMemory(), wp.GetAvgStartupDuration(), deploy.Pdb.Spec.MinAvailable, deploy.Pdb.Spec.MaxUnavailable, deploy.CountLivenessProbes(), deploy.CountReadinessProbes(), deploy.CountLifecyclePreStop())
}
fmt.Fprintf(w, formatHeader, " ", " ", "-----", "----------", "---------", "---", "--------", "----------------", "-----------", "-------------", "--------------------", "---------------", "----------------", "--------------", "-------------------", "--------------------------", "----------------", "------------------", "--------------------", "---------------------", "-----------------------")
w.Flush()
}
if csvFilePrefix != "" {
file, err := os.Create(csvFilePrefix + "-nohpa.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"Namespace", "Deployment Name", "Replicas", "Expected Replicas", "Up To Date", "Avaliable", "Age", "#Pods ->", "Requests CPU (m)", "TOP CPU (m)", "Usage CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)", "PDB MinAvailable", "PDB MaxUnavailable", "Count Liveness Probe", "Count Readiness Probe", "Count Lifecycle PreStop", "Liveness Probe", "Readiness Probe", "Lifecycle PreStop"}
err = writer.Write(header)
if err != nil {
log.Fatal(err)
}
for _, deploy := range deploymentWithoutHpa {
wp := Wrapper{Pods: deploy.Pods}
line := []string{deploy.Namespace, deploy.Name, strconv.Itoa(deploy.Replicas), strconv.Itoa(deploy.ReplicasExpected), strconv.Itoa(deploy.UpToDate), strconv.Itoa(deploy.Avaliable), deploy.Age, strconv.Itoa(len(deploy.Pods)), strconv.Itoa(wp.GetRequestsMilliCPU()), strconv.Itoa(wp.GetTopMilliCPU()), fmt.Sprintf("%.2f", wp.GetUsageCPU()), strconv.Itoa(wp.GetRequestsMiMemory()), strconv.Itoa(wp.GetTopMiMemory()), fmt.Sprintf("%.2f", wp.GetUsageMemory()), strconv.Itoa(wp.GetLimitsMilliCPU()), strconv.Itoa(wp.GetLimitsMiMemory()), fmt.Sprintf("%s", wp.GetAvgStartupDuration()), strconv.Itoa(deploy.Pdb.Spec.MinAvailable), strconv.Itoa(deploy.Pdb.Spec.MaxUnavailable), deploy.CountLivenessProbes(), deploy.CountReadinessProbes(), deploy.CountLifecyclePreStop(), deploy.GetLivenessProbes(), deploy.GetReadinessProbes(), deploy.GetLifecyclePreStop()}
err := writer.Write(line)
if err != nil {
log.Fatal(err)
}
}
}
}
func printNodesTab(nodeList []Node, csvFilePrefix string, debug bool) {
allPods := Wrapper{Pods: []Pod{}}
if csvFilePrefix == "" || debug {
fmt.Println("\n\nNODEs SNAPSHOT:")
formatHeader := "%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\t%v\n"
formatValues := "%v\t%v\t%v\t%vm\t%vMi\t%v\t%vm\t%vm\t%0.2f%%\t%vMi\t%vMi\t%0.2f%%\t%vm\t%vMi\t%v\n"
tw := tabwriter.NewWriter(os.Stdout, 0, 1, 2, ' ', tabwriter.TabIndent)
fmt.Fprintf(tw, formatHeader, "Node", "Node Pool", "Allocatable Pods", "Allocatable CPU (m)", "Allocatable Memory (Mi)", "Actual Num Pods", "Requests CPU (m)", "TOP CPU (m)", "Usage Requests CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Requests Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)")
fmt.Fprintf(tw, formatHeader, "----", "---------", "----------------", "-------------------", "-----------------------", "---------------", "----------------", "-----------", "----------------------", "--------------------", "---------------", "-------------------------", "--------------", "-------------------", "--------------------------")
min := 999
max := 0
total := 0
allocatableMilliCPU := 0
allocatableMiMemory := 0
for _, node := range nodeList {
nodeName := node.GetName()
pods := node.Pods
allPods.Pods = append(allPods.Pods, pods...)
nPods := len(pods)
total += nPods
if nPods > max {
max = nPods
}
if min > nPods {
min = nPods
}
allocatableMilliCPU += node.GetAllocatableMilliCPU()
allocatableMiMemory += node.GetAllocatableMiMemory()
w := Wrapper{Pods: pods}
fmt.Fprintf(tw, formatValues, nodeName, node.GetNodepool(), node.GetAllocatablePods(), node.GetAllocatableMilliCPU(), node.GetAllocatableMiMemory(), nPods, w.GetRequestsMilliCPU(), w.GetTopMilliCPU(), w.GetUsageCPU(), w.GetRequestsMiMemory(), w.GetTopMiMemory(), w.GetUsageMemory(), w.GetLimitsMilliCPU(), w.GetLimitsMiMemory(), w.GetAvgStartupDuration())
}
avg := 0
if len(nodeList) > 0 {
avg = total / len(nodeList)
} else {
min = 0
}
fmt.Fprintf(tw, formatHeader, " ", " ", " ", "-------------------", "-----------------------", "----------------", "----------------", "-----------", "----------------------", "--------------------", "---------------", "-------------------------", "--------------", "-------------------", "--------------------------")
summaryPods := fmt.Sprintf("Min:%d/Max:%d/Avg:%d", min, max, avg)
fmt.Fprintf(tw, formatValues, " ", " ", " ", allocatableMilliCPU, allocatableMiMemory, summaryPods, allPods.GetRequestsMilliCPU(), allPods.GetTopMilliCPU(), allPods.GetUsageCPU(), allPods.GetRequestsMiMemory(), allPods.GetTopMiMemory(), allPods.GetUsageMemory(), allPods.GetLimitsMilliCPU(), allPods.GetLimitsMiMemory(), "")
tw.Flush()
if debug {
fmt.Println()
fmt.Println("---------------------------------------------")
fmt.Println("[debug] PODS IN EACH NODE: ")
for _, node := range nodeList {
nodeName := node.GetName()
pods := node.Pods
fmt.Printf(" - %s\n [ ", nodeName)
for _, pod := range pods {
fmt.Printf("%s ", pod.GetPodKey())
}
fmt.Println("]")
}
fmt.Println("---------------------------------------------")
}
}
if csvFilePrefix != "" {
file, err := os.Create(csvFilePrefix + "-nodes.csv")
if err != nil {
log.Fatal(err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
header := []string{"Node", "Node Pool", "Allocatable Pods", "Allocatable CPU (m)", "Allocatable Memory (Mi)", "Actual Num Pods", "Requests CPU (m)", "TOP CPU (m)", "Usage Requests CPU (%)", "Requests Memory (Mi)", "TOP Memory (Mi)", "Usage Requests Memory (%)", "Limits CPU (m)", "Limitis Memory (Mi)", "Pod Startup Duration (AVG)"}
err = writer.Write(header)
if err != nil {
log.Fatal(err)
}
for _, node := range nodeList {
nodeName := node.GetName()
pods := node.Pods
nPods := len(pods)
w := Wrapper{Pods: pods}
line := []string{nodeName, node.GetNodepool(), strconv.Itoa(node.GetAllocatablePods()), strconv.Itoa(node.GetAllocatableMilliCPU()), strconv.Itoa(node.GetAllocatableMiMemory()), strconv.Itoa(nPods), strconv.Itoa(w.GetRequestsMilliCPU()), strconv.Itoa(w.GetTopMilliCPU()), fmt.Sprintf("%.2f", w.GetUsageCPU()), strconv.Itoa(w.GetRequestsMiMemory()), strconv.Itoa(w.GetTopMiMemory()), fmt.Sprintf("%.2f", w.GetUsageMemory()), strconv.Itoa(w.GetLimitsMilliCPU()), strconv.Itoa(w.GetLimitsMiMemory()), fmt.Sprintf("%s", w.GetAvgStartupDuration())}
err := writer.Write(line)
if err != nil {
log.Fatal(err)
}
}
}
}
// Wrapper contains a list of pods
type Wrapper struct {
Pods []Pod
}
// GetRequestsMilliCPU total
func (d Wrapper) GetRequestsMilliCPU() int {
total := 0
for _, p := range d.Pods {
total += p.GetRequestsMilliCPU()
}
return total
}
// GetTopMilliCPU total
func (d Wrapper) GetTopMilliCPU() int {
total := 0
for _, p := range d.Pods {
total += p.Top.GetMilliCPU()
}
return total
}
// GetUsageCPU % usage
func (d Wrapper) GetUsageCPU() float32 {
requests, top := 0, 0
for _, p := range d.Pods {
requests += p.GetRequestsMilliCPU()
top += p.Top.GetMilliCPU()
}
if top == 0 && requests != 0 {
return float32(0)
} else if requests == 0 {
return float32(100)
}
return float32(top) / float32(requests) * 100
}
// GetRequestsMiMemory total
func (d Wrapper) GetRequestsMiMemory() int {
total := 0
for _, p := range d.Pods {
total += p.GetRequestsMiMemory()
}
return total
}
// GetTopMiMemory total
func (d Wrapper) GetTopMiMemory() int {
total := 0
for _, p := range d.Pods {
total += p.Top.GetMiMemory()
}
return total
}
// GetUsageMemory % usage
func (d Wrapper) GetUsageMemory() float32 {
requests, top := 0, 0
for _, p := range d.Pods {
requests += p.GetRequestsMiMemory()
top += p.Top.GetMiMemory()
}
if top == 0 && requests != 0 {
return float32(0)
} else if requests == 0 {
return float32(100)
}
return float32(top) / float32(requests) * 100
}
// GetLimitsMilliCPU total
func (d Wrapper) GetLimitsMilliCPU() int {
total := 0
for _, p := range d.Pods {
total += p.GetLimitsMilliCPU()
}
return total
}
// GetLimitsMiMemory total
func (d Wrapper) GetLimitsMiMemory() int {
total := 0
for _, p := range d.Pods {
total += p.GetLimitsMiMemory()
}
return total
}
// GetAvgStartupDuration avg
func (d Wrapper) GetAvgStartupDuration() time.Duration {
total := time.Duration(0)
count := 0
for _, p := range d.Pods {
d := p.GetStartupDuration()
if d != time.Duration(0) {
total = total + d
count = count + 1
}
}
if count == 0 {
return time.Duration(0)
}
return time.Duration(int64(total) / int64(count))
}
func filterPod(podList []Pod, test func(Pod) bool) (ret []Pod) {
for _, pod := range podList {
if test(pod) {
ret = append(ret, pod)
}
}
return
}
func filterHpa(hpaList []Hpa, test func(Hpa) bool) (ret []Hpa) {
for _, hpa := range hpaList {
if test(hpa) {
ret = append(ret, hpa)
}
}
return
}
func filterDeployment(deploymentList []Deployment, test func(Deployment) bool) (ret []Deployment) {
for _, deploy := range deploymentList {
if test(deploy) {
ret = append(ret, deploy)
}
}
return
}