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

Add helm dependency update #208

2 changes: 2 additions & 0 deletions changelogs/fragments/208-add-dependency-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- helm - add support for helm dependency update (https://github.com/ansible-collections/kubernetes.core/pull/208).
24 changes: 24 additions & 0 deletions docs/kubernetes.core.helm_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,30 @@ Parameters
<div>Create the release namespace if not present.</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>dependency_update</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">boolean</span>
</div>
</td>
<td>
<ul style="margin: 0; padding: 0"><b>Choices:</b>
<li><div style="color: blue"><b>no</b>&nbsp;&larr;</div></li>
<li>yes</li>
</ul>
</td>
<td>
<div>Run standelone <code>helm dependency update CHART</code> before the operation.</div>
<div>Run inline <code>--dependency-update</code> with <code>helm install</code> command. This feature is not supported yet with the <code>helm upgrade</code> command.</div>
<div>So we should consider to use <em>dependency_update</em> options with <em>replace</em> option enabled when specifying <em>chart_repo_url</em>.</div>
<div>The <em>dependency_update</em> option require the add of <code>dependencies</code> block in <code>Chart.yaml/requirements.yaml</code> file.</div>
<div>For more information please visit <a href='https://helm.sh/docs/helm/helm_dependency/'>https://helm.sh/docs/helm/helm_dependency/</a></div>
<div style="font-size: small; color: darkgreen"><br/>aliases: dep_up</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down
22 changes: 22 additions & 0 deletions docs/kubernetes.core.helm_template_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ Parameters
<div>Chart version to use. If this is not specified, the latest version is installed.</div>
</td>
</tr>
<tr>
abikouo marked this conversation as resolved.
Show resolved Hide resolved
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
<b>dependency_update</b>
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
<div style="font-size: small">
<span style="color: purple">boolean</span>
</div>
</td>
<td>
<ul style="margin: 0; padding: 0"><b>Choices:</b>
<li><div style="color: blue"><b>no</b>&nbsp;&larr;</div></li>
<li>yes</li>
</ul>
</td>
<td>
<div>Run helm dependency update before the operation.</div>
<div>The <em>dependency_update</em> option require the add of <code>dependencies</code> block in <code>Chart.yaml/requirements.yaml</code> file.</div>
<div>For more information please visit <a href='https://helm.sh/docs/helm/helm_dependency/'>https://helm.sh/docs/helm/helm_dependency/</a></div>
<div style="font-size: small; color: darkgreen"><br/>aliases: dep_up</div>
</td>
</tr>
<tr>
<td colspan="1">
<div class="ansibleOptionAnchor" id="parameter-"></div>
Expand Down
57 changes: 57 additions & 0 deletions plugins/modules/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
- Chart version to install. If this is not specified, the latest version is installed.
required: false
type: str
dependency_update:
description:
- Run standalone C(helm dependency update CHART) before the operation.
- Run inline C(--dependency-update) with C(helm install) command. This feature is not supported yet with the C(helm upgrade) command.
- So we should consider to use I(dependency_update) options with I(replace) option enabled when specifying I(chart_repo_url).
- The I(dependency_update) option require the add of C(dependencies) block in C(Chart.yaml/requirements.yaml) file.
- For more information please visit U(https://helm.sh/docs/helm/helm_dependency/)
default: false
type: bool
aliases: [ dep_up ]
WissemChb marked this conversation as resolved.
Show resolved Hide resolved
version_added: "2.4.0"
release_name:
description:
- Release name to manage.
Expand Down Expand Up @@ -322,6 +333,7 @@
sample: helm upgrade ...
"""

import re
import tempfile
import traceback
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
Expand Down Expand Up @@ -385,6 +397,14 @@ def run_repo_update(module, command):
rc, out, err = run_helm(module, repo_update_command)


def run_dep_update(module, command, chart_ref):
"""
Run dependency update
"""
dep_update = command + " dependency update " + chart_ref
WissemChb marked this conversation as resolved.
Show resolved Hide resolved
rc, out, err = run_helm(module, dep_update)


def fetch_chart_info(module, command, chart_ref):
"""
Get chart info
Expand Down Expand Up @@ -413,13 +433,16 @@ def deploy(
post_renderer=None,
skip_crds=False,
timeout=None,
dependency_update=None,
):
"""
Install/upgrade/rollback release chart
"""
if replace:
# '--replace' is not supported by 'upgrade -i'
deploy_command = command + " install"
if dependency_update:
deploy_command += " --dependency-update"
else:
deploy_command = command + " upgrade -i" # install/upgrade

Expand Down Expand Up @@ -597,6 +620,7 @@ def main():
chart_ref=dict(type="path"),
chart_repo_url=dict(type="str"),
chart_version=dict(type="str"),
dependency_update=dict(type="bool", default=False, aliases=["dep_up"]),
release_name=dict(type="str", required=True, aliases=["name"]),
release_namespace=dict(type="str", required=True, aliases=["namespace"]),
release_state=dict(
Expand Down Expand Up @@ -667,6 +691,7 @@ def main():
chart_ref = module.params.get("chart_ref")
chart_repo_url = module.params.get("chart_repo_url")
chart_version = module.params.get("chart_version")
dependency_update = module.params.get("dependency_update")
release_name = module.params.get("release_name")
release_state = module.params.get("release_state")
release_values = module.params.get("release_values")
Expand Down Expand Up @@ -729,6 +754,36 @@ 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(module, helm_cmd, chart_ref)

if dependency_update:
if chart_info.get("dependencies"):
# Can't use '--dependency-update' with 'helm upgrade' that is the
# default chart install method, so if chart_repo_url is defined
# we can't use the dependency update command. But, in the near future
# we can get rid of this method and use only '--dependency-update'
# option. Please see https://github.com/helm/helm/pull/8810
if not chart_repo_url and not re.fullmatch(
r"^http[s]*://[\w.:/?&=-]+$", chart_ref
):
run_dep_update(module, helm_cmd_common, chart_ref)

# To not add --dependency-update option in the deploy function
dependency_update = False
else:
module.warn(
"This is a not stable feature with 'chart_repo_url'. Please consider to use dependency update with on-disk charts"
)
if not replace:
msg_fail = (
"'--dependency-update' hasn't been supported yet with 'helm upgrade'. "
"Please use 'helm install' instead by adding 'replace' option"
)
module.fail_json(msg=msg_fail)
else:
module.warn(
"There is no dependencies block defined in Chart.yaml. Dependency update will not be performed. "
"Please consider add dependencies block or disable dependency_update to remove this warning."
)

WissemChb marked this conversation as resolved.
Show resolved Hide resolved
if release_status is None: # Not installed
helm_cmd = deploy(
helm_cmd,
Expand All @@ -744,6 +799,7 @@ def main():
create_namespace=create_namespace,
post_renderer=post_renderer,
replace=replace,
dependency_update=dependency_update,
skip_crds=skip_crds,
history_max=history_max,
timeout=timeout,
Expand Down Expand Up @@ -800,6 +856,7 @@ def main():
skip_crds=skip_crds,
history_max=history_max,
timeout=timeout,
dependency_update=dependency_update,
)
changed = True

Expand Down
16 changes: 16 additions & 0 deletions plugins/modules/helm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
- Chart version to use. If this is not specified, the latest version is installed.
required: false
type: str
dependency_update:
description:
- Run helm dependency update before the operation.
- The I(dependency_update) option require the add of C(dependencies) block in C(Chart.yaml/requirements.yaml) file.
- For more information please visit U(https://helm.sh/docs/helm/helm_dependency/)
default: false
type: bool
aliases: [ dep_up ]
WissemChb marked this conversation as resolved.
Show resolved Hide resolved
version_added: "2.4.0"
include_crds:
description:
- Include custom resource descriptions in rendered templates.
Expand Down Expand Up @@ -167,6 +176,7 @@ def template(
chart_ref,
chart_repo_url=None,
chart_version=None,
dependency_update=None,
output_dir=None,
show_only=None,
release_values=None,
Expand All @@ -176,6 +186,9 @@ def template(
):
cmd += " template " + chart_ref

if dependency_update:
cmd += " --dependency-update"

if chart_repo_url:
cmd += " --repo=" + chart_repo_url

Expand Down Expand Up @@ -215,6 +228,7 @@ def main():
chart_ref=dict(type="path", required=True),
chart_repo_url=dict(type="str"),
chart_version=dict(type="str"),
dependency_update=dict(type="bool", default=False, aliases=["dep_up"]),
include_crds=dict(type="bool", default=False),
output_dir=dict(type="path"),
release_namespace=dict(type="str"),
Expand All @@ -231,6 +245,7 @@ def main():
chart_ref = module.params.get("chart_ref")
chart_repo_url = module.params.get("chart_repo_url")
chart_version = module.params.get("chart_version")
dependency_update = module.params.get("dependency_update")
include_crds = module.params.get("include_crds")
output_dir = module.params.get("output_dir")
show_only = module.params.get("show_only")
Expand All @@ -251,6 +266,7 @@ def main():
tmpl_cmd = template(
helm_cmd,
chart_ref,
dependency_update=dependency_update,
chart_repo_url=chart_repo_url,
chart_version=chart_version,
output_dir=output_dir,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/helm/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ test_namespace:
- "helm-local-path-001"
- "helm-local-path-002"
- "helm-local-path-003"
- "helm-dep"
10 changes: 10 additions & 0 deletions tests/integration/targets/helm/files/dep-up/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v2
name: dep_up
description: A Helm chart for molecule test
type: application
version: 0.1.0
appVersion: "default"
dependencies:
- name: test-chart
repository: file://../test-chart
version: "0.1.0"
2 changes: 2 additions & 0 deletions tests/integration/targets/helm/files/dep-up/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
chart-test:
myValue: helm update dependency test
3 changes: 3 additions & 0 deletions tests/integration/targets/helm/tasks/run_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- from_repository
- from_url

- name: test helm dependency update
include_tasks: test_up_dep.yml

- name: Test helm plugin
include_tasks: tests_helm_plugin.yml

Expand Down
Loading