15
15
*/
16
16
@ SuppressWarnings ("rawtypes" )
17
17
public class AbstractConfigurationService implements ConfigurationService {
18
- private final Map <String , ControllerConfiguration > configurations = new ConcurrentHashMap <>();
18
+ private final Map <String , Configured > configurations = new ConcurrentHashMap <>();
19
19
private final Version version ;
20
20
private KubernetesClient client ;
21
21
private Cloner cloner ;
@@ -88,12 +88,13 @@ private <R extends HasMetadata> void put(
88
88
ControllerConfiguration <R > config , boolean failIfExisting ) {
89
89
final var name = config .getName ();
90
90
if (failIfExisting ) {
91
- final var existing = configurations . get (name );
91
+ final var existing = getFor (name );
92
92
if (existing != null ) {
93
93
throwExceptionOnNameCollision (config .getAssociatedReconcilerClassName (), existing );
94
94
}
95
95
}
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 ));
97
98
}
98
99
99
100
protected <R extends HasMetadata > void throwExceptionOnNameCollision (
@@ -112,22 +113,31 @@ protected <R extends HasMetadata> void throwExceptionOnNameCollision(
112
113
public <R extends HasMetadata > ControllerConfiguration <R > getConfigurationFor (
113
114
Reconciler <R > reconciler ) {
114
115
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 ) {
117
118
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 ) {
120
126
if (reconciler instanceof ConfigurableReconciler <?> configurableReconciler ) {
121
- final var overrider = ControllerConfigurationOverrider .override (configuration );
127
+
128
+ final var overrider = ControllerConfigurationOverrider .override (config );
122
129
configurableReconciler .updateConfigurationFrom (overrider );
123
- configuration = overrider .build ();
124
- configurations .put (key , configuration );
130
+ config = overrider .build ();
125
131
}
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 ));
126
134
}
127
135
128
- return configuration ;
136
+ return config ;
129
137
}
130
138
139
+ private record Configured (boolean configured , ControllerConfiguration config ) {}
140
+
131
141
protected void logMissingReconcilerWarning (String reconcilerKey , String reconcilersNameMessage ) {
132
142
log .warn ("Cannot find reconciler named '{}'. {}" , reconcilerKey , reconcilersNameMessage );
133
143
}
@@ -142,14 +152,14 @@ protected <R extends HasMetadata> String keyFor(Reconciler<R> reconciler) {
142
152
return ReconcilerUtils .getNameFor (reconciler );
143
153
}
144
154
145
- @ SuppressWarnings ("unused" )
146
155
protected ControllerConfiguration getFor (String reconcilerName ) {
147
- return configurations .get (reconcilerName );
156
+ final var configured = configurations .get (reconcilerName );
157
+ return configured != null ? configured .config : null ;
148
158
}
149
159
150
160
@ SuppressWarnings ("unused" )
151
161
protected Stream <ControllerConfiguration > controllerConfigurations () {
152
- return configurations .values ().stream ();
162
+ return configurations .values ().stream (). map ( Configured :: config ) ;
153
163
}
154
164
155
165
@ Override
0 commit comments