mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-08 22:12:44 +00:00
helm: handle multiline output (#64)
Fixes: #399 Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
2
changelogs/fragments/399-helm_multiline.yml
Normal file
2
changelogs/fragments/399-helm_multiline.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bugfixes:
|
||||||
|
- helm - handle multiline output of ``helm plugin list`` command (https://github.com/ansible-collections/community.kubernetes/issues/399).
|
||||||
11
molecule/default/roles/helm/files/sample_plugin/plugin.yaml
Normal file
11
molecule/default/roles/helm/files/sample_plugin/plugin.yaml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
name: "sample_plugin"
|
||||||
|
version: "0.0.1"
|
||||||
|
usage: "Sample Helm Plugin"
|
||||||
|
description: |-
|
||||||
|
This plugin provides sample plugin to Helm.
|
||||||
|
usage:
|
||||||
|
This is new line
|
||||||
|
This is another line
|
||||||
|
ignoreFlags: false
|
||||||
|
useTunnel: false
|
||||||
|
command: "$HELM_PLUGIN_DIR/main.sh"
|
||||||
@@ -75,3 +75,36 @@
|
|||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- not uninstall_env.changed
|
- not uninstall_env.changed
|
||||||
|
|
||||||
|
# https://github.com/ansible-collections/community.kubernetes/issues/399
|
||||||
|
- name: Copy required plugin files
|
||||||
|
copy:
|
||||||
|
src: "files/sample_plugin"
|
||||||
|
dest: "/tmp/helm_plugin_test/"
|
||||||
|
|
||||||
|
- name: Install sample_plugin from the directory
|
||||||
|
helm_plugin:
|
||||||
|
binary_path: "{{ helm_binary }}"
|
||||||
|
state: present
|
||||||
|
plugin_path: "/tmp/helm_plugin_test/sample_plugin"
|
||||||
|
register: sample_plugin_output
|
||||||
|
|
||||||
|
- name: Assert that sample_plugin is installed or not
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- sample_plugin_output.changed
|
||||||
|
|
||||||
|
- name: Gather Helm plugin info
|
||||||
|
helm_plugin_info:
|
||||||
|
register: r
|
||||||
|
|
||||||
|
- name: Set sample_plugin version
|
||||||
|
set_fact:
|
||||||
|
plugin_version: "{{ ( r.plugin_list | selectattr('name', 'equalto', plugin_name) | list )[0].version }}"
|
||||||
|
vars:
|
||||||
|
plugin_name: "sample_plugin"
|
||||||
|
|
||||||
|
- name: Assert if sample_plugin with multiline comment is installed
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- plugin_version == "0.0.1"
|
||||||
|
|||||||
@@ -117,3 +117,44 @@ def write_temp_kubeconfig(server, validate_certs=True, ca_cert=None):
|
|||||||
with os.fdopen(_fd, 'w') as fp:
|
with os.fdopen(_fd, 'w') as fp:
|
||||||
yaml.dump(content, fp)
|
yaml.dump(content, fp)
|
||||||
return file_name
|
return file_name
|
||||||
|
|
||||||
|
|
||||||
|
def get_helm_plugin_list(module, helm_bin=None):
|
||||||
|
"""
|
||||||
|
Return `helm plugin list`
|
||||||
|
"""
|
||||||
|
if not helm_bin:
|
||||||
|
return []
|
||||||
|
helm_plugin_list = helm_bin + " list"
|
||||||
|
rc, out, err = run_helm(module, helm_plugin_list)
|
||||||
|
if rc != 0 or (out == '' and err == ''):
|
||||||
|
module.fail_json(
|
||||||
|
msg="Failed to get Helm plugin info",
|
||||||
|
command=helm_plugin_list,
|
||||||
|
stdout=out,
|
||||||
|
stderr=err,
|
||||||
|
rc=rc,
|
||||||
|
)
|
||||||
|
return (rc, out, err)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_helm_plugin_list(module, output=None):
|
||||||
|
"""
|
||||||
|
Parse `helm plugin list`, return list of plugins
|
||||||
|
"""
|
||||||
|
ret = []
|
||||||
|
if not output:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
for line in output:
|
||||||
|
if line.startswith("NAME"):
|
||||||
|
continue
|
||||||
|
name, version, description = line.split('\t', 3)
|
||||||
|
name = name.strip()
|
||||||
|
version = version.strip()
|
||||||
|
description = description.strip()
|
||||||
|
if name == '':
|
||||||
|
continue
|
||||||
|
ret.append((name, version, description))
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|||||||
@@ -272,7 +272,12 @@ except ImportError:
|
|||||||
IMP_YAML = False
|
IMP_YAML = False
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib, env_fallback
|
from ansible.module_utils.basic import AnsibleModule, missing_required_lib, env_fallback
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.helm import run_helm, get_values
|
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
|
||||||
|
run_helm,
|
||||||
|
get_values,
|
||||||
|
get_helm_plugin_list,
|
||||||
|
parse_helm_plugin_list
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_release(state, release_name):
|
def get_release(state, release_name):
|
||||||
@@ -413,13 +418,15 @@ def has_plugin(command, plugin):
|
|||||||
Check if helm plugin is installed.
|
Check if helm plugin is installed.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
cmd = command + " plugin list"
|
cmd = command + " plugin"
|
||||||
rc, out, err = run_helm(module, cmd)
|
rc, output, err = get_helm_plugin_list(module, helm_bin=cmd)
|
||||||
for line in out.splitlines():
|
out = parse_helm_plugin_list(module, output=output.splitlines())
|
||||||
if line.startswith("NAME"):
|
|
||||||
continue
|
if not out:
|
||||||
name, _rest = line.split(None, 1)
|
return False
|
||||||
if name == plugin:
|
|
||||||
|
for line in out:
|
||||||
|
if line[0] == plugin:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
# Copyright: (c) 2020, Ansible Project
|
# Copyright: (c) 2020, Ansible Project
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
@@ -96,7 +97,11 @@ rc:
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.helm import run_helm
|
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
|
||||||
|
run_helm,
|
||||||
|
get_helm_plugin_list,
|
||||||
|
parse_helm_plugin_list
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -180,60 +185,58 @@ def main():
|
|||||||
)
|
)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
plugin_name = module.params.get('plugin_name')
|
plugin_name = module.params.get('plugin_name')
|
||||||
helm_plugin_list = helm_cmd_common + " list"
|
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_cmd_common)
|
||||||
rc, out, err = run_helm(module, helm_plugin_list)
|
out = parse_helm_plugin_list(module, output=output.splitlines())
|
||||||
if rc != 0 or (out == '' and err == ''):
|
|
||||||
module.fail_json(
|
if not out:
|
||||||
msg="Failed to get Helm plugin info",
|
module.exit_json(
|
||||||
command=helm_plugin_list,
|
failed=False,
|
||||||
stdout=out,
|
changed=False,
|
||||||
|
msg="Plugin not found or is already uninstalled",
|
||||||
|
command=helm_cmd_common + " list",
|
||||||
|
stdout=output,
|
||||||
stderr=err,
|
stderr=err,
|
||||||
rc=rc,
|
rc=rc
|
||||||
)
|
)
|
||||||
|
|
||||||
if out:
|
found = False
|
||||||
found = False
|
for line in out:
|
||||||
for line in out.splitlines():
|
if line[0] == plugin_name:
|
||||||
if line.startswith("NAME"):
|
found = True
|
||||||
continue
|
break
|
||||||
name, dummy, dummy = line.split('\t', 3)
|
if not found:
|
||||||
name = name.strip()
|
module.exit_json(
|
||||||
if name == plugin_name:
|
failed=False,
|
||||||
found = True
|
changed=False,
|
||||||
break
|
msg="Plugin not found or is already uninstalled",
|
||||||
if found:
|
command=helm_cmd_common + " list",
|
||||||
helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
|
stdout=output,
|
||||||
if not module.check_mode:
|
stderr=err,
|
||||||
rc, out, err = run_helm(module, helm_uninstall_cmd, fails_on_error=False)
|
rc=rc
|
||||||
else:
|
)
|
||||||
rc, out, err = (0, '', '')
|
|
||||||
|
|
||||||
if rc == 0:
|
helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
|
||||||
module.exit_json(
|
if not module.check_mode:
|
||||||
changed=True,
|
rc, out, err = run_helm(module, helm_uninstall_cmd, fails_on_error=False)
|
||||||
msg="Plugin uninstalled successfully",
|
else:
|
||||||
command=helm_uninstall_cmd,
|
rc, out, err = (0, '', '')
|
||||||
stdout=out,
|
|
||||||
stderr=err,
|
if rc == 0:
|
||||||
rc=rc
|
module.exit_json(
|
||||||
)
|
changed=True,
|
||||||
module.fail_json(
|
msg="Plugin uninstalled successfully",
|
||||||
msg="Failed to get Helm plugin uninstall",
|
command=helm_uninstall_cmd,
|
||||||
command=helm_uninstall_cmd,
|
stdout=out,
|
||||||
stdout=out,
|
stderr=err,
|
||||||
stderr=err,
|
rc=rc
|
||||||
rc=rc,
|
)
|
||||||
)
|
module.fail_json(
|
||||||
else:
|
msg="Failed to get Helm plugin uninstall",
|
||||||
module.exit_json(
|
command=helm_uninstall_cmd,
|
||||||
failed=False,
|
stdout=out,
|
||||||
changed=False,
|
stderr=err,
|
||||||
msg="Plugin not found or is already uninstalled",
|
rc=rc,
|
||||||
command=helm_plugin_list,
|
)
|
||||||
stdout=out,
|
|
||||||
stderr=err,
|
|
||||||
rc=rc
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
# Copyright: (c) 2020, Ansible Project
|
# Copyright: (c) 2020, Ansible Project
|
||||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
@@ -77,7 +78,10 @@ rc:
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.helm import run_helm
|
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
|
||||||
|
get_helm_plugin_list,
|
||||||
|
parse_helm_plugin_list,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -117,46 +121,38 @@ def main():
|
|||||||
helm_cmd_common += " plugin"
|
helm_cmd_common += " plugin"
|
||||||
|
|
||||||
plugin_name = module.params.get('plugin_name')
|
plugin_name = module.params.get('plugin_name')
|
||||||
helm_plugin_list = helm_cmd_common + " list"
|
|
||||||
rc, out, err = run_helm(module, helm_plugin_list)
|
|
||||||
if rc != 0 or (out == '' and err == ''):
|
|
||||||
module.fail_json(
|
|
||||||
msg="Failed to get Helm plugin info",
|
|
||||||
command=helm_plugin_list,
|
|
||||||
stdout=out,
|
|
||||||
stderr=err,
|
|
||||||
rc=rc,
|
|
||||||
)
|
|
||||||
|
|
||||||
plugin_list = []
|
plugin_list = []
|
||||||
if out:
|
|
||||||
for line in out.splitlines():
|
|
||||||
if line.startswith("NAME"):
|
|
||||||
continue
|
|
||||||
name, version, description = line.split('\t', 3)
|
|
||||||
name = name.strip()
|
|
||||||
version = version.strip()
|
|
||||||
description = description.strip()
|
|
||||||
if plugin_name is None:
|
|
||||||
plugin_list.append({
|
|
||||||
'name': name,
|
|
||||||
'version': version,
|
|
||||||
'description': description,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
|
|
||||||
if plugin_name == name:
|
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_cmd_common)
|
||||||
plugin_list.append({
|
|
||||||
'name': name,
|
out = parse_helm_plugin_list(module, output=output.splitlines())
|
||||||
'version': version,
|
|
||||||
'description': description,
|
for line in out:
|
||||||
})
|
if plugin_name is None:
|
||||||
break
|
plugin_list.append(
|
||||||
|
{
|
||||||
|
"name": line[0],
|
||||||
|
"version": line[1],
|
||||||
|
"description": line[2],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if plugin_name == line[0]:
|
||||||
|
plugin_list.append(
|
||||||
|
{
|
||||||
|
"name": line[0],
|
||||||
|
"version": line[1],
|
||||||
|
"description": line[2],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
break
|
||||||
|
|
||||||
module.exit_json(
|
module.exit_json(
|
||||||
changed=True,
|
changed=True,
|
||||||
command=helm_plugin_list,
|
command=helm_cmd_common + " list",
|
||||||
stdout=out,
|
stdout=output,
|
||||||
stderr=err,
|
stderr=err,
|
||||||
rc=rc,
|
rc=rc,
|
||||||
plugin_list=plugin_list,
|
plugin_list=plugin_list,
|
||||||
|
|||||||
Reference in New Issue
Block a user