1
1
package io .javaoperatorsdk .operator ;
2
2
3
+ import java .util .Collection ;
3
4
import java .util .HashMap ;
5
+ import java .util .HashSet ;
4
6
import java .util .Map ;
7
+ import java .util .Optional ;
8
+ import java .util .Set ;
5
9
import java .util .function .Consumer ;
6
10
7
11
import org .slf4j .Logger ;
@@ -120,10 +124,10 @@ public void stop() throws OperatorException {
120
124
* registration of the reconciler is delayed till the operator is started.
121
125
*
122
126
* @param reconciler the reconciler to register
123
- * @param <R > the {@code CustomResource} type associated with the reconciler
127
+ * @param <P > the {@code CustomResource} type associated with the reconciler
124
128
* @throws OperatorException if a problem occurred during the registration process
125
129
*/
126
- public <R extends HasMetadata > RegisteredController register (Reconciler <R > reconciler )
130
+ public <P extends HasMetadata > RegisteredController < P > register (Reconciler <P > reconciler )
127
131
throws OperatorException {
128
132
final var controllerConfiguration =
129
133
ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
@@ -139,11 +143,11 @@ public <R extends HasMetadata> RegisteredController register(Reconciler<R> recon
139
143
*
140
144
* @param reconciler part of the reconciler to register
141
145
* @param configuration the configuration with which we want to register the reconciler
142
- * @param <R > the {@code HasMetadata} type associated with the reconciler
146
+ * @param <P > the {@code HasMetadata} type associated with the reconciler
143
147
* @throws OperatorException if a problem occurred during the registration process
144
148
*/
145
- public <R extends HasMetadata > RegisteredController register (Reconciler <R > reconciler ,
146
- ControllerConfiguration <R > configuration )
149
+ public <P extends HasMetadata > RegisteredController < P > register (Reconciler <P > reconciler ,
150
+ ControllerConfiguration <P > configuration )
147
151
throws OperatorException {
148
152
149
153
if (configuration == null ) {
@@ -175,17 +179,29 @@ public <R extends HasMetadata> RegisteredController register(Reconciler<R> recon
175
179
*
176
180
* @param reconciler part of the reconciler to register
177
181
* @param configOverrider consumer to use to change config values
178
- * @param <R > the {@code HasMetadata} type associated with the reconciler
182
+ * @param <P > the {@code HasMetadata} type associated with the reconciler
179
183
*/
180
- public <R extends HasMetadata > RegisteredController register (Reconciler <R > reconciler ,
181
- Consumer <ControllerConfigurationOverrider <R >> configOverrider ) {
184
+ public <P extends HasMetadata > RegisteredController < P > register (Reconciler <P > reconciler ,
185
+ Consumer <ControllerConfigurationOverrider <P >> configOverrider ) {
182
186
final var controllerConfiguration =
183
187
ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
184
188
var configToOverride = ControllerConfigurationOverrider .override (controllerConfiguration );
185
189
configOverrider .accept (configToOverride );
186
190
return register (reconciler , configToOverride .build ());
187
191
}
188
192
193
+ public Optional <RegisteredController > getRegisteredController (String name ) {
194
+ return controllers .get (name ).map (RegisteredController .class ::cast );
195
+ }
196
+
197
+ public Set <RegisteredController > getRegisteredControllers () {
198
+ return new HashSet <>(controllers .controllers ());
199
+ }
200
+
201
+ public int getRegisteredControllersNumber () {
202
+ return controllers .size ();
203
+ }
204
+
189
205
static class ControllerManager implements LifecycleAware {
190
206
private final Map <String , Controller > controllers = new HashMap <>();
191
207
private boolean started = false ;
@@ -200,12 +216,12 @@ public synchronized void shouldStart() {
200
216
}
201
217
202
218
public synchronized void start () {
203
- controllers . values ().parallelStream ().forEach (Controller ::start );
219
+ controllers ().parallelStream ().forEach (Controller ::start );
204
220
started = true ;
205
221
}
206
222
207
223
public synchronized void stop () {
208
- this . controllers . values ().parallelStream ().forEach (closeable -> {
224
+ controllers ().parallelStream ().forEach (closeable -> {
209
225
log .debug ("closing {}" , closeable );
210
226
closeable .stop ();
211
227
});
@@ -224,10 +240,24 @@ synchronized void add(Controller controller) {
224
240
+ "': another controller named '" + existing .getConfiguration ().getName ()
225
241
+ "' is already registered for resource '" + resourceTypeName + "'" );
226
242
}
227
- this . controllers .put (resourceTypeName , controller );
243
+ controllers .put (resourceTypeName , controller );
228
244
if (started ) {
229
245
controller .start ();
230
246
}
231
247
}
248
+
249
+ synchronized Optional <Controller > get (String name ) {
250
+ return controllers ().stream ()
251
+ .filter (c -> name .equals (c .getConfiguration ().getName ()))
252
+ .findFirst ();
253
+ }
254
+
255
+ synchronized Collection <Controller > controllers () {
256
+ return controllers .values ();
257
+ }
258
+
259
+ synchronized int size () {
260
+ return controllers .size ();
261
+ }
232
262
}
233
263
}
0 commit comments