Skip to content

Commit

Permalink
improve: InformerEventSourceConfiguration facade for InformerConfigur…
Browse files Browse the repository at this point in the history
…ation

Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
  • Loading branch information
csviri committed Jan 28, 2025
1 parent 39d9a4a commit f43cf54
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public List<EventSource<?, P>> prepareEventSources(

var es = new InformerEventSource<>(
InformerEventSourceConfiguration.from(ConfigMap.class, primaryClass())
.withInformerConfiguration(c -> c.withItemStore(boundedItemStore))
.withItemStore(boundedItemStore)
.withSecondaryToPrimaryMapper(
Mappers.fromOwnerReferences(context.getPrimaryResourceClass(),
this instanceof BoundedCacheClusterScopeTestReconciler))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.Set;

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
import io.javaoperatorsdk.operator.api.config.Informable;
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.SAME_AS_CONTROLLER_NAMESPACES_SET;
import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_ALL_NAMESPACE_SET;
import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE_SET;

public interface InformerEventSourceConfiguration<R extends HasMetadata>
extends Informable<R> {

Expand Down Expand Up @@ -145,12 +154,6 @@ private Builder(Class<R> resourceClass,
this.config = InformerConfiguration.builder(resourceClass);
}

public Builder<R> withInformerConfiguration(
Consumer<InformerConfiguration<R>.Builder> configurator) {
configurator.accept(config);
return this;
}

public Builder<R> withName(String name) {
this.name = name;
config.withName(name);
Expand Down Expand Up @@ -187,6 +190,83 @@ public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() {
return secondaryToPrimaryMapper;
}

public Builder<R> withNamespaces(Set<String> namespaces) {
config.withNamespaces(namespaces);
return this;
}

public Set<String> namespaces() {
return config.namespaces();
}

public Builder<R> withNamespacesInheritedFromController() {
withNamespaces(SAME_AS_CONTROLLER_NAMESPACES_SET);
return this;
}

public Builder<R> withWatchAllNamespaces() {
withNamespaces(WATCH_ALL_NAMESPACE_SET);
return this;
}

public Builder<R> withWatchCurrentNamespace() {
withNamespaces(WATCH_CURRENT_NAMESPACE_SET);
return this;
}


/**
* Whether the associated informer should track changes made to the parent
* {@link io.javaoperatorsdk.operator.processing.Controller}'s namespaces configuration.
*
* @param followChanges {@code true} to reconfigure the associated informer when the parent
* controller's namespaces are reconfigured, {@code false} otherwise
* @return the builder instance so that calls can be chained fluently
*/
public Builder<R> withFollowControllerNamespacesChanges(boolean followChanges) {
config.withFollowControllerNamespacesChanges(followChanges);
return this;
}

public Builder<R> withLabelSelector(String labelSelector) {
config.withLabelSelector(labelSelector);
return this;
}

public Builder<R> withOnAddFilter(
OnAddFilter<? super R> onAddFilter) {
config.withOnAddFilter(onAddFilter);
return this;
}

public Builder<R> withOnUpdateFilter(
OnUpdateFilter<? super R> onUpdateFilter) {
config.withOnUpdateFilter(onUpdateFilter);
return this;
}

public Builder<R> withOnDeleteFilter(
OnDeleteFilter<? super R> onDeleteFilter) {
config.withOnDeleteFilter(onDeleteFilter);
return this;
}

public Builder<R> withGenericFilter(
GenericFilter<? super R> genericFilter) {
config.withGenericFilter(genericFilter);
return this;
}

public Builder<R> withItemStore(ItemStore<R> itemStore) {
config.withItemStore(itemStore);
return this;
}

public Builder<R> withInformerListLimit(Long informerListLimit) {
config.withInformerListLimit(informerListLimit);
return this;
}

public void updateFrom(InformerConfiguration<R> informerConfig) {
if (informerConfig != null) {
final var informerConfigName = informerConfig.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public List<EventSource<?, ClusterScopedCustomResource>> prepareEventSources(
InformerEventSourceConfiguration.from(ConfigMap.class, ClusterScopedCustomResource.class)
.withSecondaryToPrimaryMapper(
Mappers.fromOwnerReferences(context.getPrimaryResourceClass(), true))
.withInformerConfiguration(
c -> c.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE))
.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE)
.build(),
context);
return List.of(ies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public List<EventSource<?, CreateUpdateEventFilterTestCustomResource>> prepareEv
InformerEventSourceConfiguration<ConfigMap> informerConfiguration =
InformerEventSourceConfiguration
.from(ConfigMap.class, CreateUpdateEventFilterTestCustomResource.class)
.withInformerConfiguration(c -> c
.withLabelSelector("integrationtest = " + this.getClass().getSimpleName()))
.withLabelSelector("integrationtest = " + this.getClass().getSimpleName())
.build();

final var informerEventSource =
new InformerEventSource<ConfigMap, CreateUpdateEventFilterTestCustomResource>(
new InformerEventSource<>(
informerConfiguration, context);
this.configMapDR.setEventSource(informerEventSource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public List<EventSource<?, FilterTestCustomResource>> prepareEventSources(

final var informerConfiguration = InformerEventSourceConfiguration
.from(ConfigMap.class, FilterTestCustomResource.class)
.withInformerConfiguration(c -> c.withOnUpdateFilter((newCM,
.withOnUpdateFilter((newCM,
oldCM) -> !newCM.getData().get(CM_VALUE_KEY)
.equals(CONFIG_MAP_FILTER_VALUE)))
.equals(CONFIG_MAP_FILTER_VALUE))
.build();
InformerEventSource<ConfigMap, FilterTestCustomResource> configMapES =
new InformerEventSource<>(informerConfiguration, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
Expand Down Expand Up @@ -57,8 +56,7 @@ public List<EventSource<?, InformerRemoteClusterCustomResource>> prepareEventSou
Mappers.fromDefaultAnnotations(InformerRemoteClusterCustomResource.class))
// setting remote client for informer
.withKubernetesClient(remoteClient)
.withInformerConfiguration(
InformerConfiguration.Builder::withWatchAllNamespaces)
.withWatchAllNamespaces()
.build(), context);

return List.of(es);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ public List<EventSource<?, MultipleSecondaryEventSourceCustomResource>> prepareE

var config = InformerEventSourceConfiguration
.from(ConfigMap.class, MultipleSecondaryEventSourceCustomResource.class)
.withInformerConfiguration(c -> c
// TODO: this shouldn't be needed since this should be the default behavior (tracking
// the controller's namespaces)
.withNamespaces(
context.getControllerConfiguration().getInformerConfig().getNamespaces())
.withLabelSelector("multisecondary"))
.withNamespacesInheritedFromController()
.withLabelSelector("multisecondary")
.withSecondaryToPrimaryMapper(s -> {
var name =
s.getMetadata().getName().subSequence(0, s.getMetadata().getName().length() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.*;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
Expand Down Expand Up @@ -71,8 +70,7 @@ public List<EventSource<?, Job>> prepareEventSources(EventSourceContext<Job> con
.byIndex(JOB_CLUSTER_INDEX, indexKey(cluster.getMetadata().getName(),
cluster.getMetadata().getNamespace()))
.stream().map(ResourceID::fromResource).collect(Collectors.toSet()))
.withInformerConfiguration(
InformerConfiguration.Builder::withNamespacesInheritedFromController);
.withNamespacesInheritedFromController();

if (addPrimaryToSecondaryMapper) {
informerConfiguration = informerConfiguration.withPrimaryToSecondaryMapper(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ public List<EventSource<?, WebPage>> prepareEventSources(EventSourceContext<WebP
var configMapEventSource =
new InformerEventSource<>(
InformerEventSourceConfiguration.from(ConfigMap.class, WebPage.class)
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
.withLabelSelector(SELECTOR)
.build(),
context);
var deploymentEventSource =
new InformerEventSource<>(
InformerEventSourceConfiguration.from(Deployment.class, WebPage.class)
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
.withLabelSelector(SELECTOR)
.build(),
context);
var serviceEventSource =
new InformerEventSource<>(
InformerEventSourceConfiguration.from(Service.class, WebPage.class)
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
.withLabelSelector(SELECTOR)
.build(),
context);
var ingressEventSource =
new InformerEventSource<>(
InformerEventSourceConfiguration.from(Ingress.class, WebPage.class)
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
.withLabelSelector(SELECTOR)
.build(),
context);
return List.of(configMapEventSource, deploymentEventSource,
Expand Down

0 comments on commit f43cf54

Please sign in to comment.