Skip to content
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
17 changes: 17 additions & 0 deletions docs/content/en/docs/documentation/working-with-es-caches.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigMap, MyResource> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,25 +91,58 @@ 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<String, Function<R, List<String>>> getIndexers() {
return Map.of();
}

class DefaultInformerEventSourceConfiguration<R extends HasMetadata>
implements InformerEventSourceConfiguration<R> {
private final PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private final SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private final GroupVersionKind groupVersionKind;
private final InformerConfiguration<R> informerConfig;
private final KubernetesClient kubernetesClient;
private final Map<String, Function<R, List<String>>> indexers;

protected DefaultInformerEventSourceConfiguration(
GroupVersionKind groupVersionKind,
PrimaryToSecondaryMapper<?> primaryToSecondaryMapper,
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper,
InformerConfiguration<R> informerConfig,
KubernetesClient kubernetesClient) {
this(
groupVersionKind,
primaryToSecondaryMapper,
secondaryToPrimaryMapper,
informerConfig,
kubernetesClient,
Map.of());
}

protected DefaultInformerEventSourceConfiguration(
GroupVersionKind groupVersionKind,
PrimaryToSecondaryMapper<?> primaryToSecondaryMapper,
SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper,
InformerConfiguration<R> informerConfig,
KubernetesClient kubernetesClient,
Map<String, Function<R, List<String>>> indexers) {
this.informerConfig = Objects.requireNonNull(informerConfig);
this.groupVersionKind = groupVersionKind;
this.primaryToSecondaryMapper = primaryToSecondaryMapper;
this.secondaryToPrimaryMapper = secondaryToPrimaryMapper;
this.kubernetesClient = kubernetesClient;
this.indexers = indexers;
}

@Override
public Map<String, Function<R, List<String>>> getIndexers() {
return indexers;
}

@Override
Expand Down Expand Up @@ -145,6 +183,7 @@ class Builder<R extends HasMetadata> {
private PrimaryToSecondaryMapper<?> primaryToSecondaryMapper;
private SecondaryToPrimaryMapper<R> secondaryToPrimaryMapper;
private KubernetesClient kubernetesClient;
private final Map<String, Function<R, List<String>>> indexers = new LinkedHashMap<>();

private Builder(Class<R> resourceClass, Class<? extends HasMetadata> primaryResourceClass) {
this(resourceClass, primaryResourceClass, null);
Expand Down Expand Up @@ -272,6 +311,29 @@ public Builder<R> withGenericFilter(GenericFilter<? super R> 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<R> withIndexer(String name, Function<R, List<String>> indexer) {
indexers.put(Objects.requireNonNull(name), Objects.requireNonNull(indexer));
return this;
}

/**
* Registers several indexers at once.
*
* @see #withIndexer(String, Function)
*/
public Builder<R> withIndexers(Map<String, Function<R, List<String>>> indexers) {
indexers.forEach(this::withIndexer);
return this;
}

public Builder<R> withItemStore(ItemStore<R> itemStore) {
config.withItemStore(itemStore);
return this;
Expand Down Expand Up @@ -345,7 +407,8 @@ public InformerEventSourceConfiguration<R> build() {
HasMetadata.getKind(primaryResourceClass),
false)),
config.build(),
kubernetesClient);
kubernetesClient,
Collections.unmodifiableMap(new LinkedHashMap<>(indexers)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +81,18 @@ public InformerEventSource(InformerEventSourceConfiguration<R> 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<String, Function<R, List<String>>> indexers) {
super.addIndexers(indexers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
*/
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;
import io.fabric8.kubernetes.api.model.Secret;
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 {

Expand All @@ -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<ConfigMap, List<String>> byName = cm -> List.of(cm.getMetadata().getName());
Function<ConfigMap, List<String>> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public List<EventSource<?, ReadOwnUpdatesCustomResource>> 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);
}

Expand Down