Helm - Fix issue with alternative kubeconfig (#563)

Helm - Fix issue with alternative kubeconfig

SUMMARY

closes #538

ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

helm modules

Reviewed-by: Mike Graves <mgraves@redhat.com>
This commit is contained in:
Bikouo Aubin
2023-01-12 10:46:42 +01:00
committed by GitHub
parent 26cd550bc0
commit 804b9ab57c
20 changed files with 872 additions and 384 deletions

View File

@@ -350,14 +350,10 @@ except ImportError:
IMP_YAML_ERR = traceback.format_exc()
IMP_YAML = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_values,
get_helm_plugin_list,
AnsibleHelmModule,
parse_helm_plugin_list,
get_helm_version,
get_helm_binary,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
@@ -376,39 +372,41 @@ def get_release(state, release_name):
return None
def get_release_status(module, command, release_name):
def get_release_status(module, release_name):
"""
Get Release state from deployed release
"""
list_command = command + " list --output=yaml --filter " + release_name
list_command = (
module.get_helm_binary() + " list --output=yaml --filter " + release_name
)
rc, out, err = run_helm(module, list_command)
rc, out, err = module.run_helm_command(list_command)
release = get_release(yaml.safe_load(out), release_name)
if release is None: # not install
return None
release["values"] = get_values(module, command, release_name)
release["values"] = module.get_values(release_name)
return release
def run_repo_update(module, command):
def run_repo_update(module):
"""
Run Repo update
"""
repo_update_command = command + " repo update"
rc, out, err = run_helm(module, repo_update_command)
repo_update_command = module.get_helm_binary() + " repo update"
rc, out, err = module.run_helm_command(repo_update_command)
def run_dep_update(module, command, chart_ref):
def run_dep_update(module, chart_ref):
"""
Run dependency update
"""
dep_update = command + " dependency update " + chart_ref
rc, out, err = run_helm(module, dep_update)
dep_update = module.get_helm_binary() + " dependency update " + chart_ref
rc, out, err = module.run_helm_command(dep_update)
def fetch_chart_info(module, command, chart_ref):
@@ -417,7 +415,7 @@ def fetch_chart_info(module, command, chart_ref):
"""
inspect_command = command + " show chart " + chart_ref
rc, out, err = run_helm(module, inspect_command)
rc, out, err = module.run_helm_command(inspect_command)
return yaml.safe_load(out)
@@ -537,13 +535,13 @@ def load_values_files(values_files):
return values
def get_plugin_version(helm_bin, plugin):
def get_plugin_version(plugin):
"""
Check if helm plugin is installed and return corresponding version
"""
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_bin)
out = parse_helm_plugin_list(module, output=output.splitlines())
rc, output, err, command = module.get_helm_plugin_list()
out = parse_helm_plugin_list(output=output.splitlines())
if not out:
return None
@@ -556,7 +554,6 @@ def get_plugin_version(helm_bin, plugin):
def helmdiff_check(
module,
helm_cmd,
release_name,
chart_ref,
release_values,
@@ -568,7 +565,7 @@ def helmdiff_check(
"""
Use helm diff to determine if a release would change by upgrading a chart.
"""
cmd = helm_cmd + " diff upgrade"
cmd = module.get_helm_binary() + " diff upgrade"
cmd += " " + release_name
cmd += " " + chart_ref
@@ -584,12 +581,13 @@ def helmdiff_check(
with open(path, "w") as yaml_file:
yaml.dump(release_values, yaml_file, default_flow_style=False)
cmd += " -f=" + path
module.add_cleanup_file(path)
if values_files:
for values_file in values_files:
cmd += " -f=" + values_file
rc, out, err = run_helm(module, cmd)
rc, out, err = module.run_helm_command(cmd)
return (len(out.strip()) > 0, out.strip())
@@ -652,7 +650,7 @@ def argument_spec():
def main():
global module
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argument_spec(),
required_if=[
("release_state", "present", ["release_name", "chart_ref"]),
@@ -660,7 +658,6 @@ def main():
],
mutually_exclusive=[
("context", "ca_cert"),
("kubeconfig", "ca_cert"),
("replace", "history_max"),
("wait_timeout", "timeout"),
],
@@ -696,24 +693,20 @@ def main():
history_max = module.params.get("history_max")
timeout = module.params.get("timeout")
helm_cmd_common = get_helm_binary(module)
helm_bin = helm_cmd_common
if update_repo_cache:
run_repo_update(module, helm_cmd_common)
run_repo_update(module)
# Get real/deployed release status
release_status = get_release_status(module, helm_cmd_common, release_name)
release_status = get_release_status(module, release_name)
# keep helm_cmd_common for get_release_status in module_exit_json
helm_cmd = helm_cmd_common
helm_cmd = module.get_helm_binary()
opt_result = {}
if release_state == "absent" and release_status is not None:
if replace:
module.fail_json(msg="replace is not applicable when state is absent")
if wait:
helm_version = get_helm_version(module, helm_cmd_common)
helm_version = module.get_helm_version()
if LooseVersion(helm_version) < LooseVersion("3.7.0"):
opt_result["warnings"] = []
opt_result["warnings"].append(
@@ -746,7 +739,7 @@ def main():
if not chart_repo_url and not re.fullmatch(
r"^http[s]*://[\w.:/?&=-]+$", chart_ref
):
run_dep_update(module, helm_cmd_common, chart_ref)
run_dep_update(module, chart_ref)
# To not add --dependency-update option in the deploy function
dependency_update = False
@@ -790,7 +783,7 @@ def main():
else:
helm_diff_version = get_plugin_version(helm_bin, "diff")
helm_diff_version = get_plugin_version("diff")
if helm_diff_version and (
not chart_repo_url
or (
@@ -800,7 +793,6 @@ def main():
):
(would_change, prepared) = helmdiff_check(
module,
helm_cmd_common,
release_name,
chart_ref,
release_values,
@@ -866,13 +858,13 @@ def main():
**opt_result,
)
rc, out, err = run_helm(module, helm_cmd)
rc, out, err = module.run_helm_command(helm_cmd)
module.exit_json(
changed=changed,
stdout=out,
stderr=err,
status=get_release_status(module, helm_cmd_common, release_name),
status=get_release_status(module, release_name),
command=helm_cmd,
**opt_result,
)

View File

@@ -131,11 +131,9 @@ except ImportError:
IMP_YAML_ERR = traceback.format_exc()
IMP_YAML = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_values,
get_helm_binary,
AnsibleHelmModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
@@ -153,10 +151,8 @@ def get_release(state, release_name):
# Get Release state from deployed release
def get_release_status(
module, command, release_name, release_state, get_all_values=False
):
list_command = command + " list --output=yaml"
def get_release_status(module, release_name, release_state, get_all_values=False):
list_command = module.get_helm_binary() + " list --output=yaml"
valid_release_states = [
"all",
@@ -173,7 +169,7 @@ def get_release_status(
list_command += " --%s" % local_release_state
list_command += " --filter " + release_name
rc, out, err = run_helm(module, list_command)
rc, out, err = module.run_helm_command(list_command)
if rc != 0:
module.fail_json(
@@ -188,7 +184,7 @@ def get_release_status(
if release is None: # not install
return None
release["values"] = get_values(module, command, release_name, get_all_values)
release["values"] = module.get_values(release_name, get_all_values)
return release
@@ -209,7 +205,7 @@ def argument_spec():
def main():
global module
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argument_spec(),
mutually_exclusive=HELM_AUTH_MUTUALLY_EXCLUSIVE,
supports_check_mode=True,
@@ -222,10 +218,8 @@ def main():
release_state = module.params.get("release_state")
get_all_values = module.params.get("get_all_values")
helm_cmd_common = get_helm_binary(module)
release_status = get_release_status(
module, helm_cmd_common, release_name, release_state, get_all_values
module, release_name, release_state, get_all_values
)
if release_status is not None:

View File

@@ -109,12 +109,9 @@ rc:
"""
import copy
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_helm_plugin_list,
AnsibleHelmModule,
parse_helm_plugin_list,
get_helm_binary,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
@@ -152,7 +149,7 @@ def mutually_exclusive():
def main():
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argument_spec(),
supports_check_mode=True,
required_if=[
@@ -164,9 +161,8 @@ def main():
)
state = module.params.get("state")
helm_bin = get_helm_binary(module)
helm_cmd_common = helm_bin + " plugin"
helm_cmd_common = module.get_helm_binary() + " plugin"
if state == "present":
helm_cmd_common += " install %s" % module.params.get("plugin_path")
@@ -174,7 +170,9 @@ def main():
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)
rc, out, err = module.run_helm_command(
helm_cmd_common, fails_on_error=False
)
else:
rc, out, err = (0, "", "")
@@ -208,15 +206,15 @@ def main():
)
elif state == "absent":
plugin_name = module.params.get("plugin_name")
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_bin)
out = parse_helm_plugin_list(module, output=output.splitlines())
rc, output, err, command = module.get_helm_plugin_list()
out = parse_helm_plugin_list(output=output.splitlines())
if not out:
module.exit_json(
failed=False,
changed=False,
msg="Plugin not found or is already uninstalled",
command=helm_cmd_common + " list",
command=command,
stdout=output,
stderr=err,
rc=rc,
@@ -232,7 +230,7 @@ def main():
failed=False,
changed=False,
msg="Plugin not found or is already uninstalled",
command=helm_cmd_common + " list",
command=command,
stdout=output,
stderr=err,
rc=rc,
@@ -240,7 +238,9 @@ def main():
helm_uninstall_cmd = "%s uninstall %s" % (helm_cmd_common, plugin_name)
if not module.check_mode:
rc, out, err = run_helm(module, helm_uninstall_cmd, fails_on_error=False)
rc, out, err = module.run_helm_command(
helm_uninstall_cmd, fails_on_error=False
)
else:
rc, out, err = (0, "", "")
@@ -262,15 +262,15 @@ def main():
)
elif state == "latest":
plugin_name = module.params.get("plugin_name")
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_bin)
out = parse_helm_plugin_list(module, output=output.splitlines())
rc, output, err, command = module.get_helm_plugin_list()
out = parse_helm_plugin_list(output=output.splitlines())
if not out:
module.exit_json(
failed=False,
changed=False,
msg="Plugin not found",
command=helm_cmd_common + " list",
command=command,
stdout=output,
stderr=err,
rc=rc,
@@ -286,7 +286,7 @@ def main():
failed=False,
changed=False,
msg="Plugin not found",
command=helm_cmd_common + " list",
command=command,
stdout=output,
stderr=err,
rc=rc,
@@ -294,7 +294,9 @@ def main():
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)
rc, out, err = module.run_helm_command(
helm_update_cmd, fails_on_error=False
)
else:
rc, out, err = (0, "", "")

View File

@@ -71,11 +71,9 @@ rc:
"""
import copy
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
get_helm_plugin_list,
parse_helm_plugin_list,
get_helm_binary,
AnsibleHelmModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
@@ -94,21 +92,19 @@ def main():
)
)
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argument_spec,
mutually_exclusive=HELM_AUTH_MUTUALLY_EXCLUSIVE,
supports_check_mode=True,
)
helm_bin = get_helm_binary(module)
plugin_name = module.params.get("plugin_name")
plugin_list = []
rc, output, err = get_helm_plugin_list(module, helm_bin=helm_bin)
rc, output, err, command = module.get_helm_plugin_list()
out = parse_helm_plugin_list(module, output=output.splitlines())
out = parse_helm_plugin_list(output=output.splitlines())
for line in out:
if plugin_name is None:
@@ -125,7 +121,7 @@ def main():
module.exit_json(
changed=True,
command=helm_bin + " plugin list",
command=command,
stdout=output,
stderr=err,
rc=rc,

View File

@@ -169,10 +169,8 @@ rc:
sample: 1
"""
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_helm_version,
AnsibleHelmModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
LooseVersion,
@@ -201,7 +199,7 @@ def main():
chart_ssl_key_file=dict(type="path"),
binary_path=dict(type="path"),
)
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argspec,
supports_check_mode=True,
required_by=dict(
@@ -211,15 +209,7 @@ def main():
mutually_exclusive=[("chart_version", "chart_devel")],
)
bin_path = module.params.get("binary_path")
if bin_path is not None:
helm_cmd_common = bin_path
else:
helm_cmd_common = "helm"
helm_cmd_common = module.get_bin_path(helm_cmd_common, required=True)
helm_version = get_helm_version(module, helm_cmd_common)
helm_version = module.get_helm_version()
if LooseVersion(helm_version) < LooseVersion("3.0.0"):
module.fail_json(
msg="This module requires helm >= 3.0.0, current version is {0}".format(
@@ -279,10 +269,12 @@ def main():
helm_pull_opts.append("--{0}".format(v["key"]))
helm_cmd_common = "{0} pull {1} {2}".format(
helm_cmd_common, module.params.get("chart_ref"), " ".join(helm_pull_opts)
module.get_helm_binary(),
module.params.get("chart_ref"),
" ".join(helm_pull_opts),
)
if not module.check_mode:
rc, out, err = run_helm(module, helm_cmd_common, fails_on_error=False)
rc, out, err = module.run_helm_command(helm_cmd_common, fails_on_error=False)
else:
rc, out, err = (0, "", "")

View File

@@ -178,10 +178,9 @@ except ImportError:
IMP_YAML_ERR = traceback.format_exc()
IMP_YAML = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_helm_binary,
AnsibleHelmModule,
)
from ansible_collections.kubernetes.core.plugins.module_utils.helm_args_common import (
HELM_AUTH_ARG_SPEC,
@@ -199,10 +198,10 @@ def get_repository(state, repo_name):
# Get repository status
def get_repository_status(module, command, repository_name):
list_command = command + " repo list --output=yaml"
def get_repository_status(module, repository_name):
list_command = module.get_helm_binary() + " repo list --output=yaml"
rc, out, err = run_helm(module, list_command, fails_on_error=False)
rc, out, err = module.run_helm_command(list_command, fails_on_error=False)
# no repo => rc=1 and 'no repositories to show' in output
if rc == 1 and "no repositories to show" in err:
@@ -271,7 +270,7 @@ def argument_spec():
def main():
global module
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=argument_spec(),
required_together=[["repo_username", "repo_password"]],
required_if=[("repo_state", "present", ["repo_url"])],
@@ -292,9 +291,9 @@ def main():
pass_credentials = module.params.get("pass_credentials")
force_update = module.params.get("force_update")
helm_cmd = get_helm_binary(module)
helm_cmd = module.get_helm_binary()
repository_status = get_repository_status(module, helm_cmd, repo_name)
repository_status = get_repository_status(module, repo_name)
if repo_state == "absent" and repository_status is not None:
helm_cmd = delete_repository(helm_cmd, repo_name)
@@ -321,7 +320,7 @@ def main():
elif not changed:
module.exit_json(changed=False, repo_name=repo_name, repo_url=repo_url)
rc, out, err = run_helm(module, helm_cmd)
rc, out, err = module.run_helm_command(helm_cmd)
if repo_password is not None:
helm_cmd = helm_cmd.replace(repo_password, "******")

View File

@@ -181,10 +181,9 @@ except ImportError:
IMP_YAML_ERR = traceback.format_exc()
IMP_YAML = False
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.kubernetes.core.plugins.module_utils.helm import (
run_helm,
get_helm_binary,
AnsibleHelmModule,
)
@@ -249,7 +248,7 @@ def template(
def main():
module = AnsibleModule(
module = AnsibleHelmModule(
argument_spec=dict(
binary_path=dict(type="path"),
chart_ref=dict(type="path", required=True),
@@ -287,11 +286,11 @@ def main():
if not IMP_YAML:
module.fail_json(msg=missing_required_lib("yaml"), exception=IMP_YAML_ERR)
helm_cmd = get_helm_binary(module)
helm_cmd = module.get_helm_binary()
if update_repo_cache:
update_cmd = helm_cmd + " repo update"
run_helm(module, update_cmd)
module.run_helm_command(update_cmd)
tmpl_cmd = template(
helm_cmd,
@@ -310,7 +309,7 @@ def main():
)
if not check_mode:
rc, out, err = run_helm(module, tmpl_cmd)
rc, out, err = module.run_helm_command(tmpl_cmd)
else:
out = err = ""
rc = 0