From f19bdaf32b1e1bb40534c720d6e4afefedf9860c Mon Sep 17 00:00:00 2001 From: Henry Chen Date: Sat, 25 Apr 2026 04:27:15 +0800 Subject: [PATCH] [chart/v1-2x-test] Fix cleanup pod lifecycle hooks not being applied (#65764) (cherry picked from commit 294dd5e7663687c796e245924a4a213ab7cf1849) Co-authored-by: Henry Chen --- chart/templates/cleanup/cleanup-cronjob.yaml | 4 ++ .../airflow_aux/test_cleanup_pods.py | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/chart/templates/cleanup/cleanup-cronjob.yaml b/chart/templates/cleanup/cleanup-cronjob.yaml index ae202fddde530..70cb53862d1cb 100644 --- a/chart/templates/cleanup/cleanup-cronjob.yaml +++ b/chart/templates/cleanup/cleanup-cronjob.yaml @@ -27,6 +27,7 @@ {{- $topologySpreadConstraints := or .Values.cleanup.topologySpreadConstraints .Values.topologySpreadConstraints }} {{- $securityContext := include "airflowPodSecurityContext" (list .Values.cleanup .Values) }} {{- $containerSecurityContext := include "containerSecurityContext" (list .Values.cleanup .Values) }} +{{- $containerLifecycleHooks := or .Values.cleanup.containerLifecycleHooks .Values.containerLifecycleHooks }} apiVersion: batch/v1 kind: CronJob metadata: @@ -92,6 +93,9 @@ spec: image: {{ template "airflow_image" . }} imagePullPolicy: {{ .Values.images.airflow.pullPolicy }} securityContext: {{ $containerSecurityContext | nindent 16 }} + {{- if $containerLifecycleHooks }} + lifecycle: {{- tpl (toYaml $containerLifecycleHooks) . | nindent 16 }} + {{- end }} {{- if .Values.cleanup.command }} command: {{ tpl (toYaml .Values.cleanup.command) . | nindent 16 }} {{- end }} diff --git a/helm-tests/tests/helm_tests/airflow_aux/test_cleanup_pods.py b/helm-tests/tests/helm_tests/airflow_aux/test_cleanup_pods.py index 31006e526253c..11c42d542025e 100644 --- a/helm-tests/tests/helm_tests/airflow_aux/test_cleanup_pods.py +++ b/helm-tests/tests/helm_tests/airflow_aux/test_cleanup_pods.py @@ -228,6 +228,50 @@ def test_default_command_and_args(self): "exec airflow kubernetes cleanup-pods --namespace=default", ] + @pytest.mark.parametrize( + "values", + [ + pytest.param( + { + "cleanup": { + "enabled": True, + "containerLifecycleHooks": {"preStop": {"exec": {"command": ["echo", "cleanup"]}}}, + }, + }, + id="component", + ), + pytest.param( + { + "cleanup": {"enabled": True}, + "containerLifecycleHooks": {"preStop": {"exec": {"command": ["echo", "cleanup"]}}}, + }, + id="global-fallback", + ), + pytest.param( + { + "cleanup": { + "enabled": True, + "containerLifecycleHooks": {"preStop": {"exec": {"command": ["echo", "cleanup"]}}}, + }, + "containerLifecycleHooks": {"postStart": {"exec": {"command": ["echo", "global"]}}}, + }, + id="component-overrides-global", + ), + ], + ) + def test_should_render_container_lifecycle_hooks(self, values): + docs = render_chart( + values={ + "executor": "KubernetesExecutor", + **values, + }, + show_only=["templates/cleanup/cleanup-cronjob.yaml"], + ) + + assert jmespath.search("spec.jobTemplate.spec.template.spec.containers[0].lifecycle", docs[0]) == { + "preStop": {"exec": {"command": ["echo", "cleanup"]}} + } + def test_should_add_extraEnvs(self): docs = render_chart( values={