Skip to content

Commit

Permalink
Support creation of configmaps & secrets and extra env & envFrom conf…
Browse files Browse the repository at this point in the history
…iguration in Helm Chart (#12164)

* Enable provisionning of extra secrets and configmaps in helm chart

Added 2 new values:
*  extraSecrets
*  extraConfigMaps

Those values enable the provisionning of ConfigMaps
and secrets directly from the airflow chart.

Those objects could be used for storing airflow variables
or (secret) connections info for instance
(the plan is to add support for extraEnv and extraEnvFrom later).

Docs and tests updated accordingly.

* Add support for extra env and envFrom items in helm chart

Added 2 new values:
*  extraEnv
*  extraEnvFrom

Those values will be added to the defintion of
airflow containers. They are expected to be string
(they can be templated).

Those new values won't be supported by "legacy" kubernetes
executor configuration (you must use the pod template).

Therefore, the value 'env' is also deprecated as it's kind
of a duplicate for extraEnv.

Docs and tests updated accordingly.

(cherry picked from commit 56ee2bb)
  • Loading branch information
Florent Chehab authored and ashb committed Dec 3, 2020
1 parent 7356ae1 commit daa725b
Show file tree
Hide file tree
Showing 17 changed files with 485 additions and 11 deletions.
6 changes: 5 additions & 1 deletion chart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ The following tables lists the configurable parameters of the Airflow chart and
| `images.pgbouncerExporter.repository` | Docker repository to pull image from. Update this to deploy a custom image | `apache/airflow` |
| `images.pgbouncerExporter.tag` | Docker image tag to pull image from. Update this to deploy a new custom image tag | `airflow-pgbouncer-exporter-2020.09.25-0.5.0` |
| `images.pgbouncerExporter.pullPolicy` | PullPolicy for pgbouncer-exporter image | `IfNotPresent` |
| `env` | Environment variables key/values to mount into Airflow pods | `[]` |
| `env` | Environment variables key/values to mount into Airflow pods (deprecated, prefer using extraEnv) | `[]` |
| `secret` | Secret name/key pairs to mount into Airflow pods | `[]` |
| `extraEnv` | Extra env 'items' that will be added to the definition of airflow containers | `~` |
| `extraEnvFrom` | Extra envFrom 'items' that will be added to the definition of airflow containers | `~` |
| `extraSecrets` | Extra Secrets that will be managed by the chart | `{}` |
| `extraConfigMaps` | Extra ConfigMaps that will be managed by the chart | `{}` |
| `data.metadataSecretName` | Secret name to mount Airflow connection string from | `~` |
| `data.resultBackendSecretName` | Secret name to mount Celery result backend connection string from | `~` |
| `data.metadataConection` | Field separated connection data (alternative to secret name) | `{}` |
Expand Down
11 changes: 6 additions & 5 deletions chart/files/pod-template-file.kubernetes-helm-yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ spec:
containers:
- args: []
command: []
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 6 }}
env:
- name: AIRFLOW__CORE__EXECUTOR
value: LocalExecutor
{{- include "standard_airflow_environment" . | indent 4 }}
{{- include "custom_airflow_environment" . | indent 4 }}
envFrom: []
- name: AIRFLOW__CORE__EXECUTOR
value: LocalExecutor
{{- include "standard_airflow_environment" . | indent 6}}
{{- include "custom_airflow_environment" . | indent 6 }}
image: {{ template "pod_template_image" . }}
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
name: base
Expand Down
15 changes: 14 additions & 1 deletion chart/templates/_helpers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,25 @@
name: {{ $config.secretName }}
key: {{ default "value" $config.secretKey }}
{{- end }}
{{- if or (eq $.Values.executor "KubernetesExecutor") (eq $.Values.executor "CeleryKubernetesExecutor") }}
{{- if or (eq $.Values.executor "KubernetesExecutor") (eq $.Values.executor "CeleryKubernetesExecutor") }}
{{- range $i, $config := .Values.secret }}
- name: AIRFLOW__KUBERNETES_SECRETS__{{ $config.envName }}
value: {{ printf "%s=%s" $config.secretName $config.secretKey }}
{{- end }}
{{ end }}
# Extra env
{{- $Global := . }}
{{- with .Values.extraEnv }}
{{- tpl . $Global | nindent 2 }}
{{- end }}
{{- end }}

{{/* User defined Airflow environment from */}}
{{- define "custom_airflow_environment_from" }}
{{- $Global := . }}
{{- with .Values.extraEnvFrom }}
{{- tpl . $Global | nindent 2 }}
{{- end }}
{{- end }}

{{/* Git ssh key volume */}}
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions chart/templates/configmaps/extra-configmaps.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

####################################################
## Extra ConfigMaps provisioned via the chart values
####################################################
{{- $Global := . }}
{{- range $configMapName, $configMapContent := .Values.extraConfigMaps }}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ tpl $configMapName $Global | quote }}
labels:
release: {{ $Global.Release.Name }}
chart: "{{ $Global.Chart.Name }}-{{ $Global.Chart.Version }}"
heritage: {{ $Global.Release.Service }}
annotations:
"helm.sh/hook": "pre-install,pre-upgrade"
"helm.sh/hook-delete-policy": "before-hook-creation"
"helm.sh/hook-weight": "0"
{{- with $Global.Values.labels }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- if $configMapContent.data }}
data:
{{- with $configMapContent.data }}
{{- tpl . $Global | nindent 2 }}
{{- end }}
{{- end }}
{{- end }}
2 changes: 2 additions & 0 deletions chart/templates/create-user-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ spec:
- {{ .Values.webserver.defaultUser.lastName }}
- "-p"
- {{ .Values.webserver.defaultUser.password }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/flower/flower-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ metadata:
{{ toYaml . | indent 4 }}
{{- end }}
annotations:
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
spec:
replicas: 1
selector:
Expand Down
2 changes: 2 additions & 0 deletions chart/templates/migrate-database-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ spec:
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
# Support running against 1.10.x and 2.0.0dev/master
args: ["bash", "-c", "airflow upgradedb || airflow db upgrade"]
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down
10 changes: 9 additions & 1 deletion chart/templates/scheduler/scheduler-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ spec:
checksum/metadata-secret: {{ include (print $.Template.BasePath "/secrets/metadata-connection-secret.yaml") . | sha256sum }}
checksum/result-backend-secret: {{ include (print $.Template.BasePath "/secrets/result-backend-connection-secret.yaml") . | sha256sum }}
checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
checksum/extra-configmaps: {{ include (print $.Template.BasePath "/configmaps/extra-configmaps.yaml") . | sha256sum }}
checksum/extra-secrets: {{ include (print $.Template.BasePath "/secrets/extra-secrets.yaml") . | sha256sum }}
{{- if .Values.scheduler.safeToEvict }}
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
{{- end }}
Expand Down Expand Up @@ -95,6 +97,8 @@ spec:
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
args:
{{- include "wait-for-migrations-command" . | indent 10 }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand All @@ -104,6 +108,8 @@ spec:
image: {{ template "airflow_image" . }}
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
args: ["scheduler"]
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down Expand Up @@ -184,6 +190,8 @@ spec:
mountPath: {{ template "airflow_config_path" . }}
subPath: airflow.cfg
readOnly: true
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down
51 changes: 51 additions & 0 deletions chart/templates/secrets/extra-secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

#################################################
## Extra Secrets provisioned via the chart values
#################################################
{{- $Global := . }}
{{- range $secretName, $secretContent := .Values.extraSecrets }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ tpl $secretName $Global | quote }}
labels:
release: {{ $Global.Release.Name }}
chart: "{{ $Global.Chart.Name }}-{{ $Global.Chart.Version }}"
heritage: {{ $Global.Release.Service }}
annotations:
"helm.sh/hook": "pre-install,pre-upgrade"
"helm.sh/hook-delete-policy": "before-hook-creation"
"helm.sh/hook-weight": "0"
{{- with $Global.Values.labels }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- if $secretContent.data }}
data:
{{- with $secretContent.data }}
{{- tpl . $Global | nindent 2 }}
{{- end }}
{{- end }}
{{- if $secretContent.stringData }}
stringData:
{{- with $secretContent.stringData }}
{{- tpl . $Global | nindent 2 }}
{{- end }}
{{- end }}
{{- end }}
8 changes: 7 additions & 1 deletion chart/templates/webserver/webserver-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ spec:
annotations:
checksum/metadata-secret: {{ include (print $.Template.BasePath "/secrets/metadata-connection-secret.yaml") . | sha256sum }}
checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
checksum/extra-configmaps: {{ include (print $.Template.BasePath "/configmaps/extra-configmaps.yaml") . | sha256sum }}
checksum/extra-secrets: {{ include (print $.Template.BasePath "/secrets/extra-secrets.yaml") . | sha256sum }}
{{- if .Values.airflowPodAnnotations }}
{{- toYaml .Values.airflowPodAnnotations | nindent 8 }}
{{- end }}
Expand All @@ -80,6 +82,8 @@ spec:
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
args:
{{- include "wait-for-migrations-command" . | indent 10 }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down Expand Up @@ -136,6 +140,8 @@ spec:
timeoutSeconds: {{ .Values.webserver.readinessProbe.timeoutSeconds | default 30 }}
failureThreshold: {{ .Values.webserver.readinessProbe.failureThreshold | default 20 }}
periodSeconds: {{ .Values.webserver.readinessProbe.periodSeconds | default 5 }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down
10 changes: 9 additions & 1 deletion chart/templates/workers/worker-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ spec:
checksum/metadata-secret: {{ include (print $.Template.BasePath "/secrets/metadata-connection-secret.yaml") . | sha256sum }}
checksum/result-backend-secret: {{ include (print $.Template.BasePath "/secrets/result-backend-connection-secret.yaml") . | sha256sum }}
checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
checksum/extra-configmaps: {{ include (print $.Template.BasePath "/configmaps/extra-configmaps.yaml") . | sha256sum }}
checksum/extra-secrets: {{ include (print $.Template.BasePath "/secrets/extra-secrets.yaml") . | sha256sum }}
{{- if .Values.workers.safeToEvict }}
cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
{{- end }}
Expand Down Expand Up @@ -101,6 +103,8 @@ spec:
imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
args:
{{- include "wait-for-migrations-command" . | indent 10 }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down Expand Up @@ -146,6 +150,8 @@ spec:
- name: dags
mountPath: {{ template "airflow_dags_mount_path" . }}
{{- end }}
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
{{- include "custom_airflow_environment" . | indent 10 }}
{{- include "standard_airflow_environment" . | indent 10 }}
Expand Down Expand Up @@ -195,6 +201,8 @@ spec:
- name: kerberos-ccache
mountPath: {{ .Values.kerberos.ccacheMountPath | quote }}
readOnly: false
envFrom:
{{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
env:
- name: KRB5_CONFIG
value: {{ .Values.kerberos.configPath | quote }}
Expand Down
12 changes: 12 additions & 0 deletions chart/tests/helm_template_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
from functools import lru_cache
from tempfile import NamedTemporaryFile
from typing import Any, Dict, Tuple

import jmespath
import jsonschema
Expand Down Expand Up @@ -81,6 +82,17 @@ def render_chart(name="RELEASE-NAME", values=None, show_only=None):
return k8s_objects


def prepare_k8s_lookup_dict(k8s_objects) -> Dict[Tuple[str, str], Dict[str, Any]]:
"""
Helper to create a lookup dict from k8s_objects.
The keys of the dict are the k8s object's kind and name
"""
k8s_obj_by_key = {
(k8s_object["kind"], k8s_object["metadata"]["name"]): k8s_object for k8s_object in k8s_objects
}
return k8s_obj_by_key


def render_k8s_object(obj, type_to_render):
"""
Function that renders dictionaries into k8s objects. For helm chart testing only.
Expand Down

0 comments on commit daa725b

Please sign in to comment.