-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcollect.go
79 lines (65 loc) · 2.19 KB
/
collect.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
package cmd
import (
"context"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func collect(clientset *kubernetes.Clientset, namespace []string, labels []string) {
if limitFlag < 0 {
log.Fatal("FATAL CONFIGURATION: --limit flag value must not be negative.")
}
if !(output == "standard" || output == "yaml" || output == "json") {
log.Fatal("FATAL CONFIGURATION: --output flag can only be: standard, json, yaml")
}
for _, ns := range namespace {
for _, lb := range labels {
pods, err := clientset.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{LabelSelector: lb})
if err != nil {
log.Fatal(err.Error())
}
for _, v := range pods.Items {
initializeContainerMap(v.ObjectMeta.Name, v.ObjectMeta.Namespace)
restarts := int32(0)
for _, vv := range v.Status.ContainerStatuses {
restarts += vv.RestartCount
trackContainers(v.ObjectMeta.Name, v.ObjectMeta.Namespace, vv.Name, vv.RestartCount)
}
trackPods(v.ObjectMeta.Name, v.ObjectMeta.Namespace, restarts)
trackNamespaces(v.ObjectMeta.Namespace, restarts)
trackLabels(labels, v.ObjectMeta.Labels, restarts)
trackNodes(v.Spec.NodeName, restarts)
}
}
}
showResults()
}
func trackNamespaces(namespace string, count int32) {
namespaceTracker[namespace] += count
}
func trackNodes(node string, count int32) {
nodeTracker[node] += count
}
func trackPods(pod, namespace string, count int32) {
podTracker[namespace+":"+pod] = count
}
func trackContainers(pod, namespace, container string, count int32) {
containerTracker[namespace+":"+pod][container] = count
}
func initializeContainerMap(pod, namespace string) {
containerTracker[namespace+":"+pod] = make(map[string]int32)
}
// plabels = Pod Labels
// tlabels = (User-defined) tracking labels
func trackLabels(tlabels []string, plabels map[string]string, count int32) {
// range through all the labels specified in the -l CLI flag
for _, l := range tlabels {
// range through plabels to see if any match the user specified labels. If so, add it to the map
// the default value "*" will match everything
for k, v := range plabels {
if l == k || l == "" {
labelTracker[k+":"+v] += count
}
}
}
}