-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add predicate-aware informer event handlers #4894
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
Open
brendandburns
wants to merge
1
commit into
kubernetes-client:master
Choose a base branch
from
brendandburns:fix-predicate-informer-event-handlers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
util/src/main/java/io/kubernetes/client/informer/FilteringResourceEventHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| Copyright 2026 The Kubernetes Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package io.kubernetes.client.informer; | ||
|
|
||
| import io.kubernetes.client.common.KubernetesObject; | ||
| import java.util.Objects; | ||
| import java.util.function.Predicate; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * FilteringResourceEventHandler dispatches events only when objects match a predicate. | ||
| */ | ||
| public class FilteringResourceEventHandler<ApiType extends KubernetesObject> | ||
| implements ResourceEventHandler<ApiType> { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(FilteringResourceEventHandler.class); | ||
|
|
||
| private final ResourceEventHandler<ApiType> delegate; | ||
| private final Predicate<ApiType> filter; | ||
|
|
||
| public FilteringResourceEventHandler( | ||
| ResourceEventHandler<ApiType> delegate, Predicate<ApiType> filter) { | ||
| this.delegate = Objects.requireNonNull(delegate); | ||
| this.filter = Objects.requireNonNull(filter); | ||
| } | ||
|
|
||
| @Override | ||
| public void onAdd(ApiType obj) { | ||
| if (matches(obj)) { | ||
| delegate.onAdd(obj); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onUpdate(ApiType oldObj, ApiType newObj) { | ||
| boolean oldMatched = matches(oldObj); | ||
| boolean newMatched = matches(newObj); | ||
| if (oldMatched && newMatched) { | ||
| delegate.onUpdate(oldObj, newObj); | ||
| return; | ||
| } | ||
| if (!oldMatched && newMatched) { | ||
| delegate.onAdd(newObj); | ||
| return; | ||
| } | ||
| if (oldMatched) { | ||
| delegate.onDelete(oldObj, false); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onDelete(ApiType obj, boolean deletedFinalStateUnknown) { | ||
| if (matches(obj)) { | ||
| delegate.onDelete(obj, deletedFinalStateUnknown); | ||
| } | ||
| } | ||
|
|
||
| private boolean matches(ApiType obj) { | ||
| if (obj == null) { | ||
| return false; | ||
| } | ||
| try { | ||
| return filter.test(obj); | ||
| } catch (RuntimeException e) { | ||
| log.warn("Predicate threw an exception; dropping informer event", e); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
util/src/test/java/io/kubernetes/client/informer/FilteringResourceEventHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| Copyright 2026 The Kubernetes Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package io.kubernetes.client.informer; | ||
|
|
||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| import io.kubernetes.client.openapi.models.V1ObjectMeta; | ||
| import io.kubernetes.client.openapi.models.V1Pod; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| class FilteringResourceEventHandlerTest { | ||
|
|
||
| private static V1Pod pod(String name) { | ||
| return new V1Pod().metadata(new V1ObjectMeta().namespace("default").name(name)); | ||
| } | ||
|
|
||
| @Test | ||
| void dropsAddForFilteredOutObject() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
|
|
||
| handler.onAdd(pod("ignored")); | ||
|
|
||
| verifyNoInteractions(delegate); | ||
| } | ||
|
|
||
| @Test | ||
| void convertsUpdateTransitionIntoAdd() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
| V1Pod oldObj = pod("ignored"); | ||
| V1Pod newObj = pod("selected"); | ||
|
|
||
| handler.onUpdate(oldObj, newObj); | ||
|
|
||
| verify(delegate).onAdd(newObj); | ||
| verify(delegate, never()).onUpdate(oldObj, newObj); | ||
| verify(delegate, never()).onDelete(oldObj, false); | ||
| } | ||
|
|
||
| @Test | ||
| void convertsUpdateTransitionIntoDelete() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
| V1Pod oldObj = pod("selected"); | ||
| V1Pod newObj = pod("ignored"); | ||
|
|
||
| handler.onUpdate(oldObj, newObj); | ||
|
|
||
| verify(delegate).onDelete(oldObj, false); | ||
| verify(delegate, never()).onUpdate(oldObj, newObj); | ||
| verify(delegate, never()).onAdd(newObj); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesUpdateWhenOldAndNewMatch() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
| V1Pod oldObj = pod("selected"); | ||
| V1Pod newObj = pod("selected"); | ||
|
|
||
| handler.onUpdate(oldObj, newObj); | ||
|
|
||
| verify(delegate).onUpdate(oldObj, newObj); | ||
| verify(delegate, never()).onAdd(newObj); | ||
| verify(delegate, never()).onDelete(oldObj, false); | ||
| } | ||
|
|
||
| @Test | ||
| void dropsDeleteForFilteredOutObject() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
|
|
||
| handler.onDelete(pod("ignored"), false); | ||
|
|
||
| verifyNoInteractions(delegate); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldIgnoreDeleteWhenPredicateThrows() { | ||
| ResourceEventHandler<V1Pod> delegate = Mockito.mock(ResourceEventHandler.class); | ||
| FilteringResourceEventHandler<V1Pod> handler = | ||
| new FilteringResourceEventHandler<>(delegate, obj -> "selected".equals(obj.getMetadata().getName())); | ||
|
|
||
| handler.onDelete(new V1Pod(), true); | ||
|
|
||
| verifyNoInteractions(delegate); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a log here so that we don't silently drop events.