From 6db9f8fa4ed2e2289d069593cb5feaf4bb82252a Mon Sep 17 00:00:00 2001 From: LucasBoisserie Date: Sat, 4 Jul 2020 19:18:51 +0200 Subject: [PATCH 1/2] Add param to create ns with helm --- molecule/default/roles/helm/tasks/main.yml | 2 +- .../default/roles/helm/tasks/tests_chart.yml | 24 +++++++++++----- plugins/modules/helm.py | 28 +++++++++++++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/molecule/default/roles/helm/tasks/main.yml b/molecule/default/roles/helm/tasks/main.yml index 458775de..e86d33df 100644 --- a/molecule/default/roles/helm/tasks/main.yml +++ b/molecule/default/roles/helm/tasks/main.yml @@ -4,4 +4,4 @@ loop_control: loop_var: helm_version with_items: - - "v3.1.2" + - "v3.2.4" diff --git a/molecule/default/roles/helm/tasks/tests_chart.yml b/molecule/default/roles/helm/tasks/tests_chart.yml index eb6d518f..b0341330 100644 --- a/molecule/default/roles/helm/tasks/tests_chart.yml +++ b/molecule/default/roles/helm/tasks/tests_chart.yml @@ -1,11 +1,4 @@ --- -- name: Create helm namespace - k8s: - api_version: v1 - kind: Namespace - name: "{{ helm_namespace }}" - wait: true - - name: Check helm_info empty helm_info: binary_path: "{{ helm_binary }}" @@ -18,6 +11,22 @@ that: - empty_info.status is undefined +- name: "Install fail {{ chart_test }} from {{ source }}" + helm: + binary_path: "{{ helm_binary }}" + name: test + chart_ref: "{{ chart_source }}" + chart_version: "{{ chart_source_version | default(omit) }}" + namespace: "{{ helm_namespace }}" + ignore_errors: yes + register: install_fail + +- name: "Assert that Install fail {{ chart_test }} from {{ source }}" + assert: + that: + - install_fail is failed + - "'Error: create: failed to create: namespaces \"' + helm_namespace + '\" not found' in install_fail.stderr" + - name: "Install {{ chart_test }} from {{ source }}" helm: binary_path: "{{ helm_binary }}" @@ -25,6 +34,7 @@ chart_ref: "{{ chart_source }}" chart_version: "{{ chart_source_version | default(omit) }}" namespace: "{{ helm_namespace }}" + create_namespace: true register: install - name: "Assert that {{ chart_test }} chart is installed from {{ source }}" diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index 9ca1beed..f67516c9 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -125,15 +125,20 @@ - If set, the installation process deletes the installation on failure. type: bool default: False + create_namespace: + description: + - Create the release namespace if not present + type: bool + default: False ''' EXAMPLES = r''' -- name: Create helm namespace as HELM 3 doesn't create it automatically - community.kubernetes.k8s: - api_version: v1 - kind: Namespace - name: "monitoring" - wait: true +- name: Deploy latest version of Prometheus chart inside monitoring namespace (and create it) + community.kubernetes.helm: + name: test + chart_ref: stable/prometheus + release_namespace: monitoring + create_namespace: true # From repository - name: Add stable chart repo @@ -328,7 +333,7 @@ def fetch_chart_info(command, chart_ref): return yaml.safe_load(out) -def deploy(command, release_name, release_values, chart_name, wait, wait_timeout, disable_hook, force, atomic=False): +def deploy(command, release_name, release_values, chart_name, wait, wait_timeout, disable_hook, force, atomic=False, create_namespace=False): """ Install/upgrade/rollback release chart """ @@ -351,6 +356,9 @@ def deploy(command, release_name, release_values, chart_name, wait, wait_timeout if disable_hook: deploy_command += " --no-hooks" + if create_namespace: + deploy_command += " --create-namespace" + if release_values != {}: fd, path = tempfile.mkstemp(suffix='.yml') with open(path, 'w') as yaml_file: @@ -403,6 +411,7 @@ def main(): wait=dict(type='bool', default=False), wait_timeout=dict(type='str'), atomic=dict(type='bool', default=False), + create_namespace=dict(type='bool', default=False), ), required_if=[ ('release_state', 'present', ['release_name', 'chart_ref']), @@ -435,6 +444,7 @@ def main(): wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') atomic = module.params.get('atomic') + create_namespace = module.params.get('create_namespace') if bin_path is not None: helm_cmd_common = bin_path @@ -473,13 +483,13 @@ 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) + disable_hook, False, atomic=atomic, create_namespace=create_namespace) 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) + disable_hook, force, atomic=atomic, create_namespace=create_namespace) changed = True if module.check_mode: From f7b69d2f717b318cada80874016a88082845f934 Mon Sep 17 00:00:00 2001 From: LucasBoisserie Date: Fri, 10 Jul 2020 10:20:00 +0200 Subject: [PATCH 2/2] fix review --- plugins/modules/helm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/helm.py b/plugins/modules/helm.py index f67516c9..bbf362a4 100644 --- a/plugins/modules/helm.py +++ b/plugins/modules/helm.py @@ -127,9 +127,10 @@ default: False create_namespace: description: - - Create the release namespace if not present + - Create the release namespace if not present. type: bool default: False + version_added: "0.11.1" ''' EXAMPLES = r'''