-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
561 lines (463 loc) · 14.4 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package main
import (
"bytes"
"encoding/csv"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned"
kcexternalversions "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/informers/externalversions"
yaml "gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
var appList = []string{"accelerator", "api-auto-registration", "api-portal", "appliveview", "appliveview-apiserver", "appliveview-connector", "appliveview-conventions",
"appsso", "bitnami-services", "buildservice", "cartographer", "cert-manager", "cnrs", "contour", "crossplane", "developer-conventions", "eventing",
"fluxcd-source-controller", "grype", "learningcenter", "learningcenter-workshops", "metadata-store", "namespace-provisioner", "ootb-delivery-basic",
"ootb-supply-chain-basic", "ootb-templates", "policy-controller", "scanning", "service-bindings", "services-toolkit", "source-controller", "spring-boot-conventions",
"tap", "tap-auth", "tap-gui", "tap-telemetry", "tekton-pipelines"}
type appDetails struct {
name string
namespace string
creationTime metav1.Time
fetchTime metav1.Time
collectedTime metav1.Time
}
type result struct {
detail map[string]appDetails // key will be app name
lock sync.Mutex
}
var installationDetail = result{
detail: make(map[string]appDetails),
}
type handler struct {
stopperChan chan struct{}
watchError error
}
type AppStatusDiff struct {
old kcv1alpha1.AppStatus
new kcv1alpha1.AppStatus
}
var appNamespaces = make(map[string][]string) // map of app and namespaces used by that app
func main() {
fmt.Printf("\n**** Collecting apps installation time... ***\n")
config, err := clientcmd.BuildConfigFromFlags("", "/Users/ktanushree/.kube/config") // need to change it to take from user
if err != nil {
log.Fatal(err)
}
// create the clientset
clientset, err := versioned.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
wg := new(sync.WaitGroup)
for _, appName := range appList {
wg.Add(1)
go sharedInformer(clientset, appName, wg)
}
wg.Wait()
writeInstallTimeIntoCSV()
// printMetrics()
// fetch cpu-memory usage by TAP components
for app, ns_list := range appNamespaces {
fmt.Printf("\nAppname: %s, Namespaces: %+v\n", app, ns_list)
fetchTapAppCPUMemUsage(ns_list, app)
}
writeCPUAndMemUsageIntoCSV()
}
func sharedInformer(clientset *versioned.Clientset, appName string, wg *sync.WaitGroup) {
defer wg.Done()
stopperChan := make(chan struct{})
shareInformer := kcexternalversions.NewFilteredSharedInformerFactory(clientset, 10*time.Second, "tap-install", func(opts *metav1.ListOptions) {
opts.FieldSelector = fmt.Sprintf("metadata.name=%s", appName)
})
h := handler{
stopperChan: stopperChan,
}
informer := shareInformer.Kappctrl().V1alpha1().Apps().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: h.udpateEventHandler,
})
go informer.Run(h.stopperChan)
if !cache.WaitForCacheSync(stopperChan, informer.HasSynced) {
fmt.Errorf("Timed out waiting for caches to sync")
return
}
<-h.stopperChan
if h.watchError != nil {
fmt.Errorf("Reconciling app: %s", h.watchError)
return
}
}
func (h *handler) udpateEventHandler(oldObj interface{}, newObj interface{}) {
newApp, _ := newObj.(*kcv1alpha1.App)
oldApp, _ := oldObj.(*kcv1alpha1.App)
installationDetail.lock.Lock()
defer installationDetail.lock.Unlock()
if _, found := installationDetail.detail[newApp.Name]; !found {
if newApp.Status.ConsecutiveReconcileSuccesses == 1 {
installationDetail.detail[newApp.Name] = appDetails{
name: newApp.Name,
namespace: newApp.Namespace,
creationTime: newApp.CreationTimestamp,
fetchTime: newApp.Status.Fetch.StartedAt,
collectedTime: metav1.Now(),
}
appNamespaces[newApp.Name] = append(appNamespaces[newApp.Name], newApp.Status.Deploy.KappDeployStatus.AssociatedResources.Namespaces...)
}
}
appStatus := AppStatusDiff{
old: oldApp.Status,
new: newApp.Status,
}
stopWatch, err := appStatus.CheckAppStatus(newApp.Name)
if err != nil {
fmt.Errorf("Error: %s\n", err.Error())
}
if stopWatch {
h.stopWatch()
}
}
func (h *handler) stopWatch() {
close(h.stopperChan)
}
// Check apps status
func (d *AppStatusDiff) CheckAppStatus(app string) (bool, error) {
if d.new.Fetch != nil {
if d.old.Fetch == nil || !d.old.Fetch.UpdatedAt.Equal(&d.new.Fetch.UpdatedAt) {
if d.new.Fetch.ExitCode != 0 && d.new.Fetch.UpdatedAt.Unix() >= d.new.Fetch.StartedAt.Unix() {
msg := "Fetch failed"
return true, fmt.Errorf(msg)
}
}
}
if d.new.Template != nil {
if d.old.Template == nil || !d.old.Template.UpdatedAt.Equal(&d.new.Template.UpdatedAt) {
if d.new.Template.ExitCode != 0 {
msg := "Template failed"
return true, fmt.Errorf(msg)
}
}
}
if d.new.Deploy != nil {
isDeleting := IsDeleting(d.new)
ongoingOp := "Deploy"
if isDeleting {
ongoingOp = "Delete"
}
if d.old.Deploy == nil || !d.old.Deploy.UpdatedAt.Equal(&d.new.Deploy.UpdatedAt) {
if d.new.Deploy.ExitCode != 0 && d.new.Deploy.Finished {
msg := fmt.Sprintf("%s failed", ongoingOp)
return true, fmt.Errorf(msg)
}
}
}
if HasReconciled(d.new) {
return true, nil
}
failed, errMsg := HasFailed(d.new)
if failed {
return true, fmt.Errorf(errMsg)
}
return false, nil
}
func HasReconciled(status kcv1alpha1.AppStatus) bool {
for _, condition := range status.Conditions {
if condition.Type == kcv1alpha1.ReconcileSucceeded && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}
func HasFailed(status kcv1alpha1.AppStatus) (bool, string) {
for _, condition := range status.Conditions {
if condition.Type == kcv1alpha1.ReconcileFailed && condition.Status == corev1.ConditionTrue {
return true, fmt.Sprintf("%s: %s", kcv1alpha1.ReconcileFailed, status.UsefulErrorMessage)
}
if condition.Type == kcv1alpha1.DeleteFailed && condition.Status == corev1.ConditionTrue {
return true, fmt.Sprintf("%s: %s", kcv1alpha1.DeleteFailed, status.UsefulErrorMessage)
}
}
return false, ""
}
func IsDeleting(status kcv1alpha1.AppStatus) bool {
for _, condition := range status.Conditions {
if condition.Type == kcv1alpha1.Deleting && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}
func writeInstallTimeIntoCSV() {
file, err := os.Create("install_time.csv") // later we can change it to take user input
defer file.Close()
if err != nil {
log.Fatalln("failed to open file", err)
}
w := csv.NewWriter(file)
defer w.Flush()
row := []string{"AppName", "CreationTimestamp", "FetchStartedAt", "DataCollectedAt", "completion-start", "completion-fetch"}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
}
installationDetail.lock.Lock()
defer installationDetail.lock.Unlock()
for _, record := range installationDetail.detail {
row := []string{record.name, record.creationTime.UTC().String(), record.fetchTime.UTC().String(), record.collectedTime.UTC().String(), installTimeTaken(record.creationTime, record.collectedTime), installTimeTaken(record.fetchTime, record.collectedTime)}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
}
}
}
func installTimeTaken(startTime, endTime metav1.Time) string {
installTime := int((endTime.Time).Sub(startTime.Time).Seconds())
return fmt.Sprintf("%dm %ds", installTime/60, installTime%60)
}
// *******************************************************************
type Container struct {
Name string `yaml:"name"`
Usage struct {
Cpu string `yaml:"cpu"`
Memory string `yaml:"memory"`
} `yaml:"usage"`
}
type ContainerResource struct {
Limits struct {
Cpu string `yaml:"cpu"`
Memory string `yaml:"memory"`
} `yaml:"limits"`
Requests struct {
Cpu string `yaml:"cpu"`
Memory string `yaml:"memory"`
} `yaml:"requests"`
}
type PodMetricYml struct {
Containers []Container `yaml:"containers"`
}
type Args struct {
Name string `yaml:"name"`
Resource ContainerResource `yaml:"resources"`
}
type PodYml struct {
Spec struct {
Containers []Args `yaml:"containers"`
} `yaml:"spec"`
}
type ContainerMetric struct {
Container
CPUReq string
MemReq string
CPULimit string
MemLimit string
}
type PodMetric struct {
AppName string
PodName string
Namespace string
Containers []ContainerMetric
TotalCPU string
TotalMemory string
}
type AppCPUMemUsage struct {
PodsMetric map[string][]PodMetric // appName will be key
Lock sync.Mutex
}
var AppMetric = AppCPUMemUsage{
PodsMetric: make(map[string][]PodMetric),
}
func runCmd(args []string) (error, string) {
var (
stderr, stdout bytes.Buffer
)
cmd := exec.Command("kubectl", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("Error in run cmd: %s\n", stderr.String()), ""
}
return nil, stdout.String()
}
func fetchTapAppCPUMemUsage(namespaces []string, appName string) {
wg := new(sync.WaitGroup)
for _, ns := range namespaces {
if ns == "kube-system" { // few apps are creating clusterRole, roleBinding kind of resources in this namespace
continue
}
wg.Add(1)
go pod_list(ns, appName, wg)
}
wg.Wait()
}
func pod_list(ns, appName string, wg *sync.WaitGroup) {
defer wg.Done()
var (
err error
out string
)
wg_pod := new(sync.WaitGroup)
// List all pods in namespace
err, out = runCmd([]string{"get", "pods", "-n", ns, "--no-headers", "-o", "custom-columns=:metadata.name"})
if err != nil {
fmt.Errorf("Error-pod: %s\n", err.Error())
return
}
pod_list := strings.Split(out, "\n")
fmt.Printf("\nNamespace: %s, pod list: %+v\n", ns, pod_list)
for _, pod := range pod_list {
if pod == "" {
continue
}
wg_pod.Add(1)
go fetch_cpu_mem_usage(pod, ns, appName, wg_pod)
}
wg_pod.Wait()
}
func fetch_cpu_mem_usage(pod, ns, appName string, wg *sync.WaitGroup) {
defer wg.Done()
// Fetch podMetrics for pods, it will give metrics per container in given pod
err, out := runCmd([]string{"get", "PodMetrics", pod, "-n", ns, "-oyaml"})
if err != nil {
fmt.Errorf("Error-data-fetch: %s\n", err.Error())
return
}
metricYml := PodMetricYml{}
err = yaml.Unmarshal([]byte(out), &metricYml)
if err != nil {
fmt.Errorf("Unmarshal error: %s\n", err.Error())
return
}
// Fetch podMetric of pod, it will give metric per pod
err, out = runCmd([]string{"get", "PodMetrics", pod, "-n", ns, "--no-headers"})
if err != nil {
fmt.Errorf("Error-data-fetch: %s\n", err.Error())
return
}
usageByPod := strings.Split(out, " ")
err, out = runCmd([]string{"get", "pod", pod, "-n", ns, "-oyaml"})
if err != nil {
fmt.Errorf("Error-data-fetch: %s\n", err.Error())
return
}
conRes := PodYml{}
err = yaml.Unmarshal([]byte(out), &conRes)
if err != nil {
fmt.Errorf("Unmarshal error: %s\n", err.Error())
return
}
cMap := make(map[string]ContainerResource)
for _, v := range conRes.Spec.Containers {
cMap[v.Name] = v.Resource
}
contMet := []ContainerMetric{}
for _, container := range metricYml.Containers {
v := cMap[container.Name]
contMet = append(contMet, ContainerMetric{
Container: container,
CPUReq: v.Requests.Cpu,
MemReq: v.Requests.Memory,
CPULimit: v.Limits.Cpu,
MemLimit: v.Limits.Memory,
})
}
temp := PodMetric{
AppName: appName,
PodName: pod,
Namespace: ns,
Containers: contMet,
TotalCPU: usageByPod[1],
TotalMemory: usageByPod[2],
}
writeToMap(&temp, appName)
}
func writeToMap(tmp *PodMetric, appName string) {
AppMetric.Lock.Lock()
defer AppMetric.Lock.Unlock()
AppMetric.PodsMetric[appName] = append(AppMetric.PodsMetric[appName], *tmp)
fmt.Printf("\nApp: %s, Pod metric lis: %+v\n", appName, AppMetric.PodsMetric[appName])
}
func printMetrics() {
AppMetric.Lock.Lock()
defer AppMetric.Lock.Unlock()
fmt.Printf("\nCPU and Memory usage by TAP: %+v\n", AppMetric.PodsMetric)
}
func writeCPUAndMemUsageIntoCSV() {
file, err := os.Create("cpu_mem_usage2.csv")
if err != nil {
log.Fatalln("File creation error: \n", err.Error())
}
defer file.Close()
w := csv.NewWriter(file)
defer w.Flush()
row := []string{"AppName", "PodName", "ContainerName", "CPU Usage", "Memory Usage", "CPU Requested", "Memory Requested", "CPU Limits", "Memory Limits"}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
return
}
AppMetric.Lock.Lock()
defer AppMetric.Lock.Unlock()
for appName, podMetrics := range AppMetric.PodsMetric {
row = []string{appName}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
return
}
for _, podMetric := range podMetrics {
row = []string{" ", podMetric.PodName, " ", convertCPUUnitToMili(podMetric.TotalCPU), convertMemoryUnitToMB(podMetric.TotalMemory)}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
return
}
for _, container := range podMetric.Containers {
row = []string{" ", " ", container.Name, convertCPUUnitToMili(container.Usage.Cpu), convertMemoryUnitToMB(container.Usage.Memory), container.CPUReq, container.MemReq, container.CPULimit, container.MemLimit}
if err := w.Write(row); err != nil {
log.Fatalln("error writing record to file", err)
return
}
}
}
}
}
func convertMemoryUnitToMB(valueInKB string) string {
valueInKB = strings.ReplaceAll(valueInKB, " ", "")
if valueInKB == "" {
return valueInKB
}
endChar := valueInKB[len(valueInKB)-2:]
if endChar == "Mi" || endChar == "Gi" {
return valueInKB
}
valueFloat, err := strconv.ParseFloat(strings.Split(valueInKB, "Ki")[0], 32)
if err != nil {
fmt.Errorf("Error in converting string [%s] into float1: %s\n", valueInKB, err.Error())
return " "
}
valueFloat /= 1024.0
return fmt.Sprintf("%.3fMi", valueFloat)
}
func convertCPUUnitToMili(valueInNano string) string {
valueInNano = strings.ReplaceAll(valueInNano, " ", "")
endChar := valueInNano[len(valueInNano)-1:]
if valueInNano == "" || endChar == "m" {
return valueInNano
}
valueFloat, err := strconv.ParseFloat(valueInNano[:len(valueInNano)-1], 32)
if err != nil {
fmt.Errorf("Error in converting string [%s] to float2: %s\n", valueInNano, err.Error())
return " "
}
if endChar == "n" {
valueFloat /= 1000000
} else if endChar == "u" {
valueFloat /= 1000
}
return fmt.Sprintf("%.3fm", valueFloat)
}