From f8bdd241c6b955a9fb9f050955b6d831a370eaa2 Mon Sep 17 00:00:00 2001 From: Sylvain Hellegouarch Date: Fri, 30 Apr 2021 12:07:37 +0200 Subject: [PATCH 1/4] Enable debug flag Signed-off-by: Sylvain Hellegouarch Signed-off-by: mickael.roger --- CHANGELOG.md | 1 + chaosk8s/__init__.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b70e5d..34b7d65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Changed +- Client have logging at debug level with `KUBERNETES_DEBUG` - Allow to not verify Kubernetes certificates even when using the default config or the one from a pod. This is useful when Kubernetes is deployed with a self-signed certificate. diff --git a/chaosk8s/__init__.py b/chaosk8s/__init__.py index 0e8946e..5b9d9a8 100644 --- a/chaosk8s/__init__.py +++ b/chaosk8s/__init__.py @@ -61,6 +61,7 @@ def lookup(k: str, d: str = None) -> str: return secrets.get(k, env.get(k, d)) verify_ssl = lookup("KUBERNETES_VERIFY_SSL", False) is not False + debug = lookup("KUBERNETES_DEBUG", False) is not False if has_local_config_file(): context = lookup("KUBERNETES_CONTEXT") @@ -69,17 +70,18 @@ def lookup(k: str, d: str = None) -> str: config.load_kube_config(context=context) client.Configuration.verify_ssl = verify_ssl + client.Configuration.debug = debug proxy_url = os.getenv('HTTP_PROXY', None) if proxy_url: client.Configuration._default.proxy = proxy_url - return client.ApiClient() elif env.get("CHAOSTOOLKIT_IN_POD") == "true": config.load_incluster_config() client.Configuration.verify_ssl = verify_ssl + client.Configuration.debug = debug proxy_url = os.getenv('HTTP_PROXY', None) if proxy_url: @@ -89,7 +91,7 @@ def lookup(k: str, d: str = None) -> str: else: configuration = client.Configuration() - configuration.debug = True + configuration.debug = debug configuration.host = lookup("KUBERNETES_HOST", "http://localhost") configuration.verify_ssl = lookup( "KUBERNETES_VERIFY_SSL", False) is not False From d3f6d6848c7fa612bb99a4d7a9baa604ea7e8246 Mon Sep 17 00:00:00 2001 From: Sylvain Hellegouarch Date: Fri, 30 Apr 2021 15:24:53 +0200 Subject: [PATCH 2/4] View config file path in logs Signed-off-by: Sylvain Hellegouarch Signed-off-by: mickael.roger --- CHANGELOG.md | 1 + chaosk8s/__init__.py | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b7d65..b8f55b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Changed +- Log the path of the Kubernetes config file used, if found - Client have logging at debug level with `KUBERNETES_DEBUG` - Allow to not verify Kubernetes certificates even when using the default config or the one from a pod. This is useful when Kubernetes is deployed with diff --git a/chaosk8s/__init__.py b/chaosk8s/__init__.py index 5b9d9a8..5616b66 100644 --- a/chaosk8s/__init__.py +++ b/chaosk8s/__init__.py @@ -14,9 +14,12 @@ __version__ = '0.25.1' -def has_local_config_file(): - config_path = os.path.expanduser( - os.environ.get('KUBECONFIG', '~/.kube/config')) +def get_config_path() -> str: + return os.path.expanduser(os.environ.get('KUBECONFIG', '~/.kube/config')) + + +def has_local_config_file(config_file: str = None): + config_path = config_file or get_config_path() return os.path.exists(config_path) @@ -62,11 +65,13 @@ def lookup(k: str, d: str = None) -> str: verify_ssl = lookup("KUBERNETES_VERIFY_SSL", False) is not False debug = lookup("KUBERNETES_DEBUG", False) is not False + config_file = get_config_path() - if has_local_config_file(): + if has_local_config_file(config_file): context = lookup("KUBERNETES_CONTEXT") - logger.debug("Using Kubernetes context: {}".format( - context or "default")) + logger.debug( + "Using Kubernetes context '{}' from config '{}'".format( + context or "default", config_file)) config.load_kube_config(context=context) client.Configuration.verify_ssl = verify_ssl From 7a6afa9ac66d6de099de5d7f4580050a9ccd1bc5 Mon Sep 17 00:00:00 2001 From: "mickael.roger" Date: Fri, 7 May 2021 11:15:23 +0200 Subject: [PATCH 3/4] Fix timedout execution issue and Add cmd parsing ehancement using shlex Signed-off-by: mickael.roger --- chaosk8s/pod/actions.py | 15 +++++++++++++-- requirements.txt | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/chaosk8s/pod/actions.py b/chaosk8s/pod/actions.py index 922325c..0acf8bb 100644 --- a/chaosk8s/pod/actions.py +++ b/chaosk8s/pod/actions.py @@ -17,6 +17,8 @@ from chaosk8s import create_k8s_api_client +import shlex + __all__ = ["terminate_pods", "exec_in_pods", "delete_pods"] @@ -112,7 +114,7 @@ def exec_in_pods(cmd: str, pods = _select_pods(v1, label_selector, name_pattern, all, rand, mode, qty, ns, order) - exec_command = cmd.strip().split() + exec_command = shlex.split(cmd) results = [] for po in pods: @@ -138,7 +140,16 @@ def exec_in_pods(cmd: str, resp.run_forever(timeout=request_timeout) - err = json.loads(resp.read_channel(ERROR_CHANNEL)) + # When timeout_request is triggered, resp.read_channel(ERROR_CHANNEL) + # could return a None object + try: + err = json.loads(resp.read_channel(ERROR_CHANNEL)) + except Exception: + err = json.loads('{"status": "Timedout", \ + "message": "Action has been timed out",\ + "details": {"causes": \ + [{"message": "Action stopped by timeout"}]}}') + out = resp.read_channel(STDOUT_CHANNEL) if err['status'] != "Success": diff --git a/requirements.txt b/requirements.txt index 1e6dc39..9770e91 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ kubernetes logzero chaostoolkit-lib>=0.20.0 pyyaml +shlex \ No newline at end of file From 971155950850c02f3102de8c5ceac02e245442e5 Mon Sep 17 00:00:00 2001 From: Mickael-Roger <33000645+Mickael-Roger@users.noreply.github.com> Date: Sun, 9 May 2021 18:33:27 +0200 Subject: [PATCH 4/4] Update requirements.txt Signed-off-by: mickael.roger --- requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 9770e91..1e6dc39 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,3 @@ kubernetes logzero chaostoolkit-lib>=0.20.0 pyyaml -shlex \ No newline at end of file