Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Create ns with helm (#157)
Browse files Browse the repository at this point in the history
Added support `--create-namespace` in helm module.
  • Loading branch information
LucasBoisserie authored Jul 11, 2020
1 parent 806c808 commit 3e971e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion molecule/default/roles/helm/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
loop_control:
loop_var: helm_version
with_items:
- "v3.1.2"
- "v3.2.4"
24 changes: 17 additions & 7 deletions molecule/default/roles/helm/tasks/tests_chart.yml
Original file line number Diff line number Diff line change
@@ -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 }}"
Expand All @@ -18,13 +11,30 @@
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 }}"
name: test
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 }}"
Expand Down
29 changes: 20 additions & 9 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,21 @@
- 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
version_added: "0.11.1"
'''

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
Expand Down Expand Up @@ -329,7 +335,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
"""
Expand All @@ -352,6 +358,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:
Expand Down Expand Up @@ -404,6 +413,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']),
Expand Down Expand Up @@ -436,6 +446,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
Expand Down Expand Up @@ -474,13 +485,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:
Expand Down

0 comments on commit 3e971e0

Please sign in to comment.