14
14
import io .fabric8 .kubernetes .api .model .HasMetadata ;
15
15
import io .fabric8 .kubernetes .client .KubernetesClient ;
16
16
import io .fabric8 .kubernetes .client .informers .cache .ItemStore ;
17
- import io .javaoperatorsdk .operator .OperatorException ;
18
17
import io .javaoperatorsdk .operator .ReconcilerUtils ;
19
18
import io .javaoperatorsdk .operator .api .config .Utils .Configurator ;
20
19
import io .javaoperatorsdk .operator .api .config .dependent .DependentResourceConfigurationResolver ;
33
32
import io .javaoperatorsdk .operator .processing .retry .Retry ;
34
33
35
34
import static io .javaoperatorsdk .operator .api .config .ControllerConfiguration .CONTROLLER_NAME_AS_FIELD_MANAGER ;
36
- import static io .javaoperatorsdk .operator .api .reconciler .Constants .DEFAULT_NAMESPACES_SET ;
37
35
38
36
public class BaseConfigurationService extends AbstractConfigurationService {
39
37
@@ -91,6 +89,7 @@ private static List<DependentResourceSpec> dependentResources(
91
89
Utils .instantiate (dependent .deletePostcondition (), Condition .class , context ),
92
90
Utils .instantiate (dependent .activationCondition (), Condition .class , context ),
93
91
eventSourceName );
92
+ specsMap .put (dependentName , spec );
94
93
95
94
// extract potential configuration
96
95
DependentResourceConfigurationResolver .configureSpecFromConfigured (spec ,
@@ -99,17 +98,24 @@ private static List<DependentResourceSpec> dependentResources(
99
98
100
99
specsMap .put (dependentName , spec );
101
100
}
101
+
102
102
return specsMap .values ().stream ().toList ();
103
103
}
104
104
105
- private static <T > T valueOrDefault (
105
+ @ SuppressWarnings ("unchecked" )
106
+ private static <T > T valueOrDefaultFromAnnotation (
106
107
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration controllerConfiguration ,
107
108
Function <io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration , T > mapper ,
108
- T defaultValue ) {
109
- if (controllerConfiguration == null ) {
110
- return defaultValue ;
111
- } else {
112
- return mapper .apply (controllerConfiguration );
109
+ String defaultMethodName ) {
110
+ try {
111
+ if (controllerConfiguration == null ) {
112
+ return (T ) io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration .class
113
+ .getDeclaredMethod (defaultMethodName ).getDefaultValue ();
114
+ } else {
115
+ return mapper .apply (controllerConfiguration );
116
+ }
117
+ } catch (NoSuchMethodException e ) {
118
+ throw new RuntimeException (e );
113
119
}
114
120
}
115
121
@@ -123,17 +129,18 @@ private static String getName(String name, Class<? extends DependentResource> de
123
129
124
130
@ SuppressWarnings ("unused" )
125
131
private static <T > Configurator <T > configuratorFor (Class <T > instanceType ,
126
- Reconciler <?> reconciler ) {
127
- return instance -> configureFromAnnotatedReconciler (instance , reconciler );
132
+ Class <? extends Reconciler <?>> reconcilerClass ) {
133
+ return instance -> configureFromAnnotatedReconciler (instance , reconcilerClass );
128
134
}
129
135
130
136
@ SuppressWarnings ({"unchecked" , "rawtypes" })
131
- private static void configureFromAnnotatedReconciler (Object instance , Reconciler <?> reconciler ) {
137
+ private static void configureFromAnnotatedReconciler (Object instance ,
138
+ Class <? extends Reconciler <?>> reconcilerClass ) {
132
139
if (instance instanceof AnnotationConfigurable configurable ) {
133
140
final Class <? extends Annotation > configurationClass =
134
141
(Class <? extends Annotation >) Utils .getFirstTypeArgumentFromSuperClassOrInterface (
135
142
instance .getClass (), AnnotationConfigurable .class );
136
- final var configAnnotation = reconciler . getClass () .getAnnotation (configurationClass );
143
+ final var configAnnotation = reconcilerClass .getAnnotation (configurationClass );
137
144
if (configAnnotation != null ) {
138
145
configurable .initFrom (configAnnotation );
139
146
}
@@ -190,101 +197,127 @@ protected ResourceClassResolver getResourceClassResolver() {
190
197
191
198
@ SuppressWarnings ({"unchecked" , "rawtypes" })
192
199
protected <P extends HasMetadata > ControllerConfiguration <P > configFor (Reconciler <P > reconciler ) {
193
- final var annotation = reconciler .getClass ().getAnnotation (
200
+ final Class <? extends Reconciler <P >> reconcilerClass =
201
+ (Class <? extends Reconciler <P >>) reconciler .getClass ();
202
+ final var controllerAnnotation = reconcilerClass .getAnnotation (
194
203
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration .class );
195
204
196
- if (annotation == null ) {
197
- throw new OperatorException (
198
- "Missing mandatory @"
199
- + io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration .class
200
- .getSimpleName ()
201
- +
202
- " annotation for reconciler: " + reconciler );
205
+ ResolvedControllerConfiguration <P > config =
206
+ controllerConfiguration (reconcilerClass , controllerAnnotation );
207
+
208
+ final var workflowAnnotation = reconcilerClass .getAnnotation (
209
+ io .javaoperatorsdk .operator .api .reconciler .Workflow .class );
210
+ if (workflowAnnotation != null ) {
211
+ final var specs = dependentResources (workflowAnnotation , config );
212
+ WorkflowSpec workflowSpec = new WorkflowSpec () {
213
+ @ Override
214
+ public List <DependentResourceSpec > getDependentResourceSpecs () {
215
+ return specs ;
216
+ }
217
+
218
+ @ Override
219
+ public boolean isExplicitInvocation () {
220
+ return workflowAnnotation .explicitInvocation ();
221
+ }
222
+
223
+ @ Override
224
+ public boolean handleExceptionsInReconciler () {
225
+ return workflowAnnotation .handleExceptionsInReconciler ();
226
+ }
227
+
228
+ };
229
+ config .setWorkflowSpec (workflowSpec );
203
230
}
204
- Class <Reconciler <P >> reconcilerClass = (Class <Reconciler <P >>) reconciler .getClass ();
231
+
232
+ return config ;
233
+ }
234
+
235
+ @ SuppressWarnings ({"unchecked" })
236
+ private <P extends HasMetadata > ResolvedControllerConfiguration <P > controllerConfiguration (
237
+ Class <? extends Reconciler <P >> reconcilerClass ,
238
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration annotation ) {
205
239
final var resourceClass = getResourceClassResolver ().getPrimaryResourceClass (reconcilerClass );
206
240
207
- final var name = ReconcilerUtils .getNameFor (reconciler );
208
- final var generationAware = valueOrDefault (
241
+ final var name = ReconcilerUtils .getNameFor (reconcilerClass );
242
+ final var generationAware = valueOrDefaultFromAnnotation (
209
243
annotation ,
210
244
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::generationAwareEventProcessing ,
211
- true );
245
+ "generationAwareEventProcessing" );
212
246
final var associatedReconcilerClass =
213
- ResolvedControllerConfiguration .getAssociatedReconcilerClassName (reconciler . getClass () );
247
+ ResolvedControllerConfiguration .getAssociatedReconcilerClassName (reconcilerClass );
214
248
215
249
final var context = Utils .contextFor (name );
216
- final Class <? extends Retry > retryClass = annotation .retry ();
250
+ final Class <? extends Retry > retryClass =
251
+ valueOrDefaultFromAnnotation (annotation ,
252
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::retry ,
253
+ "retry" );
217
254
final var retry = Utils .instantiateAndConfigureIfNeeded (retryClass , Retry .class ,
218
- context , configuratorFor (Retry .class , reconciler ));
255
+ context , configuratorFor (Retry .class , reconcilerClass ));
219
256
220
- final Class <? extends RateLimiter > rateLimiterClass = annotation .rateLimiter ();
257
+ @ SuppressWarnings ("rawtypes" )
258
+ final Class <? extends RateLimiter > rateLimiterClass = valueOrDefaultFromAnnotation (annotation ,
259
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::rateLimiter ,
260
+ "rateLimiter" );
221
261
final var rateLimiter = Utils .instantiateAndConfigureIfNeeded (rateLimiterClass ,
222
- RateLimiter .class , context , configuratorFor (RateLimiter .class , reconciler ));
262
+ RateLimiter .class , context , configuratorFor (RateLimiter .class , reconcilerClass ));
223
263
224
- final var reconciliationInterval = annotation .maxReconciliationInterval ();
264
+ final var reconciliationInterval = valueOrDefaultFromAnnotation (annotation ,
265
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::maxReconciliationInterval ,
266
+ "maxReconciliationInterval" );
225
267
long interval = -1 ;
226
268
TimeUnit timeUnit = null ;
227
269
if (reconciliationInterval != null && reconciliationInterval .interval () > 0 ) {
228
270
interval = reconciliationInterval .interval ();
229
271
timeUnit = reconciliationInterval .timeUnit ();
230
272
}
231
273
274
+ var fieldManager = valueOrDefaultFromAnnotation (annotation ,
275
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::fieldManager ,
276
+ "fieldManager" );
232
277
final var dependentFieldManager =
233
- annotation . fieldManager () .equals (CONTROLLER_NAME_AS_FIELD_MANAGER ) ? name
234
- : annotation . fieldManager () ;
278
+ fieldManager .equals (CONTROLLER_NAME_AS_FIELD_MANAGER ) ? name
279
+ : fieldManager ;
235
280
281
+ var informerListLimitValue = valueOrDefaultFromAnnotation (annotation ,
282
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::informerListLimit ,
283
+ "informerListLimit" );
236
284
final var informerListLimit =
237
- annotation . informerListLimit () == Constants .NO_LONG_VALUE_SET ? null
238
- : annotation . informerListLimit () ;
285
+ informerListLimitValue == Constants .NO_LONG_VALUE_SET ? null
286
+ : informerListLimitValue ;
239
287
240
- final var config = new ResolvedControllerConfiguration <P >(
288
+ return new ResolvedControllerConfiguration <P >(
241
289
resourceClass , name , generationAware ,
242
290
associatedReconcilerClass , retry , rateLimiter ,
243
291
ResolvedControllerConfiguration .getMaxReconciliationInterval (interval , timeUnit ),
244
- Utils .instantiate (annotation .onAddFilter (), OnAddFilter .class , context ),
245
- Utils .instantiate (annotation .onUpdateFilter (), OnUpdateFilter .class , context ),
246
- Utils .instantiate (annotation .genericFilter (), GenericFilter .class , context ),
247
- Set .of (valueOrDefault (annotation ,
292
+ Utils .instantiate (valueOrDefaultFromAnnotation (annotation ,
293
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::onAddFilter ,
294
+ "onAddFilter" ), OnAddFilter .class , context ),
295
+ Utils .instantiate (valueOrDefaultFromAnnotation (annotation ,
296
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::onUpdateFilter ,
297
+ "onUpdateFilter" ), OnUpdateFilter .class , context ),
298
+ Utils .instantiate (valueOrDefaultFromAnnotation (annotation ,
299
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::genericFilter ,
300
+ "genericFilter" ), GenericFilter .class , context ),
301
+ Set .of (valueOrDefaultFromAnnotation (annotation ,
248
302
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::namespaces ,
249
- DEFAULT_NAMESPACES_SET . toArray ( String []:: new ) )),
250
- valueOrDefault (annotation ,
303
+ "namespaces" )),
304
+ valueOrDefaultFromAnnotation (annotation ,
251
305
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::finalizerName ,
252
- Constants . NO_VALUE_SET ),
253
- valueOrDefault (annotation ,
306
+ "finalizerName" ),
307
+ valueOrDefaultFromAnnotation (annotation ,
254
308
io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::labelSelector ,
255
- Constants . NO_VALUE_SET ),
309
+ "labelSelector" ),
256
310
null ,
257
- Utils .instantiate (annotation .itemStore (), ItemStore .class , context ), dependentFieldManager ,
311
+ Utils .instantiate (
312
+ valueOrDefaultFromAnnotation (annotation ,
313
+ io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ::itemStore ,
314
+ "itemStore" ),
315
+ ItemStore .class , context ),
316
+ dependentFieldManager ,
258
317
this , informerListLimit );
259
-
260
-
261
- final var workflowAnnotation = reconciler .getClass ().getAnnotation (
262
- io .javaoperatorsdk .operator .api .reconciler .Workflow .class );
263
- if (workflowAnnotation != null ) {
264
- final var specs = dependentResources (workflowAnnotation , config );
265
- WorkflowSpec workflowSpec = new WorkflowSpec () {
266
- @ Override
267
- public List <DependentResourceSpec > getDependentResourceSpecs () {
268
- return specs ;
269
- }
270
-
271
- @ Override
272
- public boolean isExplicitInvocation () {
273
- return workflowAnnotation .explicitInvocation ();
274
- }
275
-
276
- @ Override
277
- public boolean handleExceptionsInReconciler () {
278
- return workflowAnnotation .handleExceptionsInReconciler ();
279
- }
280
-
281
- };
282
- config .setWorkflowSpec (workflowSpec );
283
- }
284
-
285
- return config ;
286
318
}
287
319
320
+
288
321
protected boolean createIfNeeded () {
289
322
return true ;
290
323
}
@@ -293,4 +326,6 @@ protected boolean createIfNeeded() {
293
326
public boolean checkCRDAndValidateLocalModel () {
294
327
return Utils .shouldCheckCRDAndValidateLocalModel ();
295
328
}
329
+
330
+
296
331
}
0 commit comments