Skip to content

Commit 7db2932

Browse files
committed
improve: mamed event sources and equality of event sources and their implications
Signed-off-by: Attila Mészáros <csviri@gmail.com>
1 parent a08d725 commit 7db2932

File tree

52 files changed

+235
-159
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+235
-159
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javaoperatorsdk.operator.processing.event.source.cache.sample;
22

33
import java.time.Duration;
4+
import java.util.List;
45
import java.util.Map;
56

67
import org.slf4j.Logger;
@@ -69,7 +70,7 @@ protected void createConfigMap(P resource, Context<P> context) {
6970
}
7071

7172
@Override
72-
public Map<String, EventSource> prepareEventSources(
73+
public List<EventSource> prepareEventSources(
7374
EventSourceContext<P> context) {
7475

7576
var boundedItemStore =
@@ -82,7 +83,7 @@ public Map<String, EventSource> prepareEventSources(
8283
Mappers.fromOwnerReference(this instanceof BoundedCacheClusterScopeTestReconciler))
8384
.build(), context);
8485

85-
return EventSourceUtils.nameEventSources(es);
86+
return List.of(es);
8687
}
8788

8889
private void ensureStatus(P resource) {

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@
55
import io.fabric8.kubernetes.api.model.HasMetadata;
66
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
77
import io.javaoperatorsdk.operator.processing.dependent.workflow.Workflow;
8+
import io.javaoperatorsdk.operator.processing.event.Event;
89
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
910
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;
11+
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
1012

13+
// todo cleanup / delete
1114
public class EventSourceUtils {
15+
16+
@SuppressWarnings("unchecked")
17+
public static <R extends HasMetadata> List<EventSource> dependentEventSources(EventSourceContext<R> eventSourceContext,DependentResource... dependentResources) {
18+
return Arrays.stream(dependentResources)
19+
.flatMap(dr-> dr.eventSource(eventSourceContext).stream()).toList();
20+
}
21+
1222
/**
1323
* Utility method to easily create map with generated name for event sources. This is for the use
1424
* case when the event sources are not access explicitly by name in the reconciler.
@@ -25,15 +35,11 @@ public static Map<String, EventSource> nameEventSources(EventSource... eventSour
2535
}
2636

2737
@SuppressWarnings("unchecked")
28-
public static <K extends HasMetadata> Map<String, EventSource> eventSourcesFromWorkflow(
38+
public static <K extends HasMetadata> List<EventSource> eventSourcesFromWorkflow(
2939
EventSourceContext<K> context,
3040
Workflow<K> workflow) {
31-
Map<String, EventSource> result = new HashMap<>();
32-
for (var e : workflow.getDependentResourcesByNameWithoutActivationCondition().entrySet()) {
33-
var eventSource = e.getValue().eventSource(context);
34-
eventSource.ifPresent(es -> result.put(e.getKey(), (EventSource) es));
35-
}
36-
return result;
41+
return workflow.getDependentResourcesByNameWithoutActivationCondition().stream()
42+
.flatMap(dr->dr.eventSource(context).stream()).toList();
3743
}
3844

3945
@SuppressWarnings("rawtypes")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ public interface Reconciler<P extends HasMetadata> {
1919
*/
2020
UpdateControl<P> reconcile(P resource, Context<P> context) throws Exception;
2121

22-
2322
/**
2423
* Prepares a map of {@link EventSource} implementations keyed by the name with which they need to
2524
* be registered by the SDK.
2625
*
2726
* @param context a {@link EventSourceContext} providing access to information useful to event
2827
* sources
29-
* @return a map of event sources to register
28+
* @return a list of event sources
3029
*/
31-
default Map<String, EventSource> prepareEventSources(EventSourceContext<P> context) {
32-
return Map.of();
30+
// todo should be ? extends EventSource
31+
default List<EventSource> prepareEventSources(EventSourceContext<P> context) {
32+
return Collections.emptyList();
3333
}
3434

3535
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ default Optional<? extends ResourceEventSource<R, P>> eventSource(
4949
return Optional.empty();
5050
}
5151

52+
53+
5254
/**
5355
* Retrieves the secondary resource (if it exists) associated with the specified primary resource
5456
* for this DependentResource.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,19 +238,19 @@ public void initAndRegisterEventSources(EventSourceContext<P> context) {
238238
managedWorkflow.getDependentResourcesByNameWithoutActivationCondition();
239239
final var size = dependentResourcesByName.size();
240240
if (size > 0) {
241-
dependentResourcesByName.forEach((key, dependentResource) -> {
241+
dependentResourcesByName.forEach(dependentResource -> {
242242
if (dependentResource instanceof EventSourceProvider provider) {
243243
final var source = provider.initEventSource(context);
244-
eventSourceManager.registerEventSource(key, source);
244+
eventSourceManager.registerEventSource(source);
245245
} else {
246246
Optional<ResourceEventSource> eventSource = dependentResource.eventSource(context);
247-
eventSource.ifPresent(es -> eventSourceManager.registerEventSource(key, es));
247+
eventSource.ifPresent(es -> eventSourceManager.registerEventSource(es));
248248
}
249249
});
250250

251251
// resolve event sources referenced by name for dependents that reuse an existing event source
252252
final Map<String, List<EventSourceReferencer>> unresolvable = new HashMap<>(size);
253-
dependentResourcesByName.values().stream()
253+
dependentResourcesByName.stream()
254254
.filter(EventSourceReferencer.class::isInstance)
255255
.map(EventSourceReferencer.class::cast)
256256
.forEach(dr -> {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/AbstractWorkflowExecutor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ protected <R> void registerOrDeregisterEventSourceBasedOnActivation(
139139
var eventSource =
140140
dr.eventSource(eventSourceRetriever.eventSourceContextForDynamicRegistration());
141141
var es = eventSource.orElseThrow();
142-
eventSourceRetriever.dynamicallyRegisterEventSource(dr.name(), es);
143-
142+
eventSourceRetriever.dynamicallyRegisterEventSource(es);
144143
} else {
145144
eventSourceRetriever.dynamicallyDeRegisterEventSource(dr.name());
146145
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/DefaultWorkflow.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,8 @@ public Map<String, DependentResource> getDependentResourcesByName() {
148148
return resources;
149149
}
150150

151-
public Map<String, DependentResource> getDependentResourcesByNameWithoutActivationCondition() {
152-
final var resources = new HashMap<String, DependentResource>(dependentResourceNodes.size());
153-
dependentResourceNodes
154-
.forEach((name, node) -> {
155-
if (node.getActivationCondition().isEmpty()) {
156-
resources.put(name, node.getDependentResource());
157-
}
158-
});
159-
return resources;
151+
public List<DependentResource> getDependentResourcesByNameWithoutActivationCondition() {
152+
return dependentResourceNodes.values().stream().filter(n->n.getActivationCondition().isEmpty())
153+
.map(DependentResourceNode::getDependentResource).toList();
160154
}
161155
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/Workflow.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javaoperatorsdk.operator.processing.dependent.workflow;
22

33
import java.util.Collections;
4+
import java.util.List;
45
import java.util.Map;
56
import java.util.Set;
67

@@ -44,7 +45,7 @@ default Map<String, DependentResource> getDependentResourcesByName() {
4445
}
4546

4647
@SuppressWarnings("rawtypes")
47-
default Map<String, DependentResource> getDependentResourcesByNameWithoutActivationCondition() {
48-
return Collections.emptyMap();
48+
default List<DependentResource> getDependentResourcesByNameWithoutActivationCondition() {
49+
return Collections.emptyList();
4950
}
5051
}

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,23 @@ private Void stopEventSource(NamedEventSource eventSource) {
144144
return null;
145145
}
146146

147-
public final void registerEventSource(EventSource eventSource) throws OperatorException {
148-
registerEventSource(null, eventSource);
149-
}
150-
151147
@SuppressWarnings("rawtypes")
152-
public final synchronized void registerEventSource(String name, EventSource eventSource)
148+
public final synchronized void registerEventSource(EventSource eventSource)
153149
throws OperatorException {
154150
Objects.requireNonNull(eventSource, "EventSource must not be null");
155151
try {
156-
if (name == null || name.isBlank()) {
157-
name = EventSourceUtils.generateNameFor(eventSource);
158-
}
159152
if (eventSource instanceof ManagedInformerEventSource managedInformerEventSource) {
153+
160154
managedInformerEventSource.setConfigurationService(
161155
controller.getConfiguration().getConfigurationService());
162156
}
163-
final var named = new NamedEventSource(eventSource, name);
157+
final var named = new NamedEventSource(eventSource, eventSource.name());
164158
eventSources.add(named);
165159
named.setEventHandler(controller.getEventProcessor());
166160
} catch (IllegalStateException | MissingCRDException e) {
167161
throw e; // leave untouched
168162
} catch (Exception e) {
169-
throw new OperatorException("Couldn't register event source: " + name + " for "
163+
throw new OperatorException("Couldn't register event source: " + eventSource.name() + " for "
170164
+ controller.getConfiguration().getName() + " controller", e);
171165
}
172166
}
@@ -232,14 +226,13 @@ public <R> List<ResourceEventSource<R, P>> getResourceEventSourcesFor(Class<R> d
232226
}
233227

234228
@Override
235-
public EventSource dynamicallyRegisterEventSource(String name,
236-
EventSource eventSource) {
229+
public EventSource dynamicallyRegisterEventSource(EventSource eventSource) {
237230
synchronized (this) {
238-
var actual = eventSources.existing(name, eventSource);
231+
var actual = eventSources.existing(eventSource.name(), eventSource);
239232
if (actual != null) {
240233
eventSource = actual.eventSource();
241234
} else {
242-
registerEventSource(name, eventSource);
235+
registerEventSource(eventSource);
243236
}
244237
}
245238
// The start itself is blocking thus blocking only the threads which are attempt to start the

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceRetriever.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ default <R> ResourceEventSource<R, P> getResourceEventSourceFor(Class<R> depende
3939
* name will ever be registered.
4040
* </p>
4141
*
42-
* @param name of the event source
4342
* @param eventSource to register
4443
* @return the actual event source registered. Might not be the same as the parameter.
4544
*/
46-
EventSource dynamicallyRegisterEventSource(String name, EventSource eventSource);
45+
EventSource dynamicallyRegisterEventSource(EventSource eventSource);
4746

4847

4948
/**

0 commit comments

Comments
 (0)