1
1
package io .javaoperatorsdk .operator .processing ;
2
2
3
3
import java .util .List ;
4
- import java .util .Objects ;
5
4
6
5
import io .fabric8 .kubernetes .api .model .HasMetadata ;
7
6
import io .fabric8 .kubernetes .api .model .KubernetesResourceList ;
28
27
import io .javaoperatorsdk .operator .processing .event .EventSourceManager ;
29
28
import io .javaoperatorsdk .operator .processing .event .source .EventSource ;
30
29
30
+ @ SuppressWarnings ({"unchecked" })
31
31
public class Controller <R extends HasMetadata > implements Reconciler <R >,
32
32
LifecycleAware , EventSourceInitializer <R > {
33
33
private final Reconciler <R > reconciler ;
@@ -165,6 +165,10 @@ public void start() throws OperatorException {
165
165
final String controllerName = configuration .getName ();
166
166
final var crdName = configuration .getResourceTypeName ();
167
167
final var specVersion = "v1" ;
168
+
169
+ // fail early if we're missing the current namespace information
170
+ failOnMissingCurrentNS ();
171
+
168
172
try {
169
173
// check that the custom resource is known by the cluster if configured that way
170
174
final CustomResourceDefinition crd ; // todo: check proper CRD spec version based on config
@@ -188,12 +192,7 @@ public void start() throws OperatorException {
188
192
configurationService (), kubernetesClient ))
189
193
.forEach (eventSourceManager ::registerEventSource );
190
194
}
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
+
197
196
eventSourceManager .start ();
198
197
} catch (MissingCRDException e ) {
199
198
throwMissingCRDException (crdName , specVersion , controllerName );
@@ -231,19 +230,18 @@ private void throwMissingCRDException(String crdName, String specVersion, String
231
230
}
232
231
233
232
/**
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.
239
235
*/
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." );
245
244
}
246
- return false ;
247
245
}
248
246
249
247
public EventSourceManager <R > getEventSourceManager () {
0 commit comments