Skip to content

Commit

Permalink
Minor refactor of log_events
Browse files Browse the repository at this point in the history
  • Loading branch information
HomayoonAlimohammadi committed Dec 5, 2024
1 parent c21ff9c commit e1ddc6e
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/storage_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,38 @@ def evaluate(self) -> Optional[str]:

def is_ready(self, obj: HashableResource, cond: AnyCondition) -> Optional[bool]:
"""Determine if the resource is ready."""
if not super().is_ready(obj, cond):
object_events = collect_events(self.client, obj.resource)
if obj.kind in ["Deployment", "DaemonSet"]:
involved_pods = self.client.list(
Pod, namespace=obj.namespace, labels={"app": obj.name}
)
object_events += [
event for pod in involved_pods for event in collect_events(self.client, pod)
]
for event in sorted(object_events, key=by_localtime):
log.info(
"Event %s/%s %s msg=%s",
event.involvedObject.kind,
event.involvedObject.name,
event.lastTimestamp
and event.lastTimestamp.astimezone()
or "Date not recorded",
event.message,
)

return super().is_ready(obj, cond)

is_ready = super().is_ready(obj, cond)
if not is_ready:
try:
log_events(self.client, obj)
except Exception as e:
log.error("failed to log events: %s", e)

return is_ready

def log_events(client, obj: HashableResource) -> None:
"""Log events for the object."""

object_events = collect_events(client, obj.resource)

if obj.kind in ["Deployment", "DaemonSet"]:
involved_pods = client.list(
Pod, namespace=obj.namespace, labels={"app": obj.name}
)
object_events += [
event for pod in involved_pods for event in collect_events(client, pod)
]

for event in sorted(object_events, key=by_localtime):
log.info(
"Event %s/%s %s msg=%s",
event.involvedObject.kind,
event.involvedObject.name,
event.lastTimestamp
and event.lastTimestamp.astimezone()
or "Date not recorded",
event.message,
)

def by_localtime(event: Event) -> datetime.datetime:
"""Return the last timestamp of the event in local time."""
Expand All @@ -189,7 +199,6 @@ def by_localtime(event: Event) -> datetime.datetime:
def collect_events(client, resource: AnyResource) -> List[Event]:
"""Collect events from the resource."""
kind: str = resource.kind or type(resource).__name__
return list(
client.list(
Event,
namespace=resource.metadata.namespace,
Expand Down

0 comments on commit e1ddc6e

Please sign in to comment.