11package io .javaoperatorsdk .operator .processing ;
22
33import java .util .List ;
4- import java .util .Objects ;
54
65import io .fabric8 .kubernetes .api .model .HasMetadata ;
76import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
2827import io .javaoperatorsdk .operator .processing .event .EventSourceManager ;
2928import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
3029
30+ @ SuppressWarnings ({"unchecked" })
3131public 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 () {
0 commit comments