@@ -86,7 +86,7 @@ public class Main {
8686 private static final TuningParameters tuningAndConfig ;
8787 private static final CallBuilderFactory callBuilderFactory = new CallBuilderFactory ();
8888 private static Map <String , NamespaceStatus > namespaceStatuses = new ConcurrentHashMap <>();
89- private static Map <String , AtomicBoolean > isNamespaceStopping = new ConcurrentHashMap <>();
89+ private static Map <String , AtomicBoolean > namespaceStoppingMap = new ConcurrentHashMap <>();
9090 private static final Map <String , ConfigMapWatcher > configMapWatchers = new ConcurrentHashMap <>();
9191 private static final Map <String , DomainWatcher > domainWatchers = new ConcurrentHashMap <>();
9292 private static final Map <String , EventWatcher > eventWatchers = new ConcurrentHashMap <>();
@@ -227,7 +227,7 @@ private static void begin() {
227227 private static void completeBegin () {
228228 try {
229229 // start the REST server
230- startRestServer (principal , isNamespaceStopping .keySet ());
230+ startRestServer (principal , namespaceStoppingMap .keySet ());
231231
232232 // start periodic retry and recheck
233233 int recheckInterval = tuningAndConfig .getMainTuning ().targetNamespaceRecheckIntervalSeconds ;
@@ -247,14 +247,23 @@ private static void completeBegin() {
247247 }
248248 }
249249
250- private static void stopNamespace (String ns , boolean remove ) {
251- processor .stopNamespace (ns );
252- AtomicBoolean stopping =
253- remove ? isNamespaceStopping .remove (ns ) : isNamespaceStopping .get (ns );
250+ private static void stopNamespace (String ns , boolean inTargetNamespaceList ) {
251+ AtomicBoolean isNamespaceStopping = isNamespaceStopping (ns );
254252
255- if (stopping != null ) {
256- stopping .set (true );
253+ // Remove if namespace not in targetNamespace list
254+ if (!inTargetNamespaceList ) {
255+ namespaceStoppingMap .remove (ns );
257256 }
257+
258+ // stop all Domains for namespace being stopped (not active)
259+ if (isNamespaceStopping .get ()) {
260+ processor .stopNamespace (ns );
261+ }
262+
263+ // set flag to indicate namespace is stopping.
264+ isNamespaceStopping .set (true );
265+
266+ // unsubscribe from resource events for given namespace
258267 namespaceStatuses .remove (ns );
259268 domainWatchers .remove (ns );
260269 eventWatchers .remove (ns );
@@ -267,12 +276,12 @@ private static void stopNamespace(String ns, boolean remove) {
267276 private static void stopNamespaces (Collection <String > targetNamespaces ,
268277 Collection <String > namespacesToStop ) {
269278 for (String ns : namespacesToStop ) {
270- stopNamespace (ns , (! targetNamespaces .contains (ns ) ));
279+ stopNamespace (ns , targetNamespaces .contains (ns ));
271280 }
272281 }
273282
274283 private static AtomicBoolean isNamespaceStopping (String ns ) {
275- return isNamespaceStopping .computeIfAbsent (ns , (key ) -> new AtomicBoolean (false ));
284+ return namespaceStoppingMap .computeIfAbsent (ns , (key ) -> new AtomicBoolean (false ));
276285 }
277286
278287 private static void runSteps (Step firstStep ) {
@@ -294,7 +303,7 @@ static Runnable recheckDomains() {
294303
295304 // Check for namespaces that are removed from the operator's
296305 // targetNamespaces list, or that are deleted from the Kubernetes cluster.
297- Set <String > namespacesToStop = new TreeSet <>(isNamespaceStopping .keySet ());
306+ Set <String > namespacesToStop = new TreeSet <>(namespaceStoppingMap .keySet ());
298307 for (String ns : targetNamespaces ) {
299308 // the active namespaces are the ones that will not be stopped
300309 if (delegate .isNamespaceRunning (ns )) {
@@ -403,7 +412,7 @@ private static Collection<String> getTargetNamespaces() {
403412 return isDedicated ()
404413 ? Collections .singleton (operatorNamespace )
405414 : getTargetNamespaces (Optional .ofNullable (getHelmVariable .apply ("OPERATOR_TARGET_NAMESPACES" ))
406- .orElse (tuningAndConfig .get ("targetNamespaces" )), operatorNamespace );
415+ .orElse (tuningAndConfig .get ("targetNamespaces" )), operatorNamespace );
407416 }
408417
409418 public static boolean isDedicated () {
@@ -450,7 +459,7 @@ private static void waitForDeath() {
450459 Thread .currentThread ().interrupt ();
451460 }
452461
453- isNamespaceStopping .forEach ((key , value ) -> value .set (true ));
462+ namespaceStoppingMap .forEach ((key , value ) -> value .set (true ));
454463 }
455464
456465 private static EventWatcher createEventWatcher (String ns , String initialResourceVersion ) {
@@ -528,15 +537,15 @@ private static void dispatchNamespaceWatch(Watch.Response<V1Namespace> item) {
528537 runSteps (Step .chain (
529538 ConfigMapHelper .createScriptConfigMapStep (operatorNamespace , ns ),
530539 createConfigMapStep (ns )));
531- isNamespaceStopping .put (ns , new AtomicBoolean (false ));
540+ namespaceStoppingMap .put (ns , new AtomicBoolean (false ));
532541 }
533542 break ;
534543
535544 case "DELETED" :
536545 // Mark the namespace as isStopping, which will cause the namespace be stopped
537546 // the next time when recheckDomains is triggered
538547 if (delegate .isNamespaceRunning (ns )) {
539- isNamespaceStopping .put (ns , new AtomicBoolean (true ));
548+ namespaceStoppingMap .put (ns , new AtomicBoolean (true ));
540549 }
541550
542551 break ;
@@ -904,17 +913,17 @@ public NextAction onSuccess(Packet packet, CallResponse<V1NamespaceList> callRes
904913 Step strategy = null ;
905914 if (!namespacesToStart .isEmpty ()) {
906915 strategy = Step .chain (createDomainCrdAndStartNamespaces (namespacesToStart ),
907- new CreateNamespaceWatcherStep (intialResourceVersion ));
916+ new CreateNamespaceWatcherStep (intialResourceVersion ));
908917 } else {
909918 strategy = CrdHelper .createDomainCrdStep (version ,
910- new CreateNamespaceWatcherStep (intialResourceVersion ));
919+ new CreateNamespaceWatcherStep (intialResourceVersion ));
911920 }
912921 return doNext (strategy , packet );
913922 }
914923
915924 private Step createDomainCrdAndStartNamespaces (Collection <String > namespacesToStart ) {
916925 return CrdHelper .createDomainCrdStep (version ,
917- new StartNamespacesStep (namespacesToStart , false ));
926+ new StartNamespacesStep (namespacesToStart , false ));
918927 }
919928
920929 private String getInitialResourceVersion (V1NamespaceList result ) {
@@ -991,7 +1000,7 @@ public boolean isNamespaceRunning(String namespace) {
9911000 // make sure the map entry is initialized the value to "false" if absent
9921001 isNamespaceStopping (namespace );
9931002
994- return !isNamespaceStopping .get (namespace ).get ();
1003+ return !namespaceStoppingMap .get (namespace ).get ();
9951004 }
9961005
9971006 @ Override
0 commit comments