33import java .io .Closeable ;
44import java .io .IOException ;
55import java .net .ConnectException ;
6- import java .util .Collections ;
7- import java .util .LinkedList ;
6+ import java .util .ArrayList ;
7+ import java .util .HashMap ;
88import java .util .List ;
9+ import java .util .Map ;
910
1011import org .slf4j .Logger ;
1112import org .slf4j .LoggerFactory ;
@@ -50,7 +51,7 @@ public ConfigurationService getConfigurationService() {
5051 }
5152
5253 public List <ConfiguredController > getControllers () {
53- return Collections . unmodifiableList (controllers .controllers );
54+ return new ArrayList <> (controllers .controllers . values () );
5455 }
5556
5657 /**
@@ -159,7 +160,7 @@ public <R extends CustomResource> void register(
159160 }
160161
161162 private static class ControllerManager implements Closeable {
162- private final List < ConfiguredController > controllers = new LinkedList <>();
163+ private final Map < String , ConfiguredController > controllers = new HashMap <>();
163164 private boolean started = false ;
164165
165166
@@ -173,7 +174,7 @@ public synchronized void shouldStart() {
173174 }
174175
175176 public synchronized void start () {
176- controllers .parallelStream ().forEach (ConfiguredController ::start );
177+ controllers .values (). parallelStream ().forEach (ConfiguredController ::start );
177178 started = true ;
178179 }
179180
@@ -183,7 +184,7 @@ public synchronized void close() {
183184 return ;
184185 }
185186
186- this .controllers .parallelStream ().forEach (closeable -> {
187+ this .controllers .values (). parallelStream ().forEach (closeable -> {
187188 try {
188189 log .debug ("closing {}" , closeable );
189190 closeable .close ();
@@ -196,7 +197,15 @@ public synchronized void close() {
196197 }
197198
198199 public synchronized void add (ConfiguredController configuredController ) {
199- this .controllers .add (configuredController );
200+ final var configuration = configuredController .getConfiguration ();
201+ final var crdName = configuration .getCRDName ();
202+ final var existing = controllers .get (crdName );
203+ if (existing != null ) {
204+ throw new OperatorException ("Cannot register controller " + configuration .getName ()
205+ + ": another controller (" + existing .getConfiguration ().getName ()
206+ + ") is already registered for CRD " + crdName );
207+ }
208+ this .controllers .put (crdName , configuredController );
200209 if (started ) {
201210 configuredController .start ();
202211 }
0 commit comments