From e475d0640342124b3704e6aa5328a0a52dbba9dc Mon Sep 17 00:00:00 2001 From: Daniel Simko Date: Tue, 11 Aug 2026 14:56:40 +0200 Subject: [PATCH] Support wait_for_jobs flag for helm release upgrade/install (#1140) * Support wait_for_jobs flag for helm release upgrade/install * Update plugins/modules/helm.py Co-authored-by: Yuriy Novostavskiy * Apply suggestion from @abikouo * fix(helm): correct wait_for_jobs implementation and add changelog - Fix double-append of --wait-for-jobs flag - Fix version check to run before appending the flag - Fetch helm_version before use to avoid undefined variable - Fix YAML indentation for version_added in docs - Add changelog fragment Co-Authored-By: Claude Opus 4.6 * test: add integration tests for wait_for_jobs - Test that --wait-for-jobs is appended when wait=true and wait_for_jobs=true - Test that wait_for_jobs fails with appropriate message on helm < 3.5.0 - Test that wait_for_jobs is ignored when wait is not set - Add test-chart-job helm chart containing a simple Job resource Co-Authored-By: Claude Opus 4.6 * fix: harden test Job pod spec Disable service account token automounting and add memory limit to the test Job container. Co-Authored-By: Claude Opus 4.6 * fix: add resource requests and pin image version in test Job Add CPU, memory, and ephemeral-storage requests. Pin busybox image to version 1.37. Co-Authored-By: Claude Opus 4.6 * style: apply linter formatting to helm argument_spec Co-Authored-By: Claude Opus 4.6 * Update sanity ignore file * fix: quote Helm template expression to pass yamllint sanity Co-Authored-By: Claude Opus 4.6 * Fix linters errors --------- Co-authored-by: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> Co-authored-by: Yuriy Novostavskiy Co-authored-by: aubin bikouo Co-authored-by: Claude Opus 4.6 --- .../20260807-helm-wait-for-jobs.yaml | 5 + plugins/modules/helm.py | 115 ++++++++++++------ .../targets/helm/defaults/main.yml | 1 + .../helm/files/test-chart-job/Chart.yaml | 6 + .../files/test-chart-job/templates/job.yaml | 21 ++++ .../targets/helm/tasks/run_test.yml | 3 + .../helm/tasks/test_helm_wait_for_jobs.yml | 59 +++++++++ tests/sanity/ignore-2.16.txt | 1 + tests/sanity/ignore-2.17.txt | 1 + tests/sanity/ignore-2.18.txt | 1 + tests/sanity/ignore-2.19.txt | 1 + tests/sanity/ignore-2.20.txt | 1 + tests/sanity/ignore-2.21.txt | 1 + tests/sanity/ignore-2.22.txt | 1 + 14 files changed, 178 insertions(+), 39 deletions(-) create mode 100644 changelogs/fragments/20260807-helm-wait-for-jobs.yaml create mode 100644 tests/integration/targets/helm/files/test-chart-job/Chart.yaml create mode 100644 tests/integration/targets/helm/files/test-chart-job/templates/job.yaml create mode 100644 tests/integration/targets/helm/tasks/test_helm_wait_for_jobs.yml diff --git a/changelogs/fragments/20260807-helm-wait-for-jobs.yaml b/changelogs/fragments/20260807-helm-wait-for-jobs.yaml new file mode 100644 index 00000000..920031da --- /dev/null +++ b/changelogs/fragments/20260807-helm-wait-for-jobs.yaml @@ -0,0 +1,5 @@ +--- +minor_changes: + - "helm - Add ``wait_for_jobs`` option to wait for all Jobs to complete before + marking a Helm release as successful. Requires Helm >= 3.5.0 + (https://github.com/ansible-collections/kubernetes.core/pull/1140)." diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index df21b95a..87449089 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -187,6 +187,15 @@ options: It will wait for as long as I(wait_timeout). This feature requires helm>=3.7.0. Added in version 2.3.0. default: False type: bool + wait_for_jobs: + description: + - When I(release_state) is set to C(present), and I(wait) is set to C(True), wait until all jobs are in a + completed state before marking the release as successful. + - Ignored when used without O(wait). + - Requires Helm >= 3.5.0 + version_added: 6.6.0 + default: False + type: bool wait_timeout: description: - Timeout when wait option is enabled (helm2 is a number of seconds, helm3 is a duration). @@ -397,6 +406,7 @@ EXAMPLES = r""" release_namespace: default force: True wait: True + wait_for_jobs: True replace: True update_repo_cache: True disable_hook: True @@ -613,6 +623,7 @@ def deploy( release_values, chart_name, wait, + wait_for_jobs, wait_timeout, disable_hook, force, @@ -668,6 +679,16 @@ def deploy( if wait: deploy_command += " --wait" + if wait_for_jobs: + helm_version = module.get_helm_version() + if LooseVersion(helm_version) < LooseVersion("3.5.0"): + module.fail_json( + msg="wait_for_jobs requires helm >= 3.5.0, current version is {0}".format( + helm_version + ) + ) + else: + deploy_command += " --wait-for-jobs" if wait_timeout is not None: deploy_command += " --timeout " + wait_timeout @@ -966,45 +987,58 @@ def default_check(release_status, chart_info, values=None, values_files=None): def argument_spec(): arg_spec = copy.deepcopy(HELM_AUTH_ARG_SPEC) arg_spec.update( - dict( - chart_ref=dict(type="path"), - chart_repo_url=dict(type="str"), - chart_version=dict(type="str"), - dependency_update=dict(type="bool", default=False, aliases=["dep_up"]), - release_name=dict(type="str", required=True, aliases=["name"]), - release_namespace=dict(type="str", required=True, aliases=["namespace"]), - release_state=dict( - default="present", choices=["present", "absent"], aliases=["state"] - ), - release_values=dict(type="dict", default={}, aliases=["values"]), - values_files=dict(type="list", default=[], elements="str"), - update_repo_cache=dict(type="bool", default=False), - disable_hook=dict(type="bool", default=False), - force=dict(type="bool", default=False), - purge=dict(type="bool", default=True), - wait=dict(type="bool", default=False), - wait_timeout=dict(type="str"), - timeout=dict(type="str"), - atomic=dict(type="bool", default=False), - cleanup_on_fail=dict(type="bool", default=False), - server_side=dict(type="str", choices=["auto", "true", "false"]), - force_conflicts=dict(type="bool", default=False), - create_namespace=dict(type="bool", default=False), - post_renderer=dict(type="str"), - replace=dict(type="bool", default=False), - skip_crds=dict(type="bool", default=False), - history_max=dict(type="int"), - set_values=dict(type="list", elements="dict"), - reuse_values=dict(type="bool"), - reset_values=dict(type="bool", default=True), - reset_then_reuse_values=dict(type="bool", default=False), - insecure_skip_tls_verify=dict( - type="bool", default=False, aliases=["skip_tls_certs_check"] - ), - plain_http=dict(type="bool", default=False), - take_ownership=dict(type="bool", default=False), - skip_schema_validation=dict(type="bool", default=False), - ) + { + "chart_ref": {"type": "path"}, + "chart_repo_url": {"type": "str"}, + "chart_version": {"type": "str"}, + "dependency_update": { + "type": "bool", + "default": False, + "aliases": ["dep_up"], + }, + "release_name": {"type": "str", "required": True, "aliases": ["name"]}, + "release_namespace": { + "type": "str", + "required": True, + "aliases": ["namespace"], + }, + "release_state": { + "default": "present", + "choices": ["present", "absent"], + "aliases": ["state"], + }, + "release_values": {"type": "dict", "default": {}, "aliases": ["values"]}, + "values_files": {"type": "list", "default": [], "elements": "str"}, + "update_repo_cache": {"type": "bool", "default": False}, + "disable_hook": {"type": "bool", "default": False}, + "force": {"type": "bool", "default": False}, + "purge": {"type": "bool", "default": True}, + "wait": {"type": "bool", "default": False}, + "wait_for_jobs": {"type": "bool", "default": False}, + "wait_timeout": {"type": "str"}, + "timeout": {"type": "str"}, + "atomic": {"type": "bool", "default": False}, + "server_side": {"type": "str", "choices": ["auto", "true", "false"]}, + "force_conflicts": {"type": "bool", "default": False}, + "create_namespace": {"type": "bool", "default": False}, + "post_renderer": {"type": "str"}, + "replace": {"type": "bool", "default": False}, + "skip_crds": {"type": "bool", "default": False}, + "history_max": {"type": "int"}, + "set_values": {"type": "list", "elements": "dict"}, + "reuse_values": {"type": "bool"}, + "reset_values": {"type": "bool", "default": True}, + "reset_then_reuse_values": {"type": "bool", "default": False}, + "insecure_skip_tls_verify": { + "type": "bool", + "default": False, + "aliases": ["skip_tls_certs_check"], + }, + "plain_http": {"type": "bool", "default": False}, + "take_ownership": {"type": "bool", "default": False}, + "skip_schema_validation": {"type": "bool", "default": False}, + "cleanup_on_fail": {"type": "bool", "default": False}, + } ) return arg_spec @@ -1050,6 +1084,7 @@ def main(): force = module.params.get("force") purge = module.params.get("purge") wait = module.params.get("wait") + wait_for_jobs = module.params.get("wait_for_jobs") wait_timeout = module.params.get("wait_timeout") atomic = module.params.get("atomic") cleanup_on_fail = module.params.get("cleanup_on_fail") @@ -1178,6 +1213,7 @@ def main(): release_values, chart_ref, wait, + wait_for_jobs, wait_timeout, disable_hook, False, @@ -1265,6 +1301,7 @@ def main(): release_values, chart_ref, wait, + wait_for_jobs, wait_timeout, disable_hook, force, diff --git a/tests/integration/targets/helm/defaults/main.yml b/tests/integration/targets/helm/defaults/main.yml index 1c5317ec..ec2c0c62 100644 --- a/tests/integration/targets/helm/defaults/main.yml +++ b/tests/integration/targets/helm/defaults/main.yml @@ -31,3 +31,4 @@ test_namespace: - "helm-insecure-{{ helm_version | replace('.', '-') }}" - "helm-test-take-ownership-{{ helm_version | replace('.', '-') }}" - "helm-skip-schema-validation-{{ helm_version | replace('.', '-') }}" + - "helm-wait-for-jobs-{{ helm_version | replace('.', '-') }}" diff --git a/tests/integration/targets/helm/files/test-chart-job/Chart.yaml b/tests/integration/targets/helm/files/test-chart-job/Chart.yaml new file mode 100644 index 00000000..d8b09707 --- /dev/null +++ b/tests/integration/targets/helm/files/test-chart-job/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: test-chart-job +description: A chart with a Job for testing wait_for_jobs +type: application +version: 0.1.0 +appVersion: "default" diff --git a/tests/integration/targets/helm/files/test-chart-job/templates/job.yaml b/tests/integration/targets/helm/files/test-chart-job/templates/job.yaml new file mode 100644 index 00000000..74461331 --- /dev/null +++ b/tests/integration/targets/helm/files/test-chart-job/templates/job.yaml @@ -0,0 +1,21 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: "{{ .Release.Name }}-job" +spec: + backoffLimit: 0 + template: + spec: + automountServiceAccountToken: false + restartPolicy: Never + containers: + - name: test + image: "busybox:1.37" + command: ["sh", "-c", "echo done"] + resources: + requests: + cpu: "50m" + memory: "32Mi" + ephemeral-storage: "16Mi" + limits: + memory: "64Mi" diff --git a/tests/integration/targets/helm/tasks/run_test.yml b/tests/integration/targets/helm/tasks/run_test.yml index 54afbe1f..1eb120b7 100644 --- a/tests/integration/targets/helm/tasks/run_test.yml +++ b/tests/integration/targets/helm/tasks/run_test.yml @@ -49,6 +49,9 @@ - name: Test helm skip_schema_validation include_tasks: test_skip_schema_validation.yml + - name: Test helm wait_for_jobs + include_tasks: test_helm_wait_for_jobs.yml + always: - name: Remove helm-diff plugin helm_plugin: diff --git a/tests/integration/targets/helm/tasks/test_helm_wait_for_jobs.yml b/tests/integration/targets/helm/tasks/test_helm_wait_for_jobs.yml new file mode 100644 index 00000000..48c7735c --- /dev/null +++ b/tests/integration/targets/helm/tasks/test_helm_wait_for_jobs.yml @@ -0,0 +1,59 @@ +--- +- name: Test helm wait_for_jobs + vars: + helm_namespace: "{{ test_namespace[15] }}" + chart_local: "{{ role_path }}/files/test-chart-job" + block: + + - name: Install chart with wait and wait_for_jobs + helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ chart_local }}" + release_name: test-wait-for-jobs + release_namespace: "{{ helm_namespace }}" + create_namespace: true + wait: true + wait_for_jobs: true + wait_timeout: "120s" + register: install + ignore_errors: true + + - name: Validate wait_for_jobs with helm >= 3.5.0 works + assert: + that: + - install is changed + - "'--wait' in install.command" + - "'--wait-for-jobs' in install.command" + when: "helm_version is ansible.builtin.version('v3.5.0', '>=')" + + - name: Validate wait_for_jobs with helm < 3.5.0 fails + assert: + that: + - install is failed + - "'wait_for_jobs requires helm >= 3.5.0' in install.msg" + when: "helm_version is ansible.builtin.version('v3.5.0', '<')" + + - name: Install chart with wait_for_jobs but without wait + helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ chart_local }}" + release_name: test-wait-for-jobs-no-wait + release_namespace: "{{ helm_namespace }}" + create_namespace: true + wait_for_jobs: true + register: install_no_wait + + - name: Validate wait_for_jobs is ignored without wait + assert: + that: + - install_no_wait is changed + - "'--wait-for-jobs' not in install_no_wait.command" + - "'--wait' not in install_no_wait.command" + + always: + - name: Remove helm namespace + k8s: + api_version: v1 + kind: Namespace + name: "{{ helm_namespace }}" + state: absent diff --git a/tests/sanity/ignore-2.16.txt b/tests/sanity/ignore-2.16.txt index 6d6103ec..d16fb3b6 100644 --- a/tests/sanity/ignore-2.16.txt +++ b/tests/sanity/ignore-2.16.txt @@ -21,6 +21,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.17.txt b/tests/sanity/ignore-2.17.txt index 6d6103ec..d16fb3b6 100644 --- a/tests/sanity/ignore-2.17.txt +++ b/tests/sanity/ignore-2.17.txt @@ -21,6 +21,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.18.txt b/tests/sanity/ignore-2.18.txt index c804f90b..5a280f74 100644 --- a/tests/sanity/ignore-2.18.txt +++ b/tests/sanity/ignore-2.18.txt @@ -18,6 +18,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.19.txt b/tests/sanity/ignore-2.19.txt index c804f90b..5a280f74 100644 --- a/tests/sanity/ignore-2.19.txt +++ b/tests/sanity/ignore-2.19.txt @@ -18,6 +18,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.20.txt b/tests/sanity/ignore-2.20.txt index bfe7532f..41831ae6 100644 --- a/tests/sanity/ignore-2.20.txt +++ b/tests/sanity/ignore-2.20.txt @@ -21,6 +21,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.21.txt b/tests/sanity/ignore-2.21.txt index 932e7ef4..00da064b 100644 --- a/tests/sanity/ignore-2.21.txt +++ b/tests/sanity/ignore-2.21.txt @@ -21,6 +21,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.22.txt b/tests/sanity/ignore-2.22.txt index 932e7ef4..00da064b 100644 --- a/tests/sanity/ignore-2.22.txt +++ b/tests/sanity/ignore-2.22.txt @@ -21,6 +21,7 @@ tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip tests/unit/module_utils/fixtures/pods.yml yamllint!skip tests/integration/targets/helm/files/appversionless-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/appversionless-chart/templates/configmap.yaml yamllint!skip +tests/integration/targets/helm/files/test-chart-job/Chart.yaml yamllint!skip tests/integration/targets/helm/files/test-chart-v2/templates/configmap.yaml yamllint!skip tests/integration/targets/helm/files/test-chart/templates/configmap.yaml yamllint!skip tests/integration/targets/helm_diff/files/test-chart/templates/configmap.yaml yamllint!skip