From f9153fe60b2d5d5f20096ed277bb2d6145cfd8e7 Mon Sep 17 00:00:00 2001 From: alikhtag <43503284+alikhtag@users.noreply.github.com> Date: Wed, 12 Aug 2020 20:12:32 +0200 Subject: [PATCH 1/3] fix podMutation to apply in airflow_local_settings.py if it was set in values.yaml --- chart/templates/configmap.yaml | 6 ++++++ chart/templates/scheduler/scheduler-deployment.yaml | 2 +- chart/values.yaml | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/chart/templates/configmap.yaml b/chart/templates/configmap.yaml index b24e474db3cd4..34b65c9e334e2 100644 --- a/chart/templates/configmap.yaml +++ b/chart/templates/configmap.yaml @@ -50,6 +50,12 @@ data: {{- if .Values.scheduler.airflowLocalSettings }} airflow_local_settings.py: | {{ .Values.scheduler.airflowLocalSettings | nindent 4 }} +{{- else if or (.Values.podMutation.tolerations) (.Values.podMutation.affinity)}} + airflow_local_settings.py: | + from airflow.contrib.kubernetes.pod import Pod + def pod_mutation_hook(pod: Pod): + pod.tolerations += {{ toJson .Values.podMutation.tolerations }} + pod.affinity.update({{ toJson .Values.podMutation.affinity }}) {{- end }} {{- if and .Values.dags.gitSync.enabled .Values.dags.gitSync.knownHosts }} known_hosts: | diff --git a/chart/templates/scheduler/scheduler-deployment.yaml b/chart/templates/scheduler/scheduler-deployment.yaml index 933155687bf56..aa105ba42c301 100644 --- a/chart/templates/scheduler/scheduler-deployment.yaml +++ b/chart/templates/scheduler/scheduler-deployment.yaml @@ -139,7 +139,7 @@ spec: mountPath: {{ template "airflow_config_path" . }} subPath: airflow.cfg readOnly: true -{{- if .Values.scheduler.airflowLocalSettings }} +{{- if or ( .Values.scheduler.airflowLocalSettings ) ( or (.Values.podMutation.tolerations) (.Values.podMutation.affinity)) }} - name: config mountPath: {{ template "airflow_local_setting_path" . }} subPath: airflow_local_settings.py diff --git a/chart/values.yaml b/chart/values.yaml index 082418bb9c8ec..d685c5afd7be0 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -404,6 +404,7 @@ quotas: {} limits: [] # Config Settings for pod_mutation_hook +# podMutation values will not apply if `scheduler.airflowLocalSettings` have been modified podMutation: # Tolerations provided here would be applied using pod_mutation_hook # So any pods spun up using KubernetesExecutor or KubernetesPodOperator will contain these tolerations. From e991b19175016e8b9121553986cfcdc1a6a7c79d Mon Sep 17 00:00:00 2001 From: alikhtag <43503284+alikhtag@users.noreply.github.com> Date: Fri, 14 Aug 2020 01:30:30 +0200 Subject: [PATCH 2/3] Template the string in values for podMutation instead of having it in configmap.yaml --- chart/templates/configmap.yaml | 7 ++----- chart/templates/scheduler/scheduler-deployment.yaml | 2 +- chart/values.yaml | 13 +++++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/chart/templates/configmap.yaml b/chart/templates/configmap.yaml index 34b65c9e334e2..becbe1347a045 100644 --- a/chart/templates/configmap.yaml +++ b/chart/templates/configmap.yaml @@ -50,12 +50,9 @@ data: {{- if .Values.scheduler.airflowLocalSettings }} airflow_local_settings.py: | {{ .Values.scheduler.airflowLocalSettings | nindent 4 }} -{{- else if or (.Values.podMutation.tolerations) (.Values.podMutation.affinity)}} +{{- else if .Values.podMutation.enabled }} airflow_local_settings.py: | - from airflow.contrib.kubernetes.pod import Pod - def pod_mutation_hook(pod: Pod): - pod.tolerations += {{ toJson .Values.podMutation.tolerations }} - pod.affinity.update({{ toJson .Values.podMutation.affinity }}) + {{ tpl .Values.podMutation.podMutationHookTpl . | nindent 4 }} {{- end }} {{- if and .Values.dags.gitSync.enabled .Values.dags.gitSync.knownHosts }} known_hosts: | diff --git a/chart/templates/scheduler/scheduler-deployment.yaml b/chart/templates/scheduler/scheduler-deployment.yaml index aa105ba42c301..d7225af04b902 100644 --- a/chart/templates/scheduler/scheduler-deployment.yaml +++ b/chart/templates/scheduler/scheduler-deployment.yaml @@ -139,7 +139,7 @@ spec: mountPath: {{ template "airflow_config_path" . }} subPath: airflow.cfg readOnly: true -{{- if or ( .Values.scheduler.airflowLocalSettings ) ( or (.Values.podMutation.tolerations) (.Values.podMutation.affinity)) }} +{{- if or ( .Values.scheduler.airflowLocalSettings ) ( .Values.podMutation.enabled ) }} - name: config mountPath: {{ template "airflow_local_setting_path" . }} subPath: airflow_local_settings.py diff --git a/chart/values.yaml b/chart/values.yaml index d685c5afd7be0..2dd319a0e5c5a 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -406,8 +406,17 @@ limits: [] # Config Settings for pod_mutation_hook # podMutation values will not apply if `scheduler.airflowLocalSettings` have been modified podMutation: - # Tolerations provided here would be applied using pod_mutation_hook - # So any pods spun up using KubernetesExecutor or KubernetesPodOperator will contain these tolerations. + enabled: false + # You can just add `tolerations` and `affinity` below + # or define this podMutationHookTpl however you like. + # Anything can be templated here since it is using `tpl` helm function + podMutationHookTpl: | + from airflow.contrib.kubernetes.pod import Pod + def pod_mutation_hook(pod: Pod): + pod.tolerations += {{ toJson .Values.podMutation.tolerations }} + pod.affinity.update({{ toJson .Values.podMutation.affinity }}) + + # Any pods spun up using KubernetesExecutor or KubernetesPodOperator will contain these tolerations. tolerations: [] # - key: "dynamic-pods" # operator: "Equal" From 3a109d98b939ee77126a8ffd411891f8a9c36631 Mon Sep 17 00:00:00 2001 From: alikhtag <43503284+alikhtag@users.noreply.github.com> Date: Thu, 19 Nov 2020 18:02:23 +0100 Subject: [PATCH 3/3] update mutation hook to airflow >2.0.0. --- chart/values.yaml | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/chart/values.yaml b/chart/values.yaml index 97c2d13e4baeb..ecdf06999d712 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -624,11 +624,16 @@ podMutation: # You can just add `tolerations` and `affinity` below # or define this podMutationHookTpl however you like. # Anything can be templated here since it is using `tpl` helm function + # If using Airflow < 2.0.0 use following mutation hook: + # from airflow.contrib.kubernetes.pod import Pod + # def pod_mutation_hook(pod: Pod): + # pod.tolerations += {{ toJson .Values.podMutation.tolerations }} + # pod.affinity.update({{ toJson .Values.podMutation.affinity }}) podMutationHookTpl: | - from airflow.contrib.kubernetes.pod import Pod - def pod_mutation_hook(pod: Pod): - pod.tolerations += {{ toJson .Values.podMutation.tolerations }} - pod.affinity.update({{ toJson .Values.podMutation.affinity }}) + # Airflow >= 2.0.0 pod_mutation_hook + def pod_mutation_hook(pod): # type pod: k8s.V1Pod + pod.spec.tolerations = {{ toJson .Values.podMutation.tolerations }} + pod.spec.affinity = {{ toJson .Values.podMutation.affinity }} # Any pods spun up using KubernetesExecutor or KubernetesPodOperator will contain these tolerations. tolerations: [] @@ -647,7 +652,7 @@ podMutation: # operator: In # values: # - "true" - + # This runs as a CronJob to cleanup old pods. cleanup: enabled: false