Skip to content

Commit f43cf54

Browse files
committed
improve: InformerEventSourceConfiguration facade for InformerConfiguration
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 39d9a4a commit f43cf54

File tree

9 files changed

+102
-31
lines changed

9 files changed

+102
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
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))

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

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

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

Lines changed: 1 addition & 2 deletions
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);

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

Lines changed: 3 additions & 3 deletions
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

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

Lines changed: 2 additions & 2 deletions
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);

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

Lines changed: 1 addition & 3 deletions
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);

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

Lines changed: 2 additions & 6 deletions
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);

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

Lines changed: 1 addition & 3 deletions
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(

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

Lines changed: 4 additions & 4 deletions
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)