Skip to content

Commit 397edba

Browse files
committed
feat: Controllers can now watch only the namespace they're deployed in
This is achieved by using `JOSDK_WATCH_CURRENT` as the only value of the namespaces configuration for the controller. Fixes #372
1 parent ac83055 commit 397edba

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void start() {
5858
* Registers the specified controller with this operator.
5959
*
6060
* @param controller the controller to register
61-
* @param <R> the {@code CustomResource} type associated with the controller
61+
* @param <R> the {@code CustomResource} type associated with the controller
6262
* @throws OperatorException if a problem occurred during the registration process
6363
*/
6464
public <R extends CustomResource> void register(ResourceController<R> controller)
@@ -68,14 +68,13 @@ public <R extends CustomResource> void register(ResourceController<R> controller
6868

6969
/**
7070
* Registers the specified controller with this operator, overriding its default configuration by
71-
* the specified one (usually created via {@link
72-
* io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider#override(ControllerConfiguration)},
71+
* the specified one (usually created via {@link io.javaoperatorsdk.operator.api.config.ControllerConfigurationOverrider#override(ControllerConfiguration)},
7372
* passing it the controller's original configuration.
7473
*
75-
* @param controller the controller to register
74+
* @param controller the controller to register
7675
* @param configuration the configuration with which we want to register the controller, if {@code
77-
* null}, the controller's original configuration is used
78-
* @param <R> the {@code CustomResource} type associated with the controller
76+
* null}, the controller's original configuration is used
77+
* @param <R> the {@code CustomResource} type associated with the controller
7978
* @throws OperatorException if a problem occurred during the registration process
8079
*/
8180
public <R extends CustomResource> void register(
@@ -95,7 +94,15 @@ public <R extends CustomResource> void register(
9594
}
9695

9796
final var retry = GenericRetry.fromConfiguration(configuration.getRetryConfiguration());
98-
final var targetNamespaces = configuration.getNamespaces().toArray(new String[] {});
97+
98+
// check if we only want to watch the current namespace
99+
var targetNamespaces = configuration.getNamespaces().toArray(new String[]{});
100+
if (configuration.watchCurrentNamespace()) {
101+
targetNamespaces = new String[]{
102+
configurationService.getClientConfiguration().getNamespace()
103+
};
104+
}
105+
99106
Class<R> resClass = configuration.getCustomResourceClass();
100107
String finalizer = configuration.getFinalizer();
101108

@@ -164,7 +171,7 @@ private CustomResourceEventSource createCustomResourceEventSource(
164171
CustomResourceEventSource customResourceEventSource =
165172
watchAllNamespaces
166173
? CustomResourceEventSource.customResourceEventSourceForAllNamespaces(
167-
customResourceCache, client, generationAware, finalizer)
174+
customResourceCache, client, generationAware, finalizer)
168175
: CustomResourceEventSource.customResourceEventSourceForTargetNamespaces(
169176
customResourceCache, client, targetNamespaces, generationAware, finalizer);
170177

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Set;
66

77
public interface ControllerConfiguration<R extends CustomResource> {
8+
String WATCH_CURRENT_NAMESPACE = "JOSDK_WATCH_CURRENT";
89
String getName();
910

1011
String getCRDName();
@@ -25,6 +26,11 @@ default boolean watchAllNamespaces() {
2526
return getNamespaces().isEmpty();
2627
}
2728

29+
default boolean watchCurrentNamespace() {
30+
final var namespaces = getNamespaces();
31+
return namespaces.size() == 1 && namespaces.contains(WATCH_CURRENT_NAMESPACE);
32+
}
33+
2834
default RetryConfiguration getRetryConfiguration() {
2935
return RetryConfiguration.DEFAULT;
3036
}

0 commit comments

Comments
 (0)