Skip to content

Commit

Permalink
helm: add support for history-max parameter (#164)
Browse files Browse the repository at this point in the history
* helm: add support for history-max parameter

The --history-max parameter allows the user to set a maximum amount of
revisions per release to be kept in the history.

By default helm keeps 10 revisions per release, which means that 10
secrets will be kept per release.

* helm: remove default for history_max

When the history_max option is not set, the module will not pass
'--history-max' to the CLI command. This ensures that the defaults of
the helm CLI will alwasy be used.

* helm: remove whitespace trail

* helm: add mutually exclusive logic

The 'history_max' parameter is not available when using the 'helm
install' command, it is only implemented for 'helm ugprade'.

The 'replace' option uses the 'install' parameter, thus 'replace' and
'history_max' have to be mutually exclusive.

* helm: formatting changes
  • Loading branch information
vvanouytsel authored Jul 15, 2021
1 parent 2e98493 commit a0a6d71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/164-add-history-max.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- helm - add support for history_max cli parameter (https://github.com/ansible-collections/kubernetes.core/pull/164).
20 changes: 16 additions & 4 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
description:
- Reuse the given name, only if that name is a deleted release which remains in the history.
- This is unsafe in production environment.
- mutually exclusive with with C(history_max).
type: bool
default: False
version_added: "1.11.0"
Expand All @@ -139,6 +140,12 @@
type: bool
default: False
version_added: "1.2.0"
history_max:
description:
- Limit the maximum number of revisions saved per release.
- mutually exclusive with with C(replace).
type: int
version_added: "2.2.0"
extends_documentation_fragment:
- kubernetes.core.helm_common_options
'''
Expand Down Expand Up @@ -357,7 +364,7 @@ def fetch_chart_info(module, command, chart_ref):


def deploy(command, release_name, release_values, chart_name, wait,
wait_timeout, disable_hook, force, values_files, atomic=False,
wait_timeout, disable_hook, force, values_files, history_max, atomic=False,
create_namespace=False, replace=False, skip_crds=False):
"""
Install/upgrade/rollback release chart
Expand Down Expand Up @@ -404,8 +411,10 @@ def deploy(command, release_name, release_values, chart_name, wait,
if skip_crds:
deploy_command += " --skip-crds"

deploy_command += " " + release_name + " " + chart_name
if history_max is not None:
deploy_command += " --history-max=%s" % str(history_max)

deploy_command += " " + release_name + " " + chart_name
return deploy_command


Expand Down Expand Up @@ -532,6 +541,7 @@ def main():
create_namespace=dict(type='bool', default=False),
replace=dict(type='bool', default=False),
skip_crds=dict(type='bool', default=False),
history_max=dict(type='int'),

# Generic auth key
host=dict(type='str', fallback=(env_fallback, ['K8S_AUTH_HOST'])),
Expand All @@ -546,6 +556,7 @@ def main():
mutually_exclusive=[
("context", "ca_cert"),
("kubeconfig", "ca_cert"),
("replace", "history_max"),
],
supports_check_mode=True,
)
Expand Down Expand Up @@ -575,6 +586,7 @@ def main():
create_namespace = module.params.get('create_namespace')
replace = module.params.get('replace')
skip_crds = module.params.get('skip_crds')
history_max = module.params.get('history_max')

if bin_path is not None:
helm_cmd_common = bin_path
Expand Down Expand Up @@ -610,7 +622,7 @@ def main():
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout,
disable_hook, False, values_files=values_files, atomic=atomic,
create_namespace=create_namespace, replace=replace,
skip_crds=skip_crds)
skip_crds=skip_crds, history_max=history_max)
changed = True

else:
Expand All @@ -627,7 +639,7 @@ def main():
helm_cmd = deploy(helm_cmd, release_name, release_values, chart_ref, wait, wait_timeout,
disable_hook, force, values_files=values_files, atomic=atomic,
create_namespace=create_namespace, replace=replace,
skip_crds=skip_crds)
skip_crds=skip_crds, history_max=history_max)
changed = True

if module.check_mode:
Expand Down

0 comments on commit a0a6d71

Please sign in to comment.