From f24019146c1df6b6c562fd259675ef990b355059 Mon Sep 17 00:00:00 2001 From: abikouo Date: Wed, 28 Feb 2024 11:56:08 +0100 Subject: [PATCH 1/4] helm - Add reuse-values when running helm diff --- ...240228-fix-helm-diff-with-reuse-values.yml | 3 + plugins/modules/helm.py | 9 ++- .../files/test-chart-reuse-values/Chart.yaml | 24 ++++++ .../templates/configmap.yaml | 7 ++ .../files/test-chart-reuse-values/values.yaml | 2 + .../targets/helm_diff/tasks/main.yml | 2 + .../targets/helm_diff/tasks/reuse_values.yml | 78 +++++++++++++++++++ 7 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/20240228-fix-helm-diff-with-reuse-values.yml create mode 100644 tests/integration/targets/helm_diff/files/test-chart-reuse-values/Chart.yaml create mode 100644 tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml create mode 100644 tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml create mode 100644 tests/integration/targets/helm_diff/tasks/reuse_values.yml diff --git a/changelogs/fragments/20240228-fix-helm-diff-with-reuse-values.yml b/changelogs/fragments/20240228-fix-helm-diff-with-reuse-values.yml new file mode 100644 index 0000000000..b38c103c35 --- /dev/null +++ b/changelogs/fragments/20240228-fix-helm-diff-with-reuse-values.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - helm - use ``reuse-values`` when running ``helm diff`` command (https://github.com/ansible-collections/kubernetes.core/issues/680). diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 11f323c4b1..da27fef946 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -640,6 +640,8 @@ def helmdiff_check( chart_repo_url=None, post_renderer=False, set_value_args=None, + reuse_values=None, + reset_values=True, ): """ Use helm diff to determine if a release would change by upgrading a chart. @@ -653,7 +655,7 @@ def helmdiff_check( if chart_version is not None: cmd += " " + "--version=" + chart_version if not replace: - cmd += " " + "--reset-values" + cmd += " " + "--reset-values=" + str(reset_values) if post_renderer: cmd += " --post-renderer=" + post_renderer @@ -671,6 +673,9 @@ def helmdiff_check( if set_value_args: cmd += " " + set_value_args + if reuse_values: + cmd += " --reuse-values" + rc, out, err = module.run_helm_command(cmd) return (len(out.strip()) > 0, out.strip()) @@ -901,6 +906,8 @@ def main(): chart_repo_url, post_renderer, set_value_args, + reuse_values=reuse_values, + reset_values=reset_values, ) if would_change and module._diff: opt_result["diff"] = {"prepared": prepared} diff --git a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/Chart.yaml b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/Chart.yaml new file mode 100644 index 0000000000..c3681dba0f --- /dev/null +++ b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: chart-reuse-values +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml new file mode 100644 index 0000000000..bb97476061 --- /dev/null +++ b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: cmap +data: + ansible_version: {{ .Values.ansible_version }} + python_version: {{ .Values.python_version }} diff --git a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml new file mode 100644 index 0000000000..05062120fe --- /dev/null +++ b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml @@ -0,0 +1,2 @@ +ansible_version: 2.16 +python_version: 3.9 diff --git a/tests/integration/targets/helm_diff/tasks/main.yml b/tests/integration/targets/helm_diff/tasks/main.yml index f7df479031..6d38eba69b 100644 --- a/tests/integration/targets/helm_diff/tasks/main.yml +++ b/tests/integration/targets/helm_diff/tasks/main.yml @@ -299,3 +299,5 @@ name: "{{ helm_namespace }}" state: absent ignore_errors: yes + +- include_tasks: reuse_values.yml diff --git a/tests/integration/targets/helm_diff/tasks/reuse_values.yml b/tests/integration/targets/helm_diff/tasks/reuse_values.yml new file mode 100644 index 0000000000..349e3f79ec --- /dev/null +++ b/tests/integration/targets/helm_diff/tasks/reuse_values.yml @@ -0,0 +1,78 @@ +--- +- 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 }}/chart" + helm_namespace: "helm-reuse-values-001" + test_release_name: "myrelease" + + block: + + - name: Install helm diff + 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 + copy: + src: "test-chart-reuse-values" + dest: "{{ test_chart_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 + python_version: 3.11 + wait: true + + - name: Upgrade helm release + kubernetes.core.helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ test_chart_path }}" + reset_values: false + 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 + + always: + - name: Remove temporary directory + file: + path: "{{ helm_dir.path }}" + state: absent + ignore_errors: true + + - name: Uninstall helm diff + helm_plugin: + binary_path: "{{ helm_binary }}" + state: absent + plugin_name: diff + ignore_errors: true + + - name: Remove helm namespace + k8s: + api_version: v1 + kind: Namespace + name: "{{ helm_namespace }}" + state: absent + ignore_errors: true From 092c56cd73140098931a81fcec8e8ee6034168f2 Mon Sep 17 00:00:00 2001 From: abikouo Date: Wed, 28 Feb 2024 12:03:51 +0100 Subject: [PATCH 2/4] fix sanity tests --- tests/sanity/ignore-2.14.txt | 1 + tests/sanity/ignore-2.15.txt | 1 + tests/sanity/ignore-2.16.txt | 1 + tests/sanity/ignore-2.17.txt | 1 + 4 files changed, 4 insertions(+) diff --git a/tests/sanity/ignore-2.14.txt b/tests/sanity/ignore-2.14.txt index 40386eb7e1..5f1713d7dc 100644 --- a/tests/sanity/ignore-2.14.txt +++ b/tests/sanity/ignore-2.14.txt @@ -24,3 +24,4 @@ plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error plugins/modules/k8s_taint.py validate-modules:return-syntax-error tests/integration/targets/k8s_delete/files/deployments.yaml yamllint!skip +tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.15.txt b/tests/sanity/ignore-2.15.txt index d9115d8a02..7eee4a23c7 100644 --- a/tests/sanity/ignore-2.15.txt +++ b/tests/sanity/ignore-2.15.txt @@ -25,3 +25,4 @@ plugins/modules/k8s.py validate-modules:return-syntax-error plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error plugins/modules/k8s_taint.py validate-modules:return-syntax-error +tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.16.txt b/tests/sanity/ignore-2.16.txt index 7d9081f47f..c154baaf2d 100644 --- a/tests/sanity/ignore-2.16.txt +++ b/tests/sanity/ignore-2.16.txt @@ -28,3 +28,4 @@ plugins/modules/k8s.py validate-modules:return-syntax-error plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error plugins/modules/k8s_taint.py validate-modules:return-syntax-error +tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml yamllint!skip diff --git a/tests/sanity/ignore-2.17.txt b/tests/sanity/ignore-2.17.txt index 7d9081f47f..c154baaf2d 100644 --- a/tests/sanity/ignore-2.17.txt +++ b/tests/sanity/ignore-2.17.txt @@ -28,3 +28,4 @@ plugins/modules/k8s.py validate-modules:return-syntax-error plugins/modules/k8s_scale.py validate-modules:return-syntax-error plugins/modules/k8s_service.py validate-modules:return-syntax-error plugins/modules/k8s_taint.py validate-modules:return-syntax-error +tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml yamllint!skip From 40d5de6f74104315b29af6c3d9bc1ee6ed9ab77a Mon Sep 17 00:00:00 2001 From: abikouo Date: Wed, 28 Feb 2024 12:42:32 +0100 Subject: [PATCH 3/4] fix integration test --- .../templates/configmap.yaml | 2 +- .../files/test-chart-reuse-values/values.yaml | 4 +-- .../targets/helm_diff/tasks/main.yml | 1 + .../targets/helm_diff/tasks/reuse_values.yml | 25 +++++++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml index bb97476061..4bbad8d690 100644 --- a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml +++ b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/templates/configmap.yaml @@ -4,4 +4,4 @@ metadata: name: cmap data: ansible_version: {{ .Values.ansible_version }} - python_version: {{ .Values.python_version }} + phase: {{ .Values.phase }} diff --git a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml index 05062120fe..f2cd9d72ba 100644 --- a/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml +++ b/tests/integration/targets/helm_diff/files/test-chart-reuse-values/values.yaml @@ -1,2 +1,2 @@ -ansible_version: 2.16 -python_version: 3.9 +ansible_version: milestone +phase: uat diff --git a/tests/integration/targets/helm_diff/tasks/main.yml b/tests/integration/targets/helm_diff/tasks/main.yml index 6d38eba69b..c37d947f5f 100644 --- a/tests/integration/targets/helm_diff/tasks/main.yml +++ b/tests/integration/targets/helm_diff/tasks/main.yml @@ -298,6 +298,7 @@ kind: Namespace name: "{{ helm_namespace }}" state: absent + wait: true ignore_errors: yes - include_tasks: reuse_values.yml diff --git a/tests/integration/targets/helm_diff/tasks/reuse_values.yml b/tests/integration/targets/helm_diff/tasks/reuse_values.yml index 349e3f79ec..4f00cba159 100644 --- a/tests/integration/targets/helm_diff/tasks/reuse_values.yml +++ b/tests/integration/targets/helm_diff/tasks/reuse_values.yml @@ -7,8 +7,7 @@ - name: Test helm diff functionality vars: - test_chart_path: "{{ helm_dir.path }}/chart" - helm_namespace: "helm-reuse-values-001" + test_chart_path: "{{ helm_dir.path }}/test-chart-reuse-values" test_release_name: "myrelease" block: @@ -23,7 +22,7 @@ - name: Copy test chart copy: src: "test-chart-reuse-values" - dest: "{{ test_chart_path }}" + dest: "{{ helm_dir.path }}" - name: Create helm release kubernetes.core.helm: @@ -35,10 +34,10 @@ create_namespace: true release_values: ansible_version: devel - python_version: 3.11 + phase: ci wait: true - - name: Upgrade helm release + - name: Upgrade helm release (reset_values=false and reuse_values=true) kubernetes.core.helm: binary_path: "{{ helm_binary }}" chart_ref: "{{ test_chart_path }}" @@ -55,6 +54,22 @@ that: - helm_upgrade is not changed + - name: Upgrade helm release (reuse_values=true with default value for reset_values) + kubernetes.core.helm: + binary_path: "{{ helm_binary }}" + chart_ref: "{{ test_chart_path }}" + 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: From acaadc0c7cc9441a2f6ce535898ef5738ecabc28 Mon Sep 17 00:00:00 2001 From: Bikouo Aubin <79859644+abikouo@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:10:14 +0100 Subject: [PATCH 4/4] use module FQCN on integration tests --- .../integration/targets/helm_diff/tasks/reuse_values.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration/targets/helm_diff/tasks/reuse_values.yml b/tests/integration/targets/helm_diff/tasks/reuse_values.yml index 4f00cba159..f2d80a8f4c 100644 --- a/tests/integration/targets/helm_diff/tasks/reuse_values.yml +++ b/tests/integration/targets/helm_diff/tasks/reuse_values.yml @@ -13,14 +13,14 @@ block: - name: Install helm diff - helm_plugin: + 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 - copy: + ansible.builtin.copy: src: "test-chart-reuse-values" dest: "{{ helm_dir.path }}" @@ -78,14 +78,14 @@ ignore_errors: true - name: Uninstall helm diff - helm_plugin: + kubernetes.core.helm_plugin: binary_path: "{{ helm_binary }}" state: absent plugin_name: diff ignore_errors: true - name: Remove helm namespace - k8s: + kubernetes.core.k8s: api_version: v1 kind: Namespace name: "{{ helm_namespace }}"