adding namespace deletion using kuberenetes python client

This commit is contained in:
Paige Rubendall
2022-05-26 14:24:36 -05:00
committed by Naga Ravi Chaitanya Elluri
parent 90e1f20d50
commit 23d9a26f52
5 changed files with 25 additions and 23 deletions
+2 -6
View File
@@ -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
```
+16 -2
View File
@@ -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)
@@ -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)
+1 -2
View File
@@ -1,6 +1,5 @@
scenarios:
- action: delete
namespace: "^.*ingress.*$"
- namespace: "^.*ingress.*$"
runs: 1
sleep: 15
wait_time: 300
+1 -2
View File
@@ -1,6 +1,5 @@
scenarios:
- action: delete
namespace: "^.*$"
- namespace: "^.*$"
delete_count: 1
runs: 2
sleep: 15