Skip to content

Commit 14705c5

Browse files
committed
feat: throw new MissingCRDException when CRD validation fails
This is useful for processes that observe the controller registration (the quarkus extension, for example) to know more specifically what happened instead of using a generic OperatorException.
1 parent 1ef4b67 commit 14705c5

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.javaoperatorsdk.operator;
2+
3+
public class MissingCRDException extends OperatorException {
4+
private final String crdName;
5+
private final String specVersion;
6+
7+
public String getCrdName() {
8+
return crdName;
9+
}
10+
11+
public String getSpecVersion() {
12+
return specVersion;
13+
}
14+
15+
public MissingCRDException(String crdName, String specVersion) {
16+
super();
17+
this.crdName = crdName;
18+
this.specVersion = specVersion;
19+
}
20+
21+
public MissingCRDException(String crdName, String specVersion, String message) {
22+
super(message);
23+
this.crdName = crdName;
24+
this.specVersion = specVersion;
25+
}
26+
27+
public MissingCRDException(String crdName, String specVersion, String message, Throwable cause) {
28+
super(message, cause);
29+
this.crdName = crdName;
30+
this.specVersion = specVersion;
31+
}
32+
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,22 @@ public <R extends CustomResource> void register(
115115
final String controllerName = configuration.getName();
116116

117117
// check that the custom resource is known by the cluster if configured that way
118-
final CustomResourceDefinition crd;
118+
final CustomResourceDefinition crd; // todo: check proper CRD spec version based on config
119119
if (configurationService.checkCRDAndValidateLocalModel()) {
120120
final var crdName = configuration.getCRDName();
121+
final var specVersion = "v1";
121122
crd = k8sClient.apiextensions().v1().customResourceDefinitions().withName(crdName).get();
122123
if (crd == null) {
123-
throw new OperatorException(
124+
throw new MissingCRDException(
125+
crdName,
126+
specVersion,
124127
"'"
125128
+ crdName
126-
+ "' CRD was not found on the cluster, controller "
129+
+ "' "
130+
+ specVersion
131+
+ " CRD was not found on the cluster, controller '"
127132
+ controllerName
128-
+ " cannot be registered");
133+
+ "' cannot be registered");
129134
}
130135

131136
// Apply validations that are not handled by fabric8

0 commit comments

Comments
 (0)