Skip to content

Commit 0214ead

Browse files
authored
Monochart support tolerance (#276)
* Monochart support tolerance * Added support pod afinity, anti afinity, tolerations and node selector
1 parent 3ac1df1 commit 0214ead

9 files changed

Lines changed: 452 additions & 60 deletions

File tree

incubator/monochart/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apiVersion: v1
22
description: A declarative helm chart for deploying common types of services on Kubernetes
33
name: monochart
4-
version: 0.27.0
5-
appVersion: 0.27.0
4+
version: 0.28.0
5+
appVersion: 0.28.0
66
home: https://github.com/cloudposse/charts/tree/master/incubator/monochart
77
icon: https://raw.githubusercontent.com/cloudposse/charts/master/incubator/monochart/logo.png
88
maintainers:

incubator/monochart/templates/_helpers.tpl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,81 @@ The pod anti-affinity rule to prefer not to be scheduled onto a node if that nod
160160
topologyKey: "kubernetes.io/hostname"
161161
{{- end -}}
162162

163+
{{/*
164+
The affinity
165+
*/}}
166+
{{- define "monochart.affinity" -}}
167+
{{- $root := first . }}
168+
{{- $specific := last . }}
169+
{{- $config := mergeOverwrite $root.Values.affinity (get $specific "affinity") -}}
170+
{{- if $config }}
171+
affinity:
172+
{{- if or $config.podAntiAffinity (eq $config.affinityRule "ShouldBeOnDifferentNode") }}
173+
podAntiAffinity:
174+
preferredDuringSchedulingIgnoredDuringExecution:
175+
{{- if $config.podAntiAffinity }}
176+
{{- if $config.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
177+
{{- with $config.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
178+
{{ toYaml . | indent 6 }}
179+
{{- end }}
180+
{{- end }}
181+
{{- end }}
182+
{{- if eq $config.affinityRule "ShouldBeOnDifferentNode" }}
183+
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" $root | nindent 6 }}
184+
{{- end }}
185+
{{- end }}
186+
{{- if $config.podAffinity }}
187+
podAffinity:
188+
requiredDuringSchedulingIgnoredDuringExecution:
189+
{{- with $config.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
190+
{{ toYaml . | indent 6 }}
191+
{{- end }}
192+
{{- end }}
193+
{{- if $config.nodeAffinity }}
194+
nodeAffinity:
195+
requiredDuringSchedulingIgnoredDuringExecution:
196+
{{- if $config.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
197+
{{- with $config.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
198+
{{ toYaml . | indent 6 }}
199+
{{- end }}
200+
{{- end }}
201+
preferredDuringSchedulingIgnoredDuringExecution:
202+
{{- if $config.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
203+
{{- with $config.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
204+
{{ toYaml . | indent 6 }}
205+
{{- end }}
206+
{{- end }}
207+
{{- end }}
208+
{{- end }}
209+
210+
{{- end -}}
211+
212+
{{/*
213+
The nodeSelector
214+
*/}}
215+
{{- define "monochart.nodeSelector" -}}
216+
{{- $root := first . }}
217+
{{- $specific := last . }}
218+
{{- $config := mergeOverwrite $root.Values.nodeSelector (get $specific "nodeSelector") -}}
219+
{{- if $config }}
220+
{{- with $config }}
221+
nodeSelector:
222+
{{ toYaml . | indent 2 }}
223+
{{- end }}
224+
{{- end }}
225+
{{- end -}}
226+
227+
{{/*
228+
The tolerations
229+
*/}}
230+
{{- define "monochart.tolerations" -}}
231+
{{- $root := first . }}
232+
{{- $specific := last . }}
233+
{{- $config := default $root.Values.tolerations (get $specific "tolerations") -}}
234+
{{- if $config }}
235+
{{- with $config }}
236+
tolerations:
237+
{{ toYaml . | indent 2 }}
238+
{{- end }}
239+
{{- end }}
240+
{{- end -}}

incubator/monochart/templates/cronjob.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ spec:
4141
serviceAccountName: {{ $root.Values.serviceAccountName }}
4242
{{- end }}
4343
restartPolicy: '{{ default "Never" $cron.restartPolicy }}'
44+
{{- include "monochart.affinity" (list $root $cron) | nindent 10 }}
45+
{{- include "monochart.nodeSelector" (list $root $cron) | nindent 10 }}
46+
{{- include "monochart.tolerations" (list $root $cron) | nindent 10 }}
4447
containers:
4548
- name: {{ $root.Release.Name }}
4649
image: {{ required "image.repository is required!" $root.Values.image.repository }}:{{ required "image.tag is required!" $root.Values.image.tag }}

incubator/monochart/templates/daemonset.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ spec:
4141
{{- if index .Values "serviceAccountName" }}
4242
serviceAccountName: {{ .Values.serviceAccountName }}
4343
{{- end }}
44+
{{- include "monochart.affinity" (list . .Values.daemonset) | nindent 6 }}
45+
{{- include "monochart.nodeSelector" (list . .Values.daemonset) | nindent 6 }}
46+
{{- include "monochart.tolerations" (list . .Values.daemonset) | nindent 6 }}
4447
containers:
4548
- name: {{ .Release.Name }}
4649
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}

incubator/monochart/templates/deployment.yaml

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spec:
2929
annotations:
3030
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
3131
checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
32-
{{- if .Values.deployment.pod}}
32+
{{- if .Values.deployment.pod }}
3333
{{- with .Values.deployment.pod.annotations }}
3434
{{ toYaml . | indent 8 }}
3535
{{- end }}
@@ -47,30 +47,9 @@ spec:
4747
{{- if index .Values "serviceAccountName" }}
4848
serviceAccountName: {{ .Values.serviceAccountName }}
4949
{{- end }}
50-
{{- if .Values.deployment.affinity }}
51-
affinity:
52-
{{- if or .Values.deployment.affinity.podAntiAffinity (eq .Values.deployment.affinity.affinityRule "ShouldBeOnDifferentNode") }}
53-
podAntiAffinity:
54-
preferredDuringSchedulingIgnoredDuringExecution:
55-
{{- if .Values.deployment.affinity.podAntiAffinity }}
56-
{{- if .Values.deployment.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
57-
{{- with .Values.deployment.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
58-
{{ toYaml . | indent 10 }}
59-
{{- end }}
60-
{{- end }}
61-
{{- end }}
62-
{{- if eq .Values.deployment.affinity.affinityRule "ShouldBeOnDifferentNode" }}
63-
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" . | nindent 10 }}
64-
{{- end }}
65-
{{- end }}
66-
{{- if .Values.deployment.affinity.podAffinity }}
67-
podAffinity:
68-
requiredDuringSchedulingIgnoredDuringExecution:
69-
{{- with .Values.deployment.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
70-
{{ toYaml . | indent 10 }}
71-
{{- end }}
72-
{{- end }}
73-
{{- end }}
50+
{{- include "monochart.affinity" (list . .Values.deployment) | nindent 6 }}
51+
{{- include "monochart.nodeSelector" (list . .Values.deployment) | nindent 6 }}
52+
{{- include "monochart.tolerations" (list . .Values.deployment) | nindent 6 }}
7453
containers:
7554
- name: {{ .Release.Name }}
7655
image: {{ required "image.repository is required!" .Values.image.repository }}:{{ required "image.tag is required!" .Values.image.tag }}

incubator/monochart/templates/job.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ spec:
3535
serviceAccountName: {{ $root.Values.serviceAccountName }}
3636
{{- end }}
3737
restartPolicy: '{{ default "Never" $job.restartPolicy }}'
38+
{{- include "monochart.affinity" (list . $job) | nindent 6 }}
39+
{{- include "monochart.nodeSelector" (list . $job) | nindent 6 }}
40+
{{- include "monochart.tolerations" (list . $job) | nindent 6 }}
3841
containers:
3942
- name: {{ $root.Release.Name }}
4043
image: {{ required "image.repository is required!" $root.Values.image.repository }}:{{ required "image.tag is required!" $root.Values.image.tag }}

incubator/monochart/templates/statefulset.yaml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,9 @@ spec:
4646
serviceAccountName: {{ .Values.serviceAccountName }}
4747
{{- end }}
4848
terminationGracePeriodSeconds: {{ .Values.statefulset.terminationGracePeriodSeconds }}
49-
{{- if .Values.statefulset.affinity }}
50-
affinity:
51-
{{- if or .Values.statefulset.affinity.podAntiAffinity (eq .Values.statefulset.affinity.affinityRule "ShouldBeOnDifferentNode") }}
52-
podAntiAffinity:
53-
preferredDuringSchedulingIgnoredDuringExecution:
54-
{{- if .Values.statefulset.affinity.podAntiAffinity }}
55-
{{- if .Values.statefulset.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
56-
{{- with .Values.statefulset.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
57-
{{ toYaml . | indent 10 }}
58-
{{- end }}
59-
{{- end }}
60-
{{- end }}
61-
{{- if eq .Values.statefulset.affinity.affinityRule "ShouldBeOnDifferentNode" }}
62-
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" . | nindent 10 }}
63-
{{- end }}
64-
{{- end }}
65-
{{- if .Values.statefulset.affinity.podAffinity }}
66-
podAffinity:
67-
requiredDuringSchedulingIgnoredDuringExecution:
68-
{{- with .Values.statefulset.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
69-
{{ toYaml . | indent 10 }}
70-
{{- end }}
71-
{{- end }}
72-
{{- end }}
49+
{{- include "monochart.affinity" (list . .Values.statefulset) | nindent 6 }}
50+
{{- include "monochart.nodeSelector" (list . .Values.statefulset) | nindent 6 }}
51+
{{- include "monochart.tolerations" (list . .Values.statefulset) | nindent 6 }}
7352
containers:
7453
- name: {{ .Release.Name }}
7554
image: {{ required "image.repository is required!" .Values.image.repository }}:{{ required "image.tag is required!" .Values.image.tag }}

incubator/monochart/values.example.yaml

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,66 @@ deployment:
9595
# use custom affinity rule. Here app MUST be on different host then postgres instance for it
9696
podAffinity:
9797
requiredDuringSchedulingIgnoredDuringExecution:
98+
- labelSelector:
99+
matchExpressions:
100+
- key: app
101+
operator: In
102+
values:
103+
- postgresql
104+
- key: release
105+
operator: In
106+
values:
107+
- "postgresql"
108+
topologyKey: "kubernetes.io/hostname"
109+
nodeAffinity:
110+
requiredDuringSchedulingIgnoredDuringExecution:
111+
nodeSelectorTerms:
112+
- matchExpressions:
113+
- key: kubernetes.io/os
114+
operator: In
115+
values:
116+
- linux
117+
preferredDuringSchedulingIgnoredDuringExecution:
118+
- weight: 1
119+
preference:
120+
matchExpressions:
121+
- key: label-1
122+
operator: In
123+
values:
124+
- key-1
125+
- weight: 50
126+
preference:
127+
matchExpressions:
128+
- key: label-2
129+
operator: In
130+
values:
131+
- key-2
132+
tolerations:
133+
- key: "gpu3"
134+
operator: "Exists"
135+
effect: "NoSchedule"
136+
137+
138+
nodeSelector:
139+
disktype: ssd
140+
141+
tolerations: []
142+
143+
144+
affinity:
145+
# use of simple rule
146+
affinityRule: "ShouldBeOnDifferentNode"
147+
# use custom affinity rule. Here app MUST be on different host then postgres instance for it
148+
podAffinity:
149+
requiredDuringSchedulingIgnoredDuringExecution:
98150
- labelSelector:
99151
matchExpressions:
100-
- key: app
101-
operator: In
102-
values:
103-
- postgresql
104-
- key: release
105-
operator: In
106-
values:
107-
- "{{ requiredEnv "RELEASE_NAME" }}-postgresql"
108-
topologyKey: "kubernetes.io/hostname"
152+
- key: app
153+
operator: In
154+
values:
155+
- postgresql
156+
- key: release
157+
operator: In
158+
values:
159+
- "postgresql"
160+
topologyKey: "kubernetes.io/hostname"

0 commit comments

Comments
 (0)