From edd1b09427a8ada0a307e290acb8bafb6eb9e743 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Mon, 22 Aug 2022 22:40:05 +0300 Subject: [PATCH 1/8] Add the force_update arg --- plugins/modules/helm_repository.py | 37 +++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 03a0e21dee..8a8c4ae92f 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -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.3.3" """ 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 @@ -260,6 +271,28 @@ def main(): module = AnsibleModule( argument_spec=argument_spec(), + # Generic auth key + host=dict(type="str", fallback=(env_fallback, ["K8S_AUTH_HOST"])), + ca_cert=dict( + type="path", + aliases=["ssl_ca_cert"], + fallback=(env_fallback, ["K8S_AUTH_SSL_CA_CERT"]), + ), + validate_certs=dict( + type="bool", + default=True, + aliases=["verify_ssl"], + fallback=(env_fallback, ["K8S_AUTH_VERIFY_SSL"]), + ), + force_update=dict( + type="bool", + default=False, + aliases=["force"], + ), + api_key=dict( + type="str", no_log=True, fallback=(env_fallback, ["K8S_AUTH_API_KEY"]) + ), + ), required_together=[["repo_username", "repo_password"]], required_if=[("repo_state", "present", ["repo_url"])], mutually_exclusive=HELM_AUTH_MUTUALLY_EXCLUSIVE, @@ -277,6 +310,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 +320,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 +328,7 @@ def main(): repo_username, repo_password, pass_credentials, + force_update ) changed = True elif repository_status["url"] != repo_url: From 5418cac9ecd5d0f069c17a6f2a5d5de3f230c215 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Fri, 9 Sep 2022 16:11:32 +0300 Subject: [PATCH 2/8] Test --- .../targets/helm/tasks/tests_repository.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/integration/targets/helm/tasks/tests_repository.yml b/tests/integration/targets/helm/tasks/tests_repository.yml index eedea01a73..dfd649fe84 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 }}" From 42eeff2afe31f3d6a621853943c5073781e5b499 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Fri, 9 Sep 2022 17:45:11 +0300 Subject: [PATCH 3/8] Use newer version of helm --- tests/integration/targets/helm/tasks/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/targets/helm/tasks/main.yml b/tests/integration/targets/helm/tasks/main.yml index e86d33df22..9d8dda3e7b 100644 --- a/tests/integration/targets/helm/tasks/main.yml +++ b/tests/integration/targets/helm/tasks/main.yml @@ -4,4 +4,4 @@ loop_control: loop_var: helm_version with_items: - - "v3.2.4" + - "v3.7.0" From be7ec3167d5417e1e5c72e1afdebece40c68dc14 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Thu, 15 Sep 2022 10:21:05 +0300 Subject: [PATCH 4/8] Fix - E111 indentation is not a multiple of 4 --- plugins/modules/helm_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 8a8c4ae92f..bf6e6d9fce 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -237,7 +237,7 @@ def install_repository( install_command += " --pass-credentials" if force_update: - install_command += " --force-update" + install_command += " --force-update" return install_command From d50c946fd07de5659a89a03e5b18379db9aeb18e Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Thu, 15 Sep 2022 10:30:18 +0300 Subject: [PATCH 5/8] Move param to the argument_spec function --- plugins/modules/helm_repository.py | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index bf6e6d9fce..81a4136da1 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -261,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 @@ -271,28 +272,6 @@ def main(): module = AnsibleModule( argument_spec=argument_spec(), - # Generic auth key - host=dict(type="str", fallback=(env_fallback, ["K8S_AUTH_HOST"])), - ca_cert=dict( - type="path", - aliases=["ssl_ca_cert"], - fallback=(env_fallback, ["K8S_AUTH_SSL_CA_CERT"]), - ), - validate_certs=dict( - type="bool", - default=True, - aliases=["verify_ssl"], - fallback=(env_fallback, ["K8S_AUTH_VERIFY_SSL"]), - ), - force_update=dict( - type="bool", - default=False, - aliases=["force"], - ), - api_key=dict( - type="str", no_log=True, fallback=(env_fallback, ["K8S_AUTH_API_KEY"]) - ), - ), required_together=[["repo_username", "repo_password"]], required_if=[("repo_state", "present", ["repo_url"])], mutually_exclusive=HELM_AUTH_MUTUALLY_EXCLUSIVE, From e63422921e235bb67c8cd66243d55229b4357508 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Wed, 21 Sep 2022 11:38:01 +0300 Subject: [PATCH 6/8] Set the right 'version_added' --- plugins/modules/helm_repository.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 81a4136da1..944232402e 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -118,7 +118,7 @@ type: bool aliases: [ force ] default: False - version_added: "2.3.3" + version_added: "2.4.0" """ EXAMPLES = r""" From d2e392c30bdb9a79e6adee5e218ec2c7e8627773 Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Wed, 21 Sep 2022 11:43:08 +0300 Subject: [PATCH 7/8] lint --- plugins/modules/helm_repository.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/modules/helm_repository.py b/plugins/modules/helm_repository.py index 944232402e..cb232a5f69 100644 --- a/plugins/modules/helm_repository.py +++ b/plugins/modules/helm_repository.py @@ -225,7 +225,7 @@ def install_repository( repository_username, repository_password, pass_credentials, - force_update + force_update, ): install_command = command + " repo add " + repository_name + " " + repository_url @@ -307,7 +307,7 @@ def main(): repo_username, repo_password, pass_credentials, - force_update + force_update, ) changed = True elif repository_status["url"] != repo_url: From bfbf8c8d1426002a023facba9d8b5277799285fd Mon Sep 17 00:00:00 2001 From: Mor Cohen Date: Wed, 21 Sep 2022 11:46:05 +0300 Subject: [PATCH 8/8] Add a changelog fragment --- .../fragments/509-helm-repo-add-force_update-argument.yaml | 3 +++ 1 file changed, 3 insertions(+) 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 0000000000..ba7588210f --- /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).