Skip to content

Commit 771cd5f

Browse files
committed
feat: introduce getEffectiveNamespaces on ControllerConfiguration
1 parent 9a0d987 commit 771cd5f

File tree

4 files changed

+21
-29
lines changed

4 files changed

+21
-29
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.io.Closeable;
1212
import java.io.IOException;
1313
import java.util.ArrayList;
14-
import java.util.Arrays;
1514
import java.util.List;
1615
import org.slf4j.Logger;
1716
import org.slf4j.LoggerFactory;
@@ -144,7 +143,7 @@ public <R extends CustomResource> void register(
144143
resClass,
145144
configuration.watchAllNamespaces()
146145
? "[all namespaces]"
147-
: Arrays.toString(eventSourceManager.getTargetNamespaces()));
146+
: configuration.getEffectiveNamespaces());
148147
}
149148
}
150149
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ControllerConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ static boolean currentNamespaceWatched(Set<String> namespaces) {
4141
&& namespaces.contains(Controller.WATCH_CURRENT_NAMESPACE);
4242
}
4343

44+
default Set<String> getEffectiveNamespaces() {
45+
var targetNamespaces = getNamespaces();
46+
if (watchCurrentNamespace()) {
47+
targetNamespaces =
48+
Collections.singleton(getConfigurationService().getClientConfiguration().getNamespace());
49+
}
50+
return targetNamespaces;
51+
}
52+
4453
default RetryConfiguration getRetryConfiguration() {
4554
return RetryConfiguration.DEFAULT;
4655
}

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class DefaultEventSourceManager implements EventSourceManager {
2727
private final ReentrantLock lock = new ReentrantLock();
2828
private final Map<String, EventSource> eventSources = new ConcurrentHashMap<>();
2929
private final DefaultEventHandler defaultEventHandler;
30-
private String[] targetNamespaces;
3130
private TimerEventSource retryTimerEventSource;
3231

3332
DefaultEventSourceManager(DefaultEventHandler defaultEventHandler, boolean supportRetry) {
@@ -39,28 +38,16 @@ public class DefaultEventSourceManager implements EventSourceManager {
3938
}
4039
}
4140

42-
public String[] getTargetNamespaces() {
43-
return targetNamespaces;
44-
}
45-
4641
public <R extends CustomResource> DefaultEventSourceManager(
4742
ResourceController<R> controller,
4843
ControllerConfiguration<R> configuration,
4944
MixedOperation<R, KubernetesResourceList<R>, Resource<R>> client) {
5045
this(new DefaultEventHandler(controller, configuration, client), true);
51-
// check if we only want to watch the current namespace
52-
targetNamespaces = configuration.getNamespaces().toArray(new String[] {});
53-
if (configuration.watchCurrentNamespace()) {
54-
targetNamespaces =
55-
new String[] {
56-
configuration.getConfigurationService().getClientConfiguration().getNamespace()
57-
};
58-
}
5946
registerEventSource(
6047
CUSTOM_RESOURCE_EVENT_SOURCE_NAME,
6148
new CustomResourceEventSource(
6249
client,
63-
targetNamespaces,
50+
configuration.getEffectiveNamespaces(),
6451
configuration.isGenerationAware(),
6552
configuration.getFinalizer(),
6653
configuration.getCustomResourceClass()));

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CustomResourceEventSource extends AbstractEventSource
2727
private static final Logger log = LoggerFactory.getLogger(CustomResourceEventSource.class);
2828

2929
private final MixedOperation client;
30-
private final String[] targetNamespaces;
30+
private final Set<String> targetNamespaces;
3131
private final boolean generationAware;
3232
private final String resourceFinalizer;
3333
private final Map<String, Long> lastGenerationProcessedSuccessfully = new ConcurrentHashMap<>();
@@ -36,7 +36,7 @@ public class CustomResourceEventSource extends AbstractEventSource
3636

3737
public CustomResourceEventSource(
3838
MixedOperation client,
39-
String[] targetNamespaces,
39+
Set<String> targetNamespaces,
4040
boolean generationAware,
4141
String resourceFinalizer,
4242
Class<?> resClass) {
@@ -48,28 +48,25 @@ public CustomResourceEventSource(
4848
this.resClass = resClass.getName();
4949
}
5050

51-
public String[] getTargetNamespaces() {
52-
return targetNamespaces;
53-
}
54-
5551
@Override
5652
public void start() {
5753
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl) client;
58-
if (ControllerConfiguration.allNamespacesWatched(Set.of(targetNamespaces))) {
54+
if (ControllerConfiguration.allNamespacesWatched(targetNamespaces)) {
5955
var w = crClient.inAnyNamespace().watch(this);
6056
watches.add(w);
6157
log.debug("Registered controller {} -> {} for any namespace", resClass, w);
62-
} else if (targetNamespaces.length == 0) {
58+
} else if (targetNamespaces.isEmpty()) {
6359
var w = client.watch(this);
6460
watches.add(w);
6561
log.debug(
6662
"Registered controller {} -> {} for namespace {}", resClass, w, crClient.getNamespace());
6763
} else {
68-
for (String targetNamespace : targetNamespaces) {
69-
var w = crClient.inNamespace(targetNamespace).watch(this);
70-
watches.add(w);
71-
log.debug("Registered controller {} -> {} for namespace: {}", resClass, w, targetNamespace);
72-
}
64+
targetNamespaces.forEach(
65+
ns -> {
66+
var w = crClient.inNamespace(ns).watch(this);
67+
watches.add(w);
68+
log.debug("Registered controller {} -> {} for namespace: {}", resClass, w, ns);
69+
});
7370
}
7471
}
7572

0 commit comments

Comments
 (0)