Debian Codegraph @

ansible_collections/kubernetes/core/plugins/modules/helm_template.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r"""
13
14 module: helm_template
15
16 short_description: Render chart templates
17
18 author:
19 - Mike Graves (@gravesm)
20
21 description:
22 - Render chart templates to an output directory or as text of concatenated yaml documents.
23
24 requirements:
25 - "helm >= 3.0.0 (https://github.com/helm/helm/releases)"
26 - "yaml (https://pypi.org/project/PyYAML/)"
27
28 options:
29 binary_path:
30 description:
31 - The path of a helm binary to use.
32 required: false
33 type: path
34 chart_ref:
35 description:
36 - Chart reference with repo prefix, for example, C(nginx-stable/nginx-ingress).
37 - Path to a packaged chart.
38 - Path to an unpacked chart directory.
39 - Absolute URL.
40 required: true
41 type: path
42 chart_repo_url:
43 description:
44 - Chart repository URL where the requested chart is located.
45 required: false
46 type: str
47 chart_version:
48 description:
49 - Chart version to use. If this is not specified, the latest version is installed.
50 required: false
51 type: str
52 dependency_update:
53 description:
54 - Run helm dependency update before the operation.
55 - The I(dependency_update) option require the add of C(dependencies) block in C(Chart.yaml/requirements.yaml) file.
56 - For more information please visit U(https://helm.sh/docs/helm/helm_dependency/)
57 default: false
58 type: bool
59 aliases: [ dep_up ]
60 version_added: 2.4.0
61 disable_hook:
62 description:
63 - Prevent hooks from running during install.
64 default: False
65 type: bool
66 version_added: 2.4.0
67 include_crds:
68 description:
69 - Include custom resource descriptions in rendered templates.
70 required: false
71 type: bool
72 default: false
73 output_dir:
74 description:
75 - Output directory where templates will be written.
76 - If the directory already exists, it will be overwritten.
77 required: false
78 type: path
79 insecure_registry:
80 description:
81 - Skip TLS certificate checks for the chart download
82 required: false
83 type: bool
84 default: false
85 version_added: 5.1.0
86 release_name:
87 description:
88 - Release name to use in rendered templates.
89 required: false
90 aliases: [ name ]
91 type: str
92 version_added: 2.4.0
93 release_namespace:
94 description:
95 - namespace scope for this request.
96 required: false
97 type: str
98 version_added: 2.4.0
99 release_values:
100 description:
101 - Values to pass to chart.
102 required: false
103 default: {}
104 aliases: [ values ]
105 type: dict
106 show_only:
107 description:
108 - Only show manifests rendered from the given templates.
109 required: false
110 type: list
111 elements: str
112 default: []
113 version_added: 2.4.0
114 values_files:
115 description:
116 - Value files to pass to chart.
117 - Paths will be read from the target host's filesystem, not the host running ansible.
118 - I(values_files) option is evaluated before I(values) option if both are used.
119 - Paths are evaluated in the order the paths are specified.
120 required: false
121 default: []
122 type: list
123 elements: str
124 update_repo_cache:
125 description:
126 - Run C(helm repo update) before the operation. Can be run as part of the template generation or as a separate step.
127 default: false
128 type: bool
129 set_values:
130 description:
131 - Values to pass to chart configuration.
132 required: false
133 type: list
134 elements: dict
135 suboptions:
136 value:
137 description:
138 - Value to pass to chart configuration (e.g phase=prod).
139 type: str
140 required: true
141 value_type:
142 description:
143 - Use C(raw) set individual value.
144 - Use C(string) to force a string for an individual value.
145 - 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.
146 - Use C(json) to set json values (scalars/objects/arrays). This feature requires helm>=3.10.0.
147 default: raw
148 choices:
149 - raw
150 - string
151 - json
152 - file
153 version_added: 2.4.0
154 plain_http:
155 description:
156 - Use HTTP instead of HTTPS when working with OCI registries
157 - Requires Helm >= 3.13.0
158 type: bool
159 default: False
160 version_added: 6.1.0
161 """
162
163 EXAMPLES = r"""
164 - name: Render templates to specified directory
165 kubernetes.core.helm_template:
166 chart_ref: stable/prometheus
167 output_dir: mycharts
168
169 - name: Render templates
170 kubernetes.core.helm_template:
171 chart_ref: stable/prometheus
172 register: result
173
174 - name: Write templates to file
175 copy:
176 dest: myfile.yaml
177 content: "{{ result.stdout }}"
178
179 - name: Render MutatingWebhooksConfiguration for revision tag "canary", rev "1-13-0"
180 kubernetes.core.helm_template:
181 chart_ref: istio/istiod
182 chart_version: "1.13.0"
183 release_namespace: "istio-system"
184 show_only:
185 - "templates/revision-tags.yaml"
186 release_values:
187 revision: "1-13-0"
188 revisionTags:
189 - "canary"
190 register: result
191
192 - name: Write templates to file
193 copy:
194 dest: myfile.yaml
195 content: "{{ result.stdout }}"
196 """
197
198 RETURN = r"""
199 stdout:
200 type: str
201 description: Full C(helm) command stdout. If no I(output_dir) has been provided this will contain the rendered templates as concatenated yaml documents.
202 returned: always
203 sample: ''
204 stderr:
205 type: str
206 description: Full C(helm) command stderr, in case you want to display it or examine the event log.
207 returned: always
208 sample: ''
209 command:
210 type: str
211 description: Full C(helm) command run by this module, in case you want to re-run the command outside the module or debug a problem.
212 returned: always
213 sample: helm template --output-dir mychart nginx-stable/nginx-ingress
214 """
215
216 import tempfile
217 import traceback
218
219 try:
220 import yaml
221
222 IMP_YAML = True
223 IMP_YAML_ERR = None
224 except ImportError:
225 IMP_YAML_ERR = traceback.format_exc()
226 IMP_YAML = False
227
228 from ansible.module_utils.basic import missing_required_lib
229 from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
230 AnsibleHelmModule,
231 )
232 from ansible_collections.kubernetes.core.plugins.module_utils.version import (
233 LooseVersion,
234 )
235
236
237 def template(
238 cmd,
239 chart_ref,
240 chart_repo_url=None,
241 chart_version=None,
242 dependency_update=None,
243 disable_hook=None,
244 output_dir=None,
245 insecure_registry=None,
246 show_only=None,
247 release_name=None,
248 release_namespace=None,
249 release_values=None,
250 values_files=None,
251 include_crds=False,
252 set_values=None,
253 plain_http=False,
254 ):
255 cmd += " template "
256
257 if release_name:
258 cmd += release_name + " "
259
260 cmd += chart_ref
261
262 if dependency_update:
263 cmd += " --dependency-update"
264
265 if chart_repo_url:
266 cmd += " --repo=" + chart_repo_url
267
268 if chart_version:
269 cmd += " --version=" + chart_version
270
271 if disable_hook:
272 cmd += " --no-hooks"
273
274 if output_dir:
275 cmd += " --output-dir=" + output_dir
276
277 if insecure_registry:
278 cmd += " --insecure-skip-tls-verify"
279
280 if plain_http:
281 cmd += " --plain-http"
282
283 if show_only:
284 for template in show_only:
285 cmd += " -s " + template
286
287 if values_files:
288 for values_file in values_files:
289 cmd += " -f=" + values_file
290
291 if release_namespace:
292 cmd += " -n " + release_namespace
293
294 if release_values:
295 fd, path = tempfile.mkstemp(suffix=".yml")
296 with open(path, "w") as yaml_file:
297 yaml.dump(release_values, yaml_file, default_flow_style=False)
298 cmd += " -f=" + path
299
300 if include_crds:
301 cmd += " --include-crds"
302
303 if set_values:
304 cmd += " " + set_values
305
306 return cmd
307
308
309 def main():
310 module = AnsibleHelmModule(
311 argument_spec=dict(
312 binary_path=dict(type="path"),
313 chart_ref=dict(type="path", required=True),
314 chart_repo_url=dict(type="str"),
315 chart_version=dict(type="str"),
316 dependency_update=dict(type="bool", default=False, aliases=["dep_up"]),
317 disable_hook=dict(type="bool", default=False),
318 include_crds=dict(type="bool", default=False),
319 release_name=dict(type="str", aliases=["name"]),
320 output_dir=dict(type="path"),
321 insecure_registry=dict(type="bool", default=False),
322 release_namespace=dict(type="str"),
323 release_values=dict(type="dict", default={}, aliases=["values"]),
324 show_only=dict(type="list", default=[], elements="str"),
325 values_files=dict(type="list", default=[], elements="str"),
326 update_repo_cache=dict(type="bool", default=False),
327 set_values=dict(type="list", elements="dict"),
328 plain_http=dict(type="bool", default=False),
329 ),
330 supports_check_mode=True,
331 )
332
333 check_mode = module.check_mode
334 chart_ref = module.params.get("chart_ref")
335 chart_repo_url = module.params.get("chart_repo_url")
336 chart_version = module.params.get("chart_version")
337 dependency_update = module.params.get("dependency_update")
338 disable_hook = module.params.get("disable_hook")
339 include_crds = module.params.get("include_crds")
340 release_name = module.params.get("release_name")
341 output_dir = module.params.get("output_dir")
342 insecure_registry = module.params.get("insecure_registry")
343 show_only = module.params.get("show_only")
344 release_namespace = module.params.get("release_namespace")
345 release_values = module.params.get("release_values")
346 values_files = module.params.get("values_files")
347 update_repo_cache = module.params.get("update_repo_cache")
348 set_values = module.params.get("set_values")
349 plain_http = module.params.get("plain_http")
350
351 if not IMP_YAML:
352 module.fail_json(msg=missing_required_lib("yaml"), exception=IMP_YAML_ERR)
353
354 # Validate Helm version >=3.0.0
355 module.validate_helm_version()
356
357 helm_cmd = module.get_helm_binary()
358
359 if plain_http:
360 helm_version = module.get_helm_version()
361 if LooseVersion(helm_version) < LooseVersion("3.13.0"):
362 module.fail_json(
363 msg="plain_http requires helm >= 3.13.0, current version is {0}".format(
364 helm_version
365 )
366 )
367
368 if update_repo_cache:
369 update_cmd = helm_cmd + " repo update"
370 module.run_helm_command(update_cmd)
371
372 set_values_args = None
373 if set_values:
374 set_values_args = module.get_helm_set_values_args(set_values)
375
376 tmpl_cmd = template(
377 helm_cmd,
378 chart_ref,
379 dependency_update=dependency_update,
380 chart_repo_url=chart_repo_url,
381 chart_version=chart_version,
382 disable_hook=disable_hook,
383 release_name=release_name,
384 output_dir=output_dir,
385 insecure_registry=insecure_registry,
386 release_namespace=release_namespace,
387 release_values=release_values,
388 show_only=show_only,
389 values_files=values_files,
390 include_crds=include_crds,
391 set_values=set_values_args,
392 plain_http=plain_http,
393 )
394
395 if not check_mode:
396 rc, out, err = module.run_helm_command(tmpl_cmd)
397 else:
398 out = err = ""
399 rc = 0
400
401 module.exit_json(
402 failed=False, changed=True, command=tmpl_cmd, stdout=out, stderr=err, rc=rc
403 )
404
405
406 if __name__ == "__main__":
407 main()