Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helm_template: add optional show_only and release_namespace arguments #388

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- helm_template - add show_only and release_namespace as module arguments (https://github.com/ansible-collections/kubernetes.core/issues/313).
46 changes: 46 additions & 0 deletions plugins/modules/helm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@
- If the directory already exists, it will be overwritten.
required: false
type: path
release_namespace:
description:
- namespace scope for this request.
required: false
type: str
version_added: 2.4.0
release_values:
description:
- Values to pass to chart.
required: false
default: {}
aliases: [ values ]
type: dict
show_only:
description:
- Only show manifests rendered from the given templates.
required: false
type: list
elements: str
version_added: 2.4.0
values_files:
description:
- Value files to pass to chart.
Expand Down Expand Up @@ -92,6 +105,24 @@
chart_ref: stable/prometheus
register: result

- name: Write templates to file
copy:
dest: myfile.yaml
content: "{{ result.stdout }}"

- name: Render MutatingWebhooksConfiguration for revision tag "canary", rev "1-13-0"
kubernetes.core.helm_template:
chart_ref: istio/istiod
chart_version: "1.13.0"
release_namespace: "istio-system"
show_only:
- "templates/revision-tags.yaml"
release_values:
revision: "1-13-0"
revisionTags:
- "canary"
register: result

- name: Write templates to file
copy:
dest: myfile.yaml
Expand Down Expand Up @@ -137,7 +168,9 @@ def template(
chart_repo_url=None,
chart_version=None,
output_dir=None,
show_only=None,
release_values=None,
release_namespace=None,
values_files=None,
include_crds=False,
):
Expand All @@ -152,10 +185,17 @@ def template(
if output_dir:
cmd += " --output-dir=" + output_dir

if show_only:
for template in show_only:
cmd += " -s " + template

if values_files:
for values_file in values_files:
cmd += " -f=" + values_file

if release_namespace:
cmd += " -n " + release_namespace

if release_values:
fd, path = tempfile.mkstemp(suffix=".yml")
with open(path, "w") as yaml_file:
Expand All @@ -177,7 +217,9 @@ def main():
chart_version=dict(type="str"),
include_crds=dict(type="bool", default=False),
output_dir=dict(type="path"),
release_namespace=dict(type="str"),
release_values=dict(type="dict", default={}, aliases=["values"]),
show_only=dict(type="list", default=[], elements="str"),
values_files=dict(type="list", default=[], elements="str"),
update_repo_cache=dict(type="bool", default=False),
),
Expand All @@ -191,6 +233,8 @@ def main():
chart_version = module.params.get("chart_version")
include_crds = module.params.get("include_crds")
output_dir = module.params.get("output_dir")
show_only = module.params.get("show_only")
release_namespace = module.params.get("release_namespace")
release_values = module.params.get("release_values")
values_files = module.params.get("values_files")
update_repo_cache = module.params.get("update_repo_cache")
Expand All @@ -210,7 +254,9 @@ def main():
chart_repo_url=chart_repo_url,
chart_version=chart_version,
output_dir=output_dir,
release_namespace=release_namespace,
release_values=release_values,
show_only=show_only,
values_files=values_files,
include_crds=include_crds,
)
Expand Down
85 changes: 85 additions & 0 deletions tests/unit/modules/test_helm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,88 @@ def test_template_with_release_values_and_values_files():
assert args.f[0] == "values1.yml"
assert args.f[1] == "values2.yml"
assert len(args.f) == 3


def test_template_with_one_show_only_template():
my_chart_ref = "testref"
helm_cmd = "helm"
parser = argparse.ArgumentParser()

parser.add_argument("cmd")
parser.add_argument("template")
# to "simulate" helm template options, include two optional parameters NAME and CHART.
# if parsed string contains only one parameter, the value will be passed
# to CHART and NAME will be set to default value "release-name" as in helm template
parser.add_argument("NAME", nargs="?", default="release-name")
parser.add_argument("CHART", nargs="+")
parser.add_argument("-f", action="append")
parser.add_argument("-s", action="append")

rv = {"revision": "1-13-0", "revisionTags": ["canary"]}
so_string = "templates/revision-tags.yaml"
so = [so_string]
mytemplate = template(
cmd=helm_cmd, chart_ref=my_chart_ref, show_only=so, release_values=rv
)

args, unknown = parser.parse_known_args(mytemplate.split())
print(mytemplate)
print(args)

assert len(args.f) == 1
assert len(args.s) == 1
assert args.s[0] == so_string


def test_template_with_two_show_only_templates():
my_chart_ref = "testref"
helm_cmd = "helm"
parser = argparse.ArgumentParser()

parser.add_argument("cmd")
parser.add_argument("template")
# to "simulate" helm template options, include two optional parameters NAME and CHART.
# if parsed string contains only one parameter, the value will be passed
# to CHART and NAME will be set to default value "release-name" as in helm template
parser.add_argument("NAME", nargs="?", default="release-name")
parser.add_argument("CHART", nargs="+")
parser.add_argument("-f", action="append")
parser.add_argument("-s", action="append")

rv = {"revision": "1-13-0", "revisionTags": ["canary"]}
so_string_1 = "templates/revision-tags.yaml"
so_string_2 = "templates/some-dummy-template.yaml"
so = [so_string_1, so_string_2]
mytemplate = template(
cmd=helm_cmd, chart_ref=my_chart_ref, show_only=so, release_values=rv
)

args, unknown = parser.parse_known_args(mytemplate.split())

assert len(args.f) == 1
assert len(args.s) == 2
assert args.s[0] == so_string_1
assert args.s[1] == so_string_2


def test_template_with_release_namespace():
my_chart_ref = "testref"
helm_cmd = "helm"
parser = argparse.ArgumentParser()

parser.add_argument("cmd")
parser.add_argument("template")
# to "simulate" helm template options, include two optional parameters NAME and CHART.
# if parsed string contains only one parameter, the value will be passed
# to CHART and NAME will be set to default value "release-name" as in helm template
parser.add_argument("NAME", nargs="?", default="release-name")
parser.add_argument("CHART", nargs="+")
parser.add_argument("-n", action="append")

ns = "istio-ingress-canary"
mytemplate = template(cmd=helm_cmd, chart_ref=my_chart_ref, release_namespace=ns)

args, unknown = parser.parse_known_args(mytemplate.split())

assert len(args.n) == 1
assert args.n[0] == ns