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,14 @@ 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
97
+ // the configuration when registering
98
+ configurations .put (name , new Configured (false , config ));
97
99
}
98
100
99
101
protected <R extends HasMetadata > void throwExceptionOnNameCollision (
@@ -112,22 +114,32 @@ protected <R extends HasMetadata> void throwExceptionOnNameCollision(
112
114
public <R extends HasMetadata > ControllerConfiguration <R > getConfigurationFor (
113
115
Reconciler <R > reconciler ) {
114
116
final var key = keyFor (reconciler );
115
- var configuration = configurations .get (key );
116
- if (configuration == null ) {
117
+ var configured = configurations .get (key );
118
+ if (configured == null ) {
117
119
logMissingReconcilerWarning (key , getReconcilersNameMessage ());
118
- } else {
119
- // if a reconciler is also a ConfigurableReconciler, update and replace its configuration
120
+ return null ;
121
+ }
122
+
123
+ var config = configured .config ;
124
+ // if a reconciler is also a ConfigurableReconciler, update and replace its configuration if it
125
+ // hasn't already been configured
126
+ if (!configured .configured ) {
120
127
if (reconciler instanceof ConfigurableReconciler <?> configurableReconciler ) {
121
- final var overrider = ControllerConfigurationOverrider .override (configuration );
128
+
129
+ final var overrider = ControllerConfigurationOverrider .override (config );
122
130
configurableReconciler .updateConfigurationFrom (overrider );
123
- configuration = overrider .build ();
124
- configurations .put (key , configuration );
131
+ config = overrider .build ();
125
132
}
133
+ // mark the reconciler as configured so that we don't attempt to do so again next time the
134
+ // configuration is requested
135
+ configurations .put (key , new Configured (true , config ));
126
136
}
127
137
128
- return configuration ;
138
+ return config ;
129
139
}
130
140
141
+ private record Configured (boolean configured , ControllerConfiguration config ) {}
142
+
131
143
protected void logMissingReconcilerWarning (String reconcilerKey , String reconcilersNameMessage ) {
132
144
log .warn ("Cannot find reconciler named '{}'. {}" , reconcilerKey , reconcilersNameMessage );
133
145
}
@@ -142,14 +154,14 @@ protected <R extends HasMetadata> String keyFor(Reconciler<R> reconciler) {
142
154
return ReconcilerUtils .getNameFor (reconciler );
143
155
}
144
156
145
- @ SuppressWarnings ("unused" )
146
157
protected ControllerConfiguration getFor (String reconcilerName ) {
147
- return configurations .get (reconcilerName );
158
+ final var configured = configurations .get (reconcilerName );
159
+ return configured != null ? configured .config : null ;
148
160
}
149
161
150
162
@ SuppressWarnings ("unused" )
151
163
protected Stream <ControllerConfiguration > controllerConfigurations () {
152
- return configurations .values ().stream ();
164
+ return configurations .values ().stream (). map ( Configured :: config ) ;
153
165
}
154
166
155
167
@ Override
0 commit comments