Skip to content

Commit

Permalink
Check and log all events on failed resources
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Jul 8, 2024
1 parent 6a28597 commit c21ff9c
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/storage_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
# See LICENSE file for licensing details.
"""Implementation of cinder-csi specific details of the kubernetes manifests."""

import datetime
import logging
import pickle
from hashlib import md5
from typing import Dict, Optional
from typing import Dict, List, Optional

from lightkube.codecs import AnyResource, from_dict
from ops.manifests import Addition, ConfigRegistry, ManifestLabel, Manifests, Patch
from lightkube.resources.core_v1 import Event, Pod
from ops.manifests import (
Addition,
ConfigRegistry,
HashableResource,
ManifestLabel,
Manifests,
Patch,
)
from ops.manifests.manipulations import AnyCondition

log = logging.getLogger(__file__)
NAMESPACE = "kube-system"
Expand Down Expand Up @@ -145,6 +155,51 @@ def evaluate(self) -> Optional[str]:
return f"Storage manifests waiting for definition of {prop}"
return None

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)


def by_localtime(event: Event) -> datetime.datetime:
"""Return the last timestamp of the event in local time."""
dt = event.lastTimestamp or datetime.datetime.now(datetime.timezone.utc)
return dt.astimezone()


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,
fields={
"involvedObject.kind": kind,
"involvedObject.name": resource.metadata.name,
},
)
)


class UpdateControllerPlugin(Patch):
"""Update the controller args in Deployments."""
Expand Down

0 comments on commit c21ff9c

Please sign in to comment.