From 0e86fe0b7b07c4ced7ac5803ae800ab13a010eb8 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Mon, 3 Oct 2022 18:00:44 +0300 Subject: [PATCH] [helm] Add the force_update arg (#509) [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 Reviewed-by: Mike Graves --- .../509-helm-repo-add-force_update-argument.yaml | 3 +++ plugins/modules/helm_repository.py | 16 +++++++++++++++- .../targets/helm/tasks/tests_repository.yml | 13 +++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/509-helm-repo-add-force_update-argument.yaml diff --git a/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml b/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml new file mode 100644 index 00000000..ba758821 --- /dev/null +++ b/changelogs/fragments/509-helm-repo-add-force_update-argument.yaml @@ -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). diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 03a0e21d..cb232a5f 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -112,6 +112,13 @@ options: 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""" @@ -218,6 +225,7 @@ def install_repository( repository_username, repository_password, pass_credentials, + force_update, ): install_command = command + " repo add " + repository_name + " " + repository_url @@ -228,6 +236,9 @@ def install_repository( if pass_credentials: install_command += " --pass-credentials" + if force_update: + install_command += " --force-update" + return install_command @@ -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 @@ -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) @@ -286,7 +299,7 @@ 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, @@ -294,6 +307,7 @@ def main(): repo_username, repo_password, pass_credentials, + force_update, ) changed = True elif repository_status["url"] != repo_url: diff --git a/tests/integration/targets/helm/tasks/tests_repository.yml b/tests/integration/targets/helm/tasks/tests_repository.yml index eedea01a..dfd649fe 100644 --- a/tests/integration/targets/helm/tasks/tests_repository.yml +++ b/tests/integration/targets/helm/tasks/tests_repository.yml @@ -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 }}"