Skip to content

improve: api for dynamic registration of event sources #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();

}