-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathselect_pods.go
93 lines (86 loc) · 2.56 KB
/
select_pods.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
package main
func selectPods(namespace string, selector *Selector, namespacePodMap *map[string][]string, podLabelMap *map[string]map[string]string) []string {
// special case: empty map selects all pods
if selectorIsEmpty(selector) {
return (*namespacePodMap)[namespace]
}
var selectedPods []string
// select pods matching at least one label pair
for _, pod := range (*namespacePodMap)[namespace] {
labels := (*podLabelMap)[pod]
for k, v := range (*selector).MatchLabels {
if len(k) > 0 && labels[k] == v {
selectedPods = append(selectedPods, pod)
}
}
}
// append pods matching any MatchExpressions
for _, requirement := range (*selector).MatchExpressions {
switch requirement.Operator {
case "In":
for _, pod := range (*namespacePodMap)[namespace] {
for k, v := range (*podLabelMap)[pod] {
if k == requirement.Key {
for _, item := range requirement.Values {
if v == item {
selectedPods = append(selectedPods, pod)
break
}
}
}
}
}
case "NotIn":
for _, pod := range (*namespacePodMap)[namespace] {
found := false
for k, v := range (*podLabelMap)[pod] {
if k == requirement.Key {
for _, item := range requirement.Values {
if v == item {
found = true
}
}
}
}
if found == false {
selectedPods = append(selectedPods, pod)
}
}
case "Exists":
for _, pod := range (*namespacePodMap)[namespace] {
for k := range (*podLabelMap)[pod] {
if k == requirement.Key {
selectedPods = append(selectedPods, pod)
continue
}
}
}
case "DoesNotExist":
for _, pod := range (*namespacePodMap)[namespace] {
found := false
for k := range (*podLabelMap)[pod] {
if k == requirement.Key {
found = true
}
}
if found == false {
selectedPods = append(selectedPods, pod)
}
}
}
}
return unique(selectedPods)
}
func selectPodsAcrossNamespaces(namespaces *[]string, selector *Selector, namespacePodMap *map[string][]string, podLabelMap *map[string]map[string]string) []string {
var allPods []string
for _, namespace := range *namespaces {
selectedPods := selectPods(namespace, selector, namespacePodMap, podLabelMap)
allPods = append(allPods, selectedPods...)
}
return unique(allPods)
}
func selectorIsEmpty(selector *Selector) bool {
matchLabelsEmpty := (*selector).MatchLabels == nil || len((*selector).MatchLabels) == 0
matchExpressionsEmpty := (*selector).MatchExpressions == nil || len((*selector).MatchExpressions) == 0
return matchLabelsEmpty && matchExpressionsEmpty
}