Create ns with helm (#157)

Added support `--create-namespace` in helm module.
This commit is contained in:
LucasBoisserie
2020-07-11 05:15:22 +02:00
committed by GitHub
parent 806c808c9d
commit 3e971e0ad3
3 changed files with 38 additions and 17 deletions

View File

@@ -4,4 +4,4 @@
loop_control: loop_control:
loop_var: helm_version loop_var: helm_version
with_items: with_items:
- "v3.1.2" - "v3.2.4"

View File

@@ -1,11 +1,4 @@
--- ---
- name: Create helm namespace
k8s:
api_version: v1
kind: Namespace
name: "{{ helm_namespace }}"
wait: true
- name: Check helm_info empty - name: Check helm_info empty
helm_info: helm_info:
binary_path: "{{ helm_binary }}" binary_path: "{{ helm_binary }}"
@@ -18,6 +11,22 @@
that: that:
- empty_info.status is undefined - 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 }}" - name: "Install {{ chart_test }} from {{ source }}"
helm: helm:
binary_path: "{{ helm_binary }}" binary_path: "{{ helm_binary }}"
@@ -25,6 +34,7 @@
chart_ref: "{{ chart_source }}" chart_ref: "{{ chart_source }}"
chart_version: "{{ chart_source_version | default(omit) }}" chart_version: "{{ chart_source_version | default(omit) }}"
namespace: "{{ helm_namespace }}" namespace: "{{ helm_namespace }}"
create_namespace: true
register: install register: install
- name: "Assert that {{ chart_test }} chart is installed from {{ source }}" - name: "Assert that {{ chart_test }} chart is installed from {{ source }}"

View File

@@ -126,15 +126,21 @@ options:
- If set, the installation process deletes the installation on failure. - If set, the installation process deletes the installation on failure.
type: bool type: bool
default: False default: False
create_namespace:
description:
- Create the release namespace if not present.
type: bool
default: False
version_added: "0.11.1"
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: Create helm namespace as HELM 3 doesn't create it automatically - name: Deploy latest version of Prometheus chart inside monitoring namespace (and create it)
community.kubernetes.k8s: community.kubernetes.helm:
api_version: v1 name: test
kind: Namespace chart_ref: stable/prometheus
name: "monitoring" release_namespace: monitoring
wait: true create_namespace: true
# From repository # From repository
- name: Add stable chart repo - name: Add stable chart repo
@@ -329,7 +335,7 @@ def fetch_chart_info(command, chart_ref):
return yaml.safe_load(out) 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 Install/upgrade/rollback release chart
""" """
@@ -352,6 +358,9 @@ def deploy(command, release_name, release_values, chart_name, wait, wait_timeout
if disable_hook: if disable_hook:
deploy_command += " --no-hooks" deploy_command += " --no-hooks"
if create_namespace:
deploy_command += " --create-namespace"
if release_values != {}: if release_values != {}:
fd, path = tempfile.mkstemp(suffix='.yml') fd, path = tempfile.mkstemp(suffix='.yml')
with open(path, 'w') as yaml_file: with open(path, 'w') as yaml_file:
@@ -404,6 +413,7 @@ def main():
wait=dict(type='bool', default=False), wait=dict(type='bool', default=False),
wait_timeout=dict(type='str'), wait_timeout=dict(type='str'),
atomic=dict(type='bool', default=False), atomic=dict(type='bool', default=False),
create_namespace=dict(type='bool', default=False),
), ),
required_if=[ required_if=[
('release_state', 'present', ['release_name', 'chart_ref']), ('release_state', 'present', ['release_name', 'chart_ref']),
@@ -436,6 +446,7 @@ def main():
wait = module.params.get('wait') wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout') wait_timeout = module.params.get('wait_timeout')
atomic = module.params.get('atomic') atomic = module.params.get('atomic')
create_namespace = module.params.get('create_namespace')
if bin_path is not None: if bin_path is not None:
helm_cmd_common = bin_path helm_cmd_common = bin_path
@@ -474,13 +485,13 @@ def main():
if release_status is None: # Not installed if release_status is None: # Not installed
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, 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 changed = True
elif force or release_values != release_status['values'] \ elif force or release_values != release_status['values'] \
or (chart_info['name'] + '-' + chart_info['version']) != release_status["chart"]: or (chart_info['name'] + '-' + chart_info['version']) != release_status["chart"]:
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout, 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 changed = True
if module.check_mode: if module.check_mode: