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
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
4 changes: 2 additions & 2 deletions docs/content/en/docs/features/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public class WebappReconciler

@Override
public Map<String, EventSource> prepareEventSources(EventSourceContext<Webapp> context) {
InformerConfiguration<Tomcat> configuration =
InformerEventSourceConfiguration<Tomcat> configuration =
InformerEventSourceConfiguration.from(Tomcat.class, Tomcat.class)
.withSecondaryToPrimaryMapper(webappsMatchingTomcatName)
.withPrimaryToSecondaryMapper(
Expand Down Expand Up @@ -547,7 +547,7 @@ fabric8 Kubernetes client class, that will listen for events associated with the
you configured your `InformerEventSource` with. If you want to listen to Kubernetes resource
events, `InformerEventSource` is probably the only thing you need to use. It's highly
configurable so you can tune it to your needs. Take a look at
[InformerConfiguration](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerConfiguration.java)
[InformerEventSourceConfiguration](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java)
and associated classes for more details but some interesting features we can mention here is the
ability to filter events so that you can only get notified for events you care about. A
particularly interesting feature of the `InformerEventSource`, as opposed to using your own
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,79 @@ public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() {
return secondaryToPrimaryMapper;
}

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

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