From 6486e646db4ff81ffce44e7e23b68dc99064bdbf Mon Sep 17 00:00:00 2001 From: "david.bidorff@gmail.com" Date: Sat, 30 Nov 2024 10:43:43 +0100 Subject: [PATCH 1/6] add reset_then_reuse_values support to helm module --- ...lm-add-reset_then_reuse_values-support.yml | 3 + docs/kubernetes.core.helm_module.rst | 21 +++++ plugins/modules/helm.py | 22 +++++ .../test_helm_reset_then_reuse_values.yml.yml | 77 +++++++++++++++ .../tasks/reset_then_reuse_values.yml.yml | 94 +++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 changelogs/fragments/800-helm-add-reset_then_reuse_values-support.yml create mode 100644 tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml create mode 100644 tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml diff --git a/changelogs/fragments/800-helm-add-reset_then_reuse_values-support.yml b/changelogs/fragments/800-helm-add-reset_then_reuse_values-support.yml new file mode 100644 index 0000000000..0db6456d2d --- /dev/null +++ b/changelogs/fragments/800-helm-add-reset_then_reuse_values-support.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - helm - add reset_then_reuse_values support to helm module (https://github.com/ansible-collections/kubernetes.core/issues/803). diff --git a/docs/kubernetes.core.helm_module.rst b/docs/kubernetes.core.helm_module.rst index 93da13595c..3ddb5b7b64 100644 --- a/docs/kubernetes.core.helm_module.rst +++ b/docs/kubernetes.core.helm_module.rst @@ -476,6 +476,27 @@ Parameters
If reset_values is set to True, this is ignored.
+ + +
+ reset_then_reuse_values + +
+ boolean +
+
added in 5.1.0
+ + + + + +
When upgrading package, reset the values to the ones built into the chart, apply the last release's values and merge in any overrides from parameters release_values, values_files or set_values.
+
If reset_values or reuse_values is set to True, this is ignored.
+ +
diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 079285d7bc..d316a9dd30 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -145,6 +145,15 @@ required: false default: True version_added: 2.5.0 + reset_then_reuse_values: + description: + - When upgrading package, reset the values to the ones built into the chart, apply the last release's values and merge in any overrides from + parameters I(release_values), I(values_files) or I(set_values). + - If I(reset_values) or I(reuse_values) is set to C(True), this is ignored. + type: bool + required: false + default: False + version_added: 5.1.0 #Helm options disable_hook: @@ -509,6 +518,7 @@ def deploy( set_value_args=None, reuse_values=None, reset_values=True, + reset_then_reuse_values=False, ): """ Install/upgrade/rollback release chart @@ -526,6 +536,9 @@ def deploy( if reuse_values is not None: deploy_command += " --reuse-values=" + str(reuse_values) + if reset_then_reuse_values: + deploy_command += " --reset-then-reuse-values" + if wait: deploy_command += " --wait" if wait_timeout is not None: @@ -642,6 +655,7 @@ def helmdiff_check( set_value_args=None, reuse_values=None, reset_values=True, + reset_then_reuse_values=False, ): """ Use helm diff to determine if a release would change by upgrading a chart. @@ -676,6 +690,9 @@ def helmdiff_check( if reuse_values: cmd += " --reuse-values" + if reset_then_reuse_values: + cmd += " --reset-then-reuse-values" + rc, out, err = module.run_helm_command(cmd) return (len(out.strip()) > 0, out.strip()) @@ -735,6 +752,7 @@ def argument_spec(): 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), ) ) return arg_spec @@ -787,6 +805,7 @@ def main(): set_values = module.params.get("set_values") reuse_values = module.params.get("reuse_values") reset_values = module.params.get("reset_values") + reset_then_reuse_values = module.params.get("reset_then_reuse_values") if update_repo_cache: run_repo_update(module) @@ -883,6 +902,7 @@ def main(): set_value_args=set_value_args, reuse_values=reuse_values, reset_values=reset_values, + reset_then_reuse_values=reset_then_reuse_values, ) changed = True @@ -908,6 +928,7 @@ def main(): set_value_args, reuse_values=reuse_values, reset_values=reset_values, + reset_then_reuse_values=reset_then_reuse_values, ) if would_change and module._diff: opt_result["diff"] = {"prepared": prepared} @@ -943,6 +964,7 @@ def main(): set_value_args=set_value_args, reuse_values=reuse_values, reset_values=reset_values, + reset_then_reuse_values=reset_then_reuse_values, ) changed = True diff --git a/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml b/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml new file mode 100644 index 0000000000..236a69ae31 --- /dev/null +++ b/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml @@ -0,0 +1,77 @@ +--- +- name: Test helm reset_then_reuse_values + vars: + helm_namespace: "{{ test_namespace[9] }}" + chart_release_values: + replica: + replicaCount: 3 + master: + count: 1 + kind: Deployment + chart_reset_then_reuse_values: + replica: + replicaCount: 1 + master: + count: 3 + block: + - name: Initial chart installation + helm: + binary_path: "{{ helm_binary }}" + chart_ref: redis + chart_repo_url: https://charts.bitnami.com/bitnami + release_name: test-redis + release_namespace: "{{ helm_namespace }}" + create_namespace: true + release_values: "{{ chart_release_values }}" + register: install + + - name: Get value set as string + helm_info: + binary_path: "{{ helm_binary }}" + release_name: test-redis + release_namespace: "{{ helm_namespace }}" + register: release_value + + - name: Validate that chart values are as expected + assert: + that: + - install is changed + - '"--reset-then-reuse-values" not in install.command' + - release_value["status"]["values"] == chart_release_values + + - name: Upgrade chart using reset_then_reuse_values=true + helm: + binary_path: "{{ helm_binary }}" + chart_ref: redis + chart_repo_url: https://charts.bitnami.com/bitnami + release_name: test-redis + release_namespace: "{{ helm_namespace }}" + reuse_values: false + reset_values: false + reset_then_reuse_values: true + release_values: "{{ chart_reset_then_reuse_values }}" + register: upgrade + + - name: Get value set as string + helm_info: + binary_path: "{{ helm_binary }}" + release_name: test-redis + release_namespace: "{{ helm_namespace }}" + register: release_value + + - name: Validate that chart values are as expected + assert: + that: + - upgrade is changed + - '"--reset-then-reuse-values" in upgrade.command' + - '"--reuse-values" not in upgrade.command' + - '"--reset-values" not in upgrade.command' + - release_value["status"]["values"] == chart_release_values | combine(chart_reset_then_reuse_values, recursive=true) + + always: + - name: Remove helm namespace + k8s: + api_version: v1 + kind: Namespace + name: "{{ helm_namespace }}" + state: absent diff --git a/tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml b/tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml new file mode 100644 index 0000000000..44a68d7bb3 --- /dev/null +++ b/tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml @@ -0,0 +1,94 @@ +--- +- name: Create temporary directory for helm chart + tempfile: + suffix: .helm + state: directory + register: helm_dir + +- name: Test helm diff functionality + vars: + test_chart_path: "{{ helm_dir.path }}/test-chart-reuse-values" + test_release_name: "myrelease" + + block: + - name: Install helm diff + kubernetes.core.helm_plugin: + binary_path: "{{ helm_binary }}" + state: present + plugin_path: https://github.com/databus23/helm-diff + plugin_version: 3.9.4 + + - name: Copy test chart + ansible.builtin.copy: + src: "test-chart-reuse-values" + dest: "{{ helm_dir.path }}" + + - name: Create helm release + kubernetes.core.helm: + state: present + binary_path: "{{ helm_binary }}" + chart_ref: "{{ test_chart_path }}" + release_name: "{{ test_release_name }}" + release_namespace: "{{ helm_namespace }}" + create_namespace: true + release_values: + ansible_version: devel + phase: ci + wait: true + + - name: Upgrade helm release (reset_values=false and reuse_values=false and reset_then_reuse_values=true) + kubernetes.core.helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ test_chart_path }}" + reset_values: false + reuse_values: false + reset_then_reuse_values: true + release_name: "{{ test_release_name }}" + release_namespace: "{{ helm_namespace }}" + values: + ansible_version: devel + register: helm_upgrade + + - name: Ensure task did not reported change + assert: + that: + - helm_upgrade is not changed + + - name: Upgrade helm release (reset_then_reuse_values=true with default value for reset_values and reuse_values=false) + kubernetes.core.helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ test_chart_path }}" + reuse_values: false + reset_then_reuse_values: true + release_name: "{{ test_release_name }}" + release_namespace: "{{ helm_namespace }}" + values: + ansible_version: devel + register: helm_upgrade + + - name: Ensure task reported change + assert: + that: + - helm_upgrade is changed + + always: + - name: Remove temporary directory + file: + path: "{{ helm_dir.path }}" + state: absent + ignore_errors: true + + - name: Uninstall helm diff + kubernetes.core.helm_plugin: + binary_path: "{{ helm_binary }}" + state: absent + plugin_name: diff + ignore_errors: true + + - name: Remove helm namespace + kubernetes.core.k8s: + api_version: v1 + kind: Namespace + name: "{{ helm_namespace }}" + state: absent + ignore_errors: true From 71cf8cad3a9821da5a0b8cfac0dc706528547780 Mon Sep 17 00:00:00 2001 From: "david.bidorff@gmail.com" Date: Sun, 15 Dec 2024 12:53:41 +0100 Subject: [PATCH 2/6] fix: helm tests (based on #827) --- .../helm/tasks/test_helm_reset_then_reuse_values.yml.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml b/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml index 236a69ae31..982ad1030c 100644 --- a/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml +++ b/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml @@ -17,8 +17,7 @@ - name: Initial chart installation helm: binary_path: "{{ helm_binary }}" - chart_ref: redis - chart_repo_url: https://charts.bitnami.com/bitnami + chart_ref: oci://registry-1.docker.io/bitnamicharts/redis release_name: test-redis release_namespace: "{{ helm_namespace }}" create_namespace: true @@ -42,8 +41,7 @@ - name: Upgrade chart using reset_then_reuse_values=true helm: binary_path: "{{ helm_binary }}" - chart_ref: redis - chart_repo_url: https://charts.bitnami.com/bitnami + chart_ref: oci://registry-1.docker.io/bitnamicharts/redis release_name: test-redis release_namespace: "{{ helm_namespace }}" reuse_values: false From ac2052a6371007c05841c2b25032b2a86708032d Mon Sep 17 00:00:00 2001 From: b0z02003 <100960797+b0z02003@users.noreply.github.com> Date: Tue, 17 Dec 2024 17:03:47 +0100 Subject: [PATCH 3/6] fix: check helm version before using --reset-then-reuse-values Co-authored-by: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> --- plugins/modules/helm.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index d316a9dd30..c97f0ae068 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -148,8 +148,9 @@ reset_then_reuse_values: description: - When upgrading package, reset the values to the ones built into the chart, apply the last release's values and merge in any overrides from - parameters I(release_values), I(values_files) or I(set_values). - - If I(reset_values) or I(reuse_values) is set to C(True), this is ignored. + parameters O(release_values), O(values_files) or O(set_values). + - If O(reset_values) or O(reuse_values) is set to V(True), this is ignored. + - This feature requires helm diff >= 3.9.12. type: bool required: false default: False @@ -537,7 +538,11 @@ def deploy( deploy_command += " --reuse-values=" + str(reuse_values) if reset_then_reuse_values: - deploy_command += " --reset-then-reuse-values" + helm_diff_version = get_plugin_version("diff") + if LooseVersion(helm_diff_version) < LooseVersion("3.9.12"): + module.warn("helm diff support option --reset-then-reuse-values starting release >= 3.9.12") + else: + deploy_command += " --reset-then-reuse-values" if wait: deploy_command += " --wait" From 847d64a88700ab9b15ea6472e4bdeea4783ef519 Mon Sep 17 00:00:00 2001 From: David Bidorff Date: Tue, 17 Dec 2024 17:16:57 +0100 Subject: [PATCH 4/6] fix: improves tests --- tests/integration/targets/helm/tasks/run_test.yml | 3 +++ ...se_values.yml.yml => test_helm_reset_then_reuse_values.yml} | 0 tests/integration/targets/helm_diff/tasks/main.yml | 2 ++ ...t_then_reuse_values.yml.yml => reset_then_reuse_values.yml} | 0 4 files changed, 5 insertions(+) rename tests/integration/targets/helm/tasks/{test_helm_reset_then_reuse_values.yml.yml => test_helm_reset_then_reuse_values.yml} (100%) rename tests/integration/targets/helm_diff/tasks/{reset_then_reuse_values.yml.yml => reset_then_reuse_values.yml} (100%) diff --git a/tests/integration/targets/helm/tasks/run_test.yml b/tests/integration/targets/helm/tasks/run_test.yml index 76d55b22ca..522122306d 100644 --- a/tests/integration/targets/helm/tasks/run_test.yml +++ b/tests/integration/targets/helm/tasks/run_test.yml @@ -28,6 +28,9 @@ - name: test helm upgrade with reuse_values include_tasks: test_helm_reuse_values.yml +- name: test helm upgrade with reset_then_reuse_values + include_tasks: test_helm_reset_then_reuse_values.yml + - name: test helm dependency update include_tasks: test_up_dep.yml diff --git a/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml b/tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml similarity index 100% rename from tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml.yml rename to tests/integration/targets/helm/tasks/test_helm_reset_then_reuse_values.yml diff --git a/tests/integration/targets/helm_diff/tasks/main.yml b/tests/integration/targets/helm_diff/tasks/main.yml index c37d947f5f..8c30142eca 100644 --- a/tests/integration/targets/helm_diff/tasks/main.yml +++ b/tests/integration/targets/helm_diff/tasks/main.yml @@ -302,3 +302,5 @@ ignore_errors: yes - include_tasks: reuse_values.yml + +- include_tasks: reset_then_reuse_values.yml diff --git a/tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml b/tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml similarity index 100% rename from tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml.yml rename to tests/integration/targets/helm_diff/tasks/reset_then_reuse_values.yml From aaee3ddb778050266ed9ee60e8c8f641edd24dbb Mon Sep 17 00:00:00 2001 From: David Bidorff Date: Tue, 17 Dec 2024 17:17:07 +0100 Subject: [PATCH 5/6] fix: improve versions checks --- plugins/modules/helm.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index c97f0ae068..23dbf83065 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -538,9 +538,9 @@ def deploy( deploy_command += " --reuse-values=" + str(reuse_values) if reset_then_reuse_values: - helm_diff_version = get_plugin_version("diff") - if LooseVersion(helm_diff_version) < LooseVersion("3.9.12"): - module.warn("helm diff support option --reset-then-reuse-values starting release >= 3.9.12") + helm_version = module.get_helm_version() + if LooseVersion(helm_version) < LooseVersion("3.14.0"): + module.warn("helm support option --reset-then-reuse-values starting release >= 3.14.0") else: deploy_command += " --reset-then-reuse-values" @@ -696,7 +696,14 @@ def helmdiff_check( cmd += " --reuse-values" if reset_then_reuse_values: - cmd += " --reset-then-reuse-values" + helm_diff_version = get_plugin_version("diff") + helm_version = module.get_helm_version() + if LooseVersion(helm_diff_version) < LooseVersion("3.9.12"): + module.warn("helm diff support option --reset-then-reuse-values starting release >= 3.9.12") + elif LooseVersion(helm_version) < LooseVersion("3.14.0"): + module.warn("helm support option --reset-then-reuse-values starting release >= 3.14.0") + else: + cmd += " --reset-then-reuse-values" rc, out, err = module.run_helm_command(cmd) return (len(out.strip()) > 0, out.strip()) From 2f449385c161a6b3a3198a322c9c80d647f5dcaf Mon Sep 17 00:00:00 2001 From: b0z02003 <100960797+b0z02003@users.noreply.github.com> Date: Sun, 22 Dec 2024 19:06:07 +0100 Subject: [PATCH 6/6] Apply suggestions from code review fix: linter issues Co-authored-by: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> --- plugins/modules/helm.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 23dbf83065..b1f4a8e8f2 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -540,7 +540,9 @@ def deploy( if reset_then_reuse_values: helm_version = module.get_helm_version() if LooseVersion(helm_version) < LooseVersion("3.14.0"): - module.warn("helm support option --reset-then-reuse-values starting release >= 3.14.0") + module.warn( + "helm support option --reset-then-reuse-values starting release >= 3.14.0" + ) else: deploy_command += " --reset-then-reuse-values" @@ -699,9 +701,13 @@ def helmdiff_check( helm_diff_version = get_plugin_version("diff") helm_version = module.get_helm_version() if LooseVersion(helm_diff_version) < LooseVersion("3.9.12"): - module.warn("helm diff support option --reset-then-reuse-values starting release >= 3.9.12") + module.warn( + "helm diff support option --reset-then-reuse-values starting release >= 3.9.12" + ) elif LooseVersion(helm_version) < LooseVersion("3.14.0"): - module.warn("helm support option --reset-then-reuse-values starting release >= 3.14.0") + module.warn( + "helm support option --reset-then-reuse-values starting release >= 3.14.0" + ) else: cmd += " --reset-then-reuse-values"