Skip to content

feat: fail if current namespaces is required but cannot be resolved #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -137,13 +138,39 @@ public <R extends CustomResource> void register(
controller.init(eventSourceManager);
closeables.add(eventSourceManager);

if (failOnMissingCurrentNS(configuration)) {
throw new OperatorException(
"Controller '"
+ controllerName
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
}

final var watchedNS =
configuration.watchAllNamespaces()
? "[all namespaces]"
: configuration.getEffectiveNamespaces();
log.info(
"Registered Controller: '{}' for CRD: '{}' for namespace(s): {}",
controllerName,
resClass,
configuration.watchAllNamespaces()
? "[all namespaces]"
: configuration.getEffectiveNamespaces());
watchedNS);
}
}

/**
* Determines whether we should fail because the current namespace is request as target namespace
* but is missing
*
* @return {@code true} if the current namespace is requested but is missing, {@code false}
* otherwise
*/
private static <R extends CustomResource> boolean failOnMissingCurrentNS(
ControllerConfiguration<R> configuration) {
if (configuration.watchCurrentNamespace()) {
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
return effectiveNamespaces.size() == 1
&& effectiveNamespaces.stream().allMatch(Objects::isNull);
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ static boolean currentNamespaceWatched(Set<String> namespaces) {
&& namespaces.contains(Controller.WATCH_CURRENT_NAMESPACE);
}

/**
* Computes the effective namespaces based on the set specified by the user, in particular
* retrieves the current namespace from the client when the user specified that they wanted to
* watch the current namespace only.
*
* @return a Set of namespace names the associated controller will watch
*/
default Set<String> getEffectiveNamespaces() {
var targetNamespaces = getNamespaces();
if (watchCurrentNamespace()) {
Expand Down