From ab0e38753bc02906cc0f44ef9ad7c18970e53daa Mon Sep 17 00:00:00 2001 From: abikouo <79859644+abikouo@users.noreply.github.com> Date: Wed, 29 Sep 2021 13:06:33 +0200 Subject: [PATCH] add plugin_version parameter for helm_plugin module (#226) add plugin_version parameter for helm_plugin module SUMMARY closes #157 ISSUE TYPE Feature Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Abhijeet Kasurde Reviewed-by: Mike Graves Reviewed-by: None --- ...6-add-version-parameter-to-helm_plugin.yml | 3 + .../roles/helm/tasks/tests_helm_plugin.yml | 46 ++++++++++ plugins/modules/helm_plugin.py | 84 ++++++++++++++++++- 3 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/226-add-version-parameter-to-helm_plugin.yml diff --git a/changelogs/fragments/226-add-version-parameter-to-helm_plugin.yml b/changelogs/fragments/226-add-version-parameter-to-helm_plugin.yml new file mode 100644 index 00000000..8ff2ffa6 --- /dev/null +++ b/changelogs/fragments/226-add-version-parameter-to-helm_plugin.yml @@ -0,0 +1,3 @@ +minor_changes: + - helm_plugin - Add plugin_version parameter to the helm_plugin module (https://github.com/ansible-collections/kubernetes.core/issues/157). + - helm_plugin - Add support for helm plugin update using state=update. diff --git a/molecule/default/roles/helm/tasks/tests_helm_plugin.yml b/molecule/default/roles/helm/tasks/tests_helm_plugin.yml index d64573c2..72f0d7a3 100644 --- a/molecule/default/roles/helm/tasks/tests_helm_plugin.yml +++ b/molecule/default/roles/helm/tasks/tests_helm_plugin.yml @@ -117,3 +117,49 @@ state: absent plugin_name: sample_plugin ignore_errors: yes + +- block: + - name: uninstall helm plugin secrets + helm_plugin: + binary_path: "{{ helm_binary }}" + plugin_name: secrets + state: absent + + - name: install helm-secrets on a specific version + helm_plugin: + binary_path: "{{ helm_binary }}" + plugin_path: https://github.com/jkroepke/helm-secrets + plugin_version: 3.4.1 + state: present + + - name: list helm plugin + helm_plugin_info: + plugin_name: secrets + binary_path: "{{ helm_binary }}" + register: plugin_list + + - name: assert that secrets has been installed with specified version + assert: + that: + - plugin_list.plugin_list[0].version == "3.4.1" + + - name: Update helm plugin version to latest + helm_plugin: + binary_path: "{{ helm_binary }}" + plugin_name: secrets + state: latest + register: _update + + - name: assert update was performed + assert: + that: + - _update.changed + - '"Updated plugin: secrets" in _update.stdout' + + always: + - name: Uninstall sample_plugin + helm_plugin: + binary_path: "{{ helm_binary }}" + state: absent + plugin_name: secrets + ignore_errors: yes diff --git a/plugins/modules/helm_plugin.py b/plugins/modules/helm_plugin.py index 222ab8f8..f8149687 100644 --- a/plugins/modules/helm_plugin.py +++ b/plugins/modules/helm_plugin.py @@ -24,14 +24,15 @@ options: state: description: - If C(state=present) the Helm plugin will be installed. + - If C(state=latest) the Helm plugin will be updated. Added in version 2.3.0. - If C(state=absent) the Helm plugin will be removed. - choices: [ absent, present ] + choices: [ absent, present, latest ] default: present type: str plugin_name: description: - Name of Helm plugin. - - Required only if C(state=absent). + - Required only if C(state=absent) or C(state=latest). type: str plugin_path: description: @@ -40,6 +41,13 @@ options: machine and not on Ansible controller. - Required only if C(state=present). type: str + plugin_version: + description: + - Plugin version to install. If this is not specified, the latest version is installed. + - Ignored when C(state=absent) or C(state=latest). + required: false + type: str + version_added: "2.3.0" extends_documentation_fragment: - kubernetes.core.helm_common_options ''' @@ -59,6 +67,17 @@ EXAMPLES = r''' kubernetes.core.helm_plugin: plugin_name: env state: absent + +- name: Install Helm plugin with a specific version + kubernetes.core.helm_plugin: + plugin_version: 2.0.1 + plugin_path: https://domain/path/to/plugin.tar.gz + state: present + +- name: Update Helm plugin + kubernetes.core.helm_plugin: + plugin_name: secrets + state: latest ''' RETURN = r''' @@ -101,9 +120,10 @@ def main(): module = AnsibleModule( argument_spec=dict( binary_path=dict(type='path'), - state=dict(type='str', default='present', choices=['present', 'absent']), + state=dict(type='str', default='present', choices=['present', 'absent', 'latest']), plugin_path=dict(type='str',), plugin_name=dict(type='str',), + plugin_version=dict(type='str',), # Helm options context=dict(type='str', aliases=['kube_context'], fallback=(env_fallback, ['K8S_AUTH_CONTEXT'])), kubeconfig=dict(type='path', aliases=['kubeconfig_path'], fallback=(env_fallback, ['K8S_AUTH_KUBECONFIG'])), @@ -118,6 +138,7 @@ def main(): required_if=[ ("state", "present", ("plugin_path",)), ("state", "absent", ("plugin_name",)), + ("state", "latest", ("plugin_name",)), ], mutually_exclusive=[ ('plugin_name', 'plugin_path'), @@ -142,6 +163,9 @@ def main(): if state == 'present': helm_cmd_common += " install %s" % module.params.get('plugin_path') + plugin_version = module.params.get('plugin_version') + if plugin_version is not None: + helm_cmd_common += " --version=%s" % plugin_version if not module.check_mode: rc, out, err = run_helm(module, helm_cmd_common, fails_on_error=False) else: @@ -229,6 +253,60 @@ def main(): stderr=err, rc=rc, ) + elif state == 'latest': + plugin_name = module.params.get('plugin_name') + rc, output, err = get_helm_plugin_list(module, helm_bin=helm_cmd_common) + out = parse_helm_plugin_list(module, output=output.splitlines()) + + if not out: + module.exit_json( + failed=False, + changed=False, + msg="Plugin not found", + command=helm_cmd_common + " list", + stdout=output, + stderr=err, + rc=rc + ) + + found = False + for line in out: + if line[0] == plugin_name: + found = True + break + if not found: + module.exit_json( + failed=False, + changed=False, + msg="Plugin not found", + command=helm_cmd_common + " list", + stdout=output, + stderr=err, + rc=rc + ) + + helm_update_cmd = "%s update %s" % (helm_cmd_common, plugin_name) + if not module.check_mode: + rc, out, err = run_helm(module, helm_update_cmd, fails_on_error=False) + else: + rc, out, err = (0, '', '') + + if rc == 0: + module.exit_json( + changed=True, + msg="Plugin updated successfully", + command=helm_update_cmd, + stdout=out, + stderr=err, + rc=rc + ) + module.fail_json( + msg="Failed to get Helm plugin update", + command=helm_update_cmd, + stdout=out, + stderr=err, + rc=rc, + ) if __name__ == '__main__':