1212import java .util .Collection ;
1313import java .util .Collections ;
1414import java .util .HashSet ;
15+ import java .util .List ;
1516import java .util .Map ;
1617import java .util .Optional ;
1718import java .util .Properties ;
@@ -208,11 +209,12 @@ private static void begin() {
208209
209210 Step strategy = Step .chain (
210211 new InitializeNamespacesSecurityStep (targetNamespaces ),
211- new NamespaceRulesReviewStep (),
212- CrdHelper .createDomainCrdStep (version ,
213- new StartNamespacesStep (targetNamespaces , false )));
212+ new NamespaceRulesReviewStep ());
214213 if (!isDedicated ()) {
215- strategy = Step .chain (strategy , readExistingNamespaces ());
214+ strategy = Step .chain (strategy , readExistingNamespaces (targetNamespaces ));
215+ } else {
216+ strategy = Step .chain (strategy , CrdHelper .createDomainCrdStep (version ,
217+ new StartNamespacesStep (targetNamespaces , false )));
216218 }
217219 runSteps (
218220 strategy ,
@@ -360,8 +362,8 @@ private static Step readExistingPods(String ns) {
360362 .listPodAsync (ns , new PodListStep (ns ));
361363 }
362364
363- private static Step readExistingNamespaces () {
364- return new CallBuilder ().listNamespaceAsync (new NamespaceListStep ());
365+ private static Step readExistingNamespaces (Collection < String > targetNamespaces ) {
366+ return new CallBuilder ().listNamespaceAsync (new NamespaceListStep (targetNamespaces ));
365367 }
366368
367369 private static ConfigMapAfterStep createConfigMapStep (String ns ) {
@@ -865,6 +867,12 @@ private String getInitialResourceVersion(V1PodList result) {
865867 }
866868
867869 private static class NamespaceListStep extends ResponseStep <V1NamespaceList > {
870+ private final Collection <String > targetNamespaces ;
871+
872+ NamespaceListStep (Collection <String > targetNamespaces ) {
873+ this .targetNamespaces = targetNamespaces ;
874+ }
875+
868876 @ Override
869877 public NextAction onFailure (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
870878 return callResponse .getStatusCode () == CallBuilder .NOT_FOUND
@@ -875,23 +883,69 @@ public NextAction onFailure(Packet packet, CallResponse<V1NamespaceList> callRes
875883 @ Override
876884 protected NextAction onFailureNoRetry (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
877885 return isNotAuthorizedOrForbidden (callResponse )
878- ? doNext (packet ) : super .onFailureNoRetry (packet , callResponse );
886+ ? doNext (createDomainCrdAndStartNamespaces (targetNamespaces ), packet ) :
887+ super .onFailureNoRetry (packet , callResponse );
879888 }
880889
881890 @ Override
882891 public NextAction onSuccess (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
883892 V1NamespaceList result = callResponse .getResult ();
884893 // don't bother processing pre-existing events
894+ String intialResourceVersion = getInitialResourceVersion (result );
895+ List <String > nsList = getExistingNamespaces (result );
885896
886- if (namespaceWatcher == null ) {
887- namespaceWatcher = createNamespaceWatcher (getInitialResourceVersion (result ));
897+ Set <String > namespacesToStart = new TreeSet <>(targetNamespaces );
898+ for (String ns : targetNamespaces ) {
899+ if (!nsList .contains (ns )) {
900+ LOGGER .warning (MessageKeys .NAMESPACE_IS_MISSING , ns );
901+ namespacesToStart .remove (ns );
902+ }
888903 }
889- return doNext (packet );
904+ Step strategy = null ;
905+ if (!namespacesToStart .isEmpty ()) {
906+ strategy = Step .chain (createDomainCrdAndStartNamespaces (namespacesToStart ),
907+ new CreateNamespaceWatcherStep (intialResourceVersion ));
908+ } else {
909+ strategy = CrdHelper .createDomainCrdStep (version ,
910+ new CreateNamespaceWatcherStep (intialResourceVersion ));
911+ }
912+ return doNext (strategy , packet );
913+ }
914+
915+ private Step createDomainCrdAndStartNamespaces (Collection <String > namespacesToStart ) {
916+ return CrdHelper .createDomainCrdStep (version ,
917+ new StartNamespacesStep (namespacesToStart , false ));
890918 }
891919
892920 private String getInitialResourceVersion (V1NamespaceList result ) {
893921 return result != null ? result .getMetadata ().getResourceVersion () : "" ;
894922 }
923+
924+ private List <String > getExistingNamespaces (V1NamespaceList result ) {
925+ List <String > namespaces = new ArrayList <>();
926+ if (result != null ) {
927+ for (V1Namespace ns :result .getItems ()) {
928+ namespaces .add (ns .getMetadata ().getName ());
929+ }
930+ }
931+ return namespaces ;
932+ }
933+ }
934+
935+ private static class CreateNamespaceWatcherStep extends Step {
936+ private final String initialResourceVersion ;
937+
938+ CreateNamespaceWatcherStep (String initialResourceVersion ) {
939+ this .initialResourceVersion = initialResourceVersion ;
940+ }
941+
942+ @ Override
943+ public NextAction apply (Packet packet ) {
944+ if (namespaceWatcher == null ) {
945+ namespaceWatcher = createNamespaceWatcher (initialResourceVersion );
946+ }
947+ return doNext (packet );
948+ }
895949 }
896950
897951 private static class NullCompletionCallback implements CompletionCallback {
0 commit comments