From 1a60ab6133832ad16cae5ebb0bd69ab1b96bc724 Mon Sep 17 00:00:00 2001 From: alfredeen Date: Fri, 23 Aug 2024 12:46:11 +0200 Subject: [PATCH] Fixed return of tuple methods. Releases with no pods are now set to status Deleted. --- serve_event_listener/status_data.py | 36 ++++++++++++++++++---------- serve_event_listener/status_queue.py | 2 +- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/serve_event_listener/status_data.py b/serve_event_listener/status_data.py index f2a2d53..1e44c2d 100644 --- a/serve_event_listener/status_data.py +++ b/serve_event_listener/status_data.py @@ -46,7 +46,7 @@ def determine_status_from_k8s(status_object: V1PodStatus) -> Tuple[str, str, str - status_object (dict): The Kubernetes status object. Returns: - - str: The status of the pod. + - Tuple[str, str, str]: The status of the pod, container message, pod message """ empty_message = "" pod_message = status_object.message if status_object.message else empty_message @@ -128,14 +128,16 @@ def fetch_status_from_k8s_api(self, release: str) -> Tuple[str, str, str]: Because this can be as costly operation it is only used at critical times such as deleted pods. Returns: - - A tuple consisting of status, pod_message, container_message + - Tuple[str, str, str]: The status of the pod, container message, pod message + + If no pod matches the release, then return None, "", "" """ logger.debug( f"Getting the status of release {release} directly from k8s via the api client" ) - status = "Unset" - pod_message = container_message = "" + status = None + container_message = pod_message = "" try: api_response = self.k8s_api_client.list_namespaced_pod( @@ -144,11 +146,13 @@ def fetch_status_from_k8s_api(self, release: str) -> Tuple[str, str, str]: for pod in api_response.items: if pod.metadata.labels.get("release") == release: - pod_status = StatusData.determine_status_from_k8s(pod.status) - if status == "Unset": + pod_status, container_message, pod_message = ( + StatusData.determine_status_from_k8s(pod.status) + ) + if status is None: status = pod_status logger.debug( - f"Preliminary status of release {release} set from Unset to {status}" + f"Preliminary status of release {release} set from None to {status}" ) elif status == "Deleted": # All other statuses override Deleted @@ -167,7 +171,7 @@ def fetch_status_from_k8s_api(self, release: str) -> Tuple[str, str, str]: f"Exception when calling CoreV1Api->list_namespaced_pod. {e}" ) - return status, pod_message, container_message + return status, container_message, pod_message def update(self, event: dict) -> None: """ @@ -189,7 +193,7 @@ def update(self, event: dict) -> None: if pod: status_object = pod.status - status, pod_message, container_message = ( + status, container_message, pod_message = ( StatusData.determine_status_from_k8s(status_object) ) release = pod.metadata.labels.get("release") @@ -279,9 +283,17 @@ def update_or_create_status( if status == "Deleted": # Status Deleted is a destructive action # Therefore we double-check the k8s status directly upon detecting this - status = self.fetch_status_from_k8s_api(release) - if status != "Deleted": - deletion_timestamp = None + if self.k8s_api_client: + # Only use if the k8s client api has been set + # Unit tests for example do not currently set a k8s api + status, *_ = self.fetch_status_from_k8s_api(release) + + if status is None: + # No pod with this release found. Set status to Deleted + status = "Deleted" + + if status != "Deleted": + deletion_timestamp = None status_data[release] = { "creation_timestamp": creation_timestamp, diff --git a/serve_event_listener/status_queue.py b/serve_event_listener/status_queue.py index 2953df6..a2862db 100644 --- a/serve_event_listener/status_queue.py +++ b/serve_event_listener/status_queue.py @@ -30,8 +30,8 @@ def process(self): release = status_data["release"] new_status = status_data["new-status"] + if new_status == "Deleted": - # TODO: Be careful with deleting releases! Check the truth in k8s. logger.info( f"Processing release: {release}. New status is Deleted!" )