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