Skip to content

Commit

Permalink
discriminator improvements (#2013)
Browse files Browse the repository at this point in the history
Signed-off-by: csviri <[email protected]>
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Sep 4, 2023
1 parent 638e742 commit 74ba7ed
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 46 deletions.
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
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) {
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));
}
}

0 comments on commit 74ba7ed

Please sign in to comment.