Skip to content

fix: fail explicitly if current namespace is requested but not available #900

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 1 commit into from
Feb 2, 2022
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
@@ -1,7 +1,6 @@
package io.javaoperatorsdk.operator.processing;

import java.util.List;
import java.util.Objects;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
Expand All @@ -28,6 +27,7 @@
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;

@SuppressWarnings({"unchecked"})
public class Controller<R extends HasMetadata> implements Reconciler<R>,
LifecycleAware, EventSourceInitializer<R> {
private final Reconciler<R> reconciler;
Expand Down Expand Up @@ -165,6 +165,10 @@ public void start() throws OperatorException {
final String controllerName = configuration.getName();
final var crdName = configuration.getResourceTypeName();
final var specVersion = "v1";

// fail early if we're missing the current namespace information
failOnMissingCurrentNS();

try {
// check that the custom resource is known by the cluster if configured that way
final CustomResourceDefinition crd; // todo: check proper CRD spec version based on config
Expand All @@ -188,12 +192,7 @@ public void start() throws OperatorException {
configurationService(), kubernetesClient))
.forEach(eventSourceManager::registerEventSource);
}
if (failOnMissingCurrentNS()) {
throw new OperatorException(
"Controller '"
+ controllerName
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
}

eventSourceManager.start();
} catch (MissingCRDException e) {
throwMissingCRDException(crdName, specVersion, controllerName);
Expand Down Expand Up @@ -231,19 +230,18 @@ private void throwMissingCRDException(String crdName, String specVersion, String
}

/**
* 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
* Throws an {@link OperatorException} if the controller is configured to watch the current
* namespace but it's absent from the configuration.
*/
private boolean failOnMissingCurrentNS() {
if (configuration.watchCurrentNamespace()) {
final var effectiveNamespaces = configuration.getEffectiveNamespaces();
return effectiveNamespaces.size() == 1
&& effectiveNamespaces.stream().allMatch(Objects::isNull);
private void failOnMissingCurrentNS() {
try {
configuration.getEffectiveNamespaces();
} catch (OperatorException e) {
throw new OperatorException(
"Controller '"
+ configuration.getName()
+ "' is configured to watch the current namespace but it couldn't be inferred from the current configuration.");
}
return false;
}

public EventSourceManager<R> getEventSourceManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,6 @@ public ResourceEventFilter<R> getEventFilter() {
: ResourceEventFilters.passthrough();
}



@Override
public Optional<Duration> reconciliationMaxInterval() {
if (annotation.reconciliationMaxInterval() != null) {
Expand Down