diff --git a/docs/content/en/docs/documentation/working-with-es-caches.md b/docs/content/en/docs/documentation/working-with-es-caches.md index d31f1e079f..f9363c4608 100644 --- a/docs/content/en/docs/documentation/working-with-es-caches.md +++ b/docs/content/en/docs/documentation/working-with-es-caches.md @@ -216,6 +216,23 @@ With this index in place, you can retrieve the target resources very efficiently .withNamespacesInheritedFromController().build()); ``` +The same can be done for the cache of a secondary resource: indexers are configured on the +`InformerEventSourceConfiguration.Builder`, and are registered before the event source starts. +`InformerEventSource.addIndexers(..)` is only meant for the dynamic case, where an informer is +registered at runtime and its indexers are only known at that point. Otherwise, use the builder: + +```java +InformerEventSource configMapEventSource = + new InformerEventSource<>( + InformerEventSourceConfiguration.from(ConfigMap.class, MyResource.class) + .withIndexer( + "by-resource-version", cm -> List.of(cm.getMetadata().getResourceVersion())) + .build()); + +// later, e.g. in the reconciler +configMapEventSource.byIndex("by-resource-version", resourceVersion); +``` + ## Read-cache-after-write consistency and event filtering From version 5.3.0 we provide stronger consistency guarantees and diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java index d40469de4c..c069ac437d 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java @@ -16,9 +16,14 @@ package io.javaoperatorsdk.operator.api.config.informer; import java.time.Duration; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import io.fabric8.kubernetes.api.model.GenericKubernetesResource; import io.fabric8.kubernetes.api.model.HasMetadata; @@ -86,6 +91,16 @@ default String name() { return getInformerConfig().getName(); } + /** + * The indexers to register on the informer event source, by index name. They are registered when + * the event source is created, so they are in place before it starts. + * + * @return the configured indexers, empty if there are none + */ + default Map>> getIndexers() { + return Map.of(); + } + class DefaultInformerEventSourceConfiguration implements InformerEventSourceConfiguration { private final PrimaryToSecondaryMapper primaryToSecondaryMapper; @@ -93,6 +108,7 @@ class DefaultInformerEventSourceConfiguration private final GroupVersionKind groupVersionKind; private final InformerConfiguration informerConfig; private final KubernetesClient kubernetesClient; + private final Map>> indexers; protected DefaultInformerEventSourceConfiguration( GroupVersionKind groupVersionKind, @@ -100,11 +116,33 @@ protected DefaultInformerEventSourceConfiguration( SecondaryToPrimaryMapper secondaryToPrimaryMapper, InformerConfiguration informerConfig, KubernetesClient kubernetesClient) { + this( + groupVersionKind, + primaryToSecondaryMapper, + secondaryToPrimaryMapper, + informerConfig, + kubernetesClient, + Map.of()); + } + + protected DefaultInformerEventSourceConfiguration( + GroupVersionKind groupVersionKind, + PrimaryToSecondaryMapper primaryToSecondaryMapper, + SecondaryToPrimaryMapper secondaryToPrimaryMapper, + InformerConfiguration informerConfig, + KubernetesClient kubernetesClient, + Map>> indexers) { this.informerConfig = Objects.requireNonNull(informerConfig); this.groupVersionKind = groupVersionKind; this.primaryToSecondaryMapper = primaryToSecondaryMapper; this.secondaryToPrimaryMapper = secondaryToPrimaryMapper; this.kubernetesClient = kubernetesClient; + this.indexers = indexers; + } + + @Override + public Map>> getIndexers() { + return indexers; } @Override @@ -145,6 +183,7 @@ class Builder { private PrimaryToSecondaryMapper primaryToSecondaryMapper; private SecondaryToPrimaryMapper secondaryToPrimaryMapper; private KubernetesClient kubernetesClient; + private final Map>> indexers = new LinkedHashMap<>(); private Builder(Class resourceClass, Class primaryResourceClass) { this(resourceClass, primaryResourceClass, null); @@ -272,6 +311,29 @@ public Builder withGenericFilter(GenericFilter genericFilter) { return this; } + /** + * Registers an indexer on the informer event source, so the resources it caches can be looked + * up by the keys the indexer computes (see {@link + * io.javaoperatorsdk.operator.processing.event.source.IndexerResourceCache#byIndex}). + * + * @param name the name of the index, unique per event source + * @param indexer computes the index keys of a resource + */ + public Builder withIndexer(String name, Function> indexer) { + indexers.put(Objects.requireNonNull(name), Objects.requireNonNull(indexer)); + return this; + } + + /** + * Registers several indexers at once. + * + * @see #withIndexer(String, Function) + */ + public Builder withIndexers(Map>> indexers) { + indexers.forEach(this::withIndexer); + return this; + } + public Builder withItemStore(ItemStore itemStore) { config.withItemStore(itemStore); return this; @@ -345,7 +407,8 @@ public InformerEventSourceConfiguration build() { HasMetadata.getKind(primaryResourceClass), false)), config.build(), - kubernetesClient); + kubernetesClient, + Collections.unmodifiableMap(new LinkedHashMap<>(indexers))); } } } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java index abde1f6992..a8418ce738 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java @@ -16,8 +16,11 @@ package io.javaoperatorsdk.operator.processing.event.source.informer; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import org.slf4j.Logger; @@ -78,6 +81,18 @@ public InformerEventSource(InformerEventSourceConfiguration configuration) { onUpdateFilter = informerConfig.getOnUpdateFilter(); onDeleteFilter = informerConfig.getOnDeleteFilter(); genericFilter = informerConfig.getGenericFilter(); + super.addIndexers(configuration.getIndexers()); + } + + /** + * Adds indexers to an already created event source, for example when the informer is registered + * dynamically and its indexers are only known at that point. Indexers known upfront should be + * configured with {@link InformerEventSourceConfiguration.Builder#withIndexer(String, Function)} + * instead. + */ + @Override + public void addIndexers(Map>> indexers) { + super.addIndexers(indexers); } @Override diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/InformerEventSourceConfigurationTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/InformerEventSourceConfigurationTest.java index 9e43b288cc..be3034af57 100644 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/InformerEventSourceConfigurationTest.java +++ b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/InformerEventSourceConfigurationTest.java @@ -15,6 +15,10 @@ */ package io.javaoperatorsdk.operator.api.config; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + import org.junit.jupiter.api.Test; import io.fabric8.kubernetes.api.model.ConfigMap; @@ -22,7 +26,10 @@ import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration; import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; class InformerEventSourceConfigurationTest { @@ -39,4 +46,38 @@ void updateFromKeepsComparableResourceVersions() { assertFalse(builder.build().getInformerConfig().isComparableResourceVersions()); } + + @Test + void indexersAreEmptyByDefault() { + var configuration = + InformerEventSourceConfiguration.from(ConfigMap.class, Secret.class).build(); + + assertTrue(configuration.getIndexers().isEmpty()); + } + + @Test + void builderCollectsIndexers() { + Function> byName = cm -> List.of(cm.getMetadata().getName()); + Function> byNamespace = cm -> List.of(cm.getMetadata().getNamespace()); + + var configuration = + InformerEventSourceConfiguration.from(ConfigMap.class, Secret.class) + .withIndexer("name", byName) + .withIndexers(Map.of("namespace", byNamespace)) + .build(); + + assertEquals(Map.of("name", byName, "namespace", byNamespace), configuration.getIndexers()); + } + + @Test + void configuredIndexersCannotBeModifiedAfterBuild() { + var configuration = + InformerEventSourceConfiguration.from(ConfigMap.class, Secret.class) + .withIndexer("name", cm -> List.of(cm.getMetadata().getName())) + .build(); + + assertThrows( + UnsupportedOperationException.class, + () -> configuration.getIndexers().put("other", cm -> List.of())); + } } diff --git a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSourceTest.java b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSourceTest.java index 4664c18c25..9dff538023 100644 --- a/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSourceTest.java +++ b/operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSourceTest.java @@ -423,6 +423,25 @@ void byIndexSkipsOlderTempCacheResourceNoLongerInIndexedInformerResult() { assertThat(result).isEmpty(); } + @Test + void indexersFromConfigurationAreRegisteredOnCreation() { + when(informerEventSourceConfiguration.getIndexers()) + .thenReturn(Map.of("idx", d -> List.of(d.getMetadata().getLabels().get("app")))); + informerEventSource = buildInformerEventSource(); + var ghost = testDeployment(); + ghost.getMetadata().setLabels(Map.of("app", "key")); + when(temporaryResourceCache.getResources()) + .thenReturn(new HashMap<>(Map.of(ResourceID.fromResource(ghost), ghost))); + var informerManager = mock(InformerManager.class); + stubLastSyncResourceVersion(informerManager, "1"); + when(informerManager.byIndexStream(any(), any())).thenReturn(Stream.empty()); + when(informerEventSource.manager()).thenReturn(informerManager); + + var result = informerEventSource.byIndexStream("idx", "key").toList(); + + assertThat(result).containsExactly(ghost); + } + @Test void byIndexIncludesGhostResourceNotYetSeenByInformer() { var ghost = testDeployment(); diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/readcacheafterwrite/readownupdates/ReadOwnUpdatesReconciler.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/readcacheafterwrite/readownupdates/ReadOwnUpdatesReconciler.java index c62d637a88..cc85793508 100644 --- a/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/readcacheafterwrite/readownupdates/ReadOwnUpdatesReconciler.java +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/readcacheafterwrite/readownupdates/ReadOwnUpdatesReconciler.java @@ -123,9 +123,9 @@ public List> prepareEventSources( new InformerEventSource<>( InformerEventSourceConfiguration.from( ConfigMap.class, ReadOwnUpdatesCustomResource.class) + .withIndexer( + RESOURCE_VERSION_INDEX, cm -> List.of(cm.getMetadata().getResourceVersion())) .build()); - configMapEventSource.addIndexers( - Map.of(RESOURCE_VERSION_INDEX, cm -> List.of(cm.getMetadata().getResourceVersion()))); return List.of(configMapEventSource); }