mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-06 13:02:37 +00:00
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:
@@ -12,9 +12,14 @@ import tempfile
|
||||
import traceback
|
||||
import re
|
||||
import json
|
||||
import copy
|
||||
|
||||
from ansible.module_utils.basic import missing_required_lib
|
||||
from ansible.module_utils.six import string_types
|
||||
from ansible_collections.kubernetes.core.plugins.module_utils.version import (
|
||||
LooseVersion,
|
||||
)
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
|
||||
try:
|
||||
import yaml
|
||||
@@ -26,119 +31,7 @@ except ImportError:
|
||||
HAS_YAML = False
|
||||
|
||||
|
||||
def prepare_helm_environ_update(module):
|
||||
environ_update = {}
|
||||
kubeconfig_path = None
|
||||
if module.params.get("kubeconfig") is not None:
|
||||
kubeconfig = module.params.get("kubeconfig")
|
||||
if isinstance(kubeconfig, string_types):
|
||||
kubeconfig_path = kubeconfig
|
||||
elif isinstance(kubeconfig, dict):
|
||||
fd, kubeconfig_path = tempfile.mkstemp()
|
||||
with os.fdopen(fd, "w") as fp:
|
||||
json.dump(kubeconfig, fp)
|
||||
module.add_cleanup_file(kubeconfig_path)
|
||||
if module.params.get("context") is not None:
|
||||
environ_update["HELM_KUBECONTEXT"] = module.params.get("context")
|
||||
if module.params.get("release_namespace"):
|
||||
environ_update["HELM_NAMESPACE"] = module.params.get("release_namespace")
|
||||
if module.params.get("api_key"):
|
||||
environ_update["HELM_KUBETOKEN"] = module.params["api_key"]
|
||||
if module.params.get("host"):
|
||||
environ_update["HELM_KUBEAPISERVER"] = module.params["host"]
|
||||
if module.params.get("validate_certs") is False or module.params.get("ca_cert"):
|
||||
kubeconfig_path = write_temp_kubeconfig(
|
||||
module.params["host"],
|
||||
validate_certs=module.params["validate_certs"],
|
||||
ca_cert=module.params["ca_cert"],
|
||||
)
|
||||
module.add_cleanup_file(kubeconfig_path)
|
||||
if kubeconfig_path is not None:
|
||||
environ_update["KUBECONFIG"] = kubeconfig_path
|
||||
|
||||
return environ_update
|
||||
|
||||
|
||||
def run_helm(module, command, fails_on_error=True):
|
||||
if not HAS_YAML:
|
||||
module.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
environ_update = prepare_helm_environ_update(module)
|
||||
rc, out, err = module.run_command(command, environ_update=environ_update)
|
||||
if fails_on_error and rc != 0:
|
||||
module.fail_json(
|
||||
msg="Failure when executing Helm command. Exited {0}.\nstdout: {1}\nstderr: {2}".format(
|
||||
rc, out, err
|
||||
),
|
||||
stdout=out,
|
||||
stderr=err,
|
||||
command=command,
|
||||
)
|
||||
return rc, out, err
|
||||
|
||||
|
||||
def get_values(module, command, release_name, get_all=False):
|
||||
"""
|
||||
Get Values from deployed release
|
||||
"""
|
||||
if not HAS_YAML:
|
||||
module.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
get_command = command + " get values --output=yaml " + release_name
|
||||
|
||||
if get_all:
|
||||
get_command += " -a"
|
||||
|
||||
rc, out, err = run_helm(module, get_command)
|
||||
# Helm 3 return "null" string when no values are set
|
||||
if out.rstrip("\n") == "null":
|
||||
return {}
|
||||
return yaml.safe_load(out)
|
||||
|
||||
|
||||
def write_temp_kubeconfig(server, validate_certs=True, ca_cert=None):
|
||||
# Workaround until https://github.com/helm/helm/pull/8622 is merged
|
||||
content = {
|
||||
"apiVersion": "v1",
|
||||
"kind": "Config",
|
||||
"clusters": [{"cluster": {"server": server}, "name": "generated-cluster"}],
|
||||
"contexts": [
|
||||
{"context": {"cluster": "generated-cluster"}, "name": "generated-context"}
|
||||
],
|
||||
"current-context": "generated-context",
|
||||
}
|
||||
|
||||
if not validate_certs:
|
||||
content["clusters"][0]["cluster"]["insecure-skip-tls-verify"] = True
|
||||
if ca_cert:
|
||||
content["clusters"][0]["cluster"]["certificate-authority"] = ca_cert
|
||||
|
||||
_fd, file_name = tempfile.mkstemp()
|
||||
with os.fdopen(_fd, "w") as fp:
|
||||
yaml.dump(content, fp)
|
||||
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 + " plugin 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):
|
||||
def parse_helm_plugin_list(output=None):
|
||||
"""
|
||||
Parse `helm plugin list`, return list of plugins
|
||||
"""
|
||||
@@ -160,20 +53,180 @@ def parse_helm_plugin_list(module, output=None):
|
||||
return ret
|
||||
|
||||
|
||||
def get_helm_version(module, helm_bin):
|
||||
def write_temp_kubeconfig(server, validate_certs=True, ca_cert=None, kubeconfig=None):
|
||||
# Workaround until https://github.com/helm/helm/pull/8622 is merged
|
||||
content = {
|
||||
"apiVersion": "v1",
|
||||
"kind": "Config",
|
||||
"clusters": [{"cluster": {"server": server}, "name": "generated-cluster"}],
|
||||
"contexts": [
|
||||
{"context": {"cluster": "generated-cluster"}, "name": "generated-context"}
|
||||
],
|
||||
"current-context": "generated-context",
|
||||
}
|
||||
if kubeconfig:
|
||||
content = copy.deepcopy(kubeconfig)
|
||||
|
||||
helm_version_command = helm_bin + " version"
|
||||
rc, out, err = module.run_command(helm_version_command)
|
||||
m = re.match(r'version.BuildInfo{Version:"v([0-9\.]*)",', out)
|
||||
if m:
|
||||
return m.group(1)
|
||||
m = re.match(r'Client: &version.Version{SemVer:"v([0-9\.]*)", ', out)
|
||||
if m:
|
||||
return m.group(1)
|
||||
return None
|
||||
for cluster in content["clusters"]:
|
||||
if server:
|
||||
cluster["cluster"]["server"] = server
|
||||
if not validate_certs:
|
||||
cluster["cluster"]["insecure-skip-tls-verify"] = True
|
||||
if ca_cert:
|
||||
cluster["cluster"]["certificate-authority"] = ca_cert
|
||||
return content
|
||||
|
||||
|
||||
def get_helm_binary(module):
|
||||
return module.params.get("binary_path") or module.get_bin_path(
|
||||
"helm", required=True
|
||||
)
|
||||
class AnsibleHelmModule(object):
|
||||
|
||||
"""
|
||||
An Ansible module class for Kubernetes.core helm modules
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
self._module = None
|
||||
if "module" in kwargs:
|
||||
self._module = kwargs.get("module")
|
||||
else:
|
||||
self._module = AnsibleModule(**kwargs)
|
||||
|
||||
self.helm_env = None
|
||||
|
||||
def __getattr__(self, name):
|
||||
return getattr(self._module, name)
|
||||
|
||||
@property
|
||||
def params(self):
|
||||
return self._module.params
|
||||
|
||||
def _prepare_helm_environment(self):
|
||||
param_to_env_mapping = [
|
||||
("context", "HELM_KUBECONTEXT"),
|
||||
("release_namespace", "HELM_NAMESPACE"),
|
||||
("api_key", "HELM_KUBETOKEN"),
|
||||
("host", "HELM_KUBEAPISERVER"),
|
||||
]
|
||||
|
||||
env_update = {}
|
||||
for p, env in param_to_env_mapping:
|
||||
if self.params.get(p):
|
||||
env_update[env] = self.params.get(p)
|
||||
|
||||
kubeconfig_content = None
|
||||
kubeconfig = self.params.get("kubeconfig")
|
||||
if kubeconfig:
|
||||
if isinstance(kubeconfig, string_types):
|
||||
with open(kubeconfig) as fd:
|
||||
kubeconfig_content = yaml.safe_load(fd)
|
||||
elif isinstance(kubeconfig, dict):
|
||||
kubeconfig_content = kubeconfig
|
||||
|
||||
if self.params.get("ca_cert"):
|
||||
ca_cert = self.params.get("ca_cert")
|
||||
if LooseVersion(self.get_helm_version()) < LooseVersion("3.5.0"):
|
||||
# update certs from kubeconfig
|
||||
kubeconfig_content = write_temp_kubeconfig(
|
||||
server=self.params.get("host"),
|
||||
ca_cert=ca_cert,
|
||||
kubeconfig=kubeconfig_content,
|
||||
)
|
||||
else:
|
||||
env_update["HELM_KUBECAFILE"] = ca_cert
|
||||
|
||||
if self.params.get("validate_certs") is False:
|
||||
validate_certs = self.params.get("validate_certs")
|
||||
if LooseVersion(self.get_helm_version()) < LooseVersion("3.10.0"):
|
||||
# update certs from kubeconfig
|
||||
kubeconfig_content = write_temp_kubeconfig(
|
||||
server=self.params.get("host"),
|
||||
validate_certs=validate_certs,
|
||||
kubeconfig=kubeconfig_content,
|
||||
)
|
||||
else:
|
||||
env_update["HELM_KUBEINSECURE_SKIP_TLS_VERIFY"] = "true"
|
||||
|
||||
if kubeconfig_content:
|
||||
fd, kubeconfig_path = tempfile.mkstemp()
|
||||
with os.fdopen(fd, "w") as fp:
|
||||
json.dump(kubeconfig_content, fp)
|
||||
|
||||
env_update["KUBECONFIG"] = kubeconfig_path
|
||||
self.add_cleanup_file(kubeconfig_path)
|
||||
|
||||
return env_update
|
||||
|
||||
@property
|
||||
def env_update(self):
|
||||
if self.helm_env is None:
|
||||
self.helm_env = self._prepare_helm_environment()
|
||||
return self.helm_env
|
||||
|
||||
def run_helm_command(self, command, fails_on_error=True):
|
||||
if not HAS_YAML:
|
||||
self.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
rc, out, err = self.run_command(command, environ_update=self.env_update)
|
||||
if fails_on_error and rc != 0:
|
||||
self.fail_json(
|
||||
msg="Failure when executing Helm command. Exited {0}.\nstdout: {1}\nstderr: {2}".format(
|
||||
rc, out, err
|
||||
),
|
||||
stdout=out,
|
||||
stderr=err,
|
||||
command=command,
|
||||
)
|
||||
return rc, out, err
|
||||
|
||||
def get_helm_binary(self):
|
||||
return self.params.get("binary_path") or self.get_bin_path(
|
||||
"helm", required=True
|
||||
)
|
||||
|
||||
def get_helm_version(self):
|
||||
|
||||
command = self.get_helm_binary() + " version"
|
||||
rc, out, err = self.run_command(command)
|
||||
m = re.match(r'version.BuildInfo{Version:"v([0-9\.]*)",', out)
|
||||
if m:
|
||||
return m.group(1)
|
||||
m = re.match(r'Client: &version.Version{SemVer:"v([0-9\.]*)", ', out)
|
||||
if m:
|
||||
return m.group(1)
|
||||
return None
|
||||
|
||||
def get_values(self, release_name, get_all=False):
|
||||
"""
|
||||
Get Values from deployed release
|
||||
"""
|
||||
if not HAS_YAML:
|
||||
self.fail_json(msg=missing_required_lib("PyYAML"), exception=YAML_IMP_ERR)
|
||||
|
||||
get_command = (
|
||||
self.get_helm_binary() + " get values --output=yaml " + release_name
|
||||
)
|
||||
|
||||
if get_all:
|
||||
get_command += " -a"
|
||||
|
||||
rc, out, err = self.run_helm_command(get_command)
|
||||
# Helm 3 return "null" string when no values are set
|
||||
if out.rstrip("\n") == "null":
|
||||
return {}
|
||||
return yaml.safe_load(out)
|
||||
|
||||
def get_helm_plugin_list(self):
|
||||
"""
|
||||
Return `helm plugin list`
|
||||
"""
|
||||
helm_plugin_list = self.get_helm_binary() + " plugin list"
|
||||
rc, out, err = self.run_helm_command(helm_plugin_list)
|
||||
if rc != 0 or (out == "" and err == ""):
|
||||
self.fail_json(
|
||||
msg="Failed to get Helm plugin info",
|
||||
command=helm_plugin_list,
|
||||
stdout=out,
|
||||
stderr=err,
|
||||
rc=rc,
|
||||
)
|
||||
return (rc, out, err, helm_plugin_list)
|
||||
|
||||
@@ -39,6 +39,4 @@ HELM_AUTH_ARG_SPEC = dict(
|
||||
HELM_AUTH_MUTUALLY_EXCLUSIVE = [
|
||||
("context", "ca_cert"),
|
||||
("context", "validate_certs"),
|
||||
("kubeconfig", "ca_cert"),
|
||||
("kubeconfig", "validate_certs"),
|
||||
]
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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, "", "")
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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, "", "")
|
||||
|
||||
|
||||
@@ -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, "******")
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user