Skip to content

Commit e0f7b10

Browse files
authored
fix: fail explicitly if current namespace is requested but not available (#900)
Fixes #897
1 parent 376bc68 commit e0f7b10

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.javaoperatorsdk.operator.processing;
22

33
import java.util.List;
4-
import java.util.Objects;
54

65
import io.fabric8.kubernetes.api.model.HasMetadata;
76
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
@@ -28,6 +27,7 @@
2827
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
2928
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
3029

30+
@SuppressWarnings({"unchecked"})
3131
public class Controller<R extends HasMetadata> implements Reconciler<R>,
3232
LifecycleAware, EventSourceInitializer<R> {
3333
private final Reconciler<R> reconciler;
@@ -165,6 +165,10 @@ public void start() throws OperatorException {
165165
final String controllerName = configuration.getName();
166166
final var crdName = configuration.getResourceTypeName();
167167
final var specVersion = "v1";
168+
169+
// fail early if we're missing the current namespace information
170+
failOnMissingCurrentNS();
171+
168172
try {
169173
// check that the custom resource is known by the cluster if configured that way
170174
final CustomResourceDefinition crd; // todo: check proper CRD spec version based on config
@@ -188,12 +192,7 @@ public void start() throws OperatorException {
188192
configurationService(), kubernetesClient))
189193
.forEach(eventSourceManager::registerEventSource);
190194
}
191-
if (failOnMissingCurrentNS()) {
192-
throw new OperatorException(
193-
"Controller '"
194-
+ controllerName
195-
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
196-
}
195+
197196
eventSourceManager.start();
198197
} catch (MissingCRDException e) {
199198
throwMissingCRDException(crdName, specVersion, controllerName);
@@ -231,19 +230,18 @@ private void throwMissingCRDException(String crdName, String specVersion, String
231230
}
232231

233232
/**
234-
* Determines whether we should fail because the current namespace is request as target namespace
235-
* but is missing
236-
*
237-
* @return {@code true} if the current namespace is requested but is missing, {@code false}
238-
* otherwise
233+
* Throws an {@link OperatorException} if the controller is configured to watch the current
234+
* namespace but it's absent from the configuration.
239235
*/
240-
private boolean failOnMissingCurrentNS() {
241-
if (configuration.watchCurrentNamespace()) {
242-
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
243-
return effectiveNamespaces.size() == 1
244-
&& effectiveNamespaces.stream().allMatch(Objects::isNull);
236+
private void failOnMissingCurrentNS() {
237+
try {
238+
configuration.getEffectiveNamespaces();
239+
} catch (OperatorException e) {
240+
throw new OperatorException(
241+
"Controller '"
242+
+ configuration.getName()
243+
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
245244
}
246-
return false;
247245
}
248246

249247
public EventSourceManager<R> getEventSourceManager() {

operator-framework/src/main/java/io/javaoperatorsdk/operator/config/runtime/AnnotationConfiguration.java

-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ public ResourceEventFilter<R> getEventFilter() {
110110
: ResourceEventFilters.passthrough();
111111
}
112112

113-
114-
115113
@Override
116114
public Optional<Duration> reconciliationMaxInterval() {
117115
if (annotation.reconciliationMaxInterval() != null) {

0 commit comments

Comments
 (0)