|
12 | 12 | import java.io.IOException;
|
13 | 13 | import java.util.ArrayList;
|
14 | 14 | import java.util.List;
|
| 15 | +import java.util.Objects; |
15 | 16 | import org.slf4j.Logger;
|
16 | 17 | import org.slf4j.LoggerFactory;
|
17 | 18 |
|
@@ -137,21 +138,39 @@ public <R extends CustomResource> void register(
|
137 | 138 | controller.init(eventSourceManager);
|
138 | 139 | closeables.add(eventSourceManager);
|
139 | 140 |
|
140 |
| - final var effectiveNamespaces = configuration.getEffectiveNamespaces(); |
141 |
| - if (configuration.isCurrentNamespaceMissing() && configuration.watchCurrentNamespace()) { |
| 141 | + if (failOnMissingCurrentNS(configuration)) { |
142 | 142 | throw new OperatorException(
|
143 | 143 | "Controller '"
|
144 | 144 | + 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."); |
146 | 146 | }
|
147 | 147 |
|
148 | 148 | final var watchedNS =
|
149 |
| - configuration.watchAllNamespaces() ? "[all namespaces]" : effectiveNamespaces; |
| 149 | + configuration.watchAllNamespaces() |
| 150 | + ? "[all namespaces]" |
| 151 | + : configuration.getEffectiveNamespaces(); |
150 | 152 | log.info(
|
151 | 153 | "Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
|
152 | 154 | controllerName,
|
153 | 155 | resClass,
|
154 | 156 | watchedNS);
|
155 | 157 | }
|
156 | 158 | }
|
| 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 | + } |
157 | 176 | }
|
0 commit comments