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

discriminator improvements #2013

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

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
Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Expand Up @@ -81,10 +81,10 @@ public Map<String, EventSource> prepareEventSources(

firstDependentResourceConfigMap
.setResourceDiscriminator(
new IndexDiscriminator(CONFIG_MAP_INDEX_1, FIRST_CONFIG_MAP_SUFFIX_1));
new TestIndexDiscriminator(CONFIG_MAP_INDEX_1, FIRST_CONFIG_MAP_SUFFIX_1));
secondDependentResourceConfigMap
.setResourceDiscriminator(
new IndexDiscriminator(CONFIG_MAP_INDEX_2, FIRST_CONFIG_MAP_SUFFIX_2));
new TestIndexDiscriminator(CONFIG_MAP_INDEX_2, FIRST_CONFIG_MAP_SUFFIX_2));
return EventSourceInitializer.nameEventSources(eventSource);
}

Expand Down
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));
}
}
Loading