fix: do not eagerly evaluate the cluster-scoped fallback in KubernetesResourceFetcher - #3527
Draft
csviri wants to merge 1 commit into
Draft
fix: do not eagerly evaluate the cluster-scoped fallback in KubernetesResourceFetcher#3527csviri wants to merge 1 commit into
csviri wants to merge 1 commit into
Conversation
…sResourceFetcher
`fetchResource` selected between a namespaced and a cluster-scoped lookup
with `Optional.orElse`:
return resourceId.getNamespace()
.map(ns -> client.resources(rClass).inNamespace(ns).withName(name).get())
.orElse(client.resources(rClass).withName(name).get());
`orElse` evaluates its argument unconditionally, so every namespaced
lookup also performed the cluster-scoped GET. For a namespaced resource
type that second request either queries whichever namespace the client
happens to default to or fails outright, and its result is then thrown
away.
This runs on the `BoundedItemStore` cache-miss path
(`refreshMissingStateFromServer`), so it doubles the API server requests
for every bounded-cache refresh of a namespaced resource.
Switches to `orElseGet` so the fallback is only evaluated for
cluster-scoped resource ids.
16 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates KubernetesResourceFetcher in operator-framework-core to avoid eagerly executing a cluster-scoped fallback lookup when fetching resources, reducing unnecessary Kubernetes API calls on the bounded-cache refresh path.
Changes:
- Replaces
Optional.orElse(...)withOptional.orElseGet(...)so the fallback GET is evaluated lazily. - Keeps the existing namespaced-vs-cluster-scoped selection logic based on presence of a namespace in the
ResourceID.
Comments suppressed due to low confidence (1)
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/cache/KubernetesResourceFetcher.java:48
Optional.map(...)becomes empty when the mapper returnsnull(it wraps viaofNullable). That means if the resource ID is namespaced but the namespaced GET returnsnull(resource deleted / not found), this code will still execute the fallback cluster-scoped/default-namespace GET and may return/cache the wrong resource under the namespaced key. The fallback should only run when the namespace is absent, not when a namespaced lookup returnsnull.
return resourceId
.getNamespace()
.map(ns -> client.resources(rClass).inNamespace(ns).withName(resourceId.getName()).get())
.orElseGet(() -> client.resources(rClass).withName(resourceId.getName()).get());
Comment on lines
43
to
+48
| public R fetchResource(String key) { | ||
| var resourceId = resourceIDFunction.apply(key); | ||
| return resourceId | ||
| .getNamespace() | ||
| .map(ns -> client.resources(rClass).inNamespace(ns).withName(resourceId.getName()).get()) | ||
| .orElse(client.resources(rClass).withName(resourceId.getName()).get()); | ||
| .orElseGet(() -> client.resources(rClass).withName(resourceId.getName()).get()); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fetchResourceselected between a namespaced and a cluster-scoped lookupwith
Optional.orElse:orElseevaluates its argument unconditionally, so every namespacedlookup also performed the cluster-scoped GET. For a namespaced resource
type that second request either queries whichever namespace the client
happens to default to or fails outright, and its result is then thrown
away.
This runs on the
BoundedItemStorecache-miss path(
refreshMissingStateFromServer), so it doubles the API server requestsfor every bounded-cache refresh of a namespaced resource.
Switches to
orElseGetso the fallback is only evaluated forcluster-scoped resource ids.
Part of #3517