From 087d4231125f08903884cf426fd1dac7cabef779 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Mon, 3 Jul 2023 18:56:57 +0200 Subject: [PATCH 01/10] Chart: add container lifecycle hooks --- .../pod-template-file.kubernetes-helm-yaml | 2 + chart/templates/_helpers.yaml | 38 ++ .../dag-processor-deployment.yaml | 2 + chart/templates/flower/flower-deployment.yaml | 2 + chart/templates/jobs/create-user-job.yaml | 2 + .../templates/jobs/migrate-database-job.yaml | 2 + .../pgbouncer/pgbouncer-deployment.yaml | 4 + chart/templates/redis/redis-statefulset.yaml | 2 + .../scheduler/scheduler-deployment.yaml | 6 + chart/templates/statsd/statsd-deployment.yaml | 2 + .../triggerer/triggerer-deployment.yaml | 6 + .../webserver/webserver-deployment.yaml | 4 + .../templates/workers/worker-deployment.yaml | 10 + chart/values.schema.json | 396 ++++++++++++++++++ chart/values.yaml | 10 + 15 files changed, 488 insertions(+) diff --git a/chart/files/pod-template-file.kubernetes-helm-yaml b/chart/files/pod-template-file.kubernetes-helm-yaml index 70ac65c8e7380..f7db14dca0e68 100644 --- a/chart/files/pod-template-file.kubernetes-helm-yaml +++ b/chart/files/pod-template-file.kubernetes-helm-yaml @@ -23,6 +23,7 @@ {{- $topologySpreadConstraints := or .Values.workers.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.workers) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.workers) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.workers) }} apiVersion: v1 kind: Pod metadata: @@ -64,6 +65,7 @@ spec: image: {{ template "pod_template_image" . }} imagePullPolicy: {{ .Values.images.pod_template.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 8 }} + {{- $containerLifecycleHooks | nindent 8 }} name: base resources: {{- toYaml .Values.workers.resources | nindent 8 }} volumeMounts: diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml index e210222846b2a..2bcaa6cb1762a 100644 --- a/chart/templates/_helpers.yaml +++ b/chart/templates/_helpers.yaml @@ -954,3 +954,41 @@ capabilities: {{- end }} {{- end }} {{- end }} + +{{/* +Set the default value for container Lifecycle Hooks. +If no value is passed for containerLifecycleHooks or .containerLifecycleHooks, lifecycle will not be set + +The template can be called like so: + include "containerLifecycleHooks" (list . .Values.statsd) + +Where `.` is the global variables scope and `.Values.statsd` the local variables scope for the webserver template. +*/}} +{{- define "containerLifecycleHooks" -}} + {{- $ := index . 0 -}} + {{- with index . 1 }} + {{- if or (.containerLifecycleHooks) ($.Values.containerLifecycleHooks) -}} +lifecycle: + {{- if .containerLifecycleHooks -}} + {{- tpl (toYaml .containerLifecycleHooks) $ | nindent 2 }} + {{- else if $.Values.containerLifecycleHooks -}} + {{- tpl (toYaml $.Values.containerLifecycleHooks) $ | nindent 2 }} + {{- end -}} + {{- end -}} + {{- end -}} +{{- end -}} + +{{/* +Set the default value for container Lifecycle Hooks (redis, statsd). +If no value is passed for .containerLifecycleHooks, lifecycle will not be set + +The template can be called like so: + include "externalContainerSecurityContext" .Values.statsd + +*/}} +{{- define "externalContainerLifecycleHooks" -}} + {{- if .containerLifecycleHooks -}} +lifecycle: + {{- tpl (toYaml .containerLifecycleHooks) $ | nindent 2 }} + {{- end -}} +{{- end -}} diff --git a/chart/templates/dag-processor/dag-processor-deployment.yaml b/chart/templates/dag-processor/dag-processor-deployment.yaml index e748e9dff401d..aaa90a9806abe 100644 --- a/chart/templates/dag-processor/dag-processor-deployment.yaml +++ b/chart/templates/dag-processor/dag-processor-deployment.yaml @@ -29,6 +29,7 @@ {{- $revisionHistoryLimit := or .Values.dagProcessor.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.dagProcessor) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.dagProcessor) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.dagProcessor) }} apiVersion: apps/v1 kind: Deployment metadata: @@ -144,6 +145,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.dagProcessor.command }} command: {{ tpl (toYaml .Values.dagProcessor.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/flower/flower-deployment.yaml b/chart/templates/flower/flower-deployment.yaml index 199d9010d327a..cd5bb9cb57edc 100644 --- a/chart/templates/flower/flower-deployment.yaml +++ b/chart/templates/flower/flower-deployment.yaml @@ -29,6 +29,7 @@ {{- $revisionHistoryLimit := or .Values.flower.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.flower) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.flower) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.flower) }} apiVersion: apps/v1 kind: Deployment metadata: @@ -90,6 +91,7 @@ spec: image: {{ template "flower_image" . }} imagePullPolicy: {{ .Values.images.flower.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.flower.command }} command: {{ tpl (toYaml .Values.flower.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/jobs/create-user-job.yaml b/chart/templates/jobs/create-user-job.yaml index 94d0970ae4f64..8a91acda97a0d 100644 --- a/chart/templates/jobs/create-user-job.yaml +++ b/chart/templates/jobs/create-user-job.yaml @@ -27,6 +27,7 @@ {{- $topologySpreadConstraints := or .Values.createUserJob.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.createUserJob) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.createUserJob) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.createUserJob) }} apiVersion: batch/v1 kind: Job metadata: @@ -88,6 +89,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.createUserJob.command }} command: {{ tpl (toYaml .Values.createUserJob.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/jobs/migrate-database-job.yaml b/chart/templates/jobs/migrate-database-job.yaml index 30dda11b5b8d3..824b9cce6723e 100644 --- a/chart/templates/jobs/migrate-database-job.yaml +++ b/chart/templates/jobs/migrate-database-job.yaml @@ -27,6 +27,7 @@ {{- $topologySpreadConstraints := or .Values.migrateDatabaseJob.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.migrateDatabaseJob) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.migrateDatabaseJob) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.migrateDatabaseJob) }} apiVersion: batch/v1 kind: Job metadata: @@ -88,6 +89,7 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.migrateDatabaseJob.command }} command: {{- tpl (toYaml .Values.migrateDatabaseJob.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/pgbouncer/pgbouncer-deployment.yaml b/chart/templates/pgbouncer/pgbouncer-deployment.yaml index 14665692fcf61..4e68ea2dc5080 100644 --- a/chart/templates/pgbouncer/pgbouncer-deployment.yaml +++ b/chart/templates/pgbouncer/pgbouncer-deployment.yaml @@ -29,6 +29,8 @@ {{- $securityContext := include "localPodSecurityContext" .Values.pgbouncer }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.pgbouncer }} {{- $containerSecurityContextMetricsExporter := include "externalContainerSecurityContext" .Values.pgbouncer.metricsExporterSidecar }} +{{- $containerLifecycleHooks := .Values.pgbouncer.containerLifecycleHooks }} +{{- $containerLifecycleHooksMetricsExporter := .Values.pgbouncer.metricsExporterSidecar.containerLifecycleHooks }} apiVersion: apps/v1 kind: Deployment metadata: @@ -94,6 +96,7 @@ spec: image: {{ template "pgbouncer_image" . }} imagePullPolicy: {{ .Values.images.pgbouncer.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.pgbouncer.command }} command: {{ tpl (toYaml .Values.pgbouncer.command) . | nindent 12 }} {{- end }} @@ -153,6 +156,7 @@ spec: image: {{ template "pgbouncer_exporter_image" . }} imagePullPolicy: {{ .Values.images.pgbouncerExporter.pullPolicy }} securityContext: {{ $containerSecurityContextMetricsExporter | nindent 12 }} + {{- $containerLifecycleHooksMetricsExporter | nindent 12 }} env: - name: DATABASE_URL valueFrom: diff --git a/chart/templates/redis/redis-statefulset.yaml b/chart/templates/redis/redis-statefulset.yaml index ec544cc6f23c8..4c782944ff724 100644 --- a/chart/templates/redis/redis-statefulset.yaml +++ b/chart/templates/redis/redis-statefulset.yaml @@ -27,6 +27,7 @@ {{- $topologySpreadConstraints := or .Values.redis.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "localPodSecurityContext" .Values.redis }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.redis }} +{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" .Values.redis }} apiVersion: apps/v1 kind: StatefulSet metadata: @@ -81,6 +82,7 @@ spec: image: {{ template "redis_image" . }} imagePullPolicy: {{ .Values.images.redis.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} command: ["/bin/sh"] resources: {{- toYaml .Values.redis.resources | nindent 12 }} args: ["-c", "redis-server --requirepass ${REDIS_PASSWORD}"] diff --git a/chart/templates/scheduler/scheduler-deployment.yaml b/chart/templates/scheduler/scheduler-deployment.yaml index 60e4101618b2a..e1c4e2c635789 100644 --- a/chart/templates/scheduler/scheduler-deployment.yaml +++ b/chart/templates/scheduler/scheduler-deployment.yaml @@ -41,6 +41,9 @@ {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.scheduler) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.scheduler.waitForMigrations) }} {{- $containerSecurityContextLogGroomerSidecar := include "containerSecurityContext" (list . .Values.scheduler.logGroomerSidecar) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.scheduler) }} +{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.scheduler.waitForMigrations) }} +{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.scheduler.logGroomerSidecar) }} apiVersion: apps/v1 kind: {{ if $stateful }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -140,6 +143,7 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} + {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -172,6 +176,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.scheduler.command }} command: {{ tpl (toYaml .Values.scheduler.command) . | nindent 12 }} {{- end }} @@ -244,6 +249,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} + {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} {{- if .Values.scheduler.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.scheduler.logGroomerSidecar.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/statsd/statsd-deployment.yaml b/chart/templates/statsd/statsd-deployment.yaml index 583151331aaf1..de69a98e8f368 100644 --- a/chart/templates/statsd/statsd-deployment.yaml +++ b/chart/templates/statsd/statsd-deployment.yaml @@ -28,6 +28,7 @@ {{- $revisionHistoryLimit := or .Values.statsd.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "localPodSecurityContext" .Values.statsd }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.statsd }} +{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" .Values.statsd }} apiVersion: apps/v1 kind: Deployment metadata: @@ -90,6 +91,7 @@ spec: image: {{ template "statsd_image" . }} imagePullPolicy: {{ .Values.images.statsd.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.statsd.args }} args: {{ tpl (toYaml .Values.statsd.args) . | nindent 12 }} {{- else}} diff --git a/chart/templates/triggerer/triggerer-deployment.yaml b/chart/templates/triggerer/triggerer-deployment.yaml index 3d327e944ac44..085e6adbfb14d 100644 --- a/chart/templates/triggerer/triggerer-deployment.yaml +++ b/chart/templates/triggerer/triggerer-deployment.yaml @@ -33,6 +33,9 @@ {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.triggerer) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.triggerer.waitForMigrations) }} {{- $containerSecurityContextLogGroomer := include "containerSecurityContext" (list . .Values.triggerer.logGroomerSidecar) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.triggerer) }} +{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.triggerer.waitForMigrations) }} +{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.triggerer.logGroomerSidecar) }} apiVersion: apps/v1 kind: {{ if $persistence }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -128,6 +131,7 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} + {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -159,6 +163,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.triggerer.command }} command: {{ tpl (toYaml .Values.triggerer.command) . | nindent 12 }} {{- end }} @@ -214,6 +219,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomer | nindent 12 }} + {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} {{- if .Values.triggerer.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.triggerer.logGroomerSidecar.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/webserver/webserver-deployment.yaml b/chart/templates/webserver/webserver-deployment.yaml index 7f326bab2264d..488dfbf67c50f 100644 --- a/chart/templates/webserver/webserver-deployment.yaml +++ b/chart/templates/webserver/webserver-deployment.yaml @@ -28,6 +28,8 @@ {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.webserver) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.webserver) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.webserver.waitForMigrations) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.webserver) }} +{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.webserver.waitForMigrations) }} apiVersion: apps/v1 kind: Deployment metadata: @@ -134,6 +136,7 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} + {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -165,6 +168,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ or $containerSecurityContext .Values.webserver.securityContexts.container .Values.securityContexts.container | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.webserver.command }} command: {{ tpl (toYaml .Values.webserver.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/workers/worker-deployment.yaml b/chart/templates/workers/worker-deployment.yaml index 235f3f783f6fd..a63985b0798e2 100644 --- a/chart/templates/workers/worker-deployment.yaml +++ b/chart/templates/workers/worker-deployment.yaml @@ -34,6 +34,11 @@ {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.workers.waitForMigrations) }} {{- $containerSecurityContextLogGroomerSidecar := include "containerSecurityContext" (list . .Values.workers.logGroomerSidecar) }} {{- $containerSecurityContextKerberosSidecar := include "containerSecurityContext" (list . .Values.workers.kerberosSidecar) }} +{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.workers) }} +{{- $containerLifecycleHooksPersistence := include "containerLifecycleHooks" (list . .Values.workers.persistence) }} +{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.workers.waitForMigrations) }} +{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.workers.logGroomerSidecar) }} +{{- $containerLifecycleHooksKerberosSidecar := include "containerLifecycleHooks" (list . .Values.workers.kerberkerberosSidecar) }} apiVersion: apps/v1 kind: {{ if $persistence }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -144,6 +149,7 @@ spec: - "{{ include "airflowPodSecurityContextsIds" (list . .Values.workers) }}" - {{ template "airflow_logs" . }} securityContext: {{ $containerSecurityContextPersistence | nindent 12 }} + {{- $containerLifecycleHooksPersistence | nindent 12 }} volumeMounts: - name: logs mountPath: {{ template "airflow_logs" . }} @@ -154,6 +160,7 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} + {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -185,6 +192,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} + {{- $containerLifecycleHooks | nindent 12 }} {{- if .Values.workers.command }} command: {{ tpl (toYaml .Values.workers.command) . | nindent 12 }} {{- end }} @@ -262,6 +270,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} + {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} {{- if .Values.workers.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.workers.logGroomerSidecar.command) . | nindent 12 }} {{- end }} @@ -292,6 +301,7 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextKerberosSidecar | nindent 12 }} + {{- $containerLifecycleHooksKerberosSidecar | nindent 12 }} args: ["kerberos"] resources: {{- toYaml .Values.workers.kerberosSidecar.resources | nindent 12 }} volumeMounts: diff --git a/chart/values.schema.json b/chart/values.schema.json index f3e62911ea65f..212a42c5c34a3 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -136,6 +136,23 @@ } } }, + "containerLifecycleHooks": { + "description": "Default Container Lifecycle Hooks definition. The values in this parameter will be used when `containerLifecycleHooks` is not defined for specific containers.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "nodeSelector": { "description": "Select certain nodes for all pods.", "type": "object", @@ -1523,6 +1540,23 @@ "type": "string" } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the persistence. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the persistence. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -1577,6 +1611,23 @@ ], "$ref": "#/definitions/io.k8s.api.core.v1.ResourceRequirements" }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the kerberos sidecar. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the kerberos sidecar. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -1774,6 +1825,24 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the worker. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + + ] + }, "securityContexts": { "description": "Security context definition for the workers. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -1843,6 +1912,24 @@ "additionalProperties": false } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the worker. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + + ] + }, "securityContexts": { "description": "Security context definition for the wait for migrations. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -2254,6 +2341,24 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the scheduler. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + + ] + }, "securityContexts": { "description": "Security context definition for the scheduler. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -2323,6 +2428,23 @@ "additionalProperties": false } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the wait for migrations. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the wait for migrations. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -2692,6 +2814,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the triggerer. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the triggerer. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -2765,6 +2904,23 @@ "additionalProperties": false } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the wait for migrations. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the wait for migrations. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -3080,6 +3236,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the dag processor. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the dag processor. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -3153,6 +3326,23 @@ "additionalProperties": false } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the wait for migrations. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the wait for migrations. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -3373,6 +3563,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the create user job. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the create user job. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -3642,6 +3849,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the migrate database job. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the migrate database job. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -4000,6 +4224,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the webserver. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the webserver. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -4342,6 +4583,24 @@ "additionalProperties": false } }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the wait for migrations. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + + ] + }, "securityContexts": { "description": "Security context definition for the wait for migrations. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -4740,6 +4999,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the network policy. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the network policy. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -4963,6 +5239,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the statsd. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the statsd.", "type": "object", @@ -5207,6 +5500,23 @@ ], "$ref": "#/definitions/io.k8s.api.core.v1.ResourceRequirements" }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the PgBouncer. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the PgBouncer.", "type": "object", @@ -5485,6 +5795,23 @@ ], "default": "disable" }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the metrics exporter sidecar. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the metrics exporter sidecar. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -5722,6 +6049,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the redis. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the redis.", "type": "object", @@ -6129,6 +6473,24 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the cleanup. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + + ] + }, "securityContexts": { "description": "Security context definition for the cleanup. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -6430,6 +6792,23 @@ } ] }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the git sync sidecar. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the git sync sidecar. If not set, the values from global `securityContexts` will be used.", "type": "object", @@ -9377,6 +9756,23 @@ ], "$ref": "#/definitions/io.k8s.api.core.v1.ResourceRequirements" }, + "containerLifecycleHooks": { + "description": "Container Lifecycle Hooks definition for the log groomer sidecar. If not set, the values from global `containerLifecycleHooks` will be used.", + "type": "object", + "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", + "default": "{}", + "x-docsSection": "Kubernetes", + "examples": [ + { + "postStart": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + }, + "preStop": { + "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + } + } + ] + }, "securityContexts": { "description": "Security context definition for the log groomer sidecar. If not set, the values from global `securityContexts` will be used.", "type": "object", diff --git a/chart/values.yaml b/chart/values.yaml index de17c8d104fff..fad831893465d 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -43,6 +43,11 @@ securityContexts: pod: {} containers: {} +# Default container level lifecycle hooks for every container +# See: +# https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ +containerLifecycleHooks: {} + # Airflow home directory # Used for mount paths airflowHome: /opt/airflow @@ -513,6 +518,11 @@ workers: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Create ServiceAccount serviceAccount: # default value is true From d6107496c8943fddd482470cd306a74d0a7072e4 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 4 Jul 2023 10:08:54 +0200 Subject: [PATCH 02/10] fix containerLifecycleHooks indentation --- .../pod-template-file.kubernetes-helm-yaml | 4 +- chart/templates/_helpers.yaml | 22 ++--- .../dag-processor-deployment.yaml | 4 +- chart/templates/flower/flower-deployment.yaml | 4 +- chart/templates/jobs/create-user-job.yaml | 4 +- .../templates/jobs/migrate-database-job.yaml | 4 +- .../pgbouncer/pgbouncer-deployment.yaml | 8 +- chart/templates/redis/redis-statefulset.yaml | 6 +- .../scheduler/scheduler-deployment.yaml | 12 ++- chart/templates/statsd/statsd-deployment.yaml | 6 +- .../triggerer/triggerer-deployment.yaml | 12 ++- .../webserver/webserver-deployment.yaml | 8 +- .../templates/workers/worker-deployment.yaml | 20 +++- chart/values.yaml | 96 +++++++++++++++++++ 14 files changed, 175 insertions(+), 35 deletions(-) diff --git a/chart/files/pod-template-file.kubernetes-helm-yaml b/chart/files/pod-template-file.kubernetes-helm-yaml index f7db14dca0e68..f073d371bae3e 100644 --- a/chart/files/pod-template-file.kubernetes-helm-yaml +++ b/chart/files/pod-template-file.kubernetes-helm-yaml @@ -65,7 +65,9 @@ spec: image: {{ template "pod_template_image" . }} imagePullPolicy: {{ .Values.images.pod_template.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 8 }} - {{- $containerLifecycleHooks | nindent 8 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 8 }} + {{- end }} name: base resources: {{- toYaml .Values.workers.resources | nindent 8 }} volumeMounts: diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml index 2bcaa6cb1762a..57d2431523e63 100644 --- a/chart/templates/_helpers.yaml +++ b/chart/templates/_helpers.yaml @@ -967,13 +967,11 @@ Where `.` is the global variables scope and `.Values.statsd` the local variables {{- define "containerLifecycleHooks" -}} {{- $ := index . 0 -}} {{- with index . 1 }} - {{- if or (.containerLifecycleHooks) ($.Values.containerLifecycleHooks) -}} -lifecycle: - {{- if .containerLifecycleHooks -}} - {{- tpl (toYaml .containerLifecycleHooks) $ | nindent 2 }} - {{- else if $.Values.containerLifecycleHooks -}} - {{- tpl (toYaml $.Values.containerLifecycleHooks) $ | nindent 2 }} - {{- end -}} + {{- if .containerLifecycleHooks -}} + {{- tpl (toYaml .containerLifecycleHooks) $ }} + {{- else if $.Values.containerLifecycleHooks -}} + {{- tpl (toYaml $.Values.containerLifecycleHooks) $ }} + {{- else -}} {{- end -}} {{- end -}} {{- end -}} @@ -983,12 +981,14 @@ Set the default value for container Lifecycle Hooks (redis, statsd). If no value is passed for .containerLifecycleHooks, lifecycle will not be set The template can be called like so: - include "externalContainerSecurityContext" .Values.statsd + include "externalContainerSecurityContext" (list . .Values.statsd) */}} {{- define "externalContainerLifecycleHooks" -}} - {{- if .containerLifecycleHooks -}} -lifecycle: - {{- tpl (toYaml .containerLifecycleHooks) $ | nindent 2 }} + {{- $ := index . 0 -}} + {{- with index . 1 }} + {{- if .containerLifecycleHooks -}} + {{- tpl (toYaml .containerLifecycleHooks) $ }} + {{- end -}} {{- end -}} {{- end -}} diff --git a/chart/templates/dag-processor/dag-processor-deployment.yaml b/chart/templates/dag-processor/dag-processor-deployment.yaml index aaa90a9806abe..b26e084b485cf 100644 --- a/chart/templates/dag-processor/dag-processor-deployment.yaml +++ b/chart/templates/dag-processor/dag-processor-deployment.yaml @@ -145,7 +145,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.dagProcessor.command }} command: {{ tpl (toYaml .Values.dagProcessor.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/flower/flower-deployment.yaml b/chart/templates/flower/flower-deployment.yaml index cd5bb9cb57edc..9f11746d8f7e3 100644 --- a/chart/templates/flower/flower-deployment.yaml +++ b/chart/templates/flower/flower-deployment.yaml @@ -91,7 +91,9 @@ spec: image: {{ template "flower_image" . }} imagePullPolicy: {{ .Values.images.flower.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.flower.command }} command: {{ tpl (toYaml .Values.flower.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/jobs/create-user-job.yaml b/chart/templates/jobs/create-user-job.yaml index 8a91acda97a0d..64eb24361969c 100644 --- a/chart/templates/jobs/create-user-job.yaml +++ b/chart/templates/jobs/create-user-job.yaml @@ -89,7 +89,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.createUserJob.command }} command: {{ tpl (toYaml .Values.createUserJob.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/jobs/migrate-database-job.yaml b/chart/templates/jobs/migrate-database-job.yaml index 824b9cce6723e..d45a0a303d8f7 100644 --- a/chart/templates/jobs/migrate-database-job.yaml +++ b/chart/templates/jobs/migrate-database-job.yaml @@ -89,7 +89,9 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.migrateDatabaseJob.command }} command: {{- tpl (toYaml .Values.migrateDatabaseJob.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/pgbouncer/pgbouncer-deployment.yaml b/chart/templates/pgbouncer/pgbouncer-deployment.yaml index 4e68ea2dc5080..c25398c20dbf7 100644 --- a/chart/templates/pgbouncer/pgbouncer-deployment.yaml +++ b/chart/templates/pgbouncer/pgbouncer-deployment.yaml @@ -96,7 +96,9 @@ spec: image: {{ template "pgbouncer_image" . }} imagePullPolicy: {{ .Values.images.pgbouncer.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.pgbouncer.command }} command: {{ tpl (toYaml .Values.pgbouncer.command) . | nindent 12 }} {{- end }} @@ -156,7 +158,9 @@ spec: image: {{ template "pgbouncer_exporter_image" . }} imagePullPolicy: {{ .Values.images.pgbouncerExporter.pullPolicy }} securityContext: {{ $containerSecurityContextMetricsExporter | nindent 12 }} - {{- $containerLifecycleHooksMetricsExporter | nindent 12 }} + {{- if $containerLifecycleHooksMetricsExporter }} + lifecycle: {{- $containerSecurityContextMetricsExporter | nindent 12 }} + {{- end }} env: - name: DATABASE_URL valueFrom: diff --git a/chart/templates/redis/redis-statefulset.yaml b/chart/templates/redis/redis-statefulset.yaml index 4c782944ff724..7158769393345 100644 --- a/chart/templates/redis/redis-statefulset.yaml +++ b/chart/templates/redis/redis-statefulset.yaml @@ -27,7 +27,7 @@ {{- $topologySpreadConstraints := or .Values.redis.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "localPodSecurityContext" .Values.redis }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.redis }} -{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" .Values.redis }} +{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" (list . .Values.redis) }} apiVersion: apps/v1 kind: StatefulSet metadata: @@ -82,7 +82,9 @@ spec: image: {{ template "redis_image" . }} imagePullPolicy: {{ .Values.images.redis.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} command: ["/bin/sh"] resources: {{- toYaml .Values.redis.resources | nindent 12 }} args: ["-c", "redis-server --requirepass ${REDIS_PASSWORD}"] diff --git a/chart/templates/scheduler/scheduler-deployment.yaml b/chart/templates/scheduler/scheduler-deployment.yaml index e1c4e2c635789..c4dc56b6049f9 100644 --- a/chart/templates/scheduler/scheduler-deployment.yaml +++ b/chart/templates/scheduler/scheduler-deployment.yaml @@ -143,7 +143,9 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} - {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- if $containerLifecycleHooksWaitForMigrations }} + lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -176,7 +178,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.scheduler.command }} command: {{ tpl (toYaml .Values.scheduler.command) . | nindent 12 }} {{- end }} @@ -249,7 +253,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} - {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- if $containerLifecycleHooksLogGroomerSidecar }} + lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- end }} {{- if .Values.scheduler.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.scheduler.logGroomerSidecar.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/statsd/statsd-deployment.yaml b/chart/templates/statsd/statsd-deployment.yaml index de69a98e8f368..ee838ef57b67c 100644 --- a/chart/templates/statsd/statsd-deployment.yaml +++ b/chart/templates/statsd/statsd-deployment.yaml @@ -28,7 +28,7 @@ {{- $revisionHistoryLimit := or .Values.statsd.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "localPodSecurityContext" .Values.statsd }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.statsd }} -{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" .Values.statsd }} +{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" (list . .Values.statsd) }} apiVersion: apps/v1 kind: Deployment metadata: @@ -91,7 +91,9 @@ spec: image: {{ template "statsd_image" . }} imagePullPolicy: {{ .Values.images.statsd.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.statsd.args }} args: {{ tpl (toYaml .Values.statsd.args) . | nindent 12 }} {{- else}} diff --git a/chart/templates/triggerer/triggerer-deployment.yaml b/chart/templates/triggerer/triggerer-deployment.yaml index 085e6adbfb14d..84a522e649e93 100644 --- a/chart/templates/triggerer/triggerer-deployment.yaml +++ b/chart/templates/triggerer/triggerer-deployment.yaml @@ -131,7 +131,9 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} - {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- if $containerLifecycleHooksWaitForMigrations }} + lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -163,7 +165,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.triggerer.command }} command: {{ tpl (toYaml .Values.triggerer.command) . | nindent 12 }} {{- end }} @@ -219,7 +223,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomer | nindent 12 }} - {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- if $containerLifecycleHooksLogGroomerSidecar }} + lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- end }} {{- if .Values.triggerer.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.triggerer.logGroomerSidecar.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/webserver/webserver-deployment.yaml b/chart/templates/webserver/webserver-deployment.yaml index 488dfbf67c50f..c51c9a81a6ac2 100644 --- a/chart/templates/webserver/webserver-deployment.yaml +++ b/chart/templates/webserver/webserver-deployment.yaml @@ -136,7 +136,9 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} - {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- if $containerLifecycleHooksWaitForMigrations }} + lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -168,7 +170,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ or $containerSecurityContext .Values.webserver.securityContexts.container .Values.securityContexts.container | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.webserver.command }} command: {{ tpl (toYaml .Values.webserver.command) . | nindent 12 }} {{- end }} diff --git a/chart/templates/workers/worker-deployment.yaml b/chart/templates/workers/worker-deployment.yaml index a63985b0798e2..1f72e3f7235c6 100644 --- a/chart/templates/workers/worker-deployment.yaml +++ b/chart/templates/workers/worker-deployment.yaml @@ -149,7 +149,9 @@ spec: - "{{ include "airflowPodSecurityContextsIds" (list . .Values.workers) }}" - {{ template "airflow_logs" . }} securityContext: {{ $containerSecurityContextPersistence | nindent 12 }} - {{- $containerLifecycleHooksPersistence | nindent 12 }} + {{- if $containerLifecycleHooksPersistence }} + lifecycle: {{- $containerLifecycleHooksPersistence | nindent 12 }} + {{- end }} volumeMounts: - name: logs mountPath: {{ template "airflow_logs" . }} @@ -160,7 +162,9 @@ spec: image: {{ template "airflow_image_for_migrations" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} - {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- if $containerLifecycleHooksWaitForMigrations }} + lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} {{- if .Values.volumeMounts }} @@ -192,7 +196,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- $containerLifecycleHooks | nindent 12 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + {{- end }} {{- if .Values.workers.command }} command: {{ tpl (toYaml .Values.workers.command) . | nindent 12 }} {{- end }} @@ -270,7 +276,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} - {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- if $containerLifecycleHooksLogGroomerSidecar }} + lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + {{- end }} {{- if .Values.workers.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.workers.logGroomerSidecar.command) . | nindent 12 }} {{- end }} @@ -301,7 +309,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextKerberosSidecar | nindent 12 }} - {{- $containerLifecycleHooksKerberosSidecar | nindent 12 }} + {{- if $containerLifecycleHooksKerberosSidecar }} + lifecycle: {{- $containerLifecycleHooksKerberosSidecar | nindent 12 }} + {{- end }} args: ["kerberos"] resources: {{- toYaml .Values.workers.kerberosSidecar.resources | nindent 12 }} volumeMounts: diff --git a/chart/values.yaml b/chart/values.yaml index fad831893465d..51b6a2cb38811 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -595,6 +595,10 @@ workers: # Detailed default security context for persistence for container level securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} kerberosSidecar: # Enable kerberos sidecar @@ -609,6 +613,10 @@ workers: # Detailed default security context for kerberosSidecar for container level securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} resources: {} # limits: @@ -709,6 +717,10 @@ workers: # Detailed default security context for waitForMigrations for container level securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} env: [] @@ -769,6 +781,11 @@ scheduler: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Create ServiceAccount serviceAccount: # default value is true @@ -869,6 +886,10 @@ scheduler: # Detailed default security context for logGroomerSidecar for container level securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} waitForMigrations: # Whether to create init container to wait for db migrations @@ -877,6 +898,10 @@ scheduler: # Detailed default security context for waitForMigrations for container level securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} env: [] @@ -927,6 +952,11 @@ createUserJob: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Create ServiceAccount serviceAccount: # default value is true @@ -1010,6 +1040,11 @@ migrateDatabaseJob: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Create ServiceAccount serviceAccount: # default value is true @@ -1139,6 +1174,11 @@ webserver: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Additional network policies as needed (Deprecated - renamed to `webserver.networkPolicy.ingress.from`) extraNetworkPolicies: [] networkPolicy: @@ -1264,6 +1304,11 @@ webserver: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + env: [] # Airflow Triggerer Config @@ -1320,6 +1365,12 @@ triggerer: securityContexts: pod: {} container: {} + + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + persistence: # Enable persistent volumes enabled: true @@ -1414,6 +1465,11 @@ triggerer: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + waitForMigrations: # Whether to create init container to wait for db migrations enabled: true @@ -1422,6 +1478,11 @@ triggerer: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + env: [] # Airflow Dag Processor Config @@ -1477,6 +1538,11 @@ dagProcessor: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + resources: {} # limits: # cpu: 100m @@ -1606,6 +1672,11 @@ flower: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Create ServiceAccount serviceAccount: # default value is true @@ -1891,6 +1962,11 @@ pgbouncer: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + metricsExporterSidecar: resources: {} # limits: @@ -1905,6 +1981,11 @@ pgbouncer: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + livenessProbe: initialDelaySeconds: 10 periodSeconds: 10 @@ -1982,6 +2063,11 @@ redis: pod: {} container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + podAnnotations: {} # Auth secret for a private registry # This is used if pulling airflow images from a private registry @@ -2090,6 +2176,11 @@ cleanup: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Specify history limit # When set, overwrite the default k8s number of successful and failed CronJob executions that are saved. failedJobsHistoryLimit: ~ @@ -2295,6 +2386,11 @@ dags: securityContexts: container: {} + # container level lifecycle hooks + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Mount additional volumes into git-sync. It can be templated like in the following example: # extraVolumeMounts: # - name: my-templated-extra-volume From 8078cb5213381c39956c6ac2ff7a84cf9f52f8f3 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 4 Jul 2023 15:12:48 +0200 Subject: [PATCH 03/10] add minimal set of tests for lifecycle --- helm_tests/airflow_aux/test_airflow_common.py | 36 +++++++++++++++++++ .../airflow_aux/test_pod_template_file.py | 19 ++++++++++ helm_tests/airflow_core/test_scheduler.py | 22 ++++++++++++ helm_tests/airflow_core/test_triggerer.py | 22 ++++++++++++ helm_tests/airflow_core/test_worker.py | 22 ++++++++++++ helm_tests/other/test_pgbouncer.py | 22 ++++++++++++ helm_tests/other/test_redis.py | 22 ++++++++++++ helm_tests/other/test_statsd.py | 22 ++++++++++++ helm_tests/security/test_security_context.py | 36 +++++++++++++++++++ helm_tests/webserver/test_webserver.py | 20 +++++++++++ 10 files changed, 243 insertions(+) diff --git a/helm_tests/airflow_aux/test_airflow_common.py b/helm_tests/airflow_aux/test_airflow_common.py index 72b39e031c9c3..bca5397e0bf95 100644 --- a/helm_tests/airflow_aux/test_airflow_common.py +++ b/helm_tests/airflow_aux/test_airflow_common.py @@ -406,3 +406,39 @@ def test_priority_class_name(self): priority = doc["spec"]["template"]["spec"]["priorityClassName"] assert priority == f"low-priority-{component}" + + # Test containerLifecycleHooks for main containers + def test_global_container_lifecycle_webhooks(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + }, + }, + show_only=[ + "templates/flower/flower-deployment.yaml", + "templates/scheduler/scheduler-deployment.yaml", + "templates/webserver/webserver-deployment.yaml", + "templates/workers/worker-deployment.yaml", + "templates/jobs/create-user-job.yaml", + "templates/jobs/migrate-database-job.yaml", + "templates/triggerer/triggerer-deployment.yaml", + "templates/statsd/statsd-deployment.yaml", + "templates/redis/redis-statefulset.yaml", + ], + ) + + for index in range(len(docs) - 2): + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[index] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[index] + ) + + # Global security context is not propagated to redis and statsd, so we test default value + for index in range(len(docs) - 2, len(docs)): + assert jmespath.search("spec.template.spec.containers[0].lifecycle", docs[index]) is None diff --git a/helm_tests/airflow_aux/test_pod_template_file.py b/helm_tests/airflow_aux/test_pod_template_file.py index 4d67b91b19656..48ce2656336d9 100644 --- a/helm_tests/airflow_aux/test_pod_template_file.py +++ b/helm_tests/airflow_aux/test_pod_template_file.py @@ -750,3 +750,22 @@ def test_workers_priority_class_name(self): ) assert "test-priority" == jmespath.search("spec.priorityClassName", docs[0]) + + def test_workers_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "workers": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/pod-template-file.yaml"], + chart_dir=self.temp_chart_dir, + ) + + assert post_start_value == jmespath.search("spec.containers[0].lifecycle.postStart", docs[0]) + assert pre_stop_value == jmespath.search("spec.containers[0].lifecycle.preStop", docs[0]) diff --git a/helm_tests/airflow_core/test_scheduler.py b/helm_tests/airflow_core/test_scheduler.py index 9bf2f0071df12..833e90f789a3b 100644 --- a/helm_tests/airflow_core/test_scheduler.py +++ b/helm_tests/airflow_core/test_scheduler.py @@ -464,6 +464,28 @@ def test_scheduler_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) + def test_scheduler_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "scheduler": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/scheduler/scheduler-deployment.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + def test_scheduler_security_context_legacy(self): docs = render_chart( values={ diff --git a/helm_tests/airflow_core/test_triggerer.py b/helm_tests/airflow_core/test_triggerer.py index d19d83f9175d7..0c4fcbfe036f1 100644 --- a/helm_tests/airflow_core/test_triggerer.py +++ b/helm_tests/airflow_core/test_triggerer.py @@ -574,6 +574,28 @@ def test_should_add_component_specific_annotations(self): assert "annotations" in jmespath.search("metadata", docs[0]) assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value" + def test_triggerer_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "triggerer": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/triggerer/triggerer-deployment.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + class TestTriggererServiceAccount: """Tests triggerer service account.""" diff --git a/helm_tests/airflow_core/test_worker.py b/helm_tests/airflow_core/test_worker.py index 81432ee7ac6f3..65f56bb5c4732 100644 --- a/helm_tests/airflow_core/test_worker.py +++ b/helm_tests/airflow_core/test_worker.py @@ -633,6 +633,28 @@ def test_should_add_component_specific_annotations(self): assert "annotations" in jmespath.search("metadata", docs[0]) assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value" + def test_workers_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "workers": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/workers/worker-deployment.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + class TestWorkerLogGroomer(LogGroomerTestBase): """Worker groomer.""" diff --git a/helm_tests/other/test_pgbouncer.py b/helm_tests/other/test_pgbouncer.py index 454912b4fb6b3..a966b6853b90e 100644 --- a/helm_tests/other/test_pgbouncer.py +++ b/helm_tests/other/test_pgbouncer.py @@ -525,6 +525,28 @@ def test_extra_ini_configs(self): assert "server_round_robin = 1" in ini assert "stats_period = 30" in ini + def test_pgbouncer_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "pgbouncer": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + class TestPgbouncerExporter: """Tests PgBouncer exporter.""" diff --git a/helm_tests/other/test_redis.py b/helm_tests/other/test_redis.py index c99503a4b7711..843ea5f41b7ce 100644 --- a/helm_tests/other/test_redis.py +++ b/helm_tests/other/test_redis.py @@ -306,6 +306,28 @@ def test_redis_resources_are_configurable(self): ) assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0]) + def test_redis_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "redis": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/redis/redis-statefulset.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + def test_redis_resources_are_not_added_by_default(self): docs = render_chart( show_only=["templates/redis/redis-statefulset.yaml"], diff --git a/helm_tests/other/test_statsd.py b/helm_tests/other/test_statsd.py index bc5b5b0785984..6cd6756ac49f3 100644 --- a/helm_tests/other/test_statsd.py +++ b/helm_tests/other/test_statsd.py @@ -198,6 +198,28 @@ def test_statsd_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) + def test_statsd_container_lifecycle_webhooks_are_configurable(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "statsd": { + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + } + }, + }, + show_only=["templates/statsd/statsd-deployment.yaml"], + ) + + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + def test_statsd_security_context_legacy(self): docs = render_chart( values={ diff --git a/helm_tests/security/test_security_context.py b/helm_tests/security/test_security_context.py index 74da2cc95d3cf..69b7170a2009a 100644 --- a/helm_tests/security/test_security_context.py +++ b/helm_tests/security/test_security_context.py @@ -216,6 +216,42 @@ def test_gitsync_sidecar_and_init_container(self): doc, ) + # Test containerLifecycleHooks for main containers + def test_global_container_lifecycle_webhooks(self): + post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} + pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + docs = render_chart( + values={ + "containerLifecycleHooks": { + "postStart": post_start_value, + "preStop": pre_stop_value, + }, + }, + show_only=[ + "templates/flower/flower-deployment.yaml", + "templates/scheduler/scheduler-deployment.yaml", + "templates/webserver/webserver-deployment.yaml", + "templates/workers/worker-deployment.yaml", + "templates/jobs/create-user-job.yaml", + "templates/jobs/migrate-database-job.yaml", + "templates/triggerer/triggerer-deployment.yaml", + "templates/statsd/statsd-deployment.yaml", + "templates/redis/redis-statefulset.yaml", + ], + ) + + for index in range(len(docs) - 2): + assert post_start_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[index] + ) + assert pre_stop_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[index] + ) + + # Global security context is not propagated to redis and statsd, so we test default value + for index in range(len(docs) - 2, len(docs)): + assert None == jmespath.search("spec.template.spec.containers[0].lifecycle", docs[index]) + # Test securityContexts for main containers def test_global_security_context(self): ctx_value_pod = {"runAsUser": 7000} diff --git a/helm_tests/webserver/test_webserver.py b/helm_tests/webserver/test_webserver.py index cbb9b0a0fa3a9..63aced3e8b993 100644 --- a/helm_tests/webserver/test_webserver.py +++ b/helm_tests/webserver/test_webserver.py @@ -569,6 +569,26 @@ def test_webserver_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) + def test_webserver_container_lifecycle_webhooks_are_configurable(self): + docs = render_chart( + values={ + "webserver": { + "containerLifecycleHooks": { + "postStart": {"exec": {"command": ["bash", "-c", "echo postStart"]}}, + "preStop": {"exec": {"command": ["bash", "-c", "echo preStop"]}}, + } + }, + }, + show_only=["templates/webserver/webserver-deployment.yaml"], + ) + + assert {"exec": {"command": ["bash", "-c", "echo postStart"]}} == jmespath.search( + "spec.template.spec.containers[0].lifecycle.postStart", docs[0] + ) + assert {"exec": {"command": ["bash", "-c", "echo preStop"]}} == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[0] + ) + def test_webserver_security_context_legacy(self): docs = render_chart( values={ From 348d8c89167d09d6ffffbd0d2f0badd491e4c964 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 4 Jul 2023 16:22:56 +0200 Subject: [PATCH 04/10] add lifecycle to pgbouncer --- chart/values.yaml | 5 +++++ helm_tests/other/test_pgbouncer.py | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/chart/values.yaml b/chart/values.yaml index 51b6a2cb38811..7e7fb988e87a2 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1861,6 +1861,11 @@ pgbouncer: # Additional network policies as needed extraNetworkPolicies: [] + # Default container level lifecycle hooks for every container + # See: + # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ + containerLifecycleHooks: {} + # Pool sizes metadataPoolSize: 10 resultBackendPoolSize: 5 diff --git a/helm_tests/other/test_pgbouncer.py b/helm_tests/other/test_pgbouncer.py index a966b6853b90e..b18d59f7f59b1 100644 --- a/helm_tests/other/test_pgbouncer.py +++ b/helm_tests/other/test_pgbouncer.py @@ -531,10 +531,11 @@ def test_pgbouncer_container_lifecycle_webhooks_are_configurable(self): docs = render_chart( values={ "pgbouncer": { + "enabled": True, "containerLifecycleHooks": { "postStart": post_start_value, "preStop": pre_stop_value, - } + }, }, }, show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], From e320b55cc2bbb474d635036bdaec4e7f29f9ddc0 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 4 Jul 2023 16:50:57 +0200 Subject: [PATCH 05/10] remove lifecycle from pgbouncer --- .../pgbouncer/pgbouncer-deployment.yaml | 6 ----- chart/values.yaml | 5 ---- helm_tests/other/test_pgbouncer.py | 23 ------------------- 3 files changed, 34 deletions(-) diff --git a/chart/templates/pgbouncer/pgbouncer-deployment.yaml b/chart/templates/pgbouncer/pgbouncer-deployment.yaml index c25398c20dbf7..35b069a7d9cad 100644 --- a/chart/templates/pgbouncer/pgbouncer-deployment.yaml +++ b/chart/templates/pgbouncer/pgbouncer-deployment.yaml @@ -96,9 +96,6 @@ spec: image: {{ template "pgbouncer_image" . }} imagePullPolicy: {{ .Values.images.pgbouncer.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} - {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} - {{- end }} {{- if .Values.pgbouncer.command }} command: {{ tpl (toYaml .Values.pgbouncer.command) . | nindent 12 }} {{- end }} @@ -158,9 +155,6 @@ spec: image: {{ template "pgbouncer_exporter_image" . }} imagePullPolicy: {{ .Values.images.pgbouncerExporter.pullPolicy }} securityContext: {{ $containerSecurityContextMetricsExporter | nindent 12 }} - {{- if $containerLifecycleHooksMetricsExporter }} - lifecycle: {{- $containerSecurityContextMetricsExporter | nindent 12 }} - {{- end }} env: - name: DATABASE_URL valueFrom: diff --git a/chart/values.yaml b/chart/values.yaml index 7e7fb988e87a2..51b6a2cb38811 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1861,11 +1861,6 @@ pgbouncer: # Additional network policies as needed extraNetworkPolicies: [] - # Default container level lifecycle hooks for every container - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ - containerLifecycleHooks: {} - # Pool sizes metadataPoolSize: 10 resultBackendPoolSize: 5 diff --git a/helm_tests/other/test_pgbouncer.py b/helm_tests/other/test_pgbouncer.py index b18d59f7f59b1..454912b4fb6b3 100644 --- a/helm_tests/other/test_pgbouncer.py +++ b/helm_tests/other/test_pgbouncer.py @@ -525,29 +525,6 @@ def test_extra_ini_configs(self): assert "server_round_robin = 1" in ini assert "stats_period = 30" in ini - def test_pgbouncer_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "pgbouncer": { - "enabled": True, - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - }, - }, - }, - show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - class TestPgbouncerExporter: """Tests PgBouncer exporter.""" From 002843d6fe7e8c699af0b8f8f2f0efe0da178e9f Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 4 Jul 2023 18:02:06 +0200 Subject: [PATCH 06/10] fix description typo --- chart/templates/_helpers.yaml | 2 +- helm_tests/airflow_aux/test_airflow_common.py | 2 +- helm_tests/security/test_security_context.py | 36 ------------------- 3 files changed, 2 insertions(+), 38 deletions(-) diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml index 57d2431523e63..28325851d9b4d 100644 --- a/chart/templates/_helpers.yaml +++ b/chart/templates/_helpers.yaml @@ -981,7 +981,7 @@ Set the default value for container Lifecycle Hooks (redis, statsd). If no value is passed for .containerLifecycleHooks, lifecycle will not be set The template can be called like so: - include "externalContainerSecurityContext" (list . .Values.statsd) + include "containerLifecycleHooks" (list . .Values.statsd) */}} {{- define "externalContainerLifecycleHooks" -}} diff --git a/helm_tests/airflow_aux/test_airflow_common.py b/helm_tests/airflow_aux/test_airflow_common.py index bca5397e0bf95..8f999e7c7d1a1 100644 --- a/helm_tests/airflow_aux/test_airflow_common.py +++ b/helm_tests/airflow_aux/test_airflow_common.py @@ -439,6 +439,6 @@ def test_global_container_lifecycle_webhooks(self): "spec.template.spec.containers[0].lifecycle.preStop", docs[index] ) - # Global security context is not propagated to redis and statsd, so we test default value + # Global containerLifecycleHooks is not propagated to redis and statsd, so we test default value for index in range(len(docs) - 2, len(docs)): assert jmespath.search("spec.template.spec.containers[0].lifecycle", docs[index]) is None diff --git a/helm_tests/security/test_security_context.py b/helm_tests/security/test_security_context.py index 69b7170a2009a..74da2cc95d3cf 100644 --- a/helm_tests/security/test_security_context.py +++ b/helm_tests/security/test_security_context.py @@ -216,42 +216,6 @@ def test_gitsync_sidecar_and_init_container(self): doc, ) - # Test containerLifecycleHooks for main containers - def test_global_container_lifecycle_webhooks(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - }, - }, - show_only=[ - "templates/flower/flower-deployment.yaml", - "templates/scheduler/scheduler-deployment.yaml", - "templates/webserver/webserver-deployment.yaml", - "templates/workers/worker-deployment.yaml", - "templates/jobs/create-user-job.yaml", - "templates/jobs/migrate-database-job.yaml", - "templates/triggerer/triggerer-deployment.yaml", - "templates/statsd/statsd-deployment.yaml", - "templates/redis/redis-statefulset.yaml", - ], - ) - - for index in range(len(docs) - 2): - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[index] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[index] - ) - - # Global security context is not propagated to redis and statsd, so we test default value - for index in range(len(docs) - 2, len(docs)): - assert None == jmespath.search("spec.template.spec.containers[0].lifecycle", docs[index]) - # Test securityContexts for main containers def test_global_security_context(self): ctx_value_pod = {"runAsUser": 7000} From 00b2685ca33383d642fc659cf00b59803379b10d Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Wed, 5 Jul 2023 07:41:32 +0200 Subject: [PATCH 07/10] fix indentation --- chart/values.schema.json | 368 ++++++++++++++++++++++++++++++++++----- 1 file changed, 322 insertions(+), 46 deletions(-) diff --git a/chart/values.schema.json b/chart/values.schema.json index 212a42c5c34a3..5d48b56f720ed 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -145,10 +145,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -1549,10 +1561,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -1620,10 +1644,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -1834,10 +1870,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } @@ -1921,10 +1969,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } @@ -2350,10 +2410,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } @@ -2437,10 +2509,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -2823,10 +2907,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -2913,10 +3009,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -3245,10 +3353,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -3335,10 +3455,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -3572,10 +3704,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -3858,10 +4002,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -4233,10 +4389,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -4592,10 +4760,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } @@ -5008,10 +5188,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -5248,10 +5440,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -5509,10 +5713,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -5804,10 +6020,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -6058,10 +6286,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -6482,10 +6722,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } @@ -6801,10 +7053,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] @@ -9765,10 +10029,22 @@ "examples": [ { "postStart": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo postStart handler > /usr/share/message" + ] + } }, "preStop": { - "exec": { "command": ["/bin/sh", "-c", "echo Hello from the preStop handler > /usr/share/message"]} + "exec": { + "command": [ + "/bin/sh", + "-c", + "echo preStop handler > /usr/share/message" + ] + } } } ] From 8b1e93ef121f50477caaca2a6948e46074b7119d Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Sat, 29 Jul 2023 08:55:40 +0200 Subject: [PATCH 08/10] apply change requests --- .../pod-template-file.kubernetes-helm-yaml | 4 +- chart/templates/_helpers.yaml | 38 --- .../dag-processor-deployment.yaml | 4 +- chart/templates/flower/flower-deployment.yaml | 4 +- chart/templates/jobs/create-user-job.yaml | 4 +- .../templates/jobs/migrate-database-job.yaml | 4 +- .../pgbouncer/pgbouncer-deployment.yaml | 11 +- chart/templates/redis/redis-statefulset.yaml | 4 +- .../scheduler/scheduler-deployment.yaml | 12 +- chart/templates/statsd/statsd-deployment.yaml | 4 +- .../triggerer/triggerer-deployment.yaml | 12 +- .../webserver/webserver-deployment.yaml | 8 +- .../templates/workers/worker-deployment.yaml | 20 +- chart/values.schema.json | 8 +- chart/values.yaml | 55 +---- helm_tests/airflow_aux/test_airflow_common.py | 36 --- .../airflow_aux/test_container_lifecycle.py | 228 ++++++++++++++++++ .../airflow_aux/test_pod_template_file.py | 34 ++- helm_tests/airflow_core/test_scheduler.py | 22 -- helm_tests/airflow_core/test_triggerer.py | 22 -- helm_tests/airflow_core/test_worker.py | 22 -- helm_tests/other/test_redis.py | 22 -- helm_tests/other/test_statsd.py | 22 -- helm_tests/webserver/test_webserver.py | 20 -- 24 files changed, 313 insertions(+), 307 deletions(-) create mode 100644 helm_tests/airflow_aux/test_container_lifecycle.py diff --git a/chart/files/pod-template-file.kubernetes-helm-yaml b/chart/files/pod-template-file.kubernetes-helm-yaml index f073d371bae3e..d0d004c90277c 100644 --- a/chart/files/pod-template-file.kubernetes-helm-yaml +++ b/chart/files/pod-template-file.kubernetes-helm-yaml @@ -23,7 +23,7 @@ {{- $topologySpreadConstraints := or .Values.workers.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.workers) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.workers) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.workers) }} +{{- $containerLifecycleHooks := or .Values.workers.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: v1 kind: Pod metadata: @@ -66,7 +66,7 @@ spec: imagePullPolicy: {{ .Values.images.pod_template.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 8 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 8 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 8 }} {{- end }} name: base resources: {{- toYaml .Values.workers.resources | nindent 8 }} diff --git a/chart/templates/_helpers.yaml b/chart/templates/_helpers.yaml index 28325851d9b4d..e210222846b2a 100644 --- a/chart/templates/_helpers.yaml +++ b/chart/templates/_helpers.yaml @@ -954,41 +954,3 @@ capabilities: {{- end }} {{- end }} {{- end }} - -{{/* -Set the default value for container Lifecycle Hooks. -If no value is passed for containerLifecycleHooks or .containerLifecycleHooks, lifecycle will not be set - -The template can be called like so: - include "containerLifecycleHooks" (list . .Values.statsd) - -Where `.` is the global variables scope and `.Values.statsd` the local variables scope for the webserver template. -*/}} -{{- define "containerLifecycleHooks" -}} - {{- $ := index . 0 -}} - {{- with index . 1 }} - {{- if .containerLifecycleHooks -}} - {{- tpl (toYaml .containerLifecycleHooks) $ }} - {{- else if $.Values.containerLifecycleHooks -}} - {{- tpl (toYaml $.Values.containerLifecycleHooks) $ }} - {{- else -}} - {{- end -}} - {{- end -}} -{{- end -}} - -{{/* -Set the default value for container Lifecycle Hooks (redis, statsd). -If no value is passed for .containerLifecycleHooks, lifecycle will not be set - -The template can be called like so: - include "containerLifecycleHooks" (list . .Values.statsd) - -*/}} -{{- define "externalContainerLifecycleHooks" -}} - {{- $ := index . 0 -}} - {{- with index . 1 }} - {{- if .containerLifecycleHooks -}} - {{- tpl (toYaml .containerLifecycleHooks) $ }} - {{- end -}} - {{- end -}} -{{- end -}} diff --git a/chart/templates/dag-processor/dag-processor-deployment.yaml b/chart/templates/dag-processor/dag-processor-deployment.yaml index b26e084b485cf..e3dbd34ff3c52 100644 --- a/chart/templates/dag-processor/dag-processor-deployment.yaml +++ b/chart/templates/dag-processor/dag-processor-deployment.yaml @@ -29,7 +29,7 @@ {{- $revisionHistoryLimit := or .Values.dagProcessor.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.dagProcessor) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.dagProcessor) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.dagProcessor) }} +{{- $containerLifecycleHooks := or .Values.dagProcessor.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: Deployment metadata: @@ -146,7 +146,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.dagProcessor.command }} command: {{ tpl (toYaml .Values.dagProcessor.command) . | nindent 12 }} diff --git a/chart/templates/flower/flower-deployment.yaml b/chart/templates/flower/flower-deployment.yaml index 9f11746d8f7e3..debc7aacded6c 100644 --- a/chart/templates/flower/flower-deployment.yaml +++ b/chart/templates/flower/flower-deployment.yaml @@ -29,7 +29,7 @@ {{- $revisionHistoryLimit := or .Values.flower.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.flower) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.flower) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.flower) }} +{{- $containerLifecycleHooks := or .Values.flower.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: Deployment metadata: @@ -92,7 +92,7 @@ spec: imagePullPolicy: {{ .Values.images.flower.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.flower.command }} command: {{ tpl (toYaml .Values.flower.command) . | nindent 12 }} diff --git a/chart/templates/jobs/create-user-job.yaml b/chart/templates/jobs/create-user-job.yaml index 64eb24361969c..c1c2f25031cc2 100644 --- a/chart/templates/jobs/create-user-job.yaml +++ b/chart/templates/jobs/create-user-job.yaml @@ -27,7 +27,7 @@ {{- $topologySpreadConstraints := or .Values.createUserJob.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.createUserJob) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.createUserJob) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.createUserJob) }} +{{- $containerLifecycleHooks := or .Values.createUserJob.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: batch/v1 kind: Job metadata: @@ -90,7 +90,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.createUserJob.command }} command: {{ tpl (toYaml .Values.createUserJob.command) . | nindent 12 }} diff --git a/chart/templates/jobs/migrate-database-job.yaml b/chart/templates/jobs/migrate-database-job.yaml index d45a0a303d8f7..8cc3fb321f77b 100644 --- a/chart/templates/jobs/migrate-database-job.yaml +++ b/chart/templates/jobs/migrate-database-job.yaml @@ -27,7 +27,7 @@ {{- $topologySpreadConstraints := or .Values.migrateDatabaseJob.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.migrateDatabaseJob) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.migrateDatabaseJob) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.migrateDatabaseJob) }} +{{- $containerLifecycleHooks := or .Values.migrateDatabaseJob.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: batch/v1 kind: Job metadata: @@ -90,7 +90,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.migrateDatabaseJob.command }} command: {{- tpl (toYaml .Values.migrateDatabaseJob.command) . | nindent 12 }} diff --git a/chart/templates/pgbouncer/pgbouncer-deployment.yaml b/chart/templates/pgbouncer/pgbouncer-deployment.yaml index 35b069a7d9cad..c5a882e1d3f59 100644 --- a/chart/templates/pgbouncer/pgbouncer-deployment.yaml +++ b/chart/templates/pgbouncer/pgbouncer-deployment.yaml @@ -145,11 +145,9 @@ spec: {{- if .Values.pgbouncer.extraVolumeMounts }} {{- tpl (toYaml .Values.pgbouncer.extraVolumeMounts) . | nindent 12 }} {{- end }} - lifecycle: - preStop: - exec: - # Allow existing queries clients to complete within 120 seconds - command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"] + {{- if $containerLifecycleHooks }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} + {{- end }} - name: metrics-exporter resources: {{- toYaml .Values.pgbouncer.metricsExporterSidecar.resources | nindent 12 }} image: {{ template "pgbouncer_exporter_image" . }} @@ -180,6 +178,9 @@ spec: initialDelaySeconds: {{ .Values.pgbouncer.metricsExporterSidecar.readinessProbe.initialDelaySeconds }} periodSeconds: {{ .Values.pgbouncer.metricsExporterSidecar.readinessProbe.periodSeconds }} timeoutSeconds: {{ .Values.pgbouncer.metricsExporterSidecar.readinessProbe.timeoutSeconds }} + {{- if $containerLifecycleHooksMetricsExporter }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksMetricsExporter) . | nindent 12 }} + {{- end }} volumes: - name: pgbouncer-config secret: diff --git a/chart/templates/redis/redis-statefulset.yaml b/chart/templates/redis/redis-statefulset.yaml index 7158769393345..d83d63967931c 100644 --- a/chart/templates/redis/redis-statefulset.yaml +++ b/chart/templates/redis/redis-statefulset.yaml @@ -27,7 +27,7 @@ {{- $topologySpreadConstraints := or .Values.redis.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "localPodSecurityContext" .Values.redis }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.redis }} -{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" (list . .Values.redis) }} +{{- $containerLifecycleHooks := .Values.redis.containerLifecycleHooks }} apiVersion: apps/v1 kind: StatefulSet metadata: @@ -83,7 +83,7 @@ spec: imagePullPolicy: {{ .Values.images.redis.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} command: ["/bin/sh"] resources: {{- toYaml .Values.redis.resources | nindent 12 }} diff --git a/chart/templates/scheduler/scheduler-deployment.yaml b/chart/templates/scheduler/scheduler-deployment.yaml index c4dc56b6049f9..04be915e8c263 100644 --- a/chart/templates/scheduler/scheduler-deployment.yaml +++ b/chart/templates/scheduler/scheduler-deployment.yaml @@ -41,9 +41,9 @@ {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.scheduler) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.scheduler.waitForMigrations) }} {{- $containerSecurityContextLogGroomerSidecar := include "containerSecurityContext" (list . .Values.scheduler.logGroomerSidecar) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.scheduler) }} -{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.scheduler.waitForMigrations) }} -{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.scheduler.logGroomerSidecar) }} +{{- $containerLifecycleHooks := or .Values.scheduler.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksWaitForMigrations := or .Values.scheduler.waitForMigrations.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksLogGroomerSidecar := or .Values.scheduler.logGroomerSidecar.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: {{ if $stateful }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -144,7 +144,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} {{- if $containerLifecycleHooksWaitForMigrations }} - lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksWaitForMigrations) . | nindent 12 }} {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} @@ -179,7 +179,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.scheduler.command }} command: {{ tpl (toYaml .Values.scheduler.command) . | nindent 12 }} @@ -254,7 +254,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} {{- if $containerLifecycleHooksLogGroomerSidecar }} - lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksLogGroomerSidecar) . | nindent 12 }} {{- end }} {{- if .Values.scheduler.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.scheduler.logGroomerSidecar.command) . | nindent 12 }} diff --git a/chart/templates/statsd/statsd-deployment.yaml b/chart/templates/statsd/statsd-deployment.yaml index ee838ef57b67c..b32caa9980472 100644 --- a/chart/templates/statsd/statsd-deployment.yaml +++ b/chart/templates/statsd/statsd-deployment.yaml @@ -28,7 +28,7 @@ {{- $revisionHistoryLimit := or .Values.statsd.revisionHistoryLimit .Values.revisionHistoryLimit }} {{- $securityContext := include "localPodSecurityContext" .Values.statsd }} {{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.statsd }} -{{- $containerLifecycleHooks := include "externalContainerLifecycleHooks" (list . .Values.statsd) }} +{{- $containerLifecycleHooks := .Values.statsd.containerLifecycleHooks }} apiVersion: apps/v1 kind: Deployment metadata: @@ -92,7 +92,7 @@ spec: imagePullPolicy: {{ .Values.images.statsd.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.statsd.args }} args: {{ tpl (toYaml .Values.statsd.args) . | nindent 12 }} diff --git a/chart/templates/triggerer/triggerer-deployment.yaml b/chart/templates/triggerer/triggerer-deployment.yaml index 84a522e649e93..c60cec3cd3371 100644 --- a/chart/templates/triggerer/triggerer-deployment.yaml +++ b/chart/templates/triggerer/triggerer-deployment.yaml @@ -33,9 +33,9 @@ {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.triggerer) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.triggerer.waitForMigrations) }} {{- $containerSecurityContextLogGroomer := include "containerSecurityContext" (list . .Values.triggerer.logGroomerSidecar) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.triggerer) }} -{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.triggerer.waitForMigrations) }} -{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.triggerer.logGroomerSidecar) }} +{{- $containerLifecycleHooks := or .Values.triggerer.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksWaitForMigrations := or .Values.triggerer.waitForMigrations.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksLogGroomerSidecar := or .Values.triggerer.logGroomerSidecar.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: {{ if $persistence }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -132,7 +132,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} {{- if $containerLifecycleHooksWaitForMigrations }} - lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksWaitForMigrations) . | nindent 12 }} {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} @@ -166,7 +166,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.triggerer.command }} command: {{ tpl (toYaml .Values.triggerer.command) . | nindent 12 }} @@ -224,7 +224,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomer | nindent 12 }} {{- if $containerLifecycleHooksLogGroomerSidecar }} - lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksLogGroomerSidecar) . | nindent 12 }} {{- end }} {{- if .Values.triggerer.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.triggerer.logGroomerSidecar.command) . | nindent 12 }} diff --git a/chart/templates/webserver/webserver-deployment.yaml b/chart/templates/webserver/webserver-deployment.yaml index c51c9a81a6ac2..3b53a064a41ea 100644 --- a/chart/templates/webserver/webserver-deployment.yaml +++ b/chart/templates/webserver/webserver-deployment.yaml @@ -28,8 +28,8 @@ {{- $securityContext := include "airflowPodSecurityContext" (list . .Values.webserver) }} {{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.webserver) }} {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.webserver.waitForMigrations) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.webserver) }} -{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.webserver.waitForMigrations) }} +{{- $containerLifecycleHooks := or .Values.webserver.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksWaitForMigrations := or .Values.webserver.waitForMigrations.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: Deployment metadata: @@ -137,7 +137,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} {{- if $containerLifecycleHooksWaitForMigrations }} - lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksWaitForMigrations) . | nindent 12 }} {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} @@ -171,7 +171,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ or $containerSecurityContext .Values.webserver.securityContexts.container .Values.securityContexts.container | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.webserver.command }} command: {{ tpl (toYaml .Values.webserver.command) . | nindent 12 }} diff --git a/chart/templates/workers/worker-deployment.yaml b/chart/templates/workers/worker-deployment.yaml index 1f72e3f7235c6..31956363d0630 100644 --- a/chart/templates/workers/worker-deployment.yaml +++ b/chart/templates/workers/worker-deployment.yaml @@ -34,11 +34,11 @@ {{- $containerSecurityContextWaitForMigrations := include "containerSecurityContext" (list . .Values.workers.waitForMigrations) }} {{- $containerSecurityContextLogGroomerSidecar := include "containerSecurityContext" (list . .Values.workers.logGroomerSidecar) }} {{- $containerSecurityContextKerberosSidecar := include "containerSecurityContext" (list . .Values.workers.kerberosSidecar) }} -{{- $containerLifecycleHooks := include "containerLifecycleHooks" (list . .Values.workers) }} -{{- $containerLifecycleHooksPersistence := include "containerLifecycleHooks" (list . .Values.workers.persistence) }} -{{- $containerLifecycleHooksWaitForMigrations := include "containerLifecycleHooks" (list . .Values.workers.waitForMigrations) }} -{{- $containerLifecycleHooksLogGroomerSidecar := include "containerLifecycleHooks" (list . .Values.workers.logGroomerSidecar) }} -{{- $containerLifecycleHooksKerberosSidecar := include "containerLifecycleHooks" (list . .Values.workers.kerberkerberosSidecar) }} +{{- $containerLifecycleHooks := or .Values.workers.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksPersistence := or .Values.workers.persistence.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksWaitForMigrations := or .Values.workers.waitForMigrations.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksLogGroomerSidecar := or .Values.workers.logGroomerSidecar.containerLifecycleHooks .Values.containerLifecycleHooks }} +{{- $containerLifecycleHooksKerberosSidecar := or .Values.workers.kerberosSidecar.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: apps/v1 kind: {{ if $persistence }}StatefulSet{{ else }}Deployment{{ end }} metadata: @@ -150,7 +150,7 @@ spec: - {{ template "airflow_logs" . }} securityContext: {{ $containerSecurityContextPersistence | nindent 12 }} {{- if $containerLifecycleHooksPersistence }} - lifecycle: {{- $containerLifecycleHooksPersistence | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksPersistence) . | nindent 12 }} {{- end }} volumeMounts: - name: logs @@ -163,7 +163,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextWaitForMigrations | nindent 12 }} {{- if $containerLifecycleHooksWaitForMigrations }} - lifecycle: {{- $containerLifecycleHooksWaitForMigrations | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksWaitForMigrations) . | nindent 12 }} {{- end }} volumeMounts: {{- include "airflow_config_mount" . | nindent 12 }} @@ -197,7 +197,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 12 }} {{- if $containerLifecycleHooks }} - lifecycle: {{- $containerLifecycleHooks | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 12 }} {{- end }} {{- if .Values.workers.command }} command: {{ tpl (toYaml .Values.workers.command) . | nindent 12 }} @@ -277,7 +277,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextLogGroomerSidecar | nindent 12 }} {{- if $containerLifecycleHooksLogGroomerSidecar }} - lifecycle: {{- $containerLifecycleHooksLogGroomerSidecar | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksLogGroomerSidecar) . | nindent 12 }} {{- end }} {{- if .Values.workers.logGroomerSidecar.command }} command: {{ tpl (toYaml .Values.workers.logGroomerSidecar.command) . | nindent 12 }} @@ -310,7 +310,7 @@ spec: imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContextKerberosSidecar | nindent 12 }} {{- if $containerLifecycleHooksKerberosSidecar }} - lifecycle: {{- $containerLifecycleHooksKerberosSidecar | nindent 12 }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooksKerberosSidecar) . | nindent 12 }} {{- end }} args: ["kerberos"] resources: {{- toYaml .Values.workers.kerberosSidecar.resources | nindent 12 }} diff --git a/chart/values.schema.json b/chart/values.schema.json index 5d48b56f720ed..401920a52da16 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -5708,7 +5708,13 @@ "description": "Container Lifecycle Hooks definition for the PgBouncer. If not set, the values from global `containerLifecycleHooks` will be used.", "type": "object", "$ref": "#/definitions/io.k8s.api.core.v1.Lifecycle", - "default": "{}", + "default": { + "preStop": { + "exec": { + "command": ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"] + } + } + }, "x-docsSection": "Kubernetes", "examples": [ { diff --git a/chart/values.yaml b/chart/values.yaml index 51b6a2cb38811..fda63d6ff7bdd 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -43,9 +43,7 @@ securityContexts: pod: {} containers: {} -# Default container level lifecycle hooks for every container -# See: -# https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ +# Default container lifecycle hooks for every service except for redis, statsd and pgbouncer. containerLifecycleHooks: {} # Airflow home directory @@ -519,8 +517,6 @@ workers: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Create ServiceAccount @@ -596,8 +592,6 @@ workers: securityContexts: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} kerberosSidecar: @@ -614,8 +608,6 @@ workers: securityContexts: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} resources: {} @@ -718,8 +710,6 @@ workers: securityContexts: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} env: [] @@ -782,8 +772,6 @@ scheduler: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Create ServiceAccount @@ -887,8 +875,6 @@ scheduler: securityContexts: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} waitForMigrations: @@ -899,8 +885,6 @@ scheduler: securityContexts: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} env: [] @@ -953,8 +937,6 @@ createUserJob: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Create ServiceAccount @@ -1041,8 +1023,6 @@ migrateDatabaseJob: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Create ServiceAccount @@ -1175,8 +1155,6 @@ webserver: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Additional network policies as needed (Deprecated - renamed to `webserver.networkPolicy.ingress.from`) @@ -1305,8 +1283,6 @@ webserver: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} env: [] @@ -1367,8 +1343,6 @@ triggerer: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} persistence: @@ -1466,8 +1440,6 @@ triggerer: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} waitForMigrations: @@ -1479,8 +1451,6 @@ triggerer: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} env: [] @@ -1539,8 +1509,6 @@ dagProcessor: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} resources: {} @@ -1673,8 +1641,6 @@ flower: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Create ServiceAccount @@ -1790,6 +1756,9 @@ statsd: pod: {} container: {} + # container level lifecycle hooks + containerLifecycleHooks: {} + # Additional network policies as needed extraNetworkPolicies: [] resources: {} @@ -1963,9 +1932,11 @@ pgbouncer: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ - containerLifecycleHooks: {} + containerLifecycleHooks: + preStop: + exec: + # Allow existing queries clients to complete within 120 seconds + command: ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"] metricsExporterSidecar: resources: {} @@ -1982,8 +1953,6 @@ pgbouncer: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} livenessProbe: @@ -2064,8 +2033,6 @@ redis: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} podAnnotations: {} @@ -2177,8 +2144,6 @@ cleanup: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Specify history limit @@ -2387,8 +2352,6 @@ dags: container: {} # container level lifecycle hooks - # See: - # https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ containerLifecycleHooks: {} # Mount additional volumes into git-sync. It can be templated like in the following example: diff --git a/helm_tests/airflow_aux/test_airflow_common.py b/helm_tests/airflow_aux/test_airflow_common.py index 8f999e7c7d1a1..72b39e031c9c3 100644 --- a/helm_tests/airflow_aux/test_airflow_common.py +++ b/helm_tests/airflow_aux/test_airflow_common.py @@ -406,39 +406,3 @@ def test_priority_class_name(self): priority = doc["spec"]["template"]["spec"]["priorityClassName"] assert priority == f"low-priority-{component}" - - # Test containerLifecycleHooks for main containers - def test_global_container_lifecycle_webhooks(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - }, - }, - show_only=[ - "templates/flower/flower-deployment.yaml", - "templates/scheduler/scheduler-deployment.yaml", - "templates/webserver/webserver-deployment.yaml", - "templates/workers/worker-deployment.yaml", - "templates/jobs/create-user-job.yaml", - "templates/jobs/migrate-database-job.yaml", - "templates/triggerer/triggerer-deployment.yaml", - "templates/statsd/statsd-deployment.yaml", - "templates/redis/redis-statefulset.yaml", - ], - ) - - for index in range(len(docs) - 2): - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[index] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[index] - ) - - # Global containerLifecycleHooks is not propagated to redis and statsd, so we test default value - for index in range(len(docs) - 2, len(docs)): - assert jmespath.search("spec.template.spec.containers[0].lifecycle", docs[index]) is None diff --git a/helm_tests/airflow_aux/test_container_lifecycle.py b/helm_tests/airflow_aux/test_container_lifecycle.py new file mode 100644 index 0000000000000..6938f768ea67e --- /dev/null +++ b/helm_tests/airflow_aux/test_container_lifecycle.py @@ -0,0 +1,228 @@ +# 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. +from __future__ import annotations + +import jmespath +import pytest + +from tests.charts.helm_template_generator import render_chart + + +@pytest.mark.parametrize( + "release_name,lifecycle_templated,lifecycle_parsed,hook_type", + [ + ( + "test-release", + {"exec": {"command": ["echo", "postStart", "{{ .Release.Name }}"]}}, + {"exec": {"command": ["echo", "postStart", "test-release"]}}, + "postStart", + ), + ( + "test-release", + {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, + {"exec": {"command": ["echo", "preStop", "test-release"]}}, + "preStop", + ), + ], +) +class TestContainerLifecycleHooks: + """Tests container lifecycle hooks.""" + + # Test container lifecycle hooks default setting + def test_check_default_setting(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + docs = render_chart( + name=release_name, + values={ + "webserver": {"defaultUser": {"enabled": True}}, + "flower": {"enabled": True}, + "statsd": {"enabled": True}, + "pgbouncer": {"enabled": True}, + }, + show_only=[ + "templates/flower/flower-deployment.yaml", + "templates/scheduler/scheduler-deployment.yaml", + "templates/webserver/webserver-deployment.yaml", + "templates/workers/worker-deployment.yaml", + "templates/jobs/create-user-job.yaml", + "templates/jobs/migrate-database-job.yaml", + "templates/triggerer/triggerer-deployment.yaml", + "templates/statsd/statsd-deployment.yaml", + "templates/redis/redis-statefulset.yaml", + "templates/pgbouncer/pgbouncer-deployment.yaml", + ], + ) + + # Default for every service is None except for pgbouncer + for doc in docs[:-1]: + assert jmespath.search("spec.template.spec.containers[0].lifecycle", doc) is None + + pgbouncer_default_value = { + "exec": {"command": ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"]} + } + assert pgbouncer_default_value == jmespath.search( + "spec.template.spec.containers[0].lifecycle.preStop", docs[-1] + ) + + # Test Global container lifecycle hooks for the main services + def test_global_setting(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + docs = render_chart( + name=release_name, + values={ + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + }, + show_only=[ + "templates/flower/flower-deployment.yaml", + "templates/scheduler/scheduler-deployment.yaml", + "templates/webserver/webserver-deployment.yaml", + "templates/workers/worker-deployment.yaml", + "templates/jobs/create-user-job.yaml", + "templates/jobs/migrate-database-job.yaml", + "templates/triggerer/triggerer-deployment.yaml", + ], + ) + + for doc in docs: + assert lifecycle_parsed == jmespath.search( + f"spec.template.spec.containers[0].lifecycle.{hook_type}", doc + ) + + # Test Global container lifecycle hooks for the main services + def test_global_setting_external(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + docs = render_chart( + name=release_name, + values={ + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + }, + show_only=[ + "templates/statsd/statsd-deployment.yaml", + "templates/redis/redis-statefulset.yaml", + "templates/pgbouncer/pgbouncer-deployment.yaml", + ], + ) + + for doc in docs: + assert lifecycle_parsed != jmespath.search("spec.template.spec.containers[0].lifecycle", doc) + + # .containerLifecycleWebhooks > containerLifecycleWebhooks + def test_check_main_container_setting( + self, release_name, lifecycle_templated, lifecycle_parsed, hook_type + ): + docs = render_chart( + name=release_name, + values={ + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "flower": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "scheduler": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "webserver": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "workers": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "migrateDatabaseJob": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "triggerer": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "redis": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "statsd": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "pgbouncer": { + "enabled": True, + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + }, + "dagProcessor": { + "enabled": True, + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + }, + }, + show_only=[ + "templates/flower/flower-deployment.yaml", + "templates/scheduler/scheduler-deployment.yaml", + "templates/webserver/webserver-deployment.yaml", + "templates/workers/worker-deployment.yaml", + "templates/jobs/create-user-job.yaml", + "templates/jobs/migrate-database-job.yaml", + "templates/triggerer/triggerer-deployment.yaml", + "templates/statsd/statsd-deployment.yaml", + "templates/redis/redis-statefulset.yaml", + "templates/pgbouncer/pgbouncer-deployment.yaml", + "templates/dag-processor/dag-processor-deployment.yaml", + ], + ) + + for doc in docs: + assert lifecycle_parsed == jmespath.search( + f"spec.template.spec.containers[0].lifecycle.{hook_type}", doc + ) + + # Test container lifecycle hooks for metrics-explorer main container + def test_metrics_explorer_container_setting( + self, release_name, lifecycle_templated, lifecycle_parsed, hook_type + ): + docs = render_chart( + name=release_name, + values={ + "pgbouncer": { + "enabled": True, + "metricsExporterSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + }, + }, + show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], + ) + + assert lifecycle_parsed == jmespath.search( + f"spec.template.spec.containers[1].lifecycle.{hook_type}", docs[0] + ) + + # Test container lifecycle hooks for worker-kerberos main container + def test_worker_kerberos_container_setting( + self, release_name, lifecycle_templated, lifecycle_parsed, hook_type + ): + docs = render_chart( + name=release_name, + values={ + "workers": { + "kerberosSidecar": { + "enabled": True, + "containerLifecycleHooks": {hook_type: lifecycle_templated}, + } + }, + }, + show_only=["templates/workers/worker-deployment.yaml"], + ) + + assert lifecycle_parsed == jmespath.search( + f"spec.template.spec.containers[2].lifecycle.{hook_type}", docs[0] + ) + + # Test container lifecycle hooks for log-groomer-sidecar main container + def test_log_groomer_sidecar_container_setting( + self, release_name, lifecycle_templated, lifecycle_parsed, hook_type + ): + docs = render_chart( + name=release_name, + values={ + "scheduler": { + "logGroomerSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}} + }, + "workers": { + "logGroomerSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}} + }, + }, + show_only=[ + "templates/scheduler/scheduler-deployment.yaml", + "templates/workers/worker-deployment.yaml", + ], + ) + + for doc in docs: + assert lifecycle_parsed == jmespath.search( + f"spec.template.spec.containers[1].lifecycle.{hook_type}", doc + ) diff --git a/helm_tests/airflow_aux/test_pod_template_file.py b/helm_tests/airflow_aux/test_pod_template_file.py index 48ce2656336d9..502b03b4d6a2a 100644 --- a/helm_tests/airflow_aux/test_pod_template_file.py +++ b/helm_tests/airflow_aux/test_pod_template_file.py @@ -751,21 +751,33 @@ def test_workers_priority_class_name(self): assert "test-priority" == jmespath.search("spec.priorityClassName", docs[0]) - def test_workers_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} + @pytest.mark.parametrize( + "release_name,lifecycle_tempated,lifecycle_parsed,hook_type", + [ + ( + "test-release", + {"exec": {"command": ["echo", "postStart", "{{ .Release.Name }}"]}}, + {"exec": {"command": ["echo", "postStart", "test-release"]}}, + "postStart", + ), + ( + "test-release", + {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, + {"exec": {"command": ["echo", "preStop", "test-release"]}}, + "preStop", + ), + ], + ) + def test_workers_container_lifecycle_webhooks_are_configurable( + self, release_name, lifecycle_tempated, lifecycle_parsed, hook_type + ): docs = render_chart( + name=release_name, values={ - "workers": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, + "workers": {"containerLifecycleHooks": {hook_type: lifecycle_tempated}}, }, show_only=["templates/pod-template-file.yaml"], chart_dir=self.temp_chart_dir, ) - assert post_start_value == jmespath.search("spec.containers[0].lifecycle.postStart", docs[0]) - assert pre_stop_value == jmespath.search("spec.containers[0].lifecycle.preStop", docs[0]) + assert lifecycle_parsed == jmespath.search(f"spec.containers[0].lifecycle.{hook_type}", docs[0]) diff --git a/helm_tests/airflow_core/test_scheduler.py b/helm_tests/airflow_core/test_scheduler.py index 833e90f789a3b..9bf2f0071df12 100644 --- a/helm_tests/airflow_core/test_scheduler.py +++ b/helm_tests/airflow_core/test_scheduler.py @@ -464,28 +464,6 @@ def test_scheduler_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) - def test_scheduler_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "scheduler": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, - }, - show_only=["templates/scheduler/scheduler-deployment.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - def test_scheduler_security_context_legacy(self): docs = render_chart( values={ diff --git a/helm_tests/airflow_core/test_triggerer.py b/helm_tests/airflow_core/test_triggerer.py index 0c4fcbfe036f1..d19d83f9175d7 100644 --- a/helm_tests/airflow_core/test_triggerer.py +++ b/helm_tests/airflow_core/test_triggerer.py @@ -574,28 +574,6 @@ def test_should_add_component_specific_annotations(self): assert "annotations" in jmespath.search("metadata", docs[0]) assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value" - def test_triggerer_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "triggerer": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, - }, - show_only=["templates/triggerer/triggerer-deployment.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - class TestTriggererServiceAccount: """Tests triggerer service account.""" diff --git a/helm_tests/airflow_core/test_worker.py b/helm_tests/airflow_core/test_worker.py index 65f56bb5c4732..81432ee7ac6f3 100644 --- a/helm_tests/airflow_core/test_worker.py +++ b/helm_tests/airflow_core/test_worker.py @@ -633,28 +633,6 @@ def test_should_add_component_specific_annotations(self): assert "annotations" in jmespath.search("metadata", docs[0]) assert jmespath.search("metadata.annotations", docs[0])["test_annotation"] == "test_annotation_value" - def test_workers_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "workers": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, - }, - show_only=["templates/workers/worker-deployment.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - class TestWorkerLogGroomer(LogGroomerTestBase): """Worker groomer.""" diff --git a/helm_tests/other/test_redis.py b/helm_tests/other/test_redis.py index 843ea5f41b7ce..c99503a4b7711 100644 --- a/helm_tests/other/test_redis.py +++ b/helm_tests/other/test_redis.py @@ -306,28 +306,6 @@ def test_redis_resources_are_configurable(self): ) assert "300m" == jmespath.search("spec.template.spec.containers[0].resources.requests.cpu", docs[0]) - def test_redis_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "redis": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, - }, - show_only=["templates/redis/redis-statefulset.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - def test_redis_resources_are_not_added_by_default(self): docs = render_chart( show_only=["templates/redis/redis-statefulset.yaml"], diff --git a/helm_tests/other/test_statsd.py b/helm_tests/other/test_statsd.py index 6cd6756ac49f3..bc5b5b0785984 100644 --- a/helm_tests/other/test_statsd.py +++ b/helm_tests/other/test_statsd.py @@ -198,28 +198,6 @@ def test_statsd_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) - def test_statsd_container_lifecycle_webhooks_are_configurable(self): - post_start_value = {"exec": {"command": ["bash", "-c", "echo postStart"]}} - pre_stop_value = {"exec": {"command": ["bash", "-c", "echo preStop"]}} - docs = render_chart( - values={ - "statsd": { - "containerLifecycleHooks": { - "postStart": post_start_value, - "preStop": pre_stop_value, - } - }, - }, - show_only=["templates/statsd/statsd-deployment.yaml"], - ) - - assert post_start_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert pre_stop_value == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - def test_statsd_security_context_legacy(self): docs = render_chart( values={ diff --git a/helm_tests/webserver/test_webserver.py b/helm_tests/webserver/test_webserver.py index 63aced3e8b993..cbb9b0a0fa3a9 100644 --- a/helm_tests/webserver/test_webserver.py +++ b/helm_tests/webserver/test_webserver.py @@ -569,26 +569,6 @@ def test_webserver_security_contexts_are_configurable(self): "runAsNonRoot": True, } == jmespath.search("spec.template.spec.securityContext", docs[0]) - def test_webserver_container_lifecycle_webhooks_are_configurable(self): - docs = render_chart( - values={ - "webserver": { - "containerLifecycleHooks": { - "postStart": {"exec": {"command": ["bash", "-c", "echo postStart"]}}, - "preStop": {"exec": {"command": ["bash", "-c", "echo preStop"]}}, - } - }, - }, - show_only=["templates/webserver/webserver-deployment.yaml"], - ) - - assert {"exec": {"command": ["bash", "-c", "echo postStart"]}} == jmespath.search( - "spec.template.spec.containers[0].lifecycle.postStart", docs[0] - ) - assert {"exec": {"command": ["bash", "-c", "echo preStop"]}} == jmespath.search( - "spec.template.spec.containers[0].lifecycle.preStop", docs[0] - ) - def test_webserver_security_context_legacy(self): docs = render_chart( values={ From cab963224e82a45ca56a96430f1e852623890eea Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Tue, 8 Aug 2023 08:27:12 +0200 Subject: [PATCH 09/10] fix json schema format --- chart/values.schema.json | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/chart/values.schema.json b/chart/values.schema.json index 401920a52da16..0333781438ada 100644 --- a/chart/values.schema.json +++ b/chart/values.schema.json @@ -1888,7 +1888,6 @@ } } } - ] }, "securityContexts": { @@ -1987,7 +1986,6 @@ } } } - ] }, "securityContexts": { @@ -2428,7 +2426,6 @@ } } } - ] }, "securityContexts": { @@ -4778,7 +4775,6 @@ } } } - ] }, "securityContexts": { @@ -5711,7 +5707,11 @@ "default": { "preStop": { "exec": { - "command": ["/bin/sh", "-c", "killall -INT pgbouncer && sleep 120"] + "command": [ + "/bin/sh", + "-c", + "killall -INT pgbouncer && sleep 120" + ] } } }, @@ -6746,7 +6746,6 @@ } } } - ] }, "securityContexts": { From 94110df7cfecd0d49cf981b33b0e721bfd5ccea4 Mon Sep 17 00:00:00 2001 From: "Reza J. Bavaghoush" Date: Fri, 11 Aug 2023 16:34:53 +0200 Subject: [PATCH 10/10] remove parametrized tests --- .../airflow_aux/test_container_lifecycle.py | 132 +++++++++--------- .../airflow_aux/test_pod_template_file.py | 32 ++--- 2 files changed, 74 insertions(+), 90 deletions(-) diff --git a/helm_tests/airflow_aux/test_container_lifecycle.py b/helm_tests/airflow_aux/test_container_lifecycle.py index 6938f768ea67e..dd2f5bb0c6fc8 100644 --- a/helm_tests/airflow_aux/test_container_lifecycle.py +++ b/helm_tests/airflow_aux/test_container_lifecycle.py @@ -17,35 +17,31 @@ from __future__ import annotations import jmespath -import pytest from tests.charts.helm_template_generator import render_chart +CONTAINER_LIFECYCLE_PARAMETERS = { + "preStop": { + "release_name": "test-release", + "lifecycle_templated": {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, + "lifecycle_parsed": {"exec": {"command": ["echo", "preStop", "test-release"]}}, + }, + "postStart": { + "release_name": "test-release", + "lifecycle_templated": {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, + "lifecycle_parsed": {"exec": {"command": ["echo", "preStop", "test-release"]}}, + }, +} + -@pytest.mark.parametrize( - "release_name,lifecycle_templated,lifecycle_parsed,hook_type", - [ - ( - "test-release", - {"exec": {"command": ["echo", "postStart", "{{ .Release.Name }}"]}}, - {"exec": {"command": ["echo", "postStart", "test-release"]}}, - "postStart", - ), - ( - "test-release", - {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, - {"exec": {"command": ["echo", "preStop", "test-release"]}}, - "preStop", - ), - ], -) class TestContainerLifecycleHooks: """Tests container lifecycle hooks.""" # Test container lifecycle hooks default setting - def test_check_default_setting(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + def test_check_default_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ "webserver": {"defaultUser": {"enabled": True}}, "flower": {"enabled": True}, @@ -76,13 +72,15 @@ def test_check_default_setting(self, release_name, lifecycle_templated, lifecycl assert pgbouncer_default_value == jmespath.search( "spec.template.spec.containers[0].lifecycle.preStop", docs[-1] ) + assert jmespath.search("spec.template.spec.containers[0].lifecycle.postStart", docs[-1]) is None # Test Global container lifecycle hooks for the main services - def test_global_setting(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + def test_global_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ - "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "containerLifecycleHooks": {hook_type: lifecycle_hook_params["lifecycle_templated"]}, }, show_only=[ "templates/flower/flower-deployment.yaml", @@ -96,16 +94,18 @@ def test_global_setting(self, release_name, lifecycle_templated, lifecycle_parse ) for doc in docs: - assert lifecycle_parsed == jmespath.search( + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( f"spec.template.spec.containers[0].lifecycle.{hook_type}", doc ) # Test Global container lifecycle hooks for the main services - def test_global_setting_external(self, release_name, lifecycle_templated, lifecycle_parsed, hook_type): + def test_global_setting_external(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ - "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "containerLifecycleHooks": lifecycle_hooks_config, }, show_only=[ "templates/statsd/statsd-deployment.yaml", @@ -115,31 +115,33 @@ def test_global_setting_external(self, release_name, lifecycle_templated, lifecy ) for doc in docs: - assert lifecycle_parsed != jmespath.search("spec.template.spec.containers[0].lifecycle", doc) + assert lifecycle_hook_params["lifecycle_parsed"] != jmespath.search( + "spec.template.spec.containers[0].lifecycle", doc + ) # .containerLifecycleWebhooks > containerLifecycleWebhooks - def test_check_main_container_setting( - self, release_name, lifecycle_templated, lifecycle_parsed, hook_type - ): + def test_check_main_container_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ - "containerLifecycleHooks": {hook_type: lifecycle_templated}, - "flower": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "scheduler": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "webserver": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "workers": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "migrateDatabaseJob": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "triggerer": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "redis": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, - "statsd": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "containerLifecycleHooks": lifecycle_hooks_config, + "flower": {"containerLifecycleHooks": lifecycle_hooks_config}, + "scheduler": {"containerLifecycleHooks": lifecycle_hooks_config}, + "webserver": {"containerLifecycleHooks": lifecycle_hooks_config}, + "workers": {"containerLifecycleHooks": lifecycle_hooks_config}, + "migrateDatabaseJob": {"containerLifecycleHooks": lifecycle_hooks_config}, + "triggerer": {"containerLifecycleHooks": lifecycle_hooks_config}, + "redis": {"containerLifecycleHooks": lifecycle_hooks_config}, + "statsd": {"containerLifecycleHooks": lifecycle_hooks_config}, "pgbouncer": { "enabled": True, - "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "containerLifecycleHooks": lifecycle_hooks_config, }, "dagProcessor": { "enabled": True, - "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "containerLifecycleHooks": lifecycle_hooks_config, }, }, show_only=[ @@ -158,63 +160,59 @@ def test_check_main_container_setting( ) for doc in docs: - assert lifecycle_parsed == jmespath.search( + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( f"spec.template.spec.containers[0].lifecycle.{hook_type}", doc ) # Test container lifecycle hooks for metrics-explorer main container - def test_metrics_explorer_container_setting( - self, release_name, lifecycle_templated, lifecycle_parsed, hook_type - ): + def test_metrics_explorer_container_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ "pgbouncer": { "enabled": True, - "metricsExporterSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}}, + "metricsExporterSidecar": {"containerLifecycleHooks": lifecycle_hooks_config}, }, }, show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"], ) - assert lifecycle_parsed == jmespath.search( + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( f"spec.template.spec.containers[1].lifecycle.{hook_type}", docs[0] ) # Test container lifecycle hooks for worker-kerberos main container - def test_worker_kerberos_container_setting( - self, release_name, lifecycle_templated, lifecycle_parsed, hook_type - ): + def test_worker_kerberos_container_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ "workers": { "kerberosSidecar": { "enabled": True, - "containerLifecycleHooks": {hook_type: lifecycle_templated}, + "containerLifecycleHooks": lifecycle_hooks_config, } }, }, show_only=["templates/workers/worker-deployment.yaml"], ) - assert lifecycle_parsed == jmespath.search( + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( f"spec.template.spec.containers[2].lifecycle.{hook_type}", docs[0] ) # Test container lifecycle hooks for log-groomer-sidecar main container - def test_log_groomer_sidecar_container_setting( - self, release_name, lifecycle_templated, lifecycle_parsed, hook_type - ): + def test_log_groomer_sidecar_container_setting(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ - "scheduler": { - "logGroomerSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}} - }, - "workers": { - "logGroomerSidecar": {"containerLifecycleHooks": {hook_type: lifecycle_templated}} - }, + "scheduler": {"logGroomerSidecar": {"containerLifecycleHooks": lifecycle_hooks_config}}, + "workers": {"logGroomerSidecar": {"containerLifecycleHooks": lifecycle_hooks_config}}, }, show_only=[ "templates/scheduler/scheduler-deployment.yaml", @@ -223,6 +221,6 @@ def test_log_groomer_sidecar_container_setting( ) for doc in docs: - assert lifecycle_parsed == jmespath.search( + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( f"spec.template.spec.containers[1].lifecycle.{hook_type}", doc ) diff --git a/helm_tests/airflow_aux/test_pod_template_file.py b/helm_tests/airflow_aux/test_pod_template_file.py index 502b03b4d6a2a..3e795d7113fe7 100644 --- a/helm_tests/airflow_aux/test_pod_template_file.py +++ b/helm_tests/airflow_aux/test_pod_template_file.py @@ -24,6 +24,7 @@ import jmespath import pytest +from helm_tests.airflow_aux.test_container_lifecycle import CONTAINER_LIFECYCLE_PARAMETERS from tests.charts.helm_template_generator import render_chart @@ -751,33 +752,18 @@ def test_workers_priority_class_name(self): assert "test-priority" == jmespath.search("spec.priorityClassName", docs[0]) - @pytest.mark.parametrize( - "release_name,lifecycle_tempated,lifecycle_parsed,hook_type", - [ - ( - "test-release", - {"exec": {"command": ["echo", "postStart", "{{ .Release.Name }}"]}}, - {"exec": {"command": ["echo", "postStart", "test-release"]}}, - "postStart", - ), - ( - "test-release", - {"exec": {"command": ["echo", "preStop", "{{ .Release.Name }}"]}}, - {"exec": {"command": ["echo", "preStop", "test-release"]}}, - "preStop", - ), - ], - ) - def test_workers_container_lifecycle_webhooks_are_configurable( - self, release_name, lifecycle_tempated, lifecycle_parsed, hook_type - ): + def test_workers_container_lifecycle_webhooks_are_configurable(self, hook_type="preStop"): + lifecycle_hook_params = CONTAINER_LIFECYCLE_PARAMETERS[hook_type] + lifecycle_hooks_config = {hook_type: lifecycle_hook_params["lifecycle_templated"]} docs = render_chart( - name=release_name, + name=lifecycle_hook_params["release_name"], values={ - "workers": {"containerLifecycleHooks": {hook_type: lifecycle_tempated}}, + "workers": {"containerLifecycleHooks": lifecycle_hooks_config}, }, show_only=["templates/pod-template-file.yaml"], chart_dir=self.temp_chart_dir, ) - assert lifecycle_parsed == jmespath.search(f"spec.containers[0].lifecycle.{hook_type}", docs[0]) + assert lifecycle_hook_params["lifecycle_parsed"] == jmespath.search( + f"spec.containers[0].lifecycle.{hook_type}", docs[0] + )