Skip to content

Commit b460971

Browse files
authored
Add support for setting tolerations for pods created by the daemonset (#162)
1 parent 571fa2a commit b460971

File tree

9 files changed

+93
-0
lines changed

9 files changed

+93
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The config values to be set are:
2828
| `IMAGE_PULL_SECRETS` | List of image pull secrets, in this format `pullsecret1;...` to add to pods created by the DaemonSet. Those secrets need to be in the image puller's namespace and a cluster administrator must create them. | `""` |
2929
| `AFFINITY` | Affinity applied to pods created by the daemonset, in this format `'{"nodeAffinity":{ ... }}'` | `'{}'` |
3030
| `KIP_IMAGE` | The image puller image to copy the `sleep` binary from | `quay.io/eclipse/kubernetes-image-puller:next` |
31+
| `TOLERATIONS` | Tolerations applied to pods created by the daemonset, provided in this format `'[{"operator":"Exists"}]'` | `'[]'` |
3132

3233
### Configuration - Helm
3334

@@ -49,6 +50,7 @@ The following values can be set:
4950
| `configMap.nodeSelector` | The value of `NODE_SELECTOR` to be set in the ConfigMap | `"{}"` |
5051
| `configMap.imagePullSecrets` | The value of `IMAGE_PULL_SECRETS` | `""` |
5152
| `configMap.affinity` | The value of `AFFINITY` to be set in the ConfigMap | `"{}"` |
53+
| `configMap.tolerations` | The value of `TOLERATIONS` to be set in the ConfigMap | `"[]"` |
5254

5355
### Configuration - OpenShift
5456

@@ -70,6 +72,7 @@ The following values can be set:
7072
| `NODE_SELECTOR` | The value of `NODE_SELECTOR` to be set in the ConfigMap | `"{}"` |
7173
| `IMAGE_PULL_SECRETS` | The value of `IMAGE_PULL_SECRETS` | `""` |
7274
| `AFFINITY` | The value of `AFFINITY` to be set in the ConfigMap | `"{}"` |
75+
| `TOLERATIONS` | The value of `TOLERATIONS` to be set in the ConfigMap | `"[]"` |
7376

7477
### Installation - Helm
7578

cfg/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Config struct {
2727
ImagePullSecrets []string
2828
Affinity *corev1.Affinity
2929
ImagePullerImage string
30+
Tolerations []corev1.Toleration
3031
}
3132

3233
func GetConfig() Config {
@@ -43,5 +44,6 @@ func GetConfig() Config {
4344
ImagePullSecrets: processImagePullSecretsEnvVar(),
4445
Affinity: processAffinityEnvVar(),
4546
ImagePullerImage: getEnvVarOrDefault(kipImageEnvVar, defaultImage),
47+
Tolerations: processTolerationsEnvVar(),
4648
}
4749
}

cfg/config_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestEnvVars(t *testing.T) {
4040
ImagePullSecrets: []string{},
4141
Affinity: &v1.Affinity{},
4242
ImagePullerImage: "quay.io/eclipse/kubernetes-image-puller:next",
43+
Tolerations: []v1.Toleration{},
4344
},
4445
},
4546
{
@@ -52,6 +53,7 @@ func TestEnvVars(t *testing.T) {
5253
"IMAGE_PULL_SECRETS": "secret1; secret2",
5354
"AFFINITY": `{"nodeAffinity":{"requiredDuringSchedulingIgnoredDuringExecution":{"nodeSelectorTerms":[{"matchExpressions":[{"key":"kubernetes.io/e2e-az-name","operator":"In","values":["e2e-az1","e2e-az2"]}]}]}}}`,
5455
"KIP_IMAGE": "quay.io/my-repo/kubernetes-image-puller:next",
56+
"TOLERATIONS": `[{"effect":"NoSchedule","key":"app","operator":"Equal","value": "prod"}]`,
5557
},
5658
want: Config{
5759
DaemonsetName: "custom-daemonset-name",
@@ -86,6 +88,14 @@ func TestEnvVars(t *testing.T) {
8688
},
8789
},
8890
ImagePullerImage: "quay.io/my-repo/kubernetes-image-puller:next",
91+
Tolerations: []v1.Toleration{
92+
{
93+
Key: "app",
94+
Operator: "Equal",
95+
Value: "prod",
96+
Effect: "NoSchedule",
97+
},
98+
},
8999
},
90100
},
91101
}

cfg/envvars.go

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
imagePullSecretsEnvVar = "IMAGE_PULL_SECRETS"
3737
affinityEnvVar = "AFFINITY"
3838
kipImageEnvVar = "KIP_IMAGE"
39+
tolerationsEnvVar = "TOLERATIONS"
3940
)
4041

4142
// Default values where applicable
@@ -52,6 +53,7 @@ const (
5253
defaultImagePullSecret = ""
5354
defaultAffinity = "{}"
5455
defaultImage = "quay.io/eclipse/kubernetes-image-puller:next"
56+
defaultTolerations = "[]"
5557
)
5658

5759
func getCachingInterval() int {
@@ -128,6 +130,15 @@ func processAffinityEnvVar() *corev1.Affinity {
128130
return affinity
129131
}
130132

133+
func processTolerationsEnvVar() []corev1.Toleration {
134+
rawTolerations := getEnvVarOrDefault(tolerationsEnvVar, defaultTolerations)
135+
var tolerations []corev1.Toleration
136+
if err := json.Unmarshal([]byte(rawTolerations), &tolerations); err != nil {
137+
log.Fatalf("Failed to unmarshal tolerations json: %s", err)
138+
}
139+
return tolerations
140+
}
141+
131142
func getEnvVarOrExit(envVar string) string {
132143
val := os.Getenv(envVar)
133144
if val == "" {

cfg/envvars_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,64 @@ func Test_processAffinityEnvVar(t *testing.T) {
164164
})
165165
}
166166
}
167+
168+
func TestProcessTolerationsEnvVar(t *testing.T) {
169+
type testcase struct {
170+
name string
171+
tolerations string
172+
isTolerationsSet bool
173+
want []v1.Toleration
174+
}
175+
176+
tests := []testcase{
177+
{
178+
name: "default tolerations, TOLERATIONS set",
179+
tolerations: "[]",
180+
isTolerationsSet: true,
181+
want: []v1.Toleration{},
182+
},
183+
{
184+
name: "app prod, TOLERATIONS set",
185+
tolerations: `[{"effect":"NoSchedule","key":"app","operator":"Equal","value": "prod"}]`,
186+
isTolerationsSet: true,
187+
want: []v1.Toleration{
188+
{
189+
Key: "app",
190+
Operator: "Equal",
191+
Value: "prod",
192+
Effect: "NoSchedule",
193+
},
194+
},
195+
},
196+
{
197+
name: "operator exists, TOLERATIONS set",
198+
tolerations: `[{"operator":"Exists"}]`,
199+
isTolerationsSet: true,
200+
want: []v1.Toleration{
201+
{
202+
Operator: "Exists",
203+
},
204+
},
205+
},
206+
{
207+
name: "default env var, TOLERATIONS not set",
208+
tolerations: "[{\"this\": \"shouldn't be set\"}]",
209+
isTolerationsSet: false,
210+
want: []v1.Toleration{},
211+
},
212+
}
213+
214+
for _, c := range tests {
215+
t.Run(c.name, func(t *testing.T) {
216+
defer os.Clearenv()
217+
if c.isTolerationsSet {
218+
os.Setenv("TOLERATIONS", c.tolerations)
219+
}
220+
got := processTolerationsEnvVar()
221+
222+
if d := cmp.Diff(c.want, got); d != "" {
223+
t.Errorf("(-want, +got): %s", d)
224+
}
225+
})
226+
}
227+
}

deploy/helm/templates/configmap.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ data:
1414
NODE_SELECTOR: "{{ .Values.configMap.nodeSelector }}"
1515
IMAGE_PULL_SECRETS: "{{ .Values.configMap.imagePullSecrets }}"
1616
AFFINITY: "{{ .Values.configMap.affinity }}"
17+
TOLERATIONS: "{{ .Values.configMap.tolerations }}"

deploy/helm/values.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ configMap:
1818
nodeSelector: "{}"
1919
imagePullSecrets: ""
2020
affinity: "{}"
21+
tolerations: "[]"

deploy/openshift/configmap.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ objects:
2323
IMAGE_PULL_SECRETS: ${IMAGE_PULL_SECRETS}
2424
AFFINITY: ${AFFINITY}
2525
KIP_IMAGE: ${KIP_IMAGE}
26+
TOLERATIONS: ${TOLERATIONS}
2627
parameters:
2728
- name: IMAGES
2829
value: >
@@ -51,3 +52,5 @@ parameters:
5152
value: "{}"
5253
- name: KIP_IMAGE
5354
value: quay.io/eclipse/kubernetes-image-puller:next
55+
- name: TOLERATIONS
56+
value: "[]"

utils/clusterutils.go

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func getDaemonset(deployment *appsv1.Deployment) *appsv1.DaemonSet {
133133
ImagePullSecrets: imgPullSecrets,
134134
Affinity: cfg.Affinity,
135135
Volumes: []corev1.Volume{{Name: kipVolumeName}},
136+
Tolerations: cfg.Tolerations,
136137
},
137138
},
138139
},

0 commit comments

Comments
 (0)