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

Commit

Permalink
helm: Add option 'atomic'
Browse files Browse the repository at this point in the history
helm command provides option to remove installation on failure using
'atomic' flag.
This fix adds this parameter in helm module.

Fixes: #109

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Jun 9, 2020
1 parent 18dfb68 commit 71b1558
Showing 1 changed file with 35 additions and 24 deletions.
59 changes: 35 additions & 24 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
description:
- Timeout when wait option is enabled (helm2 is a number of seconds, helm3 is a duration).
type: str
atomic:
description:
- If set, the installation process deletes the installation on failure.
type: bool
default: False
'''

EXAMPLES = r'''
Expand Down Expand Up @@ -230,6 +235,7 @@
sample: helm upgrade ...
"""

import tempfile
import traceback

try:
Expand Down Expand Up @@ -320,44 +326,37 @@ 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):
def deploy(**kwargs):
"""
Install/upgrade/rollback release chart
"""
deploy_command = command + " upgrade -i" # install/upgrade

deploy_command = kwargs['command'] + " upgrade -i" # install/upgrade

# Always reset values to keep release_values equal to values released
deploy_command += " --reset-values"

if wait:
if kwargs['wait']:
deploy_command += " --wait"
if wait_timeout is not None:
deploy_command += " --timeout " + wait_timeout
if kwargs['wait_timeout'] is not None:
deploy_command += " --timeout " + kwargs['wait_timeout']

if force:
if kwargs['force']:
deploy_command += " --force"

if disable_hook:
if kwargs['disable_hook']:
deploy_command += " --no-hooks"

if release_values != {}:
try:
import tempfile
except ImportError:
module.fail_json(
msg=missing_required_lib("tempfile"),
exception=traceback.format_exc(),
stdout='',
stderr='',
command='',
)
if kwargs['atomic']:
deploy_command += " --atomic"

if kwargs['release_values'] != {}:
fd, path = tempfile.mkstemp(suffix='.yml')
with open(path, 'w') as yaml_file:
yaml.dump(release_values, yaml_file, default_flow_style=False)
yaml.dump(kwargs['release_values'], yaml_file, default_flow_style=False)
deploy_command += " -f=" + path

deploy_command += " " + release_name + " " + chart_name
deploy_command += " " + kwargs['release_name'] + " " + kwargs['chart_name']

return deploy_command

Expand Down Expand Up @@ -402,6 +401,7 @@ def main():
purge=dict(type='bool', default=True),
wait=dict(type='bool', default=False),
wait_timeout=dict(type='str'),
atomic=dict(type='bool', default=False),
),
required_if=[
('release_state', 'present', ['release_name', 'chart_ref']),
Expand Down Expand Up @@ -469,15 +469,26 @@ def main():
# Fetch chart info to have real version and real name for chart_ref from archive, folder or url
chart_info = fetch_chart_info(helm_cmd, chart_ref)

options = {
'command': helm_cmd,
'release_name': release_name,
'release_values': release_values,
'chart_name': chart_ref,
'wait': wait,
'wait_timeout': wait_timeout,
'disable_hook': disable_hook,
'force': False,
'atomic': module.params.get('atomic'),
}

if release_status is None: # Not installed
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout,
disable_hook, False)
helm_cmd = deploy(**options)
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)
options['force'] = force
helm_cmd = deploy(**options)
changed = True

if module.check_mode:
Expand Down

0 comments on commit 71b1558

Please sign in to comment.