Skip to content

DefaultEventSink keeps the involvedObject and ownerReferences of the first occurrence when it aggregates a repeated event #3728

Backlog

Description

@afalhambra-hivemq

Bug Report

What did you do?

  1. Record an event about an object with a DefaultEventRecorder built on a DefaultEventSink, with ownerReference(true), and an explicit event name (EventRecord#name or an EventNamingStrategy).
  2. Delete the object and recreate it under the same name, so it gets a new uid.
  3. 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:

https://github.com/operator-framework/java-operator-sdk/blob/v5.7.0/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/event/DefaultEventSink.java#L55-L66

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 describe searches events with a field selector on involvedObject.uid whenever the object has one (searchEvents in k8s.io/kubectl/pkg/describe/describe.go, which calls events.Search, see GetFieldSelector in k8s.io/client-go/kubernetes/typed/core/v1/event_expansion.go). The event is only reachable through kubectl get events.
  • The owner reference points at a uid that no longer exists. The garbage collector ignores events (ignoredResources in pkg/controller/garbagecollector/graph_builder.go), so nothing gets deleted, but the reference is wrong for the tooling it is meant for.
  • count and firstTimestamp carry over from the deleted object too, so kubectl describe of the recreated object would report (x6 over 2d) for an object created minutes ago. Starting a new count when the involvedObject.uid changed 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions