Skip to content

fix: only throw MissingCRDException if we get a 404 on the target CRD #558

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
Sep 23, 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 @@ -24,7 +24,7 @@
/**
* Dispatches events to the Controller and handles Finalizers for a single type of Custom Resource.
*/
class EventDispatcher<R extends CustomResource<?, ?>> {
public class EventDispatcher<R extends CustomResource<?, ?>> {

private static final Logger log = LoggerFactory.getLogger(EventDispatcher.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.slf4j.LoggerFactory;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.javaoperatorsdk.operator.MissingCRDException;
import io.javaoperatorsdk.operator.OperatorException;
import io.javaoperatorsdk.operator.processing.ConfiguredController;
Expand Down Expand Up @@ -85,16 +84,10 @@ public final void registerEventSource(String name, EventSource eventSource)
eventSource.setEventHandler(defaultEventHandler);
eventSource.start();
} catch (Throwable e) {
if (e instanceof IllegalStateException) {
if (e instanceof IllegalStateException || e instanceof MissingCRDException) {
// leave untouched
throw e;
}
if (e instanceof KubernetesClientException) {
KubernetesClientException ke = (KubernetesClientException) e;
if (404 == ke.getCode()) {
throw new MissingCRDException(null, null);
}
}
throw new OperatorException("Couldn't register event source named '" + name + "'", e);
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

import io.fabric8.kubernetes.api.model.ListOptions;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.utils.Utils;
import io.javaoperatorsdk.operator.MissingCRDException;
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.processing.ConfiguredController;
import io.javaoperatorsdk.operator.processing.CustomResourceCache;
Expand Down Expand Up @@ -54,17 +56,31 @@ public void start() {
options.setLabelSelector(labelSelector);
}

if (ControllerConfiguration.allNamespacesWatched(targetNamespaces)) {
var w = client.inAnyNamespace().watch(options, this);
watches.add(w);
log.debug("Registered {} -> {} for any namespace", controller, w);
} else {
targetNamespaces.forEach(
ns -> {
var w = client.inNamespace(ns).watch(options, this);
watches.add(w);
log.debug("Registered {} -> {} for namespace: {}", controller, w, ns);
});
try {
if (ControllerConfiguration.allNamespacesWatched(targetNamespaces)) {
var w = client.inAnyNamespace().watch(options, this);
watches.add(w);
log.debug("Registered {} -> {} for any namespace", controller, w);
} else {
targetNamespaces.forEach(
ns -> {
var w = client.inNamespace(ns).watch(options, this);
watches.add(w);
log.debug("Registered {} -> {} for namespace: {}", controller, w, ns);
});
}
} catch (Exception e) {
if (e instanceof KubernetesClientException) {
KubernetesClientException ke = (KubernetesClientException) e;
if (404 == ke.getCode()) {
// only throw MissingCRDException if the 404 error occurs on the target CRD
final var targetCRDName = controller.getConfiguration().getCRDName();
if (targetCRDName.equals(ke.getFullResourceName())) {
throw new MissingCRDException(targetCRDName, null);
}
}
}
throw e;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<maven.compiler.target>${java.version}</maven.compiler.target>

<junit.version>5.8.1</junit.version>
<fabric8-client.version>5.7.2</fabric8-client.version>
<fabric8-client.version>5.8.0</fabric8-client.version>
<slf4j.version>1.7.32</slf4j.version>
<log4j.version>2.14.1</log4j.version>
<mokito.version>3.12.4</mokito.version>
Expand Down