11package io .javaoperatorsdk .operator ;
22
3+ import java .util .Collection ;
34import java .util .HashMap ;
5+ import java .util .HashSet ;
46import java .util .Map ;
7+ import java .util .Optional ;
8+ import java .util .Set ;
59import java .util .function .Consumer ;
610
711import org .slf4j .Logger ;
@@ -120,10 +124,10 @@ public void stop() throws OperatorException {
120124 * registration of the reconciler is delayed till the operator is started.
121125 *
122126 * @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
124128 * @throws OperatorException if a problem occurred during the registration process
125129 */
126- public <R extends HasMetadata > RegisteredController register (Reconciler <R > reconciler )
130+ public <P extends HasMetadata > RegisteredController < P > register (Reconciler <P > reconciler )
127131 throws OperatorException {
128132 final var controllerConfiguration =
129133 ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
@@ -139,11 +143,11 @@ public <R extends HasMetadata> RegisteredController register(Reconciler<R> recon
139143 *
140144 * @param reconciler part of the reconciler to register
141145 * @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
143147 * @throws OperatorException if a problem occurred during the registration process
144148 */
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 )
147151 throws OperatorException {
148152
149153 if (configuration == null ) {
@@ -175,17 +179,29 @@ public <R extends HasMetadata> RegisteredController register(Reconciler<R> recon
175179 *
176180 * @param reconciler part of the reconciler to register
177181 * @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
179183 */
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 ) {
182186 final var controllerConfiguration =
183187 ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
184188 var configToOverride = ControllerConfigurationOverrider .override (controllerConfiguration );
185189 configOverrider .accept (configToOverride );
186190 return register (reconciler , configToOverride .build ());
187191 }
188192
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+
189205 static class ControllerManager implements LifecycleAware {
190206 private final Map <String , Controller > controllers = new HashMap <>();
191207 private boolean started = false ;
@@ -200,12 +216,12 @@ public synchronized void shouldStart() {
200216 }
201217
202218 public synchronized void start () {
203- controllers . values ().parallelStream ().forEach (Controller ::start );
219+ controllers ().parallelStream ().forEach (Controller ::start );
204220 started = true ;
205221 }
206222
207223 public synchronized void stop () {
208- this . controllers . values ().parallelStream ().forEach (closeable -> {
224+ controllers ().parallelStream ().forEach (closeable -> {
209225 log .debug ("closing {}" , closeable );
210226 closeable .stop ();
211227 });
@@ -224,10 +240,24 @@ synchronized void add(Controller controller) {
224240 + "': another controller named '" + existing .getConfiguration ().getName ()
225241 + "' is already registered for resource '" + resourceTypeName + "'" );
226242 }
227- this . controllers .put (resourceTypeName , controller );
243+ controllers .put (resourceTypeName , controller );
228244 if (started ) {
229245 controller .start ();
230246 }
231247 }
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+ }
232262 }
233263}
0 commit comments