Skip to content

Commit

Permalink
Monochart support tolerance (#276)
Browse files Browse the repository at this point in the history
* Monochart support tolerance

* Added support pod afinity, anti afinity, tolerations and node selector
  • Loading branch information
goruha authored May 29, 2024
1 parent 3ac1df1 commit 0214ead
Show file tree
Hide file tree
Showing 9 changed files with 452 additions and 60 deletions.
4 changes: 2 additions & 2 deletions incubator/monochart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v1
description: A declarative helm chart for deploying common types of services on Kubernetes
name: monochart
version: 0.27.0
appVersion: 0.27.0
version: 0.28.0
appVersion: 0.28.0
home: https://github.com/cloudposse/charts/tree/master/incubator/monochart
icon: https://raw.githubusercontent.com/cloudposse/charts/master/incubator/monochart/logo.png
maintainers:
Expand Down
78 changes: 78 additions & 0 deletions incubator/monochart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,81 @@ The pod anti-affinity rule to prefer not to be scheduled onto a node if that nod
topologyKey: "kubernetes.io/hostname"
{{- end -}}

{{/*
The affinity
*/}}
{{- define "monochart.affinity" -}}
{{- $root := first . }}
{{- $specific := last . }}
{{- $config := mergeOverwrite $root.Values.affinity (get $specific "affinity") -}}
{{- if $config }}
affinity:
{{- if or $config.podAntiAffinity (eq $config.affinityRule "ShouldBeOnDifferentNode") }}
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
{{- if $config.podAntiAffinity }}
{{- if $config.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{- with $config.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 6 }}
{{- end }}
{{- end }}
{{- end }}
{{- if eq $config.affinityRule "ShouldBeOnDifferentNode" }}
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" $root | nindent 6 }}
{{- end }}
{{- end }}
{{- if $config.podAffinity }}
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
{{- with $config.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 6 }}
{{- end }}
{{- end }}
{{- if $config.nodeAffinity }}
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
{{- if $config.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
{{- with $config.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 6 }}
{{- end }}
{{- end }}
preferredDuringSchedulingIgnoredDuringExecution:
{{- if $config.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{- with $config.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 6 }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}

{{- end -}}

{{/*
The nodeSelector
*/}}
{{- define "monochart.nodeSelector" -}}
{{- $root := first . }}
{{- $specific := last . }}
{{- $config := mergeOverwrite $root.Values.nodeSelector (get $specific "nodeSelector") -}}
{{- if $config }}
{{- with $config }}
nodeSelector:
{{ toYaml . | indent 2 }}
{{- end }}
{{- end }}
{{- end -}}

{{/*
The tolerations
*/}}
{{- define "monochart.tolerations" -}}
{{- $root := first . }}
{{- $specific := last . }}
{{- $config := default $root.Values.tolerations (get $specific "tolerations") -}}
{{- if $config }}
{{- with $config }}
tolerations:
{{ toYaml . | indent 2 }}
{{- end }}
{{- end }}
{{- end -}}
3 changes: 3 additions & 0 deletions incubator/monochart/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ spec:
serviceAccountName: {{ $root.Values.serviceAccountName }}
{{- end }}
restartPolicy: '{{ default "Never" $cron.restartPolicy }}'
{{- include "monochart.affinity" (list $root $cron) | nindent 10 }}
{{- include "monochart.nodeSelector" (list $root $cron) | nindent 10 }}
{{- include "monochart.tolerations" (list $root $cron) | nindent 10 }}
containers:
- name: {{ $root.Release.Name }}
image: {{ required "image.repository is required!" $root.Values.image.repository }}:{{ required "image.tag is required!" $root.Values.image.tag }}
Expand Down
3 changes: 3 additions & 0 deletions incubator/monochart/templates/daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ spec:
{{- if index .Values "serviceAccountName" }}
serviceAccountName: {{ .Values.serviceAccountName }}
{{- end }}
{{- include "monochart.affinity" (list . .Values.daemonset) | nindent 6 }}
{{- include "monochart.nodeSelector" (list . .Values.daemonset) | nindent 6 }}
{{- include "monochart.tolerations" (list . .Values.daemonset) | nindent 6 }}
containers:
- name: {{ .Release.Name }}
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Expand Down
29 changes: 4 additions & 25 deletions incubator/monochart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ spec:
annotations:
checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
{{- if .Values.deployment.pod}}
{{- if .Values.deployment.pod }}
{{- with .Values.deployment.pod.annotations }}
{{ toYaml . | indent 8 }}
{{- end }}
Expand All @@ -47,30 +47,9 @@ spec:
{{- if index .Values "serviceAccountName" }}
serviceAccountName: {{ .Values.serviceAccountName }}
{{- end }}
{{- if .Values.deployment.affinity }}
affinity:
{{- if or .Values.deployment.affinity.podAntiAffinity (eq .Values.deployment.affinity.affinityRule "ShouldBeOnDifferentNode") }}
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
{{- if .Values.deployment.affinity.podAntiAffinity }}
{{- if .Values.deployment.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{- with .Values.deployment.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 10 }}
{{- end }}
{{- end }}
{{- end }}
{{- if eq .Values.deployment.affinity.affinityRule "ShouldBeOnDifferentNode" }}
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" . | nindent 10 }}
{{- end }}
{{- end }}
{{- if .Values.deployment.affinity.podAffinity }}
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
{{- with .Values.deployment.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 10 }}
{{- end }}
{{- end }}
{{- end }}
{{- include "monochart.affinity" (list . .Values.deployment) | nindent 6 }}
{{- include "monochart.nodeSelector" (list . .Values.deployment) | nindent 6 }}
{{- include "monochart.tolerations" (list . .Values.deployment) | nindent 6 }}
containers:
- name: {{ .Release.Name }}
image: {{ required "image.repository is required!" .Values.image.repository }}:{{ required "image.tag is required!" .Values.image.tag }}
Expand Down
3 changes: 3 additions & 0 deletions incubator/monochart/templates/job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ spec:
serviceAccountName: {{ $root.Values.serviceAccountName }}
{{- end }}
restartPolicy: '{{ default "Never" $job.restartPolicy }}'
{{- include "monochart.affinity" (list . $job) | nindent 6 }}
{{- include "monochart.nodeSelector" (list . $job) | nindent 6 }}
{{- include "monochart.tolerations" (list . $job) | nindent 6 }}
containers:
- name: {{ $root.Release.Name }}
image: {{ required "image.repository is required!" $root.Values.image.repository }}:{{ required "image.tag is required!" $root.Values.image.tag }}
Expand Down
27 changes: 3 additions & 24 deletions incubator/monochart/templates/statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,9 @@ spec:
serviceAccountName: {{ .Values.serviceAccountName }}
{{- end }}
terminationGracePeriodSeconds: {{ .Values.statefulset.terminationGracePeriodSeconds }}
{{- if .Values.statefulset.affinity }}
affinity:
{{- if or .Values.statefulset.affinity.podAntiAffinity (eq .Values.statefulset.affinity.affinityRule "ShouldBeOnDifferentNode") }}
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
{{- if .Values.statefulset.affinity.podAntiAffinity }}
{{- if .Values.statefulset.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{- with .Values.statefulset.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 10 }}
{{- end }}
{{- end }}
{{- end }}
{{- if eq .Values.statefulset.affinity.affinityRule "ShouldBeOnDifferentNode" }}
{{- include "monochart.affinityRule.ShouldBeOnDifferentNode" . | nindent 10 }}
{{- end }}
{{- end }}
{{- if .Values.statefulset.affinity.podAffinity }}
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
{{- with .Values.statefulset.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution }}
{{ toYaml . | indent 10 }}
{{- end }}
{{- end }}
{{- end }}
{{- include "monochart.affinity" (list . .Values.statefulset) | nindent 6 }}
{{- include "monochart.nodeSelector" (list . .Values.statefulset) | nindent 6 }}
{{- include "monochart.tolerations" (list . .Values.statefulset) | nindent 6 }}
containers:
- name: {{ .Release.Name }}
image: {{ required "image.repository is required!" .Values.image.repository }}:{{ required "image.tag is required!" .Values.image.tag }}
Expand Down
70 changes: 61 additions & 9 deletions incubator/monochart/values.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,66 @@ deployment:
# use custom affinity rule. Here app MUST be on different host then postgres instance for it
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- postgresql
- key: release
operator: In
values:
- "postgresql"
topologyKey: "kubernetes.io/hostname"
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: label-1
operator: In
values:
- key-1
- weight: 50
preference:
matchExpressions:
- key: label-2
operator: In
values:
- key-2
tolerations:
- key: "gpu3"
operator: "Exists"
effect: "NoSchedule"


nodeSelector:
disktype: ssd

tolerations: []


affinity:
# use of simple rule
affinityRule: "ShouldBeOnDifferentNode"
# use custom affinity rule. Here app MUST be on different host then postgres instance for it
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- postgresql
- key: release
operator: In
values:
- "{{ requiredEnv "RELEASE_NAME" }}-postgresql"
topologyKey: "kubernetes.io/hostname"
- key: app
operator: In
values:
- postgresql
- key: release
operator: In
values:
- "postgresql"
topologyKey: "kubernetes.io/hostname"
Loading

0 comments on commit 0214ead

Please sign in to comment.