Skip to content

Commit

Permalink
helm: Support for single or multiple values files (ansible-collection…
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy authored Oct 5, 2020
1 parent 0f33aaf commit fc1f4e5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions molecule/default/roles/helm/files/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
revisionHistoryLimit: 0
19 changes: 19 additions & 0 deletions molecule/default/roles/helm/tasks/tests_chart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@
that:
- install is changed

- name: "Install {{ chart_test }} from {{ source }} with values_files"
helm:
binary_path: "{{ helm_binary }}"
name: test
chart_ref: "{{ chart_source }}"
chart_version: "{{ chart_source_version | default(omit) }}"
namespace: "{{ helm_namespace }}"
values_files:
- "{{ role_path }}/files/values.yaml"
register: install

- name: "Assert that {{ chart_test }} chart has var from {{ source }}"
assert:
that:
- install is changed
- install.status.status | lower == 'deployed'
- install.status.chart == "{{ chart_test }}-{{ chart_test_version }}"
- "install.status['values'].revisionHistoryLimit == 0"

- name: Remove helm namespace
k8s:
api_version: v1
Expand Down
37 changes: 31 additions & 6 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@
default: {}
aliases: [ values ]
type: dict
values_files:
description:
- Value files to pass to chart.
- Paths will be read from the target host's filesystem, not the host running ansible.
- values_files option is evaluated before values option if both are used.
- Paths are evaluated in the order the paths are specified.
required: false
default: []
type: list
elements: str
version_added: '1.1.0'
update_repo_cache:
description:
- Run C(helm repo update) before the operation. Can be run as part of the package installation or as a separate step.
Expand Down Expand Up @@ -155,6 +166,14 @@
chart_version: 5.0.12
values: "{{ lookup('template', 'somefile.yaml') | from_yaml }}"
- name: Deploy Grafana chart using values files on target
community.kubernetes.helm:
name: test
chart_ref: stable/grafana
release_namespace: monitoring
values_files:
- /path/to/values.yaml
- name: Remove test release and waiting suppression ending
community.kubernetes.helm:
name: test
Expand Down Expand Up @@ -328,8 +347,8 @@ def fetch_chart_info(command, chart_ref):


def deploy(command, release_name, release_values, chart_name, wait,
wait_timeout, disable_hook, force, atomic=False, create_namespace=False,
replace=False):
wait_timeout, disable_hook, force, values_files, atomic=False,
create_namespace=False, replace=False):
"""
Install/upgrade/rollback release chart
"""
Expand Down Expand Up @@ -362,6 +381,10 @@ def deploy(command, release_name, release_values, chart_name, wait,
if create_namespace:
deploy_command += " --create-namespace"

if values_files:
for value_file in values_files:
deploy_command += " --values=" + value_file

if release_values != {}:
fd, path = tempfile.mkstemp(suffix='.yml')
with open(path, 'w') as yaml_file:
Expand Down Expand Up @@ -403,6 +426,7 @@ def main():
release_namespace=dict(type='str', required=True, aliases=['namespace']),
release_state=dict(default='present', choices=['present', 'absent'], aliases=['state']),
release_values=dict(type='dict', default={}, aliases=['values']),
values_files=dict(type='list', default=[], elements='str'),
update_repo_cache=dict(type='bool', default=False),

# Helm options
Expand Down Expand Up @@ -437,6 +461,7 @@ def main():
release_namespace = module.params.get('release_namespace')
release_state = module.params.get('release_state')
release_values = module.params.get('release_values')
values_files = module.params.get('values_files')
update_repo_cache = module.params.get('update_repo_cache')

# Helm options
Expand Down Expand Up @@ -491,15 +516,15 @@ def main():

if release_status is None: # Not installed
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout,
disable_hook, False, atomic=atomic, create_namespace=create_namespace,
replace=replace)
disable_hook, False, values_files=values_files, atomic=atomic,
create_namespace=create_namespace, replace=replace)
changed = True

elif force or release_values != release_status['values'] \
or (chart_info['name'] + '-' + chart_info['version']) != release_status["chart"]:
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout,
disable_hook, force, atomic=atomic, create_namespace=create_namespace,
replace=replace)
disable_hook, force, values_files=values_files, atomic=atomic,
create_namespace=create_namespace, replace=replace)
changed = True

if module.check_mode:
Expand Down

0 comments on commit fc1f4e5

Please sign in to comment.