Skip to content

Commit

Permalink
add reuse_values and reset_values support to helm module (#575)
Browse files Browse the repository at this point in the history
helm - add reuse_values and reset_values support

SUMMARY

closes #394

ISSUE TYPE


Feature Pull Request

COMPONENT NAME

helm
ADDITIONAL INFORMATION

Reviewed-by: Mike Graves <[email protected]>
  • Loading branch information
abikouo authored Feb 16, 2023
1 parent 2276449 commit 031cc7c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- helm - add reuse_values and reset_values support to helm module (https://github.com/ansible-collections/kubernetes.core/issues/394).
42 changes: 40 additions & 2 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@
- json
- file
version_added: '2.4.0'
reuse_values:
description:
- When upgrading package, specifies wether to reuse 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) is set to C(True), this is ignored.
type: bool
required: false
version_added: '2.5.0'
reset_values:
description:
- When upgrading package, reset the values to the ones built into the chart.
type: bool
required: false
default: True
version_added: '2.5.0'
#Helm options
disable_hook:
Expand Down Expand Up @@ -310,6 +325,17 @@
enabled: True
logging:
enabled: True
# Deploy latest version
- name: Deploy latest version of Grafana chart using reuse_values
kubernetes.core.helm:
name: test
chart_ref: stable/grafana
release_namespace: monitoring
reuse_values: true
values:
replicas: 2
version: 3e8ec0b2dffa40fb97d5342e4af887de95faa8c61a62480dd7f8aa03dffcf533
"""

RETURN = r"""
Expand Down Expand Up @@ -474,6 +500,8 @@ def deploy(
timeout=None,
dependency_update=None,
set_value_args=None,
reuse_values=None,
reset_values=True,
):
"""
Install/upgrade/rollback release chart
Expand All @@ -485,9 +513,11 @@ def deploy(
deploy_command += " --dependency-update"
else:
deploy_command = command + " upgrade -i" # install/upgrade
if reset_values:
deploy_command += " --reset-values"

# Always reset values to keep release_values equal to values released
deploy_command += " --reset-values"
if reuse_values is not None:
deploy_command += " --reuse-values=" + str(reuse_values)

if wait:
deploy_command += " --wait"
Expand Down Expand Up @@ -684,6 +714,8 @@ def argument_spec():
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),
)
)
return arg_spec
Expand Down Expand Up @@ -734,6 +766,8 @@ def main():
history_max = module.params.get("history_max")
timeout = module.params.get("timeout")
set_values = module.params.get("set_values")
reuse_values = module.params.get("reuse_values")
reset_values = module.params.get("reset_values")

if update_repo_cache:
run_repo_update(module)
Expand Down Expand Up @@ -826,6 +860,8 @@ def main():
history_max=history_max,
timeout=timeout,
set_value_args=set_value_args,
reuse_values=reuse_values,
reset_values=reset_values,
)
changed = True

Expand Down Expand Up @@ -885,6 +921,8 @@ def main():
timeout=timeout,
dependency_update=dependency_update,
set_value_args=set_value_args,
reuse_values=reuse_values,
reset_values=reset_values,
)
changed = True

Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/helm/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ test_namespace:
- "helm-local-path-003"
- "helm-from-repository"
- "helm-from-url"
- "helm-reuse-values"
3 changes: 3 additions & 0 deletions tests/integration/targets/helm/tasks/run_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
- from_repository
- from_url

- name: test helm upgrade with reuse_values
include_tasks: test_helm_reuse_values.yml

- name: test helm dependency update
include_tasks: test_up_dep.yml

Expand Down
75 changes: 75 additions & 0 deletions tests/integration/targets/helm/tasks/test_helm_reuse_values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
- name: Test helm reuse_values
vars:
helm_namespace: "{{ test_namespace[9] }}"
chart_release_values:
replica:
replicaCount: 3
master:
count: 1
kind: Deployment
chart_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
- '"--reuse-values=True" not in install.command'
- release_value["status"]["values"] == chart_release_values

- name: Upgrade chart using 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: true
reset_values: false
release_values: "{{ chart_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
- '"--reuse-values=True" in upgrade.command'
- '"--reset-values" not in upgrade.command'
- release_value["status"]["values"] == chart_release_values | combine(chart_reuse_values, recursive=true)

always:
- name: Remove helm namespace
k8s:
api_version: v1
kind: Namespace
name: "{{ helm_namespace }}"
state: absent

0 comments on commit 031cc7c

Please sign in to comment.