Skip to content

Commit

Permalink
improve: api for dynamic registration of event sources (#2162)
Browse files Browse the repository at this point in the history
fixes also a typo

Signed-off-by: Attila Mészáros <csviri@gmail.com>
  • Loading branch information
csviri authored Dec 14, 2023
1 parent c761ffa commit 5a9cdbd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private <R> void registerOrDeregisterEventSourceBasedOnActivation(boolean activa
if (activationConditionMet) {
var eventSource =
dependentResourceNode.getDependentResource().eventSource(context.eventSourceRetriever()
.eventSourceContexForDynamicRegistration());
.eventSourceContextForDynamicRegistration());
var es = eventSource.orElseThrow();
context.eventSourceRetriever()
.dynamicallyRegisterEventSource(dependentResourceNode.getName(), es);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -233,24 +229,28 @@ public <R> List<ResourceEventSource<R, P>> getResourceEventSourcesFor(Class<R> d
}

@Override
public synchronized void dynamicallyRegisterEventSource(String name, EventSource eventSource) {
if (eventSources.existing(name, eventSource) != null) {
return;
public synchronized EventSource dynamicallyRegisterEventSource(String name,
EventSource eventSource) {
var es = eventSources.existing(name, eventSource);
if (es != null) {
return es;
}
registerEventSource(name, eventSource);
eventSource.start();
return eventSource;
}

@Override
public synchronized void dynamicallyDeRegisterEventSource(String name) {
public synchronized Optional<EventSource> dynamicallyDeRegisterEventSource(String name) {
EventSource es = eventSources.remove(name);
if (es != null) {
es.stop();
}
return Optional.ofNullable(es);
}

@Override
public EventSourceContext<P> eventSourceContexForDynamicRegistration() {
public EventSourceContext<P> eventSourceContextForDynamicRegistration() {
return controller.eventSourceContext();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.javaoperatorsdk.operator.processing.event;

import java.util.List;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
Expand Down Expand Up @@ -40,8 +41,9 @@ default <R> ResourceEventSource<R, P> getResourceEventSourceFor(Class<R> depende
*
* @param name of the event source
* @param eventSource to register
* @return the actual event source registered. Might not be the same as the parameter.
*/
void dynamicallyRegisterEventSource(String name, EventSource eventSource);
EventSource dynamicallyRegisterEventSource(String name, EventSource eventSource);

/**
* De-registers (and stops) the {@link EventSource} associated with the specified name. If no such
Expand All @@ -57,9 +59,10 @@ default <R> ResourceEventSource<R, P> getResourceEventSourceFor(Class<R> depende
* </p>
*
* @param name of the event source
* @return the actual event source deregistered if there is one.
*/
void dynamicallyDeRegisterEventSource(String name);
Optional<EventSource> dynamicallyDeRegisterEventSource(String name);

EventSourceContext<P> eventSourceContexForDynamicRegistration();
EventSourceContext<P> eventSourceContextForDynamicRegistration();

}

0 comments on commit 5a9cdbd

Please sign in to comment.