Skip to content

Commit

Permalink
[helm] Add the force_update arg (#509)
Browse files Browse the repository at this point in the history
[helm] Add the force_update arg

Depends-On: ansible/ansible-zuul-jobs#1648
Depends-On: #522
SUMMARY
Sometimes a Helm repo needs to be updated with a new URL. The helm repo add command allows for this with the --force-update flag:
      --force-update               replace (overwrite) the repo if it already exists


ISSUE TYPE

Feature Pull Request - Closes #491

COMPONENT NAME
kubernetes.core.helm_repository
ADDITIONAL INFORMATION

Reviewed-by: Mor Cohen <morcohen1201@gmail.com>
Reviewed-by: Mike Graves <mgraves@redhat.com>
  • Loading branch information
mor120 authored Oct 3, 2022
1 parent 43ad31d commit 0e86fe0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- helm_repository - Ability to replace (overwrite) the repo if it already exists by forcing (https://github.com/ansible-collections/kubernetes.core/issues/491).
16 changes: 15 additions & 1 deletion plugins/modules/helm_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@
type: raw
aliases: [ kubeconfig_path ]
version_added: "2.4.0"
force_update:
description:
- Whether or not to replace (overwrite) the repo if it already exists.
type: bool
aliases: [ force ]
default: False
version_added: "2.4.0"
"""

EXAMPLES = r"""
Expand Down Expand Up @@ -218,6 +225,7 @@ def install_repository(
repository_username,
repository_password,
pass_credentials,
force_update,
):
install_command = command + " repo add " + repository_name + " " + repository_url

Expand All @@ -228,6 +236,9 @@ def install_repository(
if pass_credentials:
install_command += " --pass-credentials"

if force_update:
install_command += " --force-update"

return install_command


Expand All @@ -250,6 +261,7 @@ def argument_spec():
default="present", choices=["present", "absent"], aliases=["state"]
),
pass_credentials=dict(type="bool", default=False, no_log=True),
force_update=dict(type="bool", default=False, aliases=["force"]),
)
)
return arg_spec
Expand Down Expand Up @@ -277,6 +289,7 @@ def main():
repo_password = module.params.get("repo_password")
repo_state = module.params.get("repo_state")
pass_credentials = module.params.get("pass_credentials")
force_update = module.params.get("force_update")

helm_cmd = get_helm_binary(module)

Expand All @@ -286,14 +299,15 @@ def main():
helm_cmd = delete_repository(helm_cmd, repo_name)
changed = True
elif repo_state == "present":
if repository_status is None:
if repository_status is None or force_update:
helm_cmd = install_repository(
helm_cmd,
repo_name,
repo_url,
repo_username,
repo_password,
pass_credentials,
force_update,
)
changed = True
elif repository_status["url"] != repo_url:
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/targets/helm/tasks/tests_repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
that:
- repository_errors is failed

- name: Succesfully add repository with the same name when forcing
helm_repository:
binary_path: "{{ helm_binary }}"
name: test_helm_repo
repo_url: "{{ chart_test_repo }}"
force: true
register: repository

- name: Assert that test_helm_repo repository is changed
assert:
that:
- repository is changed

- name: Remove test_helm_repo chart repository
helm_repository:
binary_path: "{{ helm_binary }}"
Expand Down

0 comments on commit 0e86fe0

Please sign in to comment.