Skip to content

Commit 171640e

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

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.io.IOException;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Set;
16+
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
1719

@@ -137,13 +139,20 @@ public <R extends CustomResource> void register(
137139
controller.init(eventSourceManager);
138140
closeables.add(eventSourceManager);
139141

142+
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
143+
if (configuration.isCurrentNamespaceMissing() && configuration.watchCurrentNamespace()) {
144+
throw new OperatorException("Controller '" + controllerName + "' is configured to watch the current namespace but it couldn't be inferred from the current configuration. ");
145+
}
146+
147+
final var watchedNS =
148+
configuration.watchAllNamespaces()
149+
? "[all namespaces]"
150+
: effectiveNamespaces;
140151
log.info(
141152
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
142153
controllerName,
143154
resClass,
144-
configuration.watchAllNamespaces()
145-
? "[all namespaces]"
146-
: configuration.getEffectiveNamespaces());
155+
watchedNS);
147156
}
148157
}
149158
}

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)