Skip to content

Commit 1ef4b67

Browse files
committed
refactor: move missing NS checking logic to Operator
1 parent 163643f commit 1ef4b67

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.IOException;
1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.Objects;
1516
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
1718

@@ -137,21 +138,39 @@ public <R extends CustomResource> void register(
137138
controller.init(eventSourceManager);
138139
closeables.add(eventSourceManager);
139140

140-
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
141-
if (configuration.isCurrentNamespaceMissing() && configuration.watchCurrentNamespace()) {
141+
if (failOnMissingCurrentNS(configuration)) {
142142
throw new OperatorException(
143143
"Controller '"
144144
+ controllerName
145-
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration. ");
145+
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
146146
}
147147

148148
final var watchedNS =
149-
configuration.watchAllNamespaces() ? "[all namespaces]" : effectiveNamespaces;
149+
configuration.watchAllNamespaces()
150+
? "[all namespaces]"
151+
: configuration.getEffectiveNamespaces();
150152
log.info(
151153
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
152154
controllerName,
153155
resClass,
154156
watchedNS);
155157
}
156158
}
159+
160+
/**
161+
* Determines whether we should fail because the current namespace is request as target namespace
162+
* but is missing
163+
*
164+
* @return {@code true} if the current namespace is requested but is missing, {@code false}
165+
* otherwise
166+
*/
167+
private static <R extends CustomResource> boolean failOnMissingCurrentNS(
168+
ControllerConfiguration<R> configuration) {
169+
if (configuration.watchCurrentNamespace()) {
170+
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
171+
return effectiveNamespaces.size() == 1
172+
&& effectiveNamespaces.stream().allMatch(Objects::isNull);
173+
}
174+
return false;
175+
}
157176
}

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

-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.fabric8.kubernetes.client.CustomResource;
44
import io.javaoperatorsdk.operator.api.Controller;
55
import java.util.Collections;
6-
import java.util.Objects;
76
import java.util.Set;
87

98
public interface ControllerConfiguration<R extends CustomResource> {
@@ -62,12 +61,6 @@ default Set<String> getEffectiveNamespaces() {
6261
return targetNamespaces;
6362
}
6463

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

0 commit comments

Comments
 (0)