Skip to content

Commit 67bc662

Browse files
committed
Add webserver_config.py file to api-server
1 parent c034275 commit 67bc662

File tree

6 files changed

+184
-0
lines changed

6 files changed

+184
-0
lines changed

chart/templates/_helpers.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,17 @@ server_tls_key_file = /etc/pgbouncer/server.key
582582
readOnly: True
583583
{{- end }}
584584

585+
{{- define "airflow_api_server_config_configmap_name" -}}
586+
{{- default (printf "%s-api-server-config" .Release.Name) .Values.webserver.webserverConfigConfigMapName }}
587+
{{- end }}
588+
589+
{{- define "airflow_api_server_config_mount" -}}
590+
- name: api-server-config
591+
mountPath: {{ template "airflow_webserver_config_path" . }}
592+
subPath: webserver_config.py
593+
readOnly: True
594+
{{- end }}
595+
585596
{{- define "airflow_local_setting_path" -}}
586597
{{- printf "%s/config/airflow_local_settings.py" .Values.airflowHome | quote }}
587598
{{- end }}

chart/templates/api-server/api-server-deployment.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ spec:
170170
resources: {{- toYaml .Values.apiServer.resources | nindent 12 }}
171171
volumeMounts:
172172
{{- include "airflow_config_mount" . | nindent 12 }}
173+
{{- if or .Values.apiServer.apiServerConfig .Values.apiServer.apiServerConfigConfigMapName }}
174+
{{- include "airflow_api_server_config_mount" . | nindent 12 }}
175+
{{- end }}
173176
{{- if .Values.logs.persistence.enabled }}
174177
- name: logs
175178
mountPath: {{ template "airflow_logs" . }}
@@ -225,6 +228,11 @@ spec:
225228
- name: config
226229
configMap:
227230
name: {{ template "airflow_config" . }}
231+
{{- if or .Values.apiServer.apiServerConfig .Values.apiServer.apiServerConfigConfigMapName }}
232+
- name: api-server-config
233+
configMap:
234+
name: {{ template "airflow_api_server_config_configmap_name" . }}
235+
{{- end }}
228236
{{- if (semverCompare "<2.0.0" .Values.airflowVersion) }}
229237
{{- end }}
230238
{{- if .Values.logs.persistence.enabled }}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{{/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/}}
19+
20+
################################
21+
## Airflow ConfigMap
22+
#################################
23+
{{- if semverCompare ">=3.0.0" .Values.airflowVersion }}
24+
{{- if and .Values.apiServer.apiServerConfig (not .Values.apiServer.apiServerConfigConfigMapName) }}
25+
apiVersion: v1
26+
kind: ConfigMap
27+
metadata:
28+
name: {{ template "airflow_api_server_config_configmap_name" . }}
29+
labels:
30+
tier: airflow
31+
component: config
32+
release: {{ .Release.Name }}
33+
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
34+
heritage: {{ .Release.Service }}
35+
{{- with .Values.labels }}
36+
{{- toYaml . | nindent 4 }}
37+
{{- end }}
38+
{{- with .Values.apiServer.configMapAnnotations }}
39+
annotations:
40+
{{- toYaml . | nindent 4 }}
41+
{{- end }}
42+
data:
43+
webserver_config.py: |-
44+
{{- tpl .Values.apiServer.apiServerConfig . | nindent 4 }}
45+
{{- end }}
46+
{{- end }}

chart/values.schema.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5065,6 +5065,30 @@
50655065
],
50665066
"$ref": "#/definitions/io.k8s.api.core.v1.ResourceRequirements"
50675067
},
5068+
"apiServerConfig": {
5069+
"description": "This string (templated) will be mounted into the Airflow API Server as a custom `webserver_config.py`. You can bake a `webserver_config.py` in to your image instead or specify a configmap containing the webserver_config.py.",
5070+
"type": [
5071+
"string",
5072+
"null"
5073+
],
5074+
"x-docsSection": "Common",
5075+
"default": null,
5076+
"examples": [
5077+
"from airflow import configuration as conf\n\n# The SQLAlchemy connection string.\nSQLALCHEMY_DATABASE_URI = conf.get('database', 'SQL_ALCHEMY_CONN')\n\n# Flask-WTF flag for CSRF\nCSRF_ENABLED = True"
5078+
]
5079+
},
5080+
"apiServerConfigConfigMapName": {
5081+
"description": "The configmap name containing the webserver_config.py.",
5082+
"type": [
5083+
"string",
5084+
"null"
5085+
],
5086+
"x-docsSection": "Common",
5087+
"default": null,
5088+
"examples": [
5089+
"my-webserver-configmap"
5090+
]
5091+
},
50685092
"defaultUser": {
50695093
"description": "Optional default Airflow user information",
50705094
"type": "object",

chart/values.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,24 @@ apiServer:
13771377
# cpu: 100m
13781378
# memory: 128Mi
13791379

1380+
# Add custom annotations to the apiServer configmap
1381+
configMapAnnotations: {}
1382+
1383+
# This string (templated) will be mounted into the Airflow API Server
1384+
# as a custom webserver_config.py. You can bake a webserver_config.py in to
1385+
# your image instead or specify a configmap containing the
1386+
# webserver_config.py.
1387+
apiServerConfig: ~
1388+
# apiServerConfig: |
1389+
# from airflow import configuration as conf
1390+
1391+
# # The SQLAlchemy connection string.
1392+
# SQLALCHEMY_DATABASE_URI = conf.get('database', 'SQL_ALCHEMY_CONN')
1393+
1394+
# # Flask-WTF flag for CSRF
1395+
# CSRF_ENABLED = True
1396+
apiServerConfigConfigMapName: ~
1397+
13801398
livenessProbe:
13811399
initialDelaySeconds: 15
13821400
timeoutSeconds: 5
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
import jmespath
20+
from chart_utils.helm_template_generator import render_chart
21+
22+
23+
class TestAPIServerDeployment:
24+
"""Tests API Server deployment."""
25+
26+
def test_airflow_2(self):
27+
"""
28+
API Server only supports Airflow 3.0.0 and later.
29+
"""
30+
docs = render_chart(
31+
values={"airflowVersion": "2.10.5"},
32+
show_only=["templates/api-server/api-server-deployment.yaml"],
33+
)
34+
assert len(docs) == 0
35+
36+
def test_should_not_create_api_server_configmap_when_lower_than_3(self):
37+
"""
38+
API Server configmap is only created for Airflow 3.0.0 and later.
39+
"""
40+
docs = render_chart(
41+
values={"airflowVersion": "2.10.5"},
42+
show_only=["templates/configmaps/api-server-configmap.yaml"],
43+
)
44+
assert len(docs) == 0
45+
46+
def test_should_add_annotations_to_api_server_configmap(self):
47+
docs = render_chart(
48+
values={
49+
"airflowVersion": "3.0.0",
50+
"apiServer": {
51+
"apiServerConfig": "CSRF_ENABLED = True # {{ .Release.Name }}",
52+
"configMapAnnotations": {"test_annotation": "test_annotation_value"},
53+
},
54+
},
55+
show_only=["templates/configmaps/api-server-configmap.yaml"],
56+
)
57+
58+
assert "annotations" in jmespath.search("metadata", docs[0])
59+
assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value"
60+
61+
def test_should_add_volume_and_volume_mount_when_exist_api_server_config(self):
62+
docs = render_chart(
63+
values={"apiServer": {"apiServerConfig": "CSRF_ENABLED = True"}, "airflowVersion": "3.0.0"},
64+
show_only=["templates/api-server/api-server-deployment.yaml"],
65+
)
66+
67+
assert {
68+
"name": "api-server-config",
69+
"configMap": {"name": "release-name-api-server-config"},
70+
} in jmespath.search("spec.template.spec.volumes", docs[0])
71+
72+
assert {
73+
"name": "api-server-config",
74+
"mountPath": "/opt/airflow/webserver_config.py",
75+
"subPath": "webserver_config.py",
76+
"readOnly": True,
77+
} in jmespath.search("spec.template.spec.containers[0].volumeMounts", docs[0])

0 commit comments

Comments
 (0)