-
Notifications
You must be signed in to change notification settings - Fork 218
discriminator improvements #2013
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource; | ||
|
||
/** | ||
* Uses a custom index of {@link InformerEventSource} to access the target resource. The index needs | ||
* to be explicitly created when the event source is defined. This approach improves the performance | ||
* to access the resource. | ||
*/ | ||
public class IndexDiscriminator<R extends HasMetadata, P extends HasMetadata> | ||
implements ResourceDiscriminator<R, P> { | ||
|
||
private final String indexName; | ||
private final String eventSourceName; | ||
private final Function<P, String> keyMapper; | ||
|
||
public IndexDiscriminator(String indexName, Function<P, String> keyMapper) { | ||
this(indexName, null, keyMapper); | ||
} | ||
|
||
public IndexDiscriminator(String indexName, String eventSourceName, | ||
Function<P, String> keyMapper) { | ||
this.indexName = indexName; | ||
this.eventSourceName = eventSourceName; | ||
this.keyMapper = keyMapper; | ||
} | ||
|
||
@Override | ||
public Optional<R> distinguish(Class<R> resource, | ||
P primary, | ||
Context<P> context) { | ||
|
||
InformerEventSource<R, P> eventSource = | ||
(InformerEventSource<R, P>) context | ||
.eventSourceRetriever() | ||
.getResourceEventSourceFor(resource, eventSourceName); | ||
var resources = eventSource.byIndex(indexName, keyMapper.apply(primary)); | ||
if (resources.isEmpty()) { | ||
return Optional.empty(); | ||
} else if (resources.size() > 1) { | ||
throw new IllegalStateException("More than one resource found"); | ||
} else { | ||
return Optional.of(resources.get(0)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,21 +5,41 @@ | |
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
import io.javaoperatorsdk.operator.processing.event.source.Cache; | ||
|
||
public class ResourceIDMatcherDiscriminator<R extends HasMetadata, P extends HasMetadata> | ||
implements ResourceDiscriminator<R, P> { | ||
|
||
|
||
private final String eventSourceName; | ||
private final Function<P, ResourceID> mapper; | ||
|
||
public ResourceIDMatcherDiscriminator(Function<P, ResourceID> mapper) { | ||
this(null, mapper); | ||
} | ||
|
||
public ResourceIDMatcherDiscriminator(String eventSourceName, Function<P, ResourceID> mapper) { | ||
this.eventSourceName = eventSourceName; | ||
this.mapper = mapper; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Optional<R> distinguish(Class<R> resource, P primary, Context<P> context) { | ||
var resourceID = mapper.apply(primary); | ||
return context.getSecondaryResourcesAsStream(resource) | ||
.filter(resourceID::isSameResource) | ||
.findFirst(); | ||
if (eventSourceName != null) { | ||
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. In the keycloak case there is only a single Service event source, so eventSourceName doesn't seem required in all cases. Could it test for, or catch the exception, if there are multiple sources for the given type before resorting to the list? 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. makes sense, changed, pls check if this looks to you |
||
return ((Cache<R>) context.eventSourceRetriever().getResourceEventSourceFor(resource, | ||
eventSourceName)) | ||
.get(resourceID); | ||
} else { | ||
var eventSources = context.eventSourceRetriever().getResourceEventSourcesFor(resource); | ||
if (eventSources.size() == 1) { | ||
return ((Cache<R>) eventSources.get(0)).get(resourceID); | ||
} else { | ||
return context.getSecondaryResourcesAsStream(resource) | ||
.filter(resourceID::isSameResource) | ||
.findFirst(); | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.javaoperatorsdk.operator.sample.indexdiscriminator; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMap; | ||
import io.javaoperatorsdk.operator.api.reconciler.IndexDiscriminator; | ||
|
||
import static io.javaoperatorsdk.operator.sample.indexdiscriminator.IndexDiscriminatorTestReconciler.configMapKeyFromPrimary; | ||
|
||
public class TestIndexDiscriminator | ||
extends IndexDiscriminator<ConfigMap, IndexDiscriminatorTestCustomResource> { | ||
|
||
public TestIndexDiscriminator(String indexName, String nameSuffix) { | ||
super(indexName, p -> configMapKeyFromPrimary(p, nameSuffix)); | ||
} | ||
} |
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.
Some documentation to explain what this does and why it's useful would be helpful for users.
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.
added