-
Notifications
You must be signed in to change notification settings - Fork 90
/
util.go
97 lines (92 loc) · 2.93 KB
/
util.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
package k8sutil
import (
corev1 "k8s.io/api/core/v1"
)
func MergeEnvVars(desired []corev1.EnvVar, existing []corev1.EnvVar, override bool) []corev1.EnvVar {
mergedEnvVars := []corev1.EnvVar{}
mergedEnvVars = append(mergedEnvVars, existing...)
for _, desiredEnvVar := range desired {
idx := -1
for existingEnvVarIndex, existingEnvVar := range existing {
if existingEnvVar.Name != desiredEnvVar.Name {
continue
}
idx = existingEnvVarIndex
break
}
if idx == -1 {
// not found, add it
mergedEnvVars = append(mergedEnvVars, *desiredEnvVar.DeepCopy())
} else if override {
// found and should override
mergedEnvVars[idx] = *desiredEnvVar.DeepCopy()
}
}
return mergedEnvVars
}
func MergeVolumes(desired []corev1.Volume, existing []corev1.Volume, override bool) []corev1.Volume {
mergedVolumes := []corev1.Volume{}
mergedVolumes = append(mergedVolumes, existing...)
for _, desiredVolume := range desired {
idx := -1
for existingVolumeIndex, existingVolume := range existing {
if existingVolume.Name != desiredVolume.Name {
continue
}
idx = existingVolumeIndex
break
}
if idx == -1 {
// not found, add it
mergedVolumes = append(mergedVolumes, *desiredVolume.DeepCopy())
} else if override {
// found and should override
mergedVolumes[idx] = *desiredVolume.DeepCopy()
}
}
return mergedVolumes
}
func MergeVolumeMounts(desired []corev1.VolumeMount, existing []corev1.VolumeMount, override bool) []corev1.VolumeMount {
mergedVolumeMounts := []corev1.VolumeMount{}
mergedVolumeMounts = append(mergedVolumeMounts, existing...)
for _, desiredVolumeMount := range desired {
idx := -1
for existingVolumeMountIndex, existingVolumeMount := range existing {
if existingVolumeMount.Name != desiredVolumeMount.Name {
continue
}
idx = existingVolumeMountIndex
break
}
if idx == -1 {
// not found, add it
mergedVolumeMounts = append(mergedVolumeMounts, *desiredVolumeMount.DeepCopy())
} else if override {
// found and should override
mergedVolumeMounts[idx] = *desiredVolumeMount.DeepCopy()
}
}
return mergedVolumeMounts
}
func MergeImagePullSecrets(desired []corev1.LocalObjectReference, existing []corev1.LocalObjectReference, override bool) []corev1.LocalObjectReference {
mergedImagePullSecrets := []corev1.LocalObjectReference{}
mergedImagePullSecrets = append(mergedImagePullSecrets, existing...)
for _, desiredImagePullSecret := range desired {
idx := -1
for existingImagePullSecretIndex, existingImagePullSecret := range existing {
if existingImagePullSecret.Name != desiredImagePullSecret.Name {
continue
}
idx = existingImagePullSecretIndex
break
}
if idx == -1 {
// not found, add it
mergedImagePullSecrets = append(mergedImagePullSecrets, *desiredImagePullSecret.DeepCopy())
} else if override {
// found and should override
mergedImagePullSecrets[idx] = *desiredImagePullSecret.DeepCopy()
}
}
return mergedImagePullSecrets
}