Skip to content

Commit

Permalink
helm - add support for repo location when running helm diff (#389)
Browse files Browse the repository at this point in the history
helm - add support for repo location when running helm diff

SUMMARY

closes #174

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

helm

Reviewed-by: Abhijeet Kasurde <None>
Reviewed-by: None <None>
Reviewed-by: Mike Graves <mgraves@redhat.com>
  • Loading branch information
abikouo authored Feb 22, 2022
1 parent 44c8cff commit 7031829
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- helm - support repo location for helm diff (https://github.com/ansible-collections/kubernetes.core/issues/174).
74 changes: 74 additions & 0 deletions molecule/default/roles/helm/tasks/tests_helm_diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 22 additions & 7 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -495,22 +499,22 @@ 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"
rc, output, err = get_helm_plugin_list(module, helm_bin=cmd)
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(
Expand All @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 7031829

Please sign in to comment.