Skip to content

Commit a371629

Browse files
committed
fix: annotation controller not require annotation
1 parent f1d9310 commit a371629

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

Diff for: operator-framework/src/main/java/io/javaoperatorsdk/operator/config/runtime/AnnotationConfiguration.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javaoperatorsdk.operator.config.runtime;
22

33
import java.util.Set;
4+
import java.util.function.Function;
45

56
import io.fabric8.kubernetes.client.CustomResource;
67
import io.javaoperatorsdk.operator.ControllerUtils;
@@ -21,10 +22,6 @@ public class AnnotationConfiguration<R extends CustomResource>
2122
public AnnotationConfiguration(ResourceController<R> controller) {
2223
this.controller = controller;
2324
this.annotation = controller.getClass().getAnnotation(Controller.class);
24-
if (this.annotation == null) {
25-
throw new IllegalStateException(
26-
"No @Controller annotation present on controller: " + controller.getClass().getName());
27-
}
2825
}
2926

3027
@Override
@@ -34,7 +31,7 @@ public String getName() {
3431

3532
@Override
3633
public String getFinalizer() {
37-
if (annotation.finalizerName().isBlank()) {
34+
if (annotation == null || annotation.finalizerName().isBlank()) {
3835
return ControllerUtils.getDefaultFinalizerName(getCRDName());
3936
} else {
4037
return annotation.finalizerName();
@@ -43,7 +40,7 @@ public String getFinalizer() {
4340

4441
@Override
4542
public boolean isGenerationAware() {
46-
return annotation.generationAwareEventProcessing();
43+
return valueOrDefault(annotation, Controller::generationAwareEventProcessing, true);
4744
}
4845

4946
@Override
@@ -53,12 +50,12 @@ public Class<R> getCustomResourceClass() {
5350

5451
@Override
5552
public Set<String> getNamespaces() {
56-
return Set.of(annotation.namespaces());
53+
return Set.of(valueOrDefault(annotation, Controller::namespaces, new String[] {}));
5754
}
5855

5956
@Override
6057
public String getLabelSelector() {
61-
return annotation.labelSelector();
58+
return valueOrDefault(annotation, Controller::labelSelector, "");
6259
}
6360

6461
@Override
@@ -81,7 +78,9 @@ public String getAssociatedControllerClassName() {
8178
public CustomResourceEventFilter<R> getEventFilter() {
8279
CustomResourceEventFilter<R> answer = null;
8380

84-
var filterTypes = annotation.eventFilters();
81+
Class<CustomResourceEventFilter<R>>[] filterTypes =
82+
(Class<CustomResourceEventFilter<R>>[]) valueOrDefault(annotation, Controller::eventFilters,
83+
new Object[] {});
8584
if (filterTypes.length > 0) {
8685
for (var filterType : filterTypes) {
8786
try {
@@ -97,10 +96,18 @@ public CustomResourceEventFilter<R> getEventFilter() {
9796
}
9897
}
9998
}
100-
10199
return answer != null
102100
? answer
103101
: CustomResourceEventFilters.passthrough();
104102
}
103+
104+
public static <T> T valueOrDefault(Controller controller, Function<Controller, T> mapper,
105+
T defaultValue) {
106+
if (controller == null) {
107+
return defaultValue;
108+
} else {
109+
return mapper.apply(controller);
110+
}
111+
}
105112
}
106113

0 commit comments

Comments
 (0)