From 77f53b3a23f82866a97f986c344a1e054bc57a3d Mon Sep 17 00:00:00 2001 From: Robert O'Brien Date: Mon, 9 May 2022 22:14:12 +0100 Subject: [PATCH] Rework node status to use watches --- kraken/node_actions/common_node_functions.py | 74 ++++++++++++++------ node_status.py | 0 2 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 node_status.py diff --git a/kraken/node_actions/common_node_functions.py b/kraken/node_actions/common_node_functions.py index 6eeba948..67c5aef6 100644 --- a/kraken/node_actions/common_node_functions.py +++ b/kraken/node_actions/common_node_functions.py @@ -4,7 +4,7 @@ import logging import paramiko import kraken.kubernetes.client as kubecli import kraken.invoke.command as runcommand - +from kubernetes import client, watch node_general = False @@ -30,30 +30,58 @@ def get_node(node_name, label_selector, instance_kill_count): return nodes_to_return -# Wait till node status becomes Ready +# Wait until the node status becomes Ready def wait_for_ready_status(node, timeout): - for _ in range(timeout): - if kubecli.get_node_status(node) == "Ready": + w = watch.Watch() + v1 = client.CoreV1Api() + for event in w.stream( + v1.list_node, + field_selector=f"metadata.name={node}", + timeout_seconds=timeout, + limit=1, + ): + conditions = [status for status in event["object"].status.conditions if status.type == "Ready"] + if conditions[0].status == "True": + w.stop() break - time.sleep(3) - if kubecli.get_node_status(node) != "Ready": - raise Exception("Node condition status isn't Ready") + else: + logging.info("node status " + str(conditions[0].status)) -# Wait till node status becomes NotReady +# Wait until the node status becomes Unknown def wait_for_unknown_status(node, timeout): - for _ in range(timeout): - try: - node_status = kubecli.get_node_status(node, timeout) - if node_status is None or node_status == "Unknown": - break - except Exception: - logging.error("Encountered error while getting node status, waiting 3 seconds and retrying") - time.sleep(3) - node_status = kubecli.get_node_status(node, timeout) - logging.info("node status " + str(node_status)) - if node_status is not None and node_status != "Unknown": - raise Exception("Node condition status isn't Unknown after %s seconds" % str(timeout)) + w = watch.Watch() + v1 = client.CoreV1Api() + for event in w.stream( + v1.list_node, + field_selector=f"metadata.name={node}", + timeout_seconds=timeout, + limit=1, + ): + conditions = [status for status in event["object"].status.conditions if status.type == "Ready"] + if conditions[0].status == "Unknown": + w.stop() + break + else: + logging.info("node status " + str(conditions[0].status)) + + +# Wait until the node status becomes Not Ready +def wait_for_not_ready_status(node, timeout): + w = watch.Watch() + v1 = client.CoreV1Api() + for event in w.stream( + v1.list_node, + field_selector=f"metadata.name={node}", + timeout_seconds=timeout, + limit=1, + ): + conditions = [status for status in event["object"].status.conditions if status.type == "Ready"] + if conditions[0].status == "False": + w.stop() + break + else: + logging.info("node status " + str(conditions[0].status)) # Get the ip of the cluster node @@ -74,7 +102,11 @@ def check_service_status(node, service, ssh_private_key, timeout): i += sleeper logging.info("Trying to ssh to instance: %s" % (node)) connection = ssh.connect( - node, username="root", key_filename=ssh_private_key, timeout=800, banner_timeout=400 + node, + username="root", + key_filename=ssh_private_key, + timeout=800, + banner_timeout=400, ) if connection is None: break diff --git a/node_status.py b/node_status.py new file mode 100644 index 00000000..e69de29b