Updates to kube env interaction
- local KUBECONFIG var detection
- proper handling of env vars
- fixes for KUBECONFIG loading when env file is given/not given
- main IP extraction
- wording fixes
Related-PROD: PROD-35903
Change-Id: I68f1fd18a72a99502460d3b6158a43cd60d7cf1b
diff --git a/cfg_checker/common/kube_utils.py b/cfg_checker/common/kube_utils.py
index f6f499d..315f5ee 100644
--- a/cfg_checker/common/kube_utils.py
+++ b/cfg_checker/common/kube_utils.py
@@ -23,18 +23,18 @@
# Init kube library locally
_path = "local:{}".format(config.kube_config_path)
try:
- kconfig.load_kube_config()
+ kconfig.load_kube_config(config_file=config.kube_config_path)
if config.insecure:
kconfig.assert_hostname = False
kconfig.client_side_validation = False
logger_cli.debug(
- "...found Kube env: core, {}". format(
+ "... found Kube env: core, {}". format(
",".join(
kclient.CoreApi().get_api_versions().versions
)
)
)
- return kconfig, kclient.ApiClient()
+ return kconfig, kclient.ApiClient(), _path
except Exception as e:
logger.warn("Failed to init local Kube client: {}".format(
str(e)
@@ -63,14 +63,15 @@
"""
import yaml
_path = ''
- if not config.env_name == ENV_LOCAL:
+ # Try to load remote config only if it was not detected already
+ if not config.kube_config_detected and not config.env_name == ENV_LOCAL:
_path = "{}@{}:{}".format(
config.ssh_user,
config.ssh_host,
config.kube_config_path
)
_c_data = ssh_shell_p(
- "sudo cat " + config.kube_config_path,
+ "cat " + config.kube_config_path,
config.ssh_host,
username=config.ssh_user,
keypath=config.ssh_key,
@@ -116,7 +117,7 @@
)
else:
logger_cli.debug(
- "...'context' host extracted: '{}' via SSH@{}".format(
+ "... 'context' host extracted: '{}' via SSH@{}".format(
_host,
config.ssh_host
)
@@ -128,7 +129,7 @@
_tmp[0] + "://" + config.mcp_host + ":" + _tmp[2]
config.kube_port = _tmp[2]
logger_cli.debug(
- "...kube remote host updated to {}".format(
+ "... kube remote host updated to {}".format(
_kube_conf.host
)
)
@@ -169,9 +170,11 @@
self.config
)
self.is_local = True
- # Load local config data
- if os.path.exists(self.config.kube_config_path):
- _c_data = shell("sudo cat " + self.config.kube_config_path)
+ # Try to load local config data
+ if self.config.kube_config_path and \
+ os.path.exists(self.config.kube_config_path):
+ _cmd = "cat " + self.config.kube_config_path
+ _c_data = shell(_cmd)
_conf = yaml.load(_c_data, Loader=yaml.SafeLoader)
self.user_keypath = create_temp_file_with_content(
base64.standard_b64decode(
@@ -277,7 +280,7 @@
**kwargs
):
logger_cli.debug(
- "...searching for pods with the name '{}'".format(pod_name)
+ "... searching for pods with the name '{}'".format(pod_name)
)
_pods = {}
_pods = self._coreV1.list_namespaced_pod(namespace)
@@ -288,8 +291,8 @@
_pnames = [n for n in _names if n.startswith(pod_name)]
if len(_pnames) > 1:
logger_cli.debug(
- "...more than one pod found for '{}': {}\n"
- "...using first one".format(
+ "... more than one pod found for '{}': {}\n"
+ "... using first one".format(
pod_name,
", ".join(_pnames)
)
@@ -300,7 +303,7 @@
else:
_pname = pod_name
logger_cli.debug(
- "...cmd: [CoreV1] exec {} -n {} -- {}".format(
+ "... cmd: [CoreV1] exec {} -n {} -- {}".format(
_pname,
namespace,
cmd