22
22
import io .javaoperatorsdk .operator .api .reconciler .Reconciler ;
23
23
import io .javaoperatorsdk .operator .api .reconciler .dependent .Dependent ;
24
24
import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
25
- import io .javaoperatorsdk .operator .api .reconciler .dependent .VoidCondition ;
26
25
import io .javaoperatorsdk .operator .processing .dependent .kubernetes .KubernetesDependent ;
27
26
import io .javaoperatorsdk .operator .processing .dependent .kubernetes .KubernetesDependentResource ;
28
27
import io .javaoperatorsdk .operator .processing .dependent .kubernetes .KubernetesDependentResourceConfig ;
34
33
import io .javaoperatorsdk .operator .processing .event .source .filter .OnAddFilter ;
35
34
import io .javaoperatorsdk .operator .processing .event .source .filter .OnDeleteFilter ;
36
35
import io .javaoperatorsdk .operator .processing .event .source .filter .OnUpdateFilter ;
37
- import io .javaoperatorsdk .operator .processing .event .source .filter .VoidGenericFilter ;
38
- import io .javaoperatorsdk .operator .processing .event .source .filter .VoidOnAddFilter ;
39
- import io .javaoperatorsdk .operator .processing .event .source .filter .VoidOnDeleteFilter ;
40
- import io .javaoperatorsdk .operator .processing .event .source .filter .VoidOnUpdateFilter ;
41
36
import io .javaoperatorsdk .operator .processing .retry .Retry ;
42
37
43
38
import static io .javaoperatorsdk .operator .api .reconciler .Constants .DEFAULT_NAMESPACES_SET ;
46
41
public class AnnotationControllerConfiguration <P extends HasMetadata >
47
42
implements io .javaoperatorsdk .operator .api .config .ControllerConfiguration <P > {
48
43
44
+ private static final String CONTROLLER_CONFIG_ANNOTATION =
45
+ ControllerConfiguration .class .getSimpleName ();
46
+ private static final String KUBE_DEPENDENT_NAME = KubernetesDependent .class .getSimpleName ();
47
+
49
48
protected final Reconciler <P > reconciler ;
50
49
private final ControllerConfiguration annotation ;
51
50
private List <DependentResourceSpec > specs ;
@@ -153,18 +152,19 @@ public Optional<Duration> maxReconciliationInterval() {
153
152
@ Override
154
153
public RateLimiter getRateLimiter () {
155
154
final Class <? extends RateLimiter > rateLimiterClass = annotation .rateLimiter ();
156
- return instantiateAndConfigureIfNeeded (rateLimiterClass , RateLimiter .class );
155
+ return instantiateAndConfigureIfNeeded (rateLimiterClass , RateLimiter .class ,
156
+ CONTROLLER_CONFIG_ANNOTATION );
157
157
}
158
158
159
159
@ Override
160
160
public Retry getRetry () {
161
161
final Class <? extends Retry > retryClass = annotation .retry ();
162
- return instantiateAndConfigureIfNeeded (retryClass , Retry .class );
162
+ return instantiateAndConfigureIfNeeded (retryClass , Retry .class , CONTROLLER_CONFIG_ANNOTATION );
163
163
}
164
164
165
165
@ SuppressWarnings ("unchecked" )
166
- private <T > T instantiateAndConfigureIfNeeded (Class <? extends T > targetClass ,
167
- Class <T > expectedType ) {
166
+ protected <T > T instantiateAndConfigureIfNeeded (Class <? extends T > targetClass ,
167
+ Class <T > expectedType , String context ) {
168
168
try {
169
169
final Constructor <? extends T > constructor = targetClass .getDeclaredConstructor ();
170
170
constructor .setAccessible (true );
@@ -184,57 +184,39 @@ private <T> T instantiateAndConfigureIfNeeded(Class<? extends T> targetClass,
184
184
| NoSuchMethodException e ) {
185
185
throw new OperatorException ("Couldn't instantiate " + expectedType .getSimpleName () + " '"
186
186
+ targetClass .getName () + "' for '" + getName ()
187
- + "' reconciler. You need to provide an accessible no-arg constructor." , e );
187
+ + "' reconciler in " + context
188
+ + ". You need to provide an accessible no-arg constructor." , e );
188
189
}
189
190
}
190
191
191
192
@ Override
192
193
@ SuppressWarnings ("unchecked" )
193
194
public Optional <OnAddFilter <P >> onAddFilter () {
194
- return (Optional <OnAddFilter <P >>) createFilter (annotation .onAddFilter (), FilterType .onAdd ,
195
- annotation .getClass ().getSimpleName ());
196
- }
197
-
198
- private enum FilterType {
199
- onAdd (VoidOnAddFilter .class ), onUpdate (VoidOnUpdateFilter .class ), onDelete (
200
- VoidOnDeleteFilter .class ), generic (VoidGenericFilter .class );
201
-
202
- final Class <?> defaultValue ;
203
-
204
- FilterType (Class <?> defaultValue ) {
205
- this .defaultValue = defaultValue ;
206
- }
195
+ return (Optional <OnAddFilter <P >>) createFilter (annotation .onAddFilter (), OnAddFilter .class ,
196
+ CONTROLLER_CONFIG_ANNOTATION );
207
197
}
208
198
209
- private <T > Optional <T > createFilter (Class <T > filter , FilterType filterType , String origin ) {
210
- if (filterType .defaultValue .equals (filter )) {
199
+ protected <T > Optional <? extends T > createFilter (Class <? extends T > filter , Class <T > defaultValue ,
200
+ String origin ) {
201
+ if (defaultValue .equals (filter )) {
211
202
return Optional .empty ();
212
203
} else {
213
- try {
214
- var instance = (T ) filter .getDeclaredConstructor ().newInstance ();
215
- return Optional .of (instance );
216
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException
217
- | NoSuchMethodException e ) {
218
- throw new OperatorException (
219
- "Couldn't create " + filterType + " filter from " + filter .getName () + " class in "
220
- + origin + " for reconciler " + getName (),
221
- e );
222
- }
204
+ return Optional .of (instantiateAndConfigureIfNeeded (filter , defaultValue , origin ));
223
205
}
224
206
}
225
207
226
208
@ SuppressWarnings ("unchecked" )
227
209
@ Override
228
210
public Optional <OnUpdateFilter <P >> onUpdateFilter () {
229
211
return (Optional <OnUpdateFilter <P >>) createFilter (annotation .onUpdateFilter (),
230
- FilterType . onUpdate , annotation . getClass (). getSimpleName () );
212
+ OnUpdateFilter . class , CONTROLLER_CONFIG_ANNOTATION );
231
213
}
232
214
233
215
@ SuppressWarnings ("unchecked" )
234
216
@ Override
235
217
public Optional <GenericFilter <P >> genericFilter () {
236
218
return (Optional <GenericFilter <P >>) createFilter (annotation .genericFilter (),
237
- FilterType . generic , annotation . getClass (). getSimpleName () );
219
+ GenericFilter . class , CONTROLLER_CONFIG_ANNOTATION );
238
220
}
239
221
240
222
@ SuppressWarnings ({"rawtypes" , "unchecked" })
@@ -260,13 +242,14 @@ public List<DependentResourceSpec> getDependentResources() {
260
242
var spec = specsMap .get (name );
261
243
if (spec != null ) {
262
244
throw new IllegalArgumentException (
263
- "A DependentResource named: " + name + " already exists: " + spec );
245
+ "A DependentResource named ' " + name + "' already exists: " + spec );
264
246
}
247
+ final var context = "DependentResource of type '" + dependentType .getName () + "'" ;
265
248
spec = new DependentResourceSpec (dependentType , config , name ,
266
249
Set .of (dependent .dependsOn ()),
267
- instantiateConditionIfNotVoid (dependent .readyPostcondition ()),
268
- instantiateConditionIfNotVoid (dependent .reconcilePrecondition ()),
269
- instantiateConditionIfNotVoid (dependent .deletePostcondition ()));
250
+ instantiateConditionIfNotDefault (dependent .readyPostcondition (), context ),
251
+ instantiateConditionIfNotDefault (dependent .reconcilePrecondition (), context ),
252
+ instantiateConditionIfNotDefault (dependent .deletePostcondition (), context ));
270
253
specsMap .put (name , spec );
271
254
}
272
255
@@ -275,9 +258,10 @@ public List<DependentResourceSpec> getDependentResources() {
275
258
return specs ;
276
259
}
277
260
278
- private Condition <?, ?> instantiateConditionIfNotVoid (Class <? extends Condition > condition ) {
279
- if (condition != VoidCondition .class ) {
280
- return instantiateAndConfigureIfNeeded (condition , Condition .class );
261
+ protected Condition <?, ?> instantiateConditionIfNotDefault (Class <? extends Condition > condition ,
262
+ String context ) {
263
+ if (condition != Condition .class ) {
264
+ return instantiateAndConfigureIfNeeded (condition , Condition .class , context );
281
265
}
282
266
return null ;
283
267
}
@@ -313,17 +297,19 @@ private Object createKubernetesResourceConfig(Class<? extends DependentResource>
313
297
final var fromAnnotation = kubeDependent .labelSelector ();
314
298
labelSelector = Constants .NO_VALUE_SET .equals (fromAnnotation ) ? null : fromAnnotation ;
315
299
316
- final var kubeDependentName = KubernetesDependent .class .getSimpleName ();
317
- onAddFilter = createFilter (kubeDependent .onAddFilter (), FilterType .onAdd , kubeDependentName )
300
+
301
+ final var context =
302
+ KUBE_DEPENDENT_NAME + " annotation on " + dependentType .getName () + " DependentResource" ;
303
+ onAddFilter = createFilter (kubeDependent .onAddFilter (), OnAddFilter .class , context )
318
304
.orElse (null );
319
305
onUpdateFilter =
320
- createFilter (kubeDependent .onUpdateFilter (), FilterType . onUpdate , kubeDependentName )
306
+ createFilter (kubeDependent .onUpdateFilter (), OnUpdateFilter . class , context )
321
307
.orElse (null );
322
308
onDeleteFilter =
323
- createFilter (kubeDependent .onDeleteFilter (), FilterType . onDelete , kubeDependentName )
309
+ createFilter (kubeDependent .onDeleteFilter (), OnDeleteFilter . class , context )
324
310
.orElse (null );
325
311
genericFilter =
326
- createFilter (kubeDependent .genericFilter (), FilterType . generic , kubeDependentName )
312
+ createFilter (kubeDependent .genericFilter (), GenericFilter . class , context )
327
313
.orElse (null );
328
314
}
329
315
0 commit comments