1
1
package io .javaoperatorsdk .operator .config .runtime ;
2
2
3
3
import java .util .Set ;
4
+ import java .util .function .Function ;
4
5
5
6
import io .fabric8 .kubernetes .client .CustomResource ;
6
7
import io .javaoperatorsdk .operator .ControllerUtils ;
@@ -21,10 +22,6 @@ public class AnnotationConfiguration<R extends CustomResource>
21
22
public AnnotationConfiguration (ResourceController <R > controller ) {
22
23
this .controller = controller ;
23
24
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
- }
28
25
}
29
26
30
27
@ Override
@@ -34,7 +31,7 @@ public String getName() {
34
31
35
32
@ Override
36
33
public String getFinalizer () {
37
- if (annotation .finalizerName ().isBlank ()) {
34
+ if (annotation == null || annotation .finalizerName ().isBlank ()) {
38
35
return ControllerUtils .getDefaultFinalizerName (getCRDName ());
39
36
} else {
40
37
return annotation .finalizerName ();
@@ -43,7 +40,7 @@ public String getFinalizer() {
43
40
44
41
@ Override
45
42
public boolean isGenerationAware () {
46
- return annotation . generationAwareEventProcessing ( );
43
+ return valueOrDefault ( annotation , Controller :: generationAwareEventProcessing , true );
47
44
}
48
45
49
46
@ Override
@@ -53,12 +50,12 @@ public Class<R> getCustomResourceClass() {
53
50
54
51
@ Override
55
52
public Set <String > getNamespaces () {
56
- return Set .of (annotation . namespaces ( ));
53
+ return Set .of (valueOrDefault ( annotation , Controller :: namespaces , new String [] {} ));
57
54
}
58
55
59
56
@ Override
60
57
public String getLabelSelector () {
61
- return annotation . labelSelector ( );
58
+ return valueOrDefault ( annotation , Controller :: labelSelector , "" );
62
59
}
63
60
64
61
@ Override
@@ -81,7 +78,9 @@ public String getAssociatedControllerClassName() {
81
78
public CustomResourceEventFilter <R > getEventFilter () {
82
79
CustomResourceEventFilter <R > answer = null ;
83
80
84
- var filterTypes = annotation .eventFilters ();
81
+ Class <CustomResourceEventFilter <R >>[] filterTypes =
82
+ (Class <CustomResourceEventFilter <R >>[]) valueOrDefault (annotation , Controller ::eventFilters ,
83
+ new Object [] {});
85
84
if (filterTypes .length > 0 ) {
86
85
for (var filterType : filterTypes ) {
87
86
try {
@@ -97,10 +96,18 @@ public CustomResourceEventFilter<R> getEventFilter() {
97
96
}
98
97
}
99
98
}
100
-
101
99
return answer != null
102
100
? answer
103
101
: CustomResourceEventFilters .passthrough ();
104
102
}
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
+ }
105
112
}
106
113
0 commit comments