Skip to content

Commit

Permalink
Fixed return of tuple methods. Releases with no pods are now set to s…
Browse files Browse the repository at this point in the history
…tatus Deleted.
  • Loading branch information
alfredeen committed Aug 23, 2024
1 parent 20c8d09 commit 1a60ab6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
36 changes: 24 additions & 12 deletions serve_event_listener/status_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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:
"""
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion serve_event_listener/status_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
)
Expand Down

0 comments on commit 1a60ab6

Please sign in to comment.