1515 */
1616@ SuppressWarnings ("rawtypes" )
1717public class AbstractConfigurationService implements ConfigurationService {
18- private final Map <String , ControllerConfiguration > configurations = new ConcurrentHashMap <>();
18+ private final Map <String , Configured > configurations = new ConcurrentHashMap <>();
1919 private final Version version ;
2020 private KubernetesClient client ;
2121 private Cloner cloner ;
@@ -88,12 +88,13 @@ private <R extends HasMetadata> void put(
8888 ControllerConfiguration <R > config , boolean failIfExisting ) {
8989 final var name = config .getName ();
9090 if (failIfExisting ) {
91- final var existing = configurations . get (name );
91+ final var existing = getFor (name );
9292 if (existing != null ) {
9393 throwExceptionOnNameCollision (config .getAssociatedReconcilerClassName (), existing );
9494 }
9595 }
96- configurations .put (name , config );
96+ // record the configuration but mark is as un-configured in case a reconciler wants to override the configuration when registering
97+ configurations .put (name , new Configured (false , config ));
9798 }
9899
99100 protected <R extends HasMetadata > void throwExceptionOnNameCollision (
@@ -112,22 +113,31 @@ protected <R extends HasMetadata> void throwExceptionOnNameCollision(
112113 public <R extends HasMetadata > ControllerConfiguration <R > getConfigurationFor (
113114 Reconciler <R > reconciler ) {
114115 final var key = keyFor (reconciler );
115- var configuration = configurations .get (key );
116- if (configuration == null ) {
116+ var configured = configurations .get (key );
117+ if (configured == null ) {
117118 logMissingReconcilerWarning (key , getReconcilersNameMessage ());
118- } else {
119- // if a reconciler is also a ConfigurableReconciler, update and replace its configuration
119+ return null ;
120+ }
121+
122+ var config = configured .config ;
123+ // if a reconciler is also a ConfigurableReconciler, update and replace its configuration if it
124+ // hasn't already been configured
125+ if (!configured .configured ) {
120126 if (reconciler instanceof ConfigurableReconciler <?> configurableReconciler ) {
121- final var overrider = ControllerConfigurationOverrider .override (configuration );
127+
128+ final var overrider = ControllerConfigurationOverrider .override (config );
122129 configurableReconciler .updateConfigurationFrom (overrider );
123- configuration = overrider .build ();
124- configurations .put (key , configuration );
130+ config = overrider .build ();
125131 }
132+ // mark the reconciler as configured so that we don't attempt to do so again next time the configuration is requested
133+ configurations .put (key , new Configured (true , config ));
126134 }
127135
128- return configuration ;
136+ return config ;
129137 }
130138
139+ private record Configured (boolean configured , ControllerConfiguration config ) {}
140+
131141 protected void logMissingReconcilerWarning (String reconcilerKey , String reconcilersNameMessage ) {
132142 log .warn ("Cannot find reconciler named '{}'. {}" , reconcilerKey , reconcilersNameMessage );
133143 }
@@ -142,14 +152,14 @@ protected <R extends HasMetadata> String keyFor(Reconciler<R> reconciler) {
142152 return ReconcilerUtils .getNameFor (reconciler );
143153 }
144154
145- @ SuppressWarnings ("unused" )
146155 protected ControllerConfiguration getFor (String reconcilerName ) {
147- return configurations .get (reconcilerName );
156+ final var configured = configurations .get (reconcilerName );
157+ return configured != null ? configured .config : null ;
148158 }
149159
150160 @ SuppressWarnings ("unused" )
151161 protected Stream <ControllerConfiguration > controllerConfigurations () {
152- return configurations .values ().stream ();
162+ return configurations .values ().stream (). map ( Configured :: config ) ;
153163 }
154164
155165 @ Override
0 commit comments