Skip to content

Commit d257f0c

Browse files
authored
improve: InformerEventSourceConfiguration facade for InformerConfiguration (#2674)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 39d9a4a commit d257f0c

File tree

10 files changed

+100
-33
lines changed

10 files changed

+100
-33
lines changed

Diff for: caffeine-bounded-cache-support/src/test/java/io/javaoperatorsdk/operator/processing/event/source/cache/sample/AbstractTestReconciler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public List<EventSource<?, P>> prepareEventSources(
7979

8080
var es = new InformerEventSource<>(
8181
InformerEventSourceConfiguration.from(ConfigMap.class, primaryClass())
82-
.withInformerConfiguration(c -> c.withItemStore(boundedItemStore))
82+
.withItemStore(boundedItemStore)
8383
.withSecondaryToPrimaryMapper(
8484
Mappers.fromOwnerReferences(context.getPrimaryResourceClass(),
8585
this instanceof BoundedCacheClusterScopeTestReconciler))

Diff for: docs/content/en/docs/features/_index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public class WebappReconciler
472472

473473
@Override
474474
public Map<String, EventSource> prepareEventSources(EventSourceContext<Webapp> context) {
475-
InformerConfiguration<Tomcat> configuration =
475+
InformerEventSourceConfiguration<Tomcat> configuration =
476476
InformerEventSourceConfiguration.from(Tomcat.class, Tomcat.class)
477477
.withSecondaryToPrimaryMapper(webappsMatchingTomcatName)
478478
.withPrimaryToSecondaryMapper(
@@ -547,7 +547,7 @@ fabric8 Kubernetes client class, that will listen for events associated with the
547547
you configured your `InformerEventSource` with. If you want to listen to Kubernetes resource
548548
events, `InformerEventSource` is probably the only thing you need to use. It's highly
549549
configurable so you can tune it to your needs. Take a look at
550-
[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)
550+
[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)
551551
and associated classes for more details but some interesting features we can mention here is the
552552
ability to filter events so that you can only get notified for events you care about. A
553553
particularly interesting feature of the `InformerEventSource`, as opposed to using your own

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/informer/InformerEventSourceConfiguration.java

+83-7
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22

33
import java.util.Objects;
44
import java.util.Optional;
5-
import java.util.function.Consumer;
5+
import java.util.Set;
66

77
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
88
import io.fabric8.kubernetes.api.model.HasMetadata;
99
import io.fabric8.kubernetes.client.KubernetesClient;
10+
import io.fabric8.kubernetes.client.informers.cache.ItemStore;
1011
import io.javaoperatorsdk.operator.api.config.Informable;
1112
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
1213
import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper;
1314
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
15+
import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
16+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
17+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
18+
import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
1419
import io.javaoperatorsdk.operator.processing.event.source.informer.Mappers;
1520

21+
import static io.javaoperatorsdk.operator.api.reconciler.Constants.SAME_AS_CONTROLLER_NAMESPACES_SET;
22+
import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_ALL_NAMESPACE_SET;
23+
import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE_SET;
24+
1625
public interface InformerEventSourceConfiguration<R extends HasMetadata>
1726
extends Informable<R> {
1827

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

148-
public Builder<R> withInformerConfiguration(
149-
Consumer<InformerConfiguration<R>.Builder> configurator) {
150-
configurator.accept(config);
151-
return this;
152-
}
153-
154157
public Builder<R> withName(String name) {
155158
this.name = name;
156159
config.withName(name);
@@ -187,6 +190,79 @@ public SecondaryToPrimaryMapper<R> getSecondaryToPrimaryMapper() {
187190
return secondaryToPrimaryMapper;
188191
}
189192

193+
public Builder<R> withNamespaces(Set<String> namespaces) {
194+
config.withNamespaces(namespaces);
195+
return this;
196+
}
197+
198+
public Builder<R> withNamespacesInheritedFromController() {
199+
withNamespaces(SAME_AS_CONTROLLER_NAMESPACES_SET);
200+
return this;
201+
}
202+
203+
public Builder<R> withWatchAllNamespaces() {
204+
withNamespaces(WATCH_ALL_NAMESPACE_SET);
205+
return this;
206+
}
207+
208+
public Builder<R> withWatchCurrentNamespace() {
209+
withNamespaces(WATCH_CURRENT_NAMESPACE_SET);
210+
return this;
211+
}
212+
213+
214+
/**
215+
* Whether the associated informer should track changes made to the parent
216+
* {@link io.javaoperatorsdk.operator.processing.Controller}'s namespaces configuration.
217+
*
218+
* @param followChanges {@code true} to reconfigure the associated informer when the parent
219+
* controller's namespaces are reconfigured, {@code false} otherwise
220+
* @return the builder instance so that calls can be chained fluently
221+
*/
222+
public Builder<R> withFollowControllerNamespacesChanges(boolean followChanges) {
223+
config.withFollowControllerNamespacesChanges(followChanges);
224+
return this;
225+
}
226+
227+
public Builder<R> withLabelSelector(String labelSelector) {
228+
config.withLabelSelector(labelSelector);
229+
return this;
230+
}
231+
232+
public Builder<R> withOnAddFilter(
233+
OnAddFilter<? super R> onAddFilter) {
234+
config.withOnAddFilter(onAddFilter);
235+
return this;
236+
}
237+
238+
public Builder<R> withOnUpdateFilter(
239+
OnUpdateFilter<? super R> onUpdateFilter) {
240+
config.withOnUpdateFilter(onUpdateFilter);
241+
return this;
242+
}
243+
244+
public Builder<R> withOnDeleteFilter(
245+
OnDeleteFilter<? super R> onDeleteFilter) {
246+
config.withOnDeleteFilter(onDeleteFilter);
247+
return this;
248+
}
249+
250+
public Builder<R> withGenericFilter(
251+
GenericFilter<? super R> genericFilter) {
252+
config.withGenericFilter(genericFilter);
253+
return this;
254+
}
255+
256+
public Builder<R> withItemStore(ItemStore<R> itemStore) {
257+
config.withItemStore(itemStore);
258+
return this;
259+
}
260+
261+
public Builder<R> withInformerListLimit(Long informerListLimit) {
262+
config.withInformerListLimit(informerListLimit);
263+
return this;
264+
}
265+
190266
public void updateFrom(InformerConfiguration<R> informerConfig) {
191267
if (informerConfig != null) {
192268
final var informerConfigName = informerConfig.getName();

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/clusterscopedresource/ClusterScopedCustomResourceReconciler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public List<EventSource<?, ClusterScopedCustomResource>> prepareEventSources(
6363
InformerEventSourceConfiguration.from(ConfigMap.class, ClusterScopedCustomResource.class)
6464
.withSecondaryToPrimaryMapper(
6565
Mappers.fromOwnerReferences(context.getPrimaryResourceClass(), true))
66-
.withInformerConfiguration(
67-
c -> c.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE))
66+
.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE)
6867
.build(),
6968
context);
7069
return List.of(ies);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/createupdateeventfilter/CreateUpdateEventFilterTestReconciler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public List<EventSource<?, CreateUpdateEventFilterTestCustomResource>> prepareEv
7272
InformerEventSourceConfiguration<ConfigMap> informerConfiguration =
7373
InformerEventSourceConfiguration
7474
.from(ConfigMap.class, CreateUpdateEventFilterTestCustomResource.class)
75-
.withInformerConfiguration(c -> c
76-
.withLabelSelector("integrationtest = " + this.getClass().getSimpleName()))
75+
.withLabelSelector("integrationtest = " + this.getClass().getSimpleName())
7776
.build();
77+
7878
final var informerEventSource =
79-
new InformerEventSource<ConfigMap, CreateUpdateEventFilterTestCustomResource>(
79+
new InformerEventSource<>(
8080
informerConfiguration, context);
8181
this.configMapDR.setEventSource(informerEventSource);
8282

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/filter/FilterTestReconciler.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public List<EventSource<?, FilterTestCustomResource>> prepareEventSources(
5959

6060
final var informerConfiguration = InformerEventSourceConfiguration
6161
.from(ConfigMap.class, FilterTestCustomResource.class)
62-
.withInformerConfiguration(c -> c.withOnUpdateFilter((newCM,
62+
.withOnUpdateFilter((newCM,
6363
oldCM) -> !newCM.getData().get(CM_VALUE_KEY)
64-
.equals(CONFIG_MAP_FILTER_VALUE)))
64+
.equals(CONFIG_MAP_FILTER_VALUE))
6565
.build();
6666
InformerEventSource<ConfigMap, FilterTestCustomResource> configMapES =
6767
new InformerEventSource<>(informerConfiguration, context);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/informerremotecluster/InformerRemoteClusterReconciler.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.fabric8.kubernetes.api.model.ConfigMap;
66
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
77
import io.fabric8.kubernetes.client.KubernetesClient;
8-
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
98
import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration;
109
import io.javaoperatorsdk.operator.api.reconciler.Context;
1110
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
@@ -57,8 +56,7 @@ public List<EventSource<?, InformerRemoteClusterCustomResource>> prepareEventSou
5756
Mappers.fromDefaultAnnotations(InformerRemoteClusterCustomResource.class))
5857
// setting remote client for informer
5958
.withKubernetesClient(remoteClient)
60-
.withInformerConfiguration(
61-
InformerConfiguration.Builder::withWatchAllNamespaces)
59+
.withWatchAllNamespaces()
6260
.build(), context);
6361

6462
return List.of(es);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/multiplesecondaryeventsource/MultipleSecondaryEventSourceReconciler.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ public List<EventSource<?, MultipleSecondaryEventSourceCustomResource>> prepareE
7070

7171
var config = InformerEventSourceConfiguration
7272
.from(ConfigMap.class, MultipleSecondaryEventSourceCustomResource.class)
73-
.withInformerConfiguration(c -> c
74-
// TODO: this shouldn't be needed since this should be the default behavior (tracking
75-
// the controller's namespaces)
76-
.withNamespaces(
77-
context.getControllerConfiguration().getInformerConfig().getNamespaces())
78-
.withLabelSelector("multisecondary"))
73+
.withNamespacesInheritedFromController()
74+
.withLabelSelector("multisecondary")
7975
.withSecondaryToPrimaryMapper(s -> {
8076
var name =
8177
s.getMetadata().getName().subSequence(0, s.getMetadata().getName().length() - 1);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/primarytosecondary/JobReconciler.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.concurrent.atomic.AtomicInteger;
66
import java.util.stream.Collectors;
77

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

7775
if (addPrimaryToSecondaryMapper) {
7876
informerConfiguration = informerConfiguration.withPrimaryToSecondaryMapper(

Diff for: sample-operators/webpage/src/main/java/io/javaoperatorsdk/operator/sample/WebPageReconciler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ public List<EventSource<?, WebPage>> prepareEventSources(EventSourceContext<WebP
4343
var configMapEventSource =
4444
new InformerEventSource<>(
4545
InformerEventSourceConfiguration.from(ConfigMap.class, WebPage.class)
46-
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
46+
.withLabelSelector(SELECTOR)
4747
.build(),
4848
context);
4949
var deploymentEventSource =
5050
new InformerEventSource<>(
5151
InformerEventSourceConfiguration.from(Deployment.class, WebPage.class)
52-
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
52+
.withLabelSelector(SELECTOR)
5353
.build(),
5454
context);
5555
var serviceEventSource =
5656
new InformerEventSource<>(
5757
InformerEventSourceConfiguration.from(Service.class, WebPage.class)
58-
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
58+
.withLabelSelector(SELECTOR)
5959
.build(),
6060
context);
6161
var ingressEventSource =
6262
new InformerEventSource<>(
6363
InformerEventSourceConfiguration.from(Ingress.class, WebPage.class)
64-
.withInformerConfiguration(c -> c.withLabelSelector(SELECTOR))
64+
.withLabelSelector(SELECTOR)
6565
.build(),
6666
context);
6767
return List.of(configMapEventSource, deploymentEventSource,

0 commit comments

Comments
 (0)