Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Pod UID updates in PodStore #6964

Merged

Conversation

antoninbas
Copy link
Contributor

The PodStore uses the UID as the unique key in the indexer, and not the usual namespace/name. Because it is possible for the deletion of a Pod, followed by the creation of a different Pod wih the same name, to be observed as an Update by a client (see
https://pkg.go.dev/k8s.io/client-go/tools/cache#SharedInformer), the Update event handler needs to handle this case gracefully by comparing UIDs. Note that this is probably quite an unlikely situation in the case of Pods, but we should still account for this in the PodStore implementation.

@antoninbas antoninbas requested review from tnqn and yuntanghsu January 30, 2025 07:05
Comment on lines +98 to +120
// From https://pkg.go.dev/k8s.io/client-go/tools/cache#SharedInformer:
// Because `ObjectMeta.UID` has no role in identifying objects, it is possible that when (1)
// object O1 with ID (e.g. namespace and name) X and `ObjectMeta.UID` U1 in the
// SharedInformer's local cache is deleted and later (2) another object O2 with ID X and
// ObjectMeta.UID U2 is created the informer's clients are not notified of (1) and (2) but
// rather are notified only of an update from O1 to O2. Clients that need to detect such
// cases might do so by comparing the `ObjectMeta.UID` field of the old and the new object
// in the code that handles update notifications (i.e. `OnUpdate` method of
// ResourceEventHandler).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnqn I have not been able to reproduce, but looking at the issue that triggered this documentation update, it seems that it would only happen if a watch is interrupted.

There was a similar change in kube-scheduler at some point: kubernetes/kubernetes#91126. The logic was later changed, but I believe the current code still accounts for the fact that a Delete and an Add event can be "merged" as an Update under certain rare conditions.

Now I am wondering: is it ever safe to assume that the update event handler will never observe a change to an immutable field? If oldObj and newObj can have different UIDs, it feels like other fields which are supposed to be immutable can also be changed through an "update" (of course these are 2 distinct objects, but with the same name, so from the point of view of the event handler they are the "same"). Am I over thinking this?

Copy link
Member

@tnqn tnqn Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are not overthinking this. We should estabilish a principle that do not assume objects received from update handler refer to the same objects. I think we didn't encounter issues was because:

  1. It's a very corner case;
  2. In most cases we don't care about UID and these immutable fields. I can find one place that may have the problem:
    // For Pod, we only care about PodIP and NodeName update.
    // Some other attributes we care about are immutable, e.g. the named ContainerPort.
    but it's unlikely to happen because two Pods having the same name will unlikely have same PodIP but different containerPorts (although we should still fix it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I identified another possible controller with this issue earlier: #6965

tnqn
tnqn previously approved these changes Feb 25, 2025
Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +98 to +120
// From https://pkg.go.dev/k8s.io/client-go/tools/cache#SharedInformer:
// Because `ObjectMeta.UID` has no role in identifying objects, it is possible that when (1)
// object O1 with ID (e.g. namespace and name) X and `ObjectMeta.UID` U1 in the
// SharedInformer's local cache is deleted and later (2) another object O2 with ID X and
// ObjectMeta.UID U2 is created the informer's clients are not notified of (1) and (2) but
// rather are notified only of an update from O1 to O2. Clients that need to detect such
// cases might do so by comparing the `ObjectMeta.UID` field of the old and the new object
// in the code that handles update notifications (i.e. `OnUpdate` method of
// ResourceEventHandler).
Copy link
Member

@tnqn tnqn Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are not overthinking this. We should estabilish a principle that do not assume objects received from update handler refer to the same objects. I think we didn't encounter issues was because:

  1. It's a very corner case;
  2. In most cases we don't care about UID and these immutable fields. I can find one place that may have the problem:
    // For Pod, we only care about PodIP and NodeName update.
    // Some other attributes we care about are immutable, e.g. the named ContainerPort.
    but it's unlikely to happen because two Pods having the same name will unlikely have same PodIP but different containerPorts (although we should still fix it.)

The PodStore uses the UID as the unique key in the indexer, and not the
usual namespace/name. Because it is possible for the deletion of a Pod,
followed by the creation of a different Pod wih the same name, to be
observed as an Update by a client (see
https://pkg.go.dev/k8s.io/client-go/tools/cache#SharedInformer), the
Update event handler needs to handle this case gracefully by comparing
UIDs. Note that this is probably quite an unlikely situation in the case
of Pods, but we should still account for this in the PodStore
implementation.

Signed-off-by: Antonin Bas <[email protected]>
@antoninbas
Copy link
Contributor Author

@tnqn I rebased the PR, please take another look

@antoninbas
Copy link
Contributor Author

/test-all

Copy link
Member

@tnqn tnqn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@antoninbas antoninbas merged commit 25b2b81 into antrea-io:main Feb 27, 2025
56 of 63 checks passed
@antoninbas antoninbas deleted the handle-uid-updates-in-pod-store branch February 27, 2025 21:22
@antoninbas antoninbas added the action/release-note Indicates a PR that should be included in release notes. label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/release-note Indicates a PR that should be included in release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants