Skip to content

Commit 8dd37db

Browse files
committed
feat: fail if current namespaces is required but cannot be resolved
Fixes #433
1 parent 940b0cd commit 8dd37db

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,21 @@ public <R extends CustomResource> void register(
137137
controller.init(eventSourceManager);
138138
closeables.add(eventSourceManager);
139139

140+
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
141+
if (configuration.isCurrentNamespaceMissing() && configuration.watchCurrentNamespace()) {
142+
throw new OperatorException(
143+
"Controller '"
144+
+ controllerName
145+
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration. ");
146+
}
147+
148+
final var watchedNS =
149+
configuration.watchAllNamespaces() ? "[all namespaces]" : effectiveNamespaces;
140150
log.info(
141151
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
142152
controllerName,
143153
resClass,
144-
configuration.watchAllNamespaces()
145-
? "[all namespaces]"
146-
: configuration.getEffectiveNamespaces());
154+
watchedNS);
147155
}
148156
}
149157
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.fabric8.kubernetes.client.CustomResource;
44
import io.javaoperatorsdk.operator.api.Controller;
55
import java.util.Collections;
6+
import java.util.Objects;
67
import java.util.Set;
78

89
public interface ControllerConfiguration<R extends CustomResource> {
@@ -41,6 +42,13 @@ static boolean currentNamespaceWatched(Set<String> namespaces) {
4142
&& namespaces.contains(Controller.WATCH_CURRENT_NAMESPACE);
4243
}
4344

45+
/**
46+
* Computes the effective namespaces based on the set specified by the user, in particular
47+
* retrieves the current namespace from the client when the user specified that they wanted to
48+
* watch the current namespace only.
49+
*
50+
* @return a Set of namespace names the associated controller will watch
51+
*/
4452
default Set<String> getEffectiveNamespaces() {
4553
var targetNamespaces = getNamespaces();
4654
if (watchCurrentNamespace()) {
@@ -54,6 +62,12 @@ default Set<String> getEffectiveNamespaces() {
5462
return targetNamespaces;
5563
}
5664

65+
default boolean isCurrentNamespaceMissing() {
66+
final var effectiveNamespaces = getEffectiveNamespaces();
67+
return effectiveNamespaces.size() == 1
68+
&& effectiveNamespaces.stream().allMatch(Objects::isNull);
69+
}
70+
5771
default RetryConfiguration getRetryConfiguration() {
5872
return RetryConfiguration.DEFAULT;
5973
}

0 commit comments

Comments
 (0)