Skip to content

Commit

Permalink
helm, helm_template - add support for -set options when running helm …
Browse files Browse the repository at this point in the history
…install

add new module helm_get to retrieve helm releases information
  • Loading branch information
abikouo committed Nov 29, 2022
1 parent 646eb18 commit 501bd20
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
minor_changes:
- helm - add support for -set-file, -set-json, -set and -set-string options when running helm install (https://github.com/ansible-collections/kubernetes.core/issues/533).
- helm_template - add support for -set-file, -set-json, -set and -set-string options when running helm template (https://github.com/ansible-collections/kubernetes.core/pull/546).
27 changes: 27 additions & 0 deletions plugins/module_utils/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.six import string_types

from ansible_collections.kubernetes.core.plugins.module_utils.version import (
LooseVersion,
)

try:
import yaml

Expand Down Expand Up @@ -177,3 +181,26 @@ def get_helm_binary(module):
return module.params.get("binary_path") or module.get_bin_path(
"helm", required=True
)


def build_set_values_args(module, helm_cmd_common, set_values):
if any(v.get("value_type") == "json" for v in set_values):
helm_version = get_helm_version(module, helm_cmd_common)
if LooseVersion(helm_version) < LooseVersion("3.10.0"):
module.fail_json(
msg="This module requires helm >= 3.10.0, to use set_values parameter with value type set to 'json'. current version is {0}".format(
helm_version
)
)

options = []
for opt in set_values:
value_type = opt.get("value_type", "raw")
value = opt.get("value")

if value_type == "raw":
options.append("--set " + value)
else:
options.append("--set-" + value_type + " " + value)

return " ".join(options)
55 changes: 55 additions & 0 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@
- Run C(helm repo update) before the operation. Can be run as part of the package installation or as a separate step (see Examples).
default: false
type: bool
set_values:
description:
- Values to pass to chart configuration
required: false
type: list
elements: dict
suboptions:
value:
description:
- Value to pass to chart configuration (e.g phase=prod).
type: str
required: true
value_type:
description:
- Use C(raw) set individual value.
- Use C(string) to force a string for an individual value.
- Use C(file) to set individual values from a file when the value itself is too long for the command line or is dynamically generated.
- Use C(json) to set json values (scalars/objects/arrays). This feature requires helm>=3.10.0.
default: raw
choices:
- raw
- string
- json
- file
version_added: '2.4.0'
#Helm options
disable_hook:
Expand Down Expand Up @@ -232,6 +257,15 @@
state: absent
update_repo_cache: true
- name: Deploy Grafana chart using set values on target
kubernetes.core.helm:
name: test
chart_ref: stable/grafana
release_namespace: monitoring
set_values:
- value: phase=prod
value_type: string
# From git
- name: Git clone stable repo on HEAD
ansible.builtin.git:
Expand Down Expand Up @@ -358,6 +392,7 @@
parse_helm_plugin_list,
get_helm_version,
get_helm_binary,
build_set_values_args,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
Expand Down Expand Up @@ -440,6 +475,7 @@ def deploy(
skip_crds=False,
timeout=None,
dependency_update=None,
set_value_args=None,
):
"""
Install/upgrade/rollback release chart
Expand Down Expand Up @@ -497,6 +533,9 @@ def deploy(
if history_max is not None:
deploy_command += " --history-max=%s" % str(history_max)

if set_value_args:
deploy_command += " " + set_value_args

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

Expand Down Expand Up @@ -645,6 +684,7 @@ def argument_spec():
replace=dict(type="bool", default=False),
skip_crds=dict(type="bool", default=False),
history_max=dict(type="int"),
set_values=dict(type="list", elements="dict"),
)
)
return arg_spec
Expand Down Expand Up @@ -695,6 +735,7 @@ def main():
skip_crds = module.params.get("skip_crds")
history_max = module.params.get("history_max")
timeout = module.params.get("timeout")
set_values = module.params.get("set_values")

helm_cmd_common = get_helm_binary(module)
helm_bin = helm_cmd_common
Expand Down Expand Up @@ -767,6 +808,12 @@ def main():
)

if release_status is None: # Not installed
set_value_args = None
if set_values:
set_value_args = build_set_values_args(
module, helm_cmd_common, set_values
)

helm_cmd = deploy(
helm_cmd,
release_name,
Expand All @@ -785,6 +832,7 @@ def main():
skip_crds=skip_crds,
history_max=history_max,
timeout=timeout,
set_value_args=set_value_args,
)
changed = True

Expand Down Expand Up @@ -821,6 +869,12 @@ def main():
)

if force or would_change:
set_value_args = None
if set_values:
set_value_args = build_set_values_args(
module, helm_cmd_common, set_values
)

helm_cmd = deploy(
helm_cmd,
release_name,
Expand All @@ -839,6 +893,7 @@ def main():
history_max=history_max,
timeout=timeout,
dependency_update=dependency_update,
set_value_args=set_value_args,
)
changed = True

Expand Down
Loading

0 comments on commit 501bd20

Please sign in to comment.