Skip to content

Commit d2b7efd

Browse files
csvirimetacosm
andcommitted
improve: remove EventSourceInitializer (#2257)
Signed-off-by: Attila Mészáros <[email protected]> Signed-off-by: Chris Laprun <[email protected]> Co-authored-by: Chris Laprun <[email protected]> Signed-off-by: Attila Mészáros <[email protected]>
1 parent ae32be1 commit d2b7efd

File tree

40 files changed

+155
-183
lines changed

40 files changed

+155
-183
lines changed

Diff for: bootstrapper-maven-plugin/src/main/resources/templates/Reconciler.java

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
66
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
77
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
8-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
98
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
109
import io.javaoperatorsdk.operator.api.reconciler.Context;
1110
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import com.github.benmanes.caffeine.cache.Caffeine;
2929

3030
public abstract class AbstractTestReconciler<P extends CustomResource<BoundedCacheTestSpec, BoundedCacheTestStatus>>
31-
implements Reconciler<P>, EventSourceInitializer<P> {
31+
implements Reconciler<P> {
3232

3333
private static final Logger log =
3434
LoggerFactory.getLogger(BoundedCacheClusterScopeTestReconciler.class);
@@ -82,7 +82,7 @@ public Map<String, EventSource> prepareEventSources(
8282
Mappers.fromOwnerReference(this instanceof BoundedCacheClusterScopeTestReconciler))
8383
.build(), context);
8484

85-
return EventSourceInitializer.nameEventSources(es);
85+
return EventSourceUtils.nameEventSources(es);
8686
}
8787

8888
private void ensureStatus(P resource) {

Diff for: docs/documentation/v5-0-migration.md

+5
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ permalink: /docs/v5-0-migration
1212
1. [Result of managed dependent resources](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/managed/ManagedDependentResourceContext.java#L55-L57)
1313
is not `Optional` anymore. In case you use this result, simply use the result
1414
objects directly.
15+
2. `EventSourceInitializer` is not a separate interface anymore. It is part of the `Reconciler` interface with a
16+
default implementation. You can simply remove this interface from your reconciler. The
17+
[`EventSourceUtils`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceUtils.java#L11-L11)
18+
now contains all the utility methods used for event sources naming that were previously defined in
19+
the `EventSourceInitializer` interface.

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceInitializer.java renamed to operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceUtils.java

+6-24
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,15 @@
88
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
99
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;
1010

11-
/**
12-
* An interface that a {@link Reconciler} can implement to have the SDK register the provided
13-
* {@link EventSource}
14-
*
15-
* @param <P> the primary resource type handled by the associated {@link Reconciler}
16-
*/
17-
public interface EventSourceInitializer<P extends HasMetadata> {
18-
19-
/**
20-
* Prepares a map of {@link EventSource} implementations keyed by the name with which they need to
21-
* be registered by the SDK.
22-
*
23-
* @param context a {@link EventSourceContext} providing access to information useful to event
24-
* sources
25-
* @return a map of event sources to register
26-
*/
27-
Map<String, EventSource> prepareEventSources(EventSourceContext<P> context);
28-
11+
public class EventSourceUtils {
2912
/**
3013
* Utility method to easily create map with generated name for event sources. This is for the use
3114
* case when the event sources are not access explicitly by name in the reconciler.
3215
*
3316
* @param eventSources to name
3417
* @return even source with default names
3518
*/
36-
static Map<String, EventSource> nameEventSources(EventSource... eventSources) {
19+
public static Map<String, EventSource> nameEventSources(EventSource... eventSources) {
3720
Map<String, EventSource> eventSourceMap = new HashMap<>(eventSources.length);
3821
for (EventSource eventSource : eventSources) {
3922
eventSourceMap.put(generateNameFor(eventSource), eventSource);
@@ -42,7 +25,7 @@ static Map<String, EventSource> nameEventSources(EventSource... eventSources) {
4225
}
4326

4427
@SuppressWarnings("unchecked")
45-
static <K extends HasMetadata> Map<String, EventSource> eventSourcesFromWorkflow(
28+
public static <K extends HasMetadata> Map<String, EventSource> eventSourcesFromWorkflow(
4629
EventSourceContext<K> context,
4730
Workflow<K> workflow) {
4831
Map<String, EventSource> result = new HashMap<>();
@@ -54,13 +37,13 @@ static <K extends HasMetadata> Map<String, EventSource> eventSourcesFromWorkflow
5437
}
5538

5639
@SuppressWarnings("rawtypes")
57-
static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
40+
public static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
5841
EventSourceContext<K> context, DependentResource... dependentResources) {
5942
return nameEventSourcesFromDependentResource(context, Arrays.asList(dependentResources));
6043
}
6144

6245
@SuppressWarnings("unchecked,rawtypes")
63-
static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
46+
public static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
6447
EventSourceContext<K> context, Collection<DependentResource> dependentResources) {
6548

6649
if (dependentResources != null) {
@@ -81,9 +64,8 @@ static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDepe
8164
* @param eventSource EventSource
8265
* @return generated name
8366
*/
84-
static String generateNameFor(EventSource eventSource) {
67+
public static String generateNameFor(EventSource eventSource) {
8568
// we can have multiple event sources for the same class
8669
return eventSource.getClass().getName() + "#" + eventSource.hashCode();
8770
}
88-
8971
}

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/Reconciler.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package io.javaoperatorsdk.operator.api.reconciler;
22

3+
import java.util.*;
4+
35
import io.fabric8.kubernetes.api.model.HasMetadata;
6+
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
47

5-
public interface Reconciler<R extends HasMetadata> {
8+
public interface Reconciler<P extends HasMetadata> {
69

710
/**
811
* The implementation of this operation is required to be idempotent. Always use the UpdateControl
@@ -14,6 +17,19 @@ public interface Reconciler<R extends HasMetadata> {
1417
* @return UpdateControl to manage updates on the custom resource (usually the status) after
1518
* reconciliation.
1619
*/
17-
UpdateControl<R> reconcile(R resource, Context<R> context) throws Exception;
20+
UpdateControl<P> reconcile(P resource, Context<P> context) throws Exception;
21+
22+
23+
/**
24+
* Prepares a map of {@link EventSource} implementations keyed by the name with which they need to
25+
* be registered by the SDK.
26+
*
27+
* @param context a {@link EventSourceContext} providing access to information useful to event
28+
* sources
29+
* @return a map of event sources to register
30+
*/
31+
default Map<String, EventSource> prepareEventSources(EventSourceContext<P> context) {
32+
return Map.of();
33+
}
1834

1935
}

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/Controller.java

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package io.javaoperatorsdk.operator.processing;
22

3-
import java.util.ArrayList;
4-
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
7-
import java.util.Optional;
8-
import java.util.Set;
3+
import java.util.*;
94

105
import org.slf4j.Logger;
116
import org.slf4j.LoggerFactory;
@@ -25,16 +20,7 @@
2520
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
2621
import io.javaoperatorsdk.operator.api.monitoring.Metrics;
2722
import io.javaoperatorsdk.operator.api.monitoring.Metrics.ControllerExecution;
28-
import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
29-
import io.javaoperatorsdk.operator.api.reconciler.Constants;
30-
import io.javaoperatorsdk.operator.api.reconciler.Context;
31-
import io.javaoperatorsdk.operator.api.reconciler.ContextInitializer;
32-
import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
33-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
34-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
35-
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
36-
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
37-
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
23+
import io.javaoperatorsdk.operator.api.reconciler.*;
3824
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceNotFoundException;
3925
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider;
4026
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceReferencer;
@@ -223,11 +209,8 @@ private void initContextIfNeeded(P resource, Context<P> context) {
223209
}
224210

225211
public void initAndRegisterEventSources(EventSourceContext<P> context) {
226-
if (reconciler instanceof EventSourceInitializer) {
227-
final var provider = (EventSourceInitializer<P>) this.reconciler;
228-
final var ownSources = provider.prepareEventSources(context);
229-
ownSources.forEach(eventSourceManager::registerEventSource);
230-
}
212+
final var ownSources = this.reconciler.prepareEventSources(context);
213+
ownSources.forEach(eventSourceManager::registerEventSource);
231214

232215
// register created event sources
233216
final var dependentResourcesByName =

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
1515
import io.javaoperatorsdk.operator.api.config.NamespaceChangeable;
1616
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
17-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
17+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceUtils;
1818
import io.javaoperatorsdk.operator.processing.Controller;
1919
import io.javaoperatorsdk.operator.processing.LifecycleAware;
2020
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
@@ -150,7 +150,7 @@ public final synchronized void registerEventSource(String name, EventSource even
150150
Objects.requireNonNull(eventSource, "EventSource must not be null");
151151
try {
152152
if (name == null || name.isBlank()) {
153-
name = EventSourceInitializer.generateNameFor(eventSource);
153+
name = EventSourceUtils.generateNameFor(eventSource);
154154
}
155155
if (eventSource instanceof ManagedInformerEventSource) {
156156
var managedInformerEventSource = ((ManagedInformerEventSource) eventSource);

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/NamedEventSource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Optional;
55

66
import io.javaoperatorsdk.operator.OperatorException;
7-
import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
7+
import io.javaoperatorsdk.operator.api.reconciler.EventSourceUtils;
88
import io.javaoperatorsdk.operator.processing.event.source.Configurable;
99
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
1010
import io.javaoperatorsdk.operator.processing.event.source.EventSourceStartPriority;
@@ -19,7 +19,7 @@ class NamedEventSource implements EventSource, EventSourceMetadata {
1919
NamedEventSource(EventSource original, String name) {
2020
this.original = original;
2121
this.name = name;
22-
nameSet = !name.equals(EventSourceInitializer.generateNameFor(original));
22+
nameSet = !name.equals(EventSourceUtils.generateNameFor(original));
2323
}
2424

2525
@Override

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/EventSource.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
/**
99
* Creates an event source to trigger your reconciler whenever something happens to a secondary or
10-
* external resource that would not normally trigger your reconciler (as the primary resources are
11-
* not changed). To register EventSources with so that your reconciler is triggered, please make
12-
* your reconciler implement
13-
* {@link io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer}.
10+
* external resource that should cause a reconciliation of the primary resource. EventSource
11+
* generalizes the concept of Informers and extends it to external (i.e. non Kubernetes) resources.
1412
*/
1513
public interface EventSource extends LifecycleAware, EventSourceHealthIndicator {
1614

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceInitializerTest.java renamed to operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/EventSourceUtilsTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
import static org.assertj.core.api.Assertions.assertThat;
1010

11-
class EventSourceInitializerTest {
11+
class EventSourceUtilsTest {
1212

1313
@Test
1414
@SuppressWarnings({"rawtypes", "unchecked"})
1515
void defaultNameDifferentForOtherInstance() {
1616
var eventSource1 = new PollingEventSource(HashMap::new, 1000, String.class);
1717
var eventSource2 = new PollingEventSource(HashMap::new, 1000, String.class);
18-
var eventSourceName1 = EventSourceInitializer.generateNameFor(eventSource1);
19-
var eventSourceName2 = EventSourceInitializer.generateNameFor(eventSource2);
18+
var eventSourceName1 = EventSourceUtils.generateNameFor(eventSource1);
19+
var eventSourceName2 = EventSourceUtils.generateNameFor(eventSource2);
2020

2121
assertThat(eventSourceName1).isNotEqualTo(eventSourceName2);
2222
}

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerResourceEventSourceTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import io.javaoperatorsdk.operator.TestUtils;
1212
import io.javaoperatorsdk.operator.api.config.BaseConfigurationService;
1313
import io.javaoperatorsdk.operator.api.config.ResolvedControllerConfiguration;
14+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
15+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
1416
import io.javaoperatorsdk.operator.processing.Controller;
1517
import io.javaoperatorsdk.operator.processing.event.EventHandler;
1618
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
@@ -152,18 +154,21 @@ void genericFilterFiltersOutAddUpdateAndDeleteEvents() {
152154
@SuppressWarnings("unchecked")
153155
private static class TestController extends Controller<TestCustomResource> {
154156

157+
private static final Reconciler<TestCustomResource> reconciler =
158+
(resource, context) -> UpdateControl.noUpdate();
159+
155160
private final EventSourceManager<TestCustomResource> eventSourceManager =
156161
mock(EventSourceManager.class);
157162

158163
public TestController(OnAddFilter<TestCustomResource> onAddFilter,
159164
OnUpdateFilter<TestCustomResource> onUpdateFilter,
160165
GenericFilter<TestCustomResource> genericFilter) {
161-
super(null, new TestConfiguration(true, onAddFilter, onUpdateFilter, genericFilter),
166+
super(reconciler, new TestConfiguration(true, onAddFilter, onUpdateFilter, genericFilter),
162167
MockKubernetesClient.client(TestCustomResource.class));
163168
}
164169

165170
public TestController(boolean generationAware) {
166-
super(null, new TestConfiguration(generationAware, null, null, null),
171+
super(reconciler, new TestConfiguration(generationAware, null, null, null),
167172
MockKubernetesClient.client(TestCustomResource.class));
168173
}
169174

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/bulkdependent/StandaloneBulkDependentReconciler.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
@ControllerConfiguration
1111
public class StandaloneBulkDependentReconciler
12-
implements Reconciler<BulkDependentTestCustomResource>, TestExecutionInfoProvider,
13-
EventSourceInitializer<BulkDependentTestCustomResource> {
12+
implements Reconciler<BulkDependentTestCustomResource>, TestExecutionInfoProvider {
1413

1514
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
1615

@@ -38,7 +37,7 @@ public int getNumberOfExecutions() {
3837
@Override
3938
public Map<String, EventSource> prepareEventSources(
4039
EventSourceContext<BulkDependentTestCustomResource> context) {
41-
return EventSourceInitializer
40+
return EventSourceUtils
4241
.nameEventSources(dependent.initEventSource(context));
4342
}
4443
}

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/changenamespace/ChangeNamespaceTestReconciler.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
@ControllerConfiguration
1515
public class ChangeNamespaceTestReconciler
16-
implements Reconciler<ChangeNamespaceTestCustomResource>,
17-
EventSourceInitializer<ChangeNamespaceTestCustomResource> {
16+
implements Reconciler<ChangeNamespaceTestCustomResource> {
1817

1918
private final ConcurrentHashMap<ResourceID, Integer> numberOfResourceReconciliations =
2019
new ConcurrentHashMap<>();
@@ -27,7 +26,7 @@ public Map<String, EventSource> prepareEventSources(
2726
new InformerEventSource<>(InformerConfiguration.from(ConfigMap.class, context)
2827
.build(), context);
2928

30-
return EventSourceInitializer.nameEventSources(configMapES);
29+
return EventSourceUtils.nameEventSources(configMapES);
3130
}
3231

3332
@Override

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
@ControllerConfiguration
1515
public class ClusterScopedCustomResourceReconciler
16-
implements Reconciler<ClusterScopedCustomResource>,
17-
EventSourceInitializer<ClusterScopedCustomResource> {
16+
implements Reconciler<ClusterScopedCustomResource> {
1817

1918
public static final String DATA_KEY = "data-key";
2019

@@ -59,6 +58,6 @@ public Map<String, EventSource> prepareEventSources(
5958
.withSecondaryToPrimaryMapper(Mappers.fromOwnerReference(true))
6059
.withLabelSelector(TEST_LABEL_KEY + "=" + TEST_LABEL_VALUE)
6160
.build(), context);
62-
return EventSourceInitializer.nameEventSources(ies);
61+
return EventSourceUtils.nameEventSources(ies);
6362
}
6463
}

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/complexdependent/ComplexDependentReconciler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
readyPostcondition = StatefulSetReadyCondition.class),
3232
})
3333
@ControllerConfiguration(name = "project-operator")
34-
public class ComplexDependentReconciler implements Reconciler<ComplexDependentCustomResource>,
35-
EventSourceInitializer<ComplexDependentCustomResource> {
34+
public class ComplexDependentReconciler implements Reconciler<ComplexDependentCustomResource> {
3635

3736
public static final String SERVICE_EVENT_SOURCE_NAME = "serviceEventSource";
3837
public static final String STATEFUL_SET_EVENT_SOURCE_NAME = "statefulSetEventSource";

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515

1616
@ControllerConfiguration
1717
public class CreateUpdateEventFilterTestReconciler
18-
implements Reconciler<CreateUpdateEventFilterTestCustomResource>,
19-
EventSourceInitializer<CreateUpdateEventFilterTestCustomResource> {
18+
implements Reconciler<CreateUpdateEventFilterTestCustomResource> {
2019

2120
private static final class DirectConfigMapDependentResource
2221
extends
@@ -97,7 +96,7 @@ public Map<String, EventSource> prepareEventSources(
9796
informerEventSource = new InformerEventSource<>(informerConfiguration, context.getClient());
9897
this.configMapDR.setEventSource(informerEventSource);
9998

100-
return EventSourceInitializer.nameEventSources(informerEventSource);
99+
return EventSourceUtils.nameEventSources(informerEventSource);
101100
}
102101

103102
public int getNumberOfExecutions() {

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/dependentreinitialization/DependentReInitializationReconciler.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
@ControllerConfiguration
99
public class DependentReInitializationReconciler
10-
implements Reconciler<DependentReInitializationCustomResource>,
11-
EventSourceInitializer<DependentReInitializationCustomResource> {
10+
implements Reconciler<DependentReInitializationCustomResource> {
1211

1312
private final ConfigMapDependentResource configMapDependentResource;
1413

@@ -27,7 +26,7 @@ public UpdateControl<DependentReInitializationCustomResource> reconcile(
2726
@Override
2827
public Map<String, EventSource> prepareEventSources(
2928
EventSourceContext<DependentReInitializationCustomResource> context) {
30-
return EventSourceInitializer.nameEventSourcesFromDependentResource(context,
29+
return EventSourceUtils.nameEventSourcesFromDependentResource(context,
3130
configMapDependentResource);
3231
}
3332

0 commit comments

Comments
 (0)