-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutate.go
194 lines (172 loc) · 5.57 KB
/
mutate.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
package main
import (
"encoding/json"
"fmt"
"log"
"k8s.io/api/admission/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/klog"
)
func mutate(namespacesLister corev1listers.NamespaceLister, request v1beta1.AdmissionRequest, namespaceConfig map[string][]string) (v1beta1.AdmissionResponse, error) {
response := v1beta1.AdmissionResponse{}
// Default response
response.Allowed = true
response.UID = request.UID
// Decode the pod object
pod := v1.Pod{}
if err := json.Unmarshal(request.Object.Raw, &pod); err != nil {
return response, fmt.Errorf("unable to decode Pod %w", err)
}
log.Printf("Identifying if toleration injection should be applied to request %s/%s", request.Namespace, pod.Name)
inject := true
for _, toleration := range pod.Spec.Tolerations {
if toleration.Key == "CriticalAddonsEarly" || toleration.Key == "node.statcan.gc.ca/purpose" {
klog.Infof("not injecting pod %s/%s, appropriate tolerations already exist", request.Namespace, pod.Name)
inject = false
break
}
}
if inject {
namespace, err := namespacesLister.Get(request.Namespace)
if err != nil {
response.Result = &metav1.Status{
Status: metav1.StatusFailure,
}
return response, fmt.Errorf("unable to find namespace %q", request.Namespace)
}
tolerations := []v1.Toleration{}
purpose := ""
ok := false
if purpose, ok = namespace.ObjectMeta.Labels["namespace.statcan.gc.ca/purpose"]; !ok {
purpose = "user"
}
// Schedule `daaas` namespaces on `system` nodes
if purpose == "daaas" {
purpose = "system"
}
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/purpose",
Value: purpose,
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
// Check for a GPU
numGPU := 0
for _, container := range pod.Spec.Containers {
// if container.Resources.Requests.
if limit, ok := container.Resources.Requests["nvidia.com/gpu"]; ok {
if !limit.IsZero() {
numGPU = int(limit.Value())
break
}
}
}
// Check for CPU request values -> pods can have many containers requesting different CPU resources, need to select max(requests)
numCPU := 0
for _, container := range pod.Spec.Containers {
if req, ok := container.Resources.Requests["cpu"]; ok {
if numCPU < int(req.Value()) {
numCPU = int(req.Value())
}
}
}
log.Printf("Max CPU/GPU requested: %d/%d", numCPU, numGPU)
// conditional eval
if numGPU != 0 {
if numGPU == 1 {
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/use",
Value: "gpu",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
} else if numGPU == 4 {
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/use",
Value: "gpu-4",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
}
} else if numCPU > 14 && numCPU <= 72 {
bigCpuNamespaces := namespaceConfig["bigCPUns"]
log.Printf("request.Namespace: %s", request.Namespace)
for _, ns := range bigCpuNamespaces { // store namespaces in map[ns string] string for indexablility if needed
log.Printf("ns: %s", ns)
if request.Namespace == ns {
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/use",
Value: "cpu-72",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
}
}
} else if request.Namespace == "cloud-main-system" {
/*
If the pod namespace is "cloud-main-system", then it is an istio egress gateway pod that
should be scheduled to the `cloud-main-system` node pool. This node pool specifically has
the taint `node.statcan.gc.ca/use=cloud-main-system`, so this block is adding the corresponding
toleration.
*/
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/use",
Value: "cloud-main-system",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
} else {
tolerations = append(tolerations, v1.Toleration{
Key: "node.statcan.gc.ca/use",
Value: "general",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
}
// System pools are always protected-b
if purpose == "system" {
tolerations = append(tolerations, v1.Toleration{
Key: "data.statcan.gc.ca/classification",
Value: "protected-b",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
} else {
if classification, ok := pod.ObjectMeta.Labels["data.statcan.gc.ca/classification"]; ok {
tolerations = append(tolerations, v1.Toleration{
Key: "data.statcan.gc.ca/classification",
Value: classification,
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
} else {
tolerations = append(tolerations, v1.Toleration{
Key: "data.statcan.gc.ca/classification",
Value: "unclassified",
Operator: v1.TolerationOpEqual,
Effect: v1.TaintEffectNoSchedule,
})
}
}
patch := v1beta1.PatchTypeJSONPatch
response.PatchType = &patch
patches := []map[string]interface{}{}
for _, toleration := range tolerations {
patches = append(patches, map[string]interface{}{
"op": "add",
"path": "/spec/tolerations/-",
"value": toleration,
})
}
response.Patch, err = json.Marshal(patches)
if err != nil {
return response, err
}
response.Result = &metav1.Status{
Status: metav1.StatusSuccess,
}
}
return response, nil
}