Skip to content

Custom affinities and tolerations #758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions charts/drupal/templates/backup-cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ spec:
{{- .Values.backup.nodeSelector | toYaml | nindent 12 }}
tolerations:
{{- include "drupal.tolerations" .Values.backup.nodeSelector | nindent 12 }}
{{- if .Values.backup.tolerations }}
{{- .Values.backup.tolerations | toYaml | nindent 12 }}
{{- end }}
{{- if .Values.backup.affinity }}
affinity:
{{- tpl ( .Values.backup.affinity | toYaml ) . | nindent 12 }}
{{- end }}
volumes:
{{- include "drupal.volumes" . | nindent 12 }}
- name: {{ .Release.Name }}-backup
Expand Down
23 changes: 23 additions & 0 deletions charts/drupal/templates/drupal-cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ spec:
# Use the exit code of the actual cron command.
exit "$exit_code"

{{- if or $job.resources ($.Values.php.cronJobDefaults).resources $.Values.php.resources }}
resources:
{{- if $job.resources }}
{{- if ($.Values.php.cronJobDefaults).resources }}
Expand All @@ -127,7 +128,9 @@ spec:
{{- $.Values.php.resources | toYaml | nindent 14 }}
{{- end }}
{{- end }}
{{- end }}
restartPolicy: Never
{{- if or $job.nodeSelector ($.Values.php.cronJobDefaults).nodeSelector $.Values.php.nodeSelector }}
nodeSelector:
{{- if $job.nodeSelector }}
{{- if ($.Values.php.cronJobDefaults).nodeSelector }}
Expand All @@ -142,6 +145,8 @@ spec:
{{- $.Values.php.nodeSelector | toYaml | nindent 12 }}
{{- end }}
{{- end }}
{{- end }}
{{- if or $job.tolerations ($.Values.php.cronJobDefaults).tolerations $.Values.php.tolerations $job.nodeSelector ($.Values.php.cronJobDefaults).nodeSelector $.Values.php.nodeSelector }}
tolerations:
{{- if $job.nodeSelector }}
{{- if ($.Values.php.cronJobDefaults).nodeSelector }}
Expand All @@ -156,6 +161,24 @@ spec:
{{- include "drupal.tolerations" $.Values.php.nodeSelector | nindent 12 }}
{{- end }}
{{- end }}
{{- if $job.tolerations }}
{{- $job.tolerations | toYaml | nindent 12 }}
{{- else if ($.Values.php.cronJobDefaults).tolerations }}
{{- $.Values.php.cronJobDefaults.tolerations | toYaml | nindent 12 }}
{{- else if $.Values.php.tolerations }}
{{- $.Values.php.tolerations | toYaml | nindent 12 }}
{{- end }}
{{- end }}
{{- if or $job.affinity ($.Values.php.cronJobDefaults).affinity $.Values.php.affinity }}
affinity:
{{- if $job.affinity }}
{{- tpl ($job.affinity | toYaml ) $ | nindent 12 }}
{{- else if ($.Values.php.cronJobDefaults).affinity }}
{{- tpl ($.Values.php.cronJobDefaults.affinity | toYaml ) $ | nindent 12 }}
{{- else if $.Values.php.affinity }}
{{- tpl ($.Values.php.affinity | toYaml ) $ | nindent 12 }}
{{- end }}
{{- end }}
volumes:
{{- include "drupal.volumes" $ | nindent 12 }}

Expand Down
61 changes: 16 additions & 45 deletions charts/drupal/templates/drupal-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ spec:
preStop:
exec:
command: [ "/bin/sleep", "15" ]
{{- if .Values.php.resources }}
resources:
{{- .Values.php.resources | toYaml | nindent 10 }}
{{- end }}

# Nginx container
- name: nginx
Expand Down Expand Up @@ -120,8 +122,10 @@ spec:
preStop:
exec:
command: ["/bin/sleep", "15"]
{{- if .Values.nginx.resources }}
resources:
{{- .Values.nginx.resources | toYaml | nindent 10 }}
{{- end }}

{{- if .Values.signalsciences.enabled }}
# Signal Services container
Expand All @@ -148,8 +152,10 @@ spec:
preStop:
exec:
command: ["/bin/sleep", "30"]
{{- if .Values.signalsciences.resources }}
resources:
{{- .Values.signalsciences.resources | toYaml | nindent 10 }}
{{- end }}
{{- end }}

volumes:
Expand All @@ -167,56 +173,21 @@ spec:
emptyDir: {}
{{- end }}
serviceAccountName: {{ include "drupal.serviceAccountName" . }}
{{- if .Values.php.nodeSelector }}
nodeSelector:
{{- .Values.php.nodeSelector | toYaml | nindent 8 }}
{{- end }}
{{- if or .Values.php.tolerations (include "drupal.tolerations" .Values.php.nodeSelector) }}
tolerations:
{{- include "drupal.tolerations" .Values.php.nodeSelector | nindent 8 }}
{{- if .Values.php.tolerations }}
{{- .Values.php.tolerations | toYaml | nindent 8 }}
{{- end }}
{{- end }}
{{- if .Values.php.affinity }}
affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
# Preferrably keep pods on the same node as the database.
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: release
operator: In
values:
- "{{ .Release.Name }}"
- key: mariadb
operator: In
values:
- mariadb
topologyKey: kubernetes.io/hostname
# Preferrably keep pods in the same zone as the database.
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: release
operator: In
values:
- "{{ .Release.Name }}"
- key: mariadb
operator: In
values:
- mariadb
topologyKey: topology.kubernetes.io/zone
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 10
podAffinityTerm:
topologyKey: kubernetes.io/hostname
labelSelector:
matchExpressions:
- key: release
operator: In
values:
- "{{ .Release.Name }}"
- key: deployment
operator: In
values:
- drupal
{{- tpl ( .Values.php.affinity | toYaml ) . | nindent 8 }}
{{- end }}
---
{{- if .Values.autoscaling.enabled }}
apiVersion: {{ include "drupal.autoscaling.api-version" . | trim }}
Expand Down
26 changes: 23 additions & 3 deletions charts/drupal/templates/shell-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,40 @@ spec:
mountPath: /backups
readOnly: true
{{- end }}
{{- if or $.Values.shell.resources .Values.php.resources }}
resources:
{{- merge .Values.shell.resources .Values.php.resources | toYaml | nindent 10 }}
{{- end }}
{{- if or .Values.shell.nodeSelector .Values.php.nodeSelector }}
nodeSelector:
{{ if .Values.shell.nodeSelector }}
{{- if .Values.shell.nodeSelector }}
{{- .Values.shell.nodeSelector | toYaml | nindent 8 }}
{{ else }}
{{- else }}
{{- .Values.php.nodeSelector | toYaml | nindent 8 }}
{{- end }}
{{- end }}
{{- if or .Values.shell.tolerations .Values.php.tolerations .Values.shell.nodeSelector .Values.php.nodeSelector }}
tolerations:
{{ if .Values.shell.nodeSelector }}
{{- include "drupal.tolerations" .Values.shell.nodeSelector | nindent 8 }}
{{ else }}
{{- else }}
{{- include "drupal.tolerations" .Values.php.nodeSelector | nindent 8 }}
{{- end }}
{{- if .Values.shell.tolerations }}
{{- .Values.shell.tolerations | toYaml | nindent 8 }}
{{- end }}
{{- if .Values.php.tolerations }}
{{- .Values.php.tolerations | toYaml | nindent 8 }}
{{- end }}
{{- end }}
{{- if or .Values.shell.affinity .Values.php.affinity }}
affinity:
{{- if .Values.shell.affinity }}
{{- tpl ( .Values.shell.affinity | toYaml ) . | nindent 8 }}
{{- else }}
{{- tpl ( .Values.php.affinity | toYaml ) . | nindent 8 }}
{{- end }}
{{- end }}
volumes:
{{- include "drupal.volumes" . | nindent 6 }}
- name: ssh-keys
Expand Down
175 changes: 175 additions & 0 deletions charts/drupal/tests/drupal_affinity_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
suite: affinity test
templates:
- drupal-configmap.yaml
- drupal-cron.yaml
- drupal-deployment.yaml
- drupal-secret.yaml
- shell-configmap.yaml
- shell-secret.yaml
- shell-deployment.yaml
capabilities:
apiVersions:
- pxc.percona.com/v1
tests:
- it: can set custom pod affinity for php pods
template: drupal-deployment.yaml
set:
php.affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "custom-node-label"
operator: "In"
values:
- "true"
asserts:
- exists:
path: spec.template.spec.affinity
- exists:
path: spec.template.spec.affinity.nodeAffinity
- exists:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
- contains:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions
content:
key: "custom-node-label"
operator: "In"
values:
- "true"

- it: applies custom affinity to shell deployment
template: shell-deployment.yaml
set:
shell.affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "shell-node-label"
operator: "In"
values:
- "true"
asserts:
- isNotNull:
path: spec.template.spec.affinity.nodeAffinity
- isNotNull:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
- contains:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions
content:
key: "shell-node-label"
operator: "In"
values:
- "true"

- it: applies custom affinity to cron jobs
template: drupal-cron.yaml
set:
php.cron.drupal.affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "cron-node-label"
operator: "In"
values:
- "true"
asserts:
- isNotNull:
path: spec.jobTemplate.spec.template.spec.affinity.nodeAffinity
- isNotNull:
path: spec.jobTemplate.spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution
- contains:
path: spec.jobTemplate.spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions
content:
key: "cron-node-label"
operator: "In"
values:
- "true"

- it: can set different affinities for php and shell
template: drupal-deployment.yaml
set:
php.affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "php-node-label"
operator: "In"
values:
- "true"
shell.affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "shell-node-label"
operator: "In"
values:
- "true"
asserts:
- contains:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions
content:
key: "php-node-label"
operator: "In"
values:
- "true"
- template: shell-deployment.yaml
contains:
path: spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions
content:
key: "shell-node-label"
operator: "In"
values:
- "true"

- it: can set pod affinity and pod anti-affinity
template: drupal-deployment.yaml
set:
php.affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: "In"
values:
- "database"
topologyKey: "kubernetes.io/hostname"
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: "app"
operator: "In"
values:
- "frontend"
topologyKey: "kubernetes.io/hostname"
asserts:
- isNotNull:
path: spec.template.spec.affinity.podAffinity
- isNotNull:
path: spec.template.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution
- contains:
path: spec.template.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution[0].labelSelector.matchExpressions
content:
key: "app"
operator: "In"
values:
- "database"
- isNotNull:
path: spec.template.spec.affinity.podAntiAffinity
- isNotNull:
path: spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution
- contains:
path: spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution[0].podAffinityTerm.labelSelector.matchExpressions
content:
key: "app"
operator: "In"
values:
- "frontend"
Loading