Skip to content

Make it easier to detect CRD validation errors #432

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
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,22 @@ public <R extends CustomResource> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 <code>name</code> from the event
Expand Down