Repository navigation
Description
Bug Report
What did you do?
- Record an event about an object with a
DefaultEventRecorderbuilt on aDefaultEventSink, withownerReference(true), and an explicit event name (EventRecord#nameor anEventNamingStrategy). - Delete the object and recreate it under the same name, so it gets a new
uid. - Record the same event (same reason, same key) about the recreated object.
What did you expect to see?
The aggregated event describes the latest occurrence, as it already does for message, type, reason and action: involvedObject and metadata.ownerReferences point at the recreated object, and metadata.labels / metadata.annotations are those of the latest record.
What did you see instead? Under which circumstances?
DefaultEventSink.emit aggregates onto the existing event by patching only count, lastTimestamp, message, type, reason and action:
involvedObject (including uid and resourceVersion), metadata.ownerReferences, metadata.labels and metadata.annotations stay those of the first occurrence. An explicit event name derived from the name of the regarding object rather than its uid resolves to the same event for the recreated object, so every event recorded about it lands on the stale one for as long as it exists. Events with the default name are not affected: it hashes the uid, so a recreated object already gets a new event. The API server keeps an event for the event TTL (1 h by default) and resets the TTL on every write (TTLFunc in kube-apiserver pkg/registry/core/event/storage/storage.go), so a repeating event keeps the stale reference alive indefinitely.
Consequences:
kubectl describe <recreated object>does not list the event.kubectl describesearches events with a field selector oninvolvedObject.uidwhenever the object has one (searchEventsink8s.io/kubectl/pkg/describe/describe.go, which callsevents.Search, seeGetFieldSelectorink8s.io/client-go/kubernetes/typed/core/v1/event_expansion.go). The event is only reachable throughkubectl get events.- The owner reference points at a
uidthat no longer exists. The garbage collector ignores events (ignoredResourcesinpkg/controller/garbagecollector/graph_builder.go), so nothing gets deleted, but the reference is wrong for the tooling it is meant for. countandfirstTimestampcarry over from the deleted object too, sokubectl describeof the recreated object would report(x6 over 2d)for an object created minutes ago. Starting a new count when theinvolvedObject.uidchanged seems the natural complement to refreshing the references; happy to follow whichever semantics you prefer there.
Environment
Kubernetes cluster type: any (the core v1 event update accepts a changed involvedObject)
java-operator-sdk 5.7.0, unchanged on main.
Proposed fix
Take involvedObject and the metadata that describes the latest occurrence from the new event when aggregating, alongside message, type, reason and action:
var aggregated =
new EventBuilder(existing)
.editMetadata()
.withLabels(event.getMetadata().getLabels())
.withAnnotations(event.getMetadata().getAnnotations())
.withOwnerReferences(event.getMetadata().getOwnerReferences())
.endMetadata()
.withInvolvedObject(event.getInvolvedObject())
.withCount(requireNonNullElse(existing.getCount(), 1) + 1)
.withLastTimestamp(event.getLastTimestamp())
.withMessage(event.getMessage())
.withType(event.getType())
.withReason(event.getReason())
.withAction(action)
.build();Optionally reset count to 1 and firstTimestamp to the new event's when the involvedObject.uid differs. Plus a test that records the same event about two objects sharing a name but not a uid, and asserts that the aggregated event's involvedObject.uid and owner reference uid are those of the second object. I am happy to open a PR with this change.
Activity