diff --git a/changelogs/fragments/389-helm-add-support-chart_repo_url-on-helm_diff.yml b/changelogs/fragments/389-helm-add-support-chart_repo_url-on-helm_diff.yml new file mode 100644 index 0000000000..3999721eb0 --- /dev/null +++ b/changelogs/fragments/389-helm-add-support-chart_repo_url-on-helm_diff.yml @@ -0,0 +1,3 @@ +--- +minor_changes: +- helm - support repo location for helm diff (https://github.com/ansible-collections/kubernetes.core/issues/174). diff --git a/molecule/default/roles/helm/tasks/tests_helm_diff.yml b/molecule/default/roles/helm/tasks/tests_helm_diff.yml index 4ba3f9af51..fdeddfd0a7 100644 --- a/molecule/default/roles/helm/tasks/tests_helm_diff.yml +++ b/molecule/default/roles/helm/tasks/tests_helm_diff.yml @@ -9,6 +9,7 @@ binary_path: "{{ helm_binary }}" state: present plugin_path: https://github.com/databus23/helm-diff + plugin_version: 3.4.0 - name: Copy test chart copy: @@ -160,6 +161,79 @@ that: - install is not changed + # Test helm diff with chart_repo_url + - name: Define Redis chart values + set_fact: + redis_chart_values: + commonLabels: + phase: testing + company: RedHat + image: + tag: 6.2.6-debian-10-r135 + architecture: standalone + + - name: Install Redis chart + helm: + binary_path: "{{ helm_binary }}" + chart_repo_url: https://charts.bitnami.com/bitnami + chart_ref: redis + namespace: "{{ helm_namespace }}" + name: redis-chart + chart_version: '16.0.0' + release_values: "{{ redis_chart_values }}" + + - name: Upgrade Redis chart + helm: + binary_path: "{{ helm_binary }}" + chart_repo_url: https://charts.bitnami.com/bitnami + chart_ref: redis + namespace: "{{ helm_namespace }}" + name: redis-chart + chart_version: '16.0.0' + release_values: "{{ redis_chart_values }}" + check_mode: yes + register: redis_upgrade + + - name: Assert that module raised a warning + assert: + that: + - not redis_upgrade.changed + - redis_upgrade.warnings is defined + - redis_upgrade.warnings | length == 1 + - redis_upgrade.warnings[0] == "The default idempotency check can fail to report changes in certain cases. Install helm diff >= 3.4.1 for better results." + + - name: Uninstall helm diff + helm_plugin: + binary_path: "{{ helm_binary }}" + state: absent + plugin_name: diff + ignore_errors: yes + + - name: Install helm diff (version=3.4.1) + helm_plugin: + binary_path: "{{ helm_binary }}" + state: present + plugin_path: https://github.com/databus23/helm-diff + plugin_version: 3.4.1 + + - name: Upgrade Redis chart once again + helm: + binary_path: "{{ helm_binary }}" + chart_repo_url: https://charts.bitnami.com/bitnami + chart_ref: redis + namespace: "{{ helm_namespace }}" + name: redis-chart + chart_version: '16.0.0' + release_values: "{{ redis_chart_values }}" + check_mode: yes + register: redis_upgrade_2 + + - name: Assert that module raised a warning + assert: + that: + - redis_upgrade_2.changed + - redis_upgrade_2.warnings is not defined + always: - name: Remove chart directory file: diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index ca6b7712cf..ac17ce9150 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -27,6 +27,10 @@ description: - Install, upgrade, delete packages with the Helm package manager. +notes: + - The default idempotency check can fail to report changes when C(release_state) is set to C(present) + and C(chart_repo_url) is defined. Install helm diff >= 3.4.1 for better results. + options: chart_ref: description: @@ -495,9 +499,9 @@ def load_values_files(values_files): return values -def has_plugin(command, plugin): +def get_plugin_version(command, plugin): """ - Check if helm plugin is installed. + Check if helm plugin is installed and return corresponding version """ cmd = command + " plugin" @@ -505,12 +509,12 @@ def has_plugin(command, plugin): out = parse_helm_plugin_list(module, output=output.splitlines()) if not out: - return False + return None for line in out: if line[0] == plugin: - return True - return False + return line[1] + return None def helmdiff_check( @@ -522,6 +526,7 @@ def helmdiff_check( values_files=None, chart_version=None, replace=False, + chart_repo_url=None, ): """ Use helm diff to determine if a release would change by upgrading a chart. @@ -530,6 +535,8 @@ def helmdiff_check( cmd += " " + release_name cmd += " " + chart_ref + if chart_repo_url is not None: + cmd += " " + "--repo=" + chart_repo_url if chart_version is not None: cmd += " " + "--version=" + chart_version if not replace: @@ -733,7 +740,14 @@ def main(): else: - if has_plugin(helm_cmd_common, "diff") and not chart_repo_url: + helm_diff_version = get_plugin_version(helm_cmd_common, "diff") + if helm_diff_version and ( + not chart_repo_url + or ( + chart_repo_url + and LooseVersion(helm_diff_version) >= LooseVersion("3.4.1") + ) + ): (would_change, prepared) = helmdiff_check( module, helm_cmd_common, @@ -743,13 +757,14 @@ def main(): values_files, chart_version, replace, + chart_repo_url, ) if would_change and module._diff: opt_result["diff"] = {"prepared": prepared} else: module.warn( "The default idempotency check can fail to report changes in certain cases. " - "Install helm diff for better results." + "Install helm diff >= 3.4.1 for better results." ) would_change = default_check( release_status, chart_info, release_values, values_files