From 23d9a26f52812e000050e756ab91d90fcd1d4540 Mon Sep 17 00:00:00 2001 From: Paige Rubendall Date: Thu, 26 May 2022 11:39:22 -0400 Subject: [PATCH] adding namespace deletion using kuberenetes python client --- docs/namespace_scenarios.md | 8 ++------ kraken/kubernetes/client.py | 18 ++++++++++++++++-- .../common_namespace_functions.py | 16 +++++----------- scenarios/ingress_namespace.yaml | 3 +-- scenarios/regex_namespace.yaml | 3 +-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/docs/namespace_scenarios.md b/docs/namespace_scenarios.md index f78fd3eb..cac1402c 100644 --- a/docs/namespace_scenarios.md +++ b/docs/namespace_scenarios.md @@ -4,8 +4,6 @@ Using this type of scenario configuration one is able to delete a specific names Configuration Options: -**action:** Default is `delete` - **namespace:** Specific namespace or regex style namespace of what you want to delete. Gets all namespaces if not specified; set to "" if you want to use the label_selector field. Set to '^.*$' and label_selector to "" to randomly select any namespace in your cluster. @@ -22,11 +20,9 @@ Refer to [namespace_scenarios_example](https://github.com/chaos-kubox/krkn/blob/ ``` scenarios: -- action: delete - namespace: "^.*$" +- namespace: "^.*$" runs: 1 -- action: delete - namespace: "^.*ingress.*$" +- namespace: "^.*ingress.*$" runs: 1 sleep: 15 ``` diff --git a/kraken/kubernetes/client.py b/kraken/kubernetes/client.py index f7ff0120..b3acdbe3 100644 --- a/kraken/kubernetes/client.py +++ b/kraken/kubernetes/client.py @@ -65,8 +65,8 @@ def list_namespaces(label_selector=None): return namespaces -# Get namespace status def get_namespace_status(namespace_name): + """Get status of a given namespace""" ret = "" try: ret = cli.read_namespace_status(namespace_name) @@ -75,8 +75,22 @@ def get_namespace_status(namespace_name): return ret.status.phase -# Check if all the watch_namespaces are valid +def delete_namespace(namespace): + """Deletes a given namespace using kubernetes python client""" + try: + api_response = cli.delete_namespace(namespace) + logging.debug("Namespace deleted. status='%s'" % str(api_response.status)) + return api_response + except Exception as e: + logging.error( + "Exception when calling \ + CoreV1Api->delete_namespace: %s\n" + % e + ) + + def check_namespaces(namespaces, label_selectors=None): + """Check if all the watch_namespaces are valid""" try: valid_namespaces = list_namespaces(label_selectors) regex_namespaces = set(namespaces) - set(valid_namespaces) diff --git a/kraken/namespace_actions/common_namespace_functions.py b/kraken/namespace_actions/common_namespace_functions.py index 12ba97e1..751e5170 100644 --- a/kraken/namespace_actions/common_namespace_functions.py +++ b/kraken/namespace_actions/common_namespace_functions.py @@ -1,7 +1,6 @@ import time import random import logging -import kraken.invoke.command as runcommand import kraken.kubernetes.client as kubecli import kraken.cerberus.setup as cerberus import kraken.post_actions.actions as post_actions @@ -33,7 +32,6 @@ def run(scenarios_list, config, wait_duration, failed_post_scenarios, kubeconfig sys.exit(1) delete_count = scenario.get("delete_count", 1) run_count = scenario.get("runs", 1) - namespace_action = scenario.get("action", "delete") run_sleep = scenario.get("sleep", 10) wait_time = scenario.get("wait_time", 30) killed_namespaces = [] @@ -43,21 +41,17 @@ def run(scenarios_list, config, wait_duration, failed_post_scenarios, kubeconfig for j in range(delete_count): if len(namespaces) == 0: logging.error( - "Couldn't %s %s namespaces, not enough namespaces matching %s with label %s" - % (namespace_action, str(run_count), scenario_namespace, str(scenario_label)) + "Couldn't delete %s namespaces, not enough namespaces matching %s with label %s" + % (str(run_count), scenario_namespace, str(scenario_label)) ) sys.exit(1) selected_namespace = namespaces[random.randint(0, len(namespaces) - 1)] killed_namespaces.append(selected_namespace) try: - runcommand.invoke("oc %s project %s" % (namespace_action, selected_namespace)) - logging.info( - namespace_action + " on namespace " + str(selected_namespace) + " was successful" - ) + kubecli.delete_namespace(selected_namespace) + logging.info("Delete on namespace %s was successful" % str(selected_namespace)) except Exception as e: - logging.info( - namespace_action + " on namespace " + str(selected_namespace) + " was unsuccessful" - ) + logging.info("Delete on namespace %s was unsuccessful" % str(selected_namespace)) logging.info("Namespace action error: " + str(e)) sys.exit(1) namespaces.remove(selected_namespace) diff --git a/scenarios/ingress_namespace.yaml b/scenarios/ingress_namespace.yaml index bf6fca9e..510ecb8a 100644 --- a/scenarios/ingress_namespace.yaml +++ b/scenarios/ingress_namespace.yaml @@ -1,6 +1,5 @@ scenarios: -- action: delete - namespace: "^.*ingress.*$" +- namespace: "^.*ingress.*$" runs: 1 sleep: 15 wait_time: 300 diff --git a/scenarios/regex_namespace.yaml b/scenarios/regex_namespace.yaml index 43397719..844fdac5 100644 --- a/scenarios/regex_namespace.yaml +++ b/scenarios/regex_namespace.yaml @@ -1,6 +1,5 @@ scenarios: -- action: delete - namespace: "^.*$" +- namespace: "^.*$" delete_count: 1 runs: 2 sleep: 15