worker-deployment.yaml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. ################################
  18. ## Airflow Worker Deployment
  19. #################################
  20. {{- $persistence := .Values.workers.persistence.enabled }}
  21. {{- if or (eq .Values.executor "CeleryExecutor") (eq .Values.executor "CeleryKubernetesExecutor") }}
  22. {{- $nodeSelector := or .Values.workers.nodeSelector .Values.nodeSelector }}
  23. {{- $affinity := or .Values.workers.affinity .Values.affinity }}
  24. {{- $tolerations := or .Values.workers.tolerations .Values.tolerations }}
  25. {{- $topologySpreadConstraints := or .Values.workers.topologySpreadConstraints .Values.topologySpreadConstraints }}
  26. {{- $securityContext := include "airflowSecurityContext" (list . .Values.workers) }}
  27. kind: {{ if $persistence }}StatefulSet{{ else }}Deployment{{ end }}
  28. apiVersion: apps/v1
  29. metadata:
  30. name: {{ .Release.Name }}-worker
  31. labels:
  32. tier: airflow
  33. component: worker
  34. release: {{ .Release.Name }}
  35. chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
  36. heritage: {{ .Release.Service }}
  37. {{- with .Values.labels }}
  38. {{ toYaml . | indent 4 }}
  39. {{- end }}
  40. spec:
  41. {{- if $persistence }}
  42. serviceName: {{ .Release.Name }}-worker
  43. {{- end }}
  44. replicas: {{ .Values.workers.replicas }}
  45. selector:
  46. matchLabels:
  47. tier: airflow
  48. component: worker
  49. release: {{ .Release.Name }}
  50. {{- if and $persistence .Values.workers.updateStrategy }}
  51. updateStrategy:
  52. {{- toYaml .Values.workers.updateStrategy | nindent 4 }}
  53. {{- end }}
  54. {{- if and (not $persistence) (.Values.workers.strategy) }}
  55. strategy:
  56. {{- toYaml .Values.workers.strategy | nindent 4 }}
  57. {{- end }}
  58. template:
  59. metadata:
  60. labels:
  61. tier: airflow
  62. component: worker
  63. release: {{ .Release.Name }}
  64. {{- with .Values.labels }}
  65. {{ toYaml . | indent 8 }}
  66. {{- end }}
  67. annotations:
  68. checksum/metadata-secret: {{ include (print $.Template.BasePath "/secrets/metadata-connection-secret.yaml") . | sha256sum }}
  69. checksum/result-backend-secret: {{ include (print $.Template.BasePath "/secrets/result-backend-connection-secret.yaml") . | sha256sum }}
  70. checksum/pgbouncer-config-secret: {{ include (print $.Template.BasePath "/secrets/pgbouncer-config-secret.yaml") . | sha256sum }}
  71. checksum/webserver-secret-key: {{ include (print $.Template.BasePath "/secrets/webserver-secret-key-secret.yaml") . | sha256sum }}
  72. checksum/kerberos-keytab: {{ include (print $.Template.BasePath "/secrets/kerberos-keytab-secret.yaml") . | sha256sum }}
  73. checksum/airflow-config: {{ include (print $.Template.BasePath "/configmaps/configmap.yaml") . | sha256sum }}
  74. checksum/extra-configmaps: {{ include (print $.Template.BasePath "/configmaps/extra-configmaps.yaml") . | sha256sum }}
  75. checksum/extra-secrets: {{ include (print $.Template.BasePath "/secrets/extra-secrets.yaml") . | sha256sum }}
  76. {{- if .Values.workers.safeToEvict }}
  77. cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
  78. {{- end }}
  79. {{- if .Values.airflowPodAnnotations }}
  80. {{- toYaml .Values.airflowPodAnnotations | nindent 8 }}
  81. {{- end }}
  82. {{- if .Values.workers.podAnnotations }}
  83. {{- toYaml .Values.workers.podAnnotations | nindent 8 }}
  84. {{- end }}
  85. spec:
  86. {{- if .Values.workers.priorityClassName }}
  87. priorityClassName: {{ .Values.workers.priorityClassName }}
  88. {{- end }}
  89. nodeSelector:
  90. {{ toYaml $nodeSelector | indent 8 }}
  91. affinity:
  92. {{- if $affinity }}
  93. {{ toYaml $affinity | indent 8 }}
  94. {{- else }}
  95. podAntiAffinity:
  96. preferredDuringSchedulingIgnoredDuringExecution:
  97. - podAffinityTerm:
  98. labelSelector:
  99. matchLabels:
  100. component: worker
  101. topologyKey: kubernetes.io/hostname
  102. weight: 100
  103. {{- end }}
  104. tolerations:
  105. {{ toYaml $tolerations | indent 8 }}
  106. topologySpreadConstraints:
  107. {{ toYaml $topologySpreadConstraints | indent 8 }}
  108. {{- if .Values.workers.hostAliases }}
  109. hostAliases:
  110. {{ toYaml .Values.workers.hostAliases | indent 8 }}
  111. {{- end }}
  112. terminationGracePeriodSeconds: {{ .Values.workers.terminationGracePeriodSeconds }}
  113. restartPolicy: Always
  114. serviceAccountName: {{ include "worker.serviceAccountName" . }}
  115. securityContext: {{ $securityContext | nindent 8 }}
  116. {{- if or .Values.registry.secretName .Values.registry.connection }}
  117. imagePullSecrets:
  118. - name: {{ template "registry_secret" . }}
  119. {{- end }}
  120. initContainers:
  121. {{- if and $persistence .Values.workers.persistence.fixPermissions }}
  122. - name: volume-permissions
  123. resources:
  124. {{ toYaml .Values.workers.resources | indent 12 }}
  125. image: {{ template "airflow_image" . }}
  126. imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
  127. command:
  128. - chown
  129. - -R
  130. - "{{ include "airflowSecurityContextIds" (list . .Values.workers) }}"
  131. - {{ template "airflow_logs" . }}
  132. securityContext:
  133. runAsUser: 0
  134. volumeMounts:
  135. - name: logs
  136. mountPath: {{ template "airflow_logs" . }}
  137. {{- end }}
  138. - name: wait-for-airflow-migrations
  139. resources:
  140. {{ toYaml .Values.workers.resources | indent 12 }}
  141. image: {{ template "airflow_image_for_migrations" . }}
  142. imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
  143. volumeMounts:
  144. - name: config
  145. mountPath: {{ template "airflow_config_path" . }}
  146. subPath: airflow.cfg
  147. readOnly: true
  148. args:
  149. {{- include "wait-for-migrations-command" . | indent 10 }}
  150. envFrom:
  151. {{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
  152. env:
  153. {{- include "custom_airflow_environment" . | indent 10 }}
  154. {{- include "standard_airflow_environment" . | indent 10 }}
  155. {{- if and (.Values.dags.gitSync.enabled) (not .Values.dags.persistence.enabled) }}
  156. {{- include "git_sync_container" (dict "Values" .Values "is_init" "true") | nindent 8 }}
  157. {{- end }}
  158. {{- if .Values.workers.extraInitContainers }}
  159. {{- toYaml .Values.workers.extraInitContainers | nindent 8 }}
  160. {{- end }}
  161. containers:
  162. - name: worker
  163. image: {{ template "airflow_image" . }}
  164. imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
  165. {{- if .Values.workers.command }}
  166. command: {{ tpl (toYaml .Values.workers.command) . | nindent 12 }}
  167. {{- end }}
  168. {{- if .Values.workers.args }}
  169. args: {{ tpl (toYaml .Values.workers.args) . | nindent 12 }}
  170. {{- end }}
  171. resources:
  172. {{ toYaml .Values.workers.resources | indent 12 }}
  173. ports:
  174. - name: worker-logs
  175. containerPort: {{ .Values.ports.workerLogs }}
  176. volumeMounts:
  177. {{- if .Values.workers.extraVolumeMounts }}
  178. {{ toYaml .Values.workers.extraVolumeMounts | indent 12 }}
  179. {{- end }}
  180. - name: logs
  181. mountPath: {{ template "airflow_logs" . }}
  182. - name: config
  183. mountPath: {{ template "airflow_config_path" . }}
  184. subPath: airflow.cfg
  185. readOnly: true
  186. {{- if .Values.workers.kerberosSidecar.enabled }}
  187. - name: config
  188. mountPath: {{ .Values.kerberos.configPath | quote }}
  189. subPath: krb5.conf
  190. readOnly: true
  191. - name: kerberos-ccache
  192. mountPath: {{ .Values.kerberos.ccacheMountPath | quote }}
  193. readOnly: true
  194. {{- end }}
  195. {{- if .Values.airflowLocalSettings }}
  196. - name: config
  197. mountPath: {{ template "airflow_local_setting_path" . }}
  198. subPath: airflow_local_settings.py
  199. readOnly: true
  200. {{- end }}
  201. {{- if or .Values.dags.persistence.enabled .Values.dags.gitSync.enabled }}
  202. {{- include "airflow_dags_mount" . | nindent 12 }}
  203. {{- end }}
  204. envFrom:
  205. {{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
  206. env:
  207. # Only signal the main process, not the process group, to make Warm Shutdown work properly
  208. - name: DUMB_INIT_SETSID
  209. value: "0"
  210. {{- include "custom_airflow_environment" . | indent 10 }}
  211. {{- include "standard_airflow_environment" . | indent 10 }}
  212. {{- if .Values.workers.kerberosSidecar.enabled }}
  213. - name: KRB5_CONFIG
  214. value: {{ .Values.kerberos.configPath | quote }}
  215. - name: KRB5CCNAME
  216. value: {{ include "kerberos_ccache_path" . | quote }}
  217. {{- end }}
  218. {{- if and (.Values.dags.gitSync.enabled) (not .Values.dags.persistence.enabled) }}
  219. {{- include "git_sync_container" . | indent 8 }}
  220. {{- end }}
  221. {{- if $persistence }}
  222. - name: worker-log-groomer
  223. image: {{ template "airflow_image" . }}
  224. imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
  225. {{- if .Values.workers.logGroomerSidecar.command }}
  226. command: {{ tpl (toYaml .Values.workers.logGroomerSidecar.command) . | nindent 12 }}
  227. {{- end }}
  228. {{- if .Values.workers.logGroomerSidecar.args }}
  229. args: {{ tpl (toYaml .Values.workers.logGroomerSidecar.args) . | nindent 12 }}
  230. {{- end }}
  231. {{ if .Values.workers.logGroomerSidecar.retentionDays }}
  232. env:
  233. - name: AIRFLOW__LOG_RETENTION_DAYS
  234. value: "{{ .Values.workers.logGroomerSidecar.retentionDays }}"
  235. {{- end }}
  236. resources:
  237. {{ toYaml .Values.workers.logGroomerSidecar.resources | indent 12 }}
  238. volumeMounts:
  239. - name: logs
  240. mountPath: {{ template "airflow_logs" . }}
  241. {{- end }}
  242. {{- if .Values.workers.kerberosSidecar.enabled }}
  243. - name: worker-kerberos
  244. image: {{ template "airflow_image" . }}
  245. imagePullPolicy: {{ .Values.images.airflow.pullPolicy }}
  246. args: ["kerberos"]
  247. resources:
  248. {{ toYaml .Values.workers.kerberosSidecar.resources | indent 12 }}
  249. volumeMounts:
  250. - name: logs
  251. mountPath: {{ template "airflow_logs" . }}
  252. - name: config
  253. mountPath: {{ template "airflow_config_path" . }}
  254. subPath: airflow.cfg
  255. readOnly: true
  256. - name: config
  257. mountPath: {{ .Values.kerberos.configPath | quote }}
  258. subPath: krb5.conf
  259. readOnly: true
  260. {{- if .Values.airflowLocalSettings }}
  261. - name: config
  262. mountPath: {{ template "airflow_local_setting_path" . }}
  263. subPath: airflow_local_settings.py
  264. readOnly: true
  265. {{- end }}
  266. - name: kerberos-keytab
  267. subPath: "kerberos.keytab"
  268. mountPath: {{ .Values.kerberos.keytabPath | quote }}
  269. readOnly: true
  270. - name: kerberos-ccache
  271. mountPath: {{ .Values.kerberos.ccacheMountPath | quote }}
  272. readOnly: false
  273. envFrom:
  274. {{- include "custom_airflow_environment_from" . | default "\n []" | indent 10 }}
  275. env:
  276. - name: KRB5_CONFIG
  277. value: {{ .Values.kerberos.configPath | quote }}
  278. - name: KRB5CCNAME
  279. value: {{ include "kerberos_ccache_path" . | quote }}
  280. {{- include "custom_airflow_environment" . | indent 10 }}
  281. {{- include "standard_airflow_environment" . | indent 10 }}
  282. {{- end }}
  283. {{- if .Values.workers.extraContainers }}
  284. {{- toYaml .Values.workers.extraContainers | nindent 8 }}
  285. {{- end }}
  286. volumes:
  287. {{- if .Values.workers.extraVolumes }}
  288. {{ toYaml .Values.workers.extraVolumes | indent 8 }}
  289. {{- end }}
  290. - name: config
  291. configMap:
  292. name: {{ template "airflow_config" . }}
  293. {{- if .Values.kerberos.enabled }}
  294. - name: kerberos-keytab
  295. secret:
  296. secretName: {{ include "kerberos_keytab_secret" . | quote }}
  297. - name: kerberos-ccache
  298. emptyDir: {}
  299. {{- end }}
  300. {{- if .Values.dags.persistence.enabled }}
  301. - name: dags
  302. persistentVolumeClaim:
  303. claimName: {{ template "airflow_dags_volume_claim" . }}
  304. {{- else if .Values.dags.gitSync.enabled }}
  305. - name: dags
  306. emptyDir: {}
  307. {{- if .Values.dags.gitSync.sshKeySecret }}
  308. {{- include "git_sync_ssh_key_volume" . | indent 8 }}
  309. {{- end }}
  310. {{- end }}
  311. {{- if .Values.logs.persistence.enabled }}
  312. - name: logs
  313. persistentVolumeClaim:
  314. claimName: {{ template "airflow_logs_volume_claim" . }}
  315. {{- else if not $persistence }}
  316. - name: logs
  317. emptyDir: {}
  318. {{- else }}
  319. volumeClaimTemplates:
  320. - metadata:
  321. name: logs
  322. {{- if .Values.workers.persistence.annotations }}
  323. annotations:
  324. {{- toYaml .Values.workers.persistence.annotations | nindent 10 }}
  325. {{- end }}
  326. spec:
  327. {{- if .Values.workers.persistence.storageClassName }}
  328. storageClassName: {{ .Values.workers.persistence.storageClassName }}
  329. {{- end }}
  330. accessModes: ["ReadWriteOnce"]
  331. resources:
  332. requests:
  333. storage: {{ .Values.workers.persistence.size }}
  334. {{- end }}
  335. {{- end }}