Skip to content

Commit

Permalink
Updated the event listener with som better logics (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman authored Dec 28, 2023
1 parent a324174 commit 864a8f3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
USERNAME = os.environ.get("EVENT_LISTENER_USERNAME", None)
PASSWORD = os.environ.get("EVENT_LISTENER_PASSWORD", None)

K8S_STATUS_MAP = {
"CrashLoopBackOff": "Error",
"Completed": "Retrying...",
"ContainerCreating": "Created",
"PodInitializing": "Pending",
}

token = None

# Number of retries
Expand Down Expand Up @@ -128,8 +135,10 @@ def init_event_listener():


def get_status(pod):
print("Getting status...")

"""
Returns the status of a pod by looping through each container
and checking the status.
"""
container_statuses = pod.status.container_statuses

if container_statuses is not None:
Expand All @@ -140,23 +149,27 @@ def get_status(pod):
terminated = state.terminated

if terminated is not None:
return terminated.reason
reason = terminated.reason
return mapped_status(reason)

waiting = state.waiting

if waiting is not None:
return waiting.reason

running = state.running
reason = waiting.reason
return mapped_status(reason)
else:
running = state.running
ready = container_status.ready
if running and ready:
return "Running"
else:
return "Pending"

if running is not None:
return "Running"
return pod.status.phase

print("Last state not found.")
else:
print("Container statuses not found.")

return pod.status.phase
def mapped_status(reason: str) -> str:
return K8S_STATUS_MAP.get(reason, reason)


def post(url, data):
Expand Down

0 comments on commit 864a8f3

Please sign in to comment.