-
Notifications
You must be signed in to change notification settings - Fork 235
feat: distinct latest resource collector #3130
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
csviri
wants to merge
6
commits into
operator-framework:next
Choose a base branch
from
csviri:latest-distinct
base: next
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,16 @@ | |
| package io.javaoperatorsdk.operator.api.reconciler; | ||
|
|
||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Predicate; | ||
| import java.util.function.UnaryOperator; | ||
| import java.util.stream.Collector; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -364,13 +372,13 @@ public static <R extends HasMetadata> R resourcePatch( | |
| if (esList.isEmpty()) { | ||
| throw new IllegalStateException("No event source found for type: " + resource.getClass()); | ||
| } | ||
| var es = esList.get(0); | ||
| if (esList.size() > 1) { | ||
| throw new IllegalStateException( | ||
| "Multiple event sources found for: " | ||
| + resource.getClass() | ||
| + " please provide the target event source"); | ||
| log.warn( | ||
| "Multiple event sources found for type: {}, selecting first with name {}", | ||
| resource.getClass(), | ||
| es.name()); | ||
| } | ||
| var es = esList.get(0); | ||
| if (es instanceof ManagedInformerEventSource mes) { | ||
| return resourcePatch(resource, updateOperation, mes); | ||
| } else { | ||
|
|
@@ -714,4 +722,56 @@ private static int validateResourceVersion(String v1) { | |
| } | ||
| return v1Length; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a collector that deduplicates Kubernetes objects by keeping only the one with the | ||
| * latest metadata.resourceVersion for each unique name and namespace combination. The intended | ||
| * use case is for the rather rare setup when there are overlapping {@link | ||
| * io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource}s for a | ||
| * resource type. | ||
| * | ||
| * @param <T> the type of HasMetadata objects | ||
| * @return a collector that produces a collection of deduplicated Kubernetes objects | ||
| */ | ||
| public static <T extends HasMetadata> Collector<T, ?, Collection<T>> latestDistinct() { | ||
| return Collectors.collectingAndThen(latestDistinctToMap(), Map::values); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a collector that deduplicates Kubernetes objects by keeping only the one with the | ||
| * latest metadata.resourceVersion for each unique name and namespace combination. The intended | ||
| * use case is for the rather rare setup when there are overlapping {@link | ||
| * io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource}s for a | ||
| * resource type. | ||
| * | ||
| * @param <T> the type of HasMetadata objects | ||
| * @return a collector that produces a List of deduplicated Kubernetes objects | ||
| */ | ||
| public static <T extends HasMetadata> Collector<T, ?, List<T>> latestDistinctList() { | ||
| return Collectors.collectingAndThen( | ||
| latestDistinctToMap(), map -> new ArrayList<>(map.values())); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a collector that deduplicates Kubernetes objects by keeping only the one with the | ||
| * latest metadata.resourceVersion for each unique name and namespace combination. The intended | ||
| * use case is for the rather rare setup when there are overlapping {@link | ||
| * io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource}s for a | ||
| * resource type. | ||
| * | ||
| * @param <T> the type of HasMetadata objects | ||
| * @return a collector that produces a Set of deduplicated Kubernetes objects | ||
| */ | ||
| public static <T extends HasMetadata> Collector<T, ?, Set<T>> latestDistinctSet() { | ||
| return Collectors.collectingAndThen(latestDistinctToMap(), map -> new HashSet<>(map.values())); | ||
| } | ||
|
|
||
| private static <T extends HasMetadata> Collector<T, ?, Map<ResourceID, T>> latestDistinctToMap() { | ||
| return Collectors.toMap( | ||
| resource -> | ||
| new ResourceID(resource.getMetadata().getName(), resource.getMetadata().getNamespace()), | ||
| resource -> resource, | ||
| (existing, replacement) -> | ||
| compareResourceVersions(existing, replacement) >= 0 ? existing : replacement); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @metacosm this is the point to keep of this PR, just to keep the latest |
||
| } | ||
| } | ||
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.
I don't get why this method or the set variant are needed or even useful. The semantic should be a
Setsince instances should be unique, no?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.
The
Setvariant is needed since we are covering the use case whern there are multiple event sources for same type without distinct set of resource watched, so we returning the latest of those.I added List just for convinience. If somebody would prefer working above Lists, that is quite common I beleive, but you are right that the semantically
Setis the correct one.