diff --git a/charts/drupal/templates/backup-cron.yaml b/charts/drupal/templates/backup-cron.yaml index c83cf2bf..0910d2ea 100644 --- a/charts/drupal/templates/backup-cron.yaml +++ b/charts/drupal/templates/backup-cron.yaml @@ -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 diff --git a/charts/drupal/templates/drupal-cron.yaml b/charts/drupal/templates/drupal-cron.yaml index e2385be0..9f755d79 100644 --- a/charts/drupal/templates/drupal-cron.yaml +++ b/charts/drupal/templates/drupal-cron.yaml @@ -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 }} @@ -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 }} @@ -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 }} @@ -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 }} diff --git a/charts/drupal/templates/drupal-deployment.yaml b/charts/drupal/templates/drupal-deployment.yaml index 7875c89f..2da8bd55 100644 --- a/charts/drupal/templates/drupal-deployment.yaml +++ b/charts/drupal/templates/drupal-deployment.yaml @@ -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 @@ -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 @@ -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: @@ -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 }} diff --git a/charts/drupal/templates/shell-deployment.yaml b/charts/drupal/templates/shell-deployment.yaml index aefc82f4..fe568b2d 100644 --- a/charts/drupal/templates/shell-deployment.yaml +++ b/charts/drupal/templates/shell-deployment.yaml @@ -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 diff --git a/charts/drupal/tests/drupal_affinity_test.yaml b/charts/drupal/tests/drupal_affinity_test.yaml new file mode 100644 index 00000000..328656fd --- /dev/null +++ b/charts/drupal/tests/drupal_affinity_test.yaml @@ -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" \ No newline at end of file diff --git a/charts/drupal/tests/drupal_node_selector_test.yaml b/charts/drupal/tests/drupal_node_selector_test.yaml index 7bbb7caa..aa089b01 100644 --- a/charts/drupal/tests/drupal_node_selector_test.yaml +++ b/charts/drupal/tests/drupal_node_selector_test.yaml @@ -14,10 +14,8 @@ tests: - it: has no node selector by default template: drupal-deployment.yaml asserts: - - equal: + - notExists: path: spec.template.spec.nodeSelector - value: {} - - it: can set a node selector template: drupal-deployment.yaml set: @@ -69,9 +67,8 @@ tests: php.cron.drupal.nodeSelector: foo: bar asserts: - - equal: + - notExists: path: spec.template.spec.nodeSelector - value: {} - template: drupal-cron.yaml equal: path: spec.jobTemplate.spec.template.spec.nodeSelector.foo @@ -90,9 +87,8 @@ tests: shell.nodeSelector: foo: bar asserts: - - equal: + - notExists: path: spec.template.spec.nodeSelector - value: {} - template: shell-deployment.yaml equal: path: spec.template.spec.nodeSelector.foo diff --git a/charts/drupal/tests/drupal_tolerations_test.yaml b/charts/drupal/tests/drupal_tolerations_test.yaml new file mode 100644 index 00000000..cc23455c --- /dev/null +++ b/charts/drupal/tests/drupal_tolerations_test.yaml @@ -0,0 +1,101 @@ +suite: tolerations 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: has no custom tolerations by default + template: drupal-deployment.yaml + asserts: + - notExists: + path: spec.template.spec.tolerations + + - it: can set custom tolerations for php pods + template: drupal-deployment.yaml + set: + php.tolerations: + - key: "custom-key" + operator: "Equal" + value: "custom-value" + effect: "NoSchedule" + asserts: + - exists: + path: spec.template.spec.tolerations + - contains: + path: spec.template.spec.tolerations + content: + key: "custom-key" + operator: "Equal" + value: "custom-value" + effect: "NoSchedule" + + - it: applies custom tolerations to shell deployment + template: shell-deployment.yaml + set: + shell.tolerations: + - key: "shell-key" + operator: "Equal" + value: "shell-value" + effect: "NoSchedule" + asserts: + - contains: + path: spec.template.spec.tolerations + content: + key: "shell-key" + operator: "Equal" + value: "shell-value" + effect: "NoSchedule" + + - it: applies custom tolerations to cron jobs + template: drupal-cron.yaml + set: + php.cron.drupal.tolerations: + - key: "cron-key" + operator: "Equal" + value: "cron-value" + effect: "NoSchedule" + asserts: + - contains: + path: spec.jobTemplate.spec.template.spec.tolerations + content: + key: "cron-key" + operator: "Equal" + value: "cron-value" + effect: "NoSchedule" + + - it: can set different tolerations for php and shell + template: drupal-deployment.yaml + set: + php.tolerations: + - key: "php-key" + operator: "Equal" + value: "php-value" + effect: "NoSchedule" + shell.tolerations: + - key: "shell-key" + operator: "Equal" + value: "shell-value" + effect: "NoSchedule" + asserts: + - contains: + path: spec.template.spec.tolerations + content: + key: "php-key" + operator: "Equal" + value: "php-value" + effect: "NoSchedule" + - template: shell-deployment.yaml + contains: + path: spec.template.spec.tolerations + content: + key: "shell-key" + operator: "Equal" + value: "shell-value" + effect: "NoSchedule" \ No newline at end of file diff --git a/charts/drupal/values.schema.json b/charts/drupal/values.schema.json index 754e5b57..f3a27a3a 100644 --- a/charts/drupal/values.schema.json +++ b/charts/drupal/values.schema.json @@ -278,6 +278,14 @@ "nodeSelector": { "type": "object", "additionalProperties": { "type": "string" } + }, + "tolerations": { + "type": ["array", "null"], + "additionalProperties": { "type": "string" } + }, + "affinity": { + "type": ["object", "null"], + "additionalProperties": { "type": "string" } } } }, @@ -291,6 +299,8 @@ "command": { "type": "string" }, "backoffLimit": { "type": "integer" }, "nodeSelector": { "type": ["object", "null"] }, + "tolerations": { "type": ["array", "null"] }, + "affinity": { "type": ["object", "null"] }, "php_ini": { "type": "string" }, "resources": { "type": "object", @@ -369,7 +379,9 @@ }, "drupalConfigPath": { "type": "string" }, "drupalCoreVersion": { "type": "string" }, - "nodeSelector": { "type": "object" } + "nodeSelector": { "type": "object" }, + "tolerations": { "type": ["array", "null"] }, + "affinity": { "type": ["object", "null"] } } }, "shell": { @@ -426,7 +438,9 @@ } } }, - "nodeSelector": { "type": "object" } + "nodeSelector": { "type": "object" }, + "tolerations": { "type": ["array", "null"] }, + "affinity": { "type": ["object", "null"] } } }, "mounts": { @@ -531,6 +545,8 @@ "ignoreTableContent": { "type": "string" }, "skipFiles": { "type": "boolean" }, "nodeSelector": { "type": "object" }, + "tolerations": { "type": ["array", "null"] }, + "affinity": { "type": ["object", "null"] }, "resources": { "type": "object", "additionalProperties": false, diff --git a/charts/drupal/values.yaml b/charts/drupal/values.yaml index 03e8c253..8b370f75 100644 --- a/charts/drupal/values.yaml +++ b/charts/drupal/values.yaml @@ -291,17 +291,16 @@ php: drush cron; fi - # Add custom php.ini file overrides for the cronjob. - # Use the same syntax as in php.ini file - # Example: - # php_ini: | - # [PHP] - # memory_limit=128M + # Add custom php.ini file overrides for the cronjob. + # Use the same syntax as in php.ini file + # Example: + # php_ini: | + # [PHP] + # memory_limit=128M - # php_ini: | - - # Set a nodeSelector specifically for cron jobs. nodeSelector: {} + tolerations: [] + affinity: {} # Specify a how many attempts should be made before considering a cron job as failed. backoffLimit: 1 @@ -384,6 +383,53 @@ php: # Set a restriction on where the php pod are provisioned. # This can be used for example to get a static IP for egress traffic. nodeSelector: {} + tolerations: [] + 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 # Configuration for everything that runs in shell container. shell: @@ -421,6 +467,8 @@ shell: # Set a restriction on where the shell pod are provisioned. # This can be used for example to get a static IP for egress traffic. nodeSelector: {} + tolerations: [] + affinity: {} # Configure the dynamically mounted volumes mounts: @@ -558,8 +606,9 @@ backup: # Do not backup files #skipFiles: true - # Set a nodeSelector specifically for backup jobs. nodeSelector: {} + tolerations: [] + affinity: {} # Resources for the backup cron job. resources: