Skip to content

Commit 39684d5

Browse files
committed
feat: utility method for event source default names
1 parent 9d36fb3 commit 39684d5

File tree

7 files changed

+20
-9
lines changed

7 files changed

+20
-9
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceInitializer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.operator.api.reconciler;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45

56
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -22,4 +23,12 @@ public interface EventSourceInitializer<P extends HasMetadata> {
2223
*/
2324
Map<String, EventSource> prepareEventSources(EventSourceContext<P> context);
2425

26+
static Map<String, EventSource> defaultNamedEventSources(EventSource... eventSources) {
27+
Map<String, EventSource> eventSourceMap = new HashMap<>(eventSources.length);
28+
for (EventSource eventSource : eventSources) {
29+
eventSourceMap.put(EventSource.defaultNameFor(eventSource), eventSource);
30+
}
31+
return eventSourceMap;
32+
}
33+
2534
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Map<String, EventSource> prepareEventSources(
106106
.withLabelSelector("integrationtest = " + this.getClass().getSimpleName())
107107
.build();
108108
informerEventSource = new InformerEventSource<>(informerConfiguration, client);
109-
return Map.of("test-informer", informerEventSource);
109+
return EventSourceInitializer.defaultNamedEventSources(informerEventSource);
110110
}
111111

112112
@Override

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/informereventsource/InformerEventSourceTestCustomReconciler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public Map<String, EventSource> prepareEventSources(
4646
.withPrimaryResourcesRetriever(Mappers.fromAnnotation(RELATED_RESOURCE_NAME))
4747
.build();
4848

49-
return Map.of("test-informer", new InformerEventSource<>(config, context));
49+
return EventSourceInitializer
50+
.defaultNamedEventSources(new InformerEventSource<>(config, context));
5051
}
5152

5253
@Override

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/standalonedependent/StandaloneDependentTestReconciler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public StandaloneDependentTestReconciler() {
3939
@Override
4040
public Map<String, EventSource> prepareEventSources(
4141
EventSourceContext<StandaloneDependentTestCustomResource> context) {
42-
return Map.of("deployment", deploymentDependent.initEventSource(context));
42+
return EventSourceInitializer
43+
.defaultNamedEventSources(deploymentDependent.initEventSource(context));
4344
}
4445

4546
@Override

sample-operators/tomcat-operator/src/main/java/io/javaoperatorsdk/operator/sample/WebappReconciler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public Map<String, EventSource> prepareEventSources(EventSourceContext<Webapp> c
7272
.withPrimaryResourcesRetriever(webappsMatchingTomcatName)
7373
.withAssociatedSecondaryResourceIdentifier(tomcatFromWebAppSpec)
7474
.build();
75-
return Map.of("tomcat-informer", new InformerEventSource<>(configuration, context));
75+
return EventSourceInitializer
76+
.defaultNamedEventSources(new InformerEventSource<>(configuration, context));
7677
}
7778

7879
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public Map<String, EventSource> prepareEventSources(EventSourceContext<WebPage>
6565
new InformerEventSource<>(InformerConfiguration.from(context, Service.class)
6666
.withLabelSelector(LOW_LEVEL_LABEL_KEY)
6767
.build(), context);
68-
return Map.of("configmap", configMapEventSource, "deployment", deploymentEventSource, "service",
68+
return EventSourceInitializer.defaultNamedEventSources(configMapEventSource,
69+
deploymentEventSource,
6970
serviceEventSource);
7071
}
7172

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ public WebPageStandaloneDependentsReconciler(KubernetesClient kubernetesClient)
4747

4848
@Override
4949
public Map<String, EventSource> prepareEventSources(EventSourceContext<WebPage> context) {
50-
return Map.of(
51-
"configmap", configMapDR.initEventSource(context),
52-
"deployment", deploymentDR.initEventSource(context),
53-
"service", serviceDR.initEventSource(context));
50+
return EventSourceInitializer.defaultNamedEventSources(configMapDR.initEventSource(context),
51+
deploymentDR.initEventSource(context), serviceDR.initEventSource(context));
5452
}
5553

5654
@Override

0 commit comments

Comments
 (0)