diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/MissingCRDException.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/MissingCRDException.java new file mode 100644 index 0000000000..caf310fb22 --- /dev/null +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/MissingCRDException.java @@ -0,0 +1,32 @@ +package io.javaoperatorsdk.operator; + +public class MissingCRDException extends OperatorException { + private final String crdName; + private final String specVersion; + + public String getCrdName() { + return crdName; + } + + public String getSpecVersion() { + return specVersion; + } + + public MissingCRDException(String crdName, String specVersion) { + super(); + this.crdName = crdName; + this.specVersion = specVersion; + } + + public MissingCRDException(String crdName, String specVersion, String message) { + super(message); + this.crdName = crdName; + this.specVersion = specVersion; + } + + public MissingCRDException(String crdName, String specVersion, String message, Throwable cause) { + super(message, cause); + this.crdName = crdName; + this.specVersion = specVersion; + } +} diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java index 9bcd72673d..56e11af4da 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java @@ -114,17 +114,22 @@ public void register( final String controllerName = configuration.getName(); // check that the custom resource is known by the cluster if configured that way - final CustomResourceDefinition crd; + final CustomResourceDefinition crd; // todo: check proper CRD spec version based on config if (configurationService.checkCRDAndValidateLocalModel()) { final var crdName = configuration.getCRDName(); + final var specVersion = "v1"; crd = k8sClient.apiextensions().v1().customResourceDefinitions().withName(crdName).get(); if (crd == null) { - throw new OperatorException( + throw new MissingCRDException( + crdName, + specVersion, "'" + crdName - + "' CRD was not found on the cluster, controller " + + "' " + + specVersion + + " CRD was not found on the cluster, controller '" + controllerName - + " cannot be registered"); + + "' cannot be registered"); } // Apply validations that are not handled by fabric8 diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java index a3ea01461b..7d3b733338 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/DefaultEventSourceManager.java @@ -4,6 +4,7 @@ import io.fabric8.kubernetes.client.CustomResource; import io.fabric8.kubernetes.client.dsl.MixedOperation; import io.fabric8.kubernetes.client.dsl.Resource; +import io.javaoperatorsdk.operator.OperatorException; import io.javaoperatorsdk.operator.api.ResourceController; import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; import io.javaoperatorsdk.operator.processing.CustomResourceCache; @@ -69,7 +70,8 @@ public void close() { } @Override - public final void registerEventSource(String name, EventSource eventSource) { + public final void registerEventSource(String name, EventSource eventSource) + throws OperatorException { Objects.requireNonNull(eventSource, "EventSource must not be null"); try { @@ -81,6 +83,12 @@ public final void registerEventSource(String name, EventSource eventSource) { eventSources.put(name, eventSource); eventSource.setEventHandler(defaultEventHandler); eventSource.start(); + } catch (Throwable e) { + if (e instanceof IllegalStateException) { + // leave untouched + throw e; + } + throw new OperatorException("Couldn't register event source named '" + name + "'", e); } finally { lock.unlock(); } diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java index 0aadc5247a..9879b51013 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java @@ -1,5 +1,6 @@ package io.javaoperatorsdk.operator.processing.event; +import io.javaoperatorsdk.operator.OperatorException; import java.io.Closeable; import java.util.Map; import java.util.Optional; @@ -13,8 +14,10 @@ public interface EventSourceManager extends Closeable { * @param eventSource the {@link EventSource} to register * @throws IllegalStateException if an {@link EventSource} with the same name is already * registered. + * @throws OperatorException if an error occurred during the registration process */ - void registerEventSource(String name, EventSource eventSource); + void registerEventSource(String name, EventSource eventSource) + throws IllegalStateException, OperatorException; /** * Remove the {@link EventSource} identified by the given name from the event