File tree Expand file tree Collapse file tree 2 files changed +25
-3
lines changed
operator-framework-core/src/main/java/io/javaoperatorsdk/operator Expand file tree Collapse file tree 2 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -137,13 +137,21 @@ public <R extends CustomResource> void register(
137137 controller .init (eventSourceManager );
138138 closeables .add (eventSourceManager );
139139
140+ final var effectiveNamespaces = configuration .getEffectiveNamespaces ();
141+ if (configuration .isCurrentNamespaceMissing () && configuration .watchCurrentNamespace ()) {
142+ throw new OperatorException (
143+ "Controller '"
144+ + controllerName
145+ + "' is configured to watch the current namespace but it couldn't be inferred from the current configuration. " );
146+ }
147+
148+ final var watchedNS =
149+ configuration .watchAllNamespaces () ? "[all namespaces]" : effectiveNamespaces ;
140150 log .info (
141151 "Registered Controller: '{}' for CRD: '{}' for namespace(s): {}" ,
142152 controllerName ,
143153 resClass ,
144- configuration .watchAllNamespaces ()
145- ? "[all namespaces]"
146- : configuration .getEffectiveNamespaces ());
154+ watchedNS );
147155 }
148156 }
149157}
Original file line number Diff line number Diff line change 33import io .fabric8 .kubernetes .client .CustomResource ;
44import io .javaoperatorsdk .operator .api .Controller ;
55import java .util .Collections ;
6+ import java .util .Objects ;
67import java .util .Set ;
78
89public 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 }
You can’t perform that action at this time.
0 commit comments