forked from operator-framework/operator-lifecycle-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject.go
378 lines (313 loc) · 10.5 KB
/
inject.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
package inject
import (
"errors"
"reflect"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
)
// InjectEnvIntoDeployment injects the proxy env variables specified in
// proxyEnvVar into the container(s) of the given PodSpec.
//
// If any Container in PodSpec already defines an env variable of the same name
// as any of the proxy env variables then it will be overwritten.
func InjectEnvIntoDeployment(podSpec *corev1.PodSpec, envVars []corev1.EnvVar) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
for i := range podSpec.Containers {
container := &podSpec.Containers[i]
container.Env = mergeEnvVars(container.Env, envVars)
}
return nil
}
func mergeEnvVars(containerEnvVars []corev1.EnvVar, newEnvVars []corev1.EnvVar) []corev1.EnvVar {
// Build a map of environment variables.
// newEnvVars always overrides containerEnvVars.
mergedMap := map[string]corev1.EnvVar{}
for _, envVar := range containerEnvVars {
mergedMap[envVar.Name] = envVar
}
for _, envVar := range newEnvVars {
mergedMap[envVar.Name] = envVar
}
// To keep things in the expected order, always put the
// original environment variable names into the merged
// output in place first.
merged := make([]corev1.EnvVar, 0, len(mergedMap))
for _, e := range containerEnvVars {
envVar := mergedMap[e.Name]
merged = append(merged, envVar)
delete(mergedMap, e.Name)
}
// Then for any remaining newEnvVars (i.e. env vars
// that weren't present in the containerEnvVars), add
// them at the end in the order they were provided in
// the subscription.
for _, e := range newEnvVars {
envVar, ok := mergedMap[e.Name]
if !ok {
continue
}
merged = append(merged, envVar)
}
return merged
}
// InjectEnvFromIntoDeployment injects the envFrom variables
// into the container(s) of the given PodSpec.
//
// If any Container in PodSpec already defines an envFrom variable
// as any of the provided envFrom then it will be overwritten.
func InjectEnvFromIntoDeployment(podSpec *corev1.PodSpec, envFromVars []corev1.EnvFromSource) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
for i := range podSpec.Containers {
container := &podSpec.Containers[i]
container.EnvFrom = mergeEnvFromVars(container.EnvFrom, envFromVars)
}
return nil
}
func mergeEnvFromVars(containerEnvFromVars []corev1.EnvFromSource, newEnvFromVars []corev1.EnvFromSource) []corev1.EnvFromSource {
merged := containerEnvFromVars
for _, newEnvFromVar := range newEnvFromVars {
if !findEnvFromVar(containerEnvFromVars, newEnvFromVar) {
merged = append(merged, newEnvFromVar)
}
}
return merged
}
func findEnvFromVar(envFromVar []corev1.EnvFromSource, newEnvFromVar corev1.EnvFromSource) bool {
for i := range envFromVar {
if reflect.DeepEqual(envFromVar[i], newEnvFromVar) {
return true
}
}
return false
}
// InjectVolumesIntoDeployment injects the provided Volumes
// into the container(s) of the given PodSpec.
//
// If any Container in PodSpec already defines a Volume of the same name
// as any of the provided Volumes then it will be overwritten.
func InjectVolumesIntoDeployment(podSpec *corev1.PodSpec, volumes []corev1.Volume) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
podSpec.Volumes = mergeVolumes(podSpec.Volumes, volumes)
return nil
}
func mergeVolumes(podSpecVolumes []corev1.Volume, newVolumes []corev1.Volume) (merged []corev1.Volume) {
merged = podSpecVolumes
for _, newVolume := range newVolumes {
existing, found := findVolume(podSpecVolumes, newVolume.Name)
if found {
*existing = newVolume
continue
}
merged = append(merged, newVolume)
}
return
}
func findVolume(volumes []corev1.Volume, name string) (foundVolume *corev1.Volume, found bool) {
for i := range volumes {
if name == volumes[i].Name {
// Environment variable names are case sensitive.
found = true
foundVolume = &volumes[i]
break
}
}
return
}
// InjectVolumeMountsIntoDeployment injects the provided VolumeMounts
// into the given PodSpec.
//
// If the PodSpec already defines a VolumeMount of the same name
// as any of the provided VolumeMounts then it will be overwritten.
func InjectVolumeMountsIntoDeployment(podSpec *corev1.PodSpec, volumeMounts []corev1.VolumeMount) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
for i := range podSpec.Containers {
container := &podSpec.Containers[i]
container.VolumeMounts = mergeVolumeMounts(container.VolumeMounts, volumeMounts)
}
return nil
}
func mergeVolumeMounts(containerVolumeMounts []corev1.VolumeMount, newVolumeMounts []corev1.VolumeMount) (merged []corev1.VolumeMount) {
merged = containerVolumeMounts
for _, newVolumeMount := range newVolumeMounts {
existing, found := findVolumeMount(containerVolumeMounts, newVolumeMount.Name)
if found {
*existing = newVolumeMount
continue
}
merged = append(merged, newVolumeMount)
}
return
}
func findVolumeMount(volumeMounts []corev1.VolumeMount, name string) (foundVolumeMount *corev1.VolumeMount, found bool) {
for i := range volumeMounts {
if name == volumeMounts[i].Name {
// Environment variable names are case sensitive.
found = true
foundVolumeMount = &volumeMounts[i]
break
}
}
return
}
// InjectTolerationsIntoDeployment injects provided Tolerations
// into the given Pod Spec
//
// Tolerations will be appended to the existing once if it
// does not already exist
func InjectTolerationsIntoDeployment(podSpec *corev1.PodSpec, tolerations []corev1.Toleration) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
podSpec.Tolerations = mergeTolerations(podSpec.Tolerations, tolerations)
return nil
}
func mergeTolerations(podTolerations []corev1.Toleration, newTolerations []corev1.Toleration) (mergedTolerations []corev1.Toleration) {
mergedTolerations = podTolerations
for _, newToleration := range newTolerations {
_, found := findToleration(podTolerations, newToleration)
if !found {
mergedTolerations = append(mergedTolerations, newToleration)
}
}
return
}
func findToleration(tolerations []corev1.Toleration, toleration corev1.Toleration) (foundToleration *corev1.Toleration, found bool) {
for i := range tolerations {
if reflect.DeepEqual(toleration, tolerations[i]) {
found = true
foundToleration = &toleration
break
}
}
return
}
// InjectResourcesIntoDeployment will inject provided Resources
// into given podSpec
//
// If podSpec already defines Resources, it will be overwritten
func InjectResourcesIntoDeployment(podSpec *corev1.PodSpec, resources *corev1.ResourceRequirements) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
if resources == nil {
return nil
}
for i := range podSpec.Containers {
container := &podSpec.Containers[i]
container.Resources = *resources
}
return nil
}
// InjectNodeSelectorIntoDeployment injects the provided NodeSelector
// into the container(s) of the given PodSpec.
//
// If the PodSpec already defines a NodeSelector it will be overwritten.
func InjectNodeSelectorIntoDeployment(podSpec *corev1.PodSpec, nodeSelector map[string]string) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
if nodeSelector != nil {
podSpec.NodeSelector = nodeSelector
}
return nil
}
// OverrideDeploymentAffinity will override the corev1.Affinity defined in the Deployment
// with the given corev1.Affinity. Any nil top-level sub-attributes (e.g. NodeAffinity, PodAffinity, and PodAntiAffinity)
// will be ignored. Hint: to overwrite those top-level attributes, empty them out. I.e. use the empty/default object ({})
// e.g. NodeAffinity{}. In yaml:
//
// affinity:
// nodeAffinity: {}
// podAffinity: {}
// podAntiAffinity: {}
//
// will completely remove the deployment podSpec.affinity and is equivalent to
// affinity: {}
func OverrideDeploymentAffinity(podSpec *corev1.PodSpec, affinity *corev1.Affinity) error {
if podSpec == nil {
return errors.New("no pod spec provided")
}
if affinity == nil {
return nil
}
// if podSpec.Affinity is nil or empty/default then completely override podSpec.Affinity with overrides
if podSpec.Affinity == nil || reflect.DeepEqual(podSpec.Affinity, &corev1.Affinity{}) {
if reflect.DeepEqual(affinity, &corev1.Affinity{}) {
podSpec.Affinity = nil
} else {
podSpec.Affinity = affinity
}
return nil
}
// if overriding affinity is empty/default then nil out podSpec.Affinity
if reflect.DeepEqual(affinity, &corev1.Affinity{}) {
podSpec.Affinity = nil
return nil
}
// override podSpec.Affinity each attribute as necessary nilling out any default/empty overrides on the podSpec
if affinity.NodeAffinity != nil {
if reflect.DeepEqual(affinity.NodeAffinity, &corev1.NodeAffinity{}) {
podSpec.Affinity.NodeAffinity = nil
} else {
podSpec.Affinity.NodeAffinity = affinity.NodeAffinity
}
}
if affinity.PodAffinity != nil {
if reflect.DeepEqual(affinity.PodAffinity, &corev1.PodAffinity{}) {
podSpec.Affinity.PodAffinity = nil
} else {
podSpec.Affinity.PodAffinity = affinity.PodAffinity
}
}
if affinity.PodAntiAffinity != nil {
if reflect.DeepEqual(affinity.PodAntiAffinity, &corev1.PodAntiAffinity{}) {
podSpec.Affinity = nil
} else {
podSpec.Affinity.PodAntiAffinity = affinity.PodAntiAffinity
}
}
// special case: if after being overridden, podSpec is the same as default/empty then nil it out
if reflect.DeepEqual(&corev1.Affinity{}, podSpec.Affinity) {
podSpec.Affinity = nil
}
return nil
}
// InjectAnnotationsIntoDeployment injects the provided Annotations
// into the container(s) of the given PodSpec.
//
// If the Deployment already defines any Annotations they will NOT be overwritten.
func InjectAnnotationsIntoDeployment(deployment *appsv1.Deployment, newAnnotations map[string]string) error {
if deployment == nil {
return errors.New("no deployment provided")
}
// do not override existing annotations
if newAnnotations != nil {
mergedDeploymentAnnotations := map[string]string{}
mergedPodAnnotations := map[string]string{}
// add newAnnotations first to prevent them from overwriting the defaults
for k, v := range newAnnotations {
mergedDeploymentAnnotations[k] = v
mergedPodAnnotations[k] = v
}
// then replace any duplicate annotations with the default annotation
for k, v := range deployment.Annotations {
mergedDeploymentAnnotations[k] = v
}
for k, v := range deployment.Spec.Template.GetAnnotations() {
mergedPodAnnotations[k] = v
}
// Inject Into Deployment
deployment.SetAnnotations(mergedDeploymentAnnotations)
// Inject Into Pod Spec
deployment.Spec.Template.SetAnnotations(mergedPodAnnotations)
}
return nil
}