62
62
import oracle .kubernetes .operator .logging .MessageKeys ;
63
63
import oracle .kubernetes .operator .rest .RestConfigImpl ;
64
64
import oracle .kubernetes .operator .rest .RestServer ;
65
+ import oracle .kubernetes .operator .steps .ActionResponseStep ;
65
66
import oracle .kubernetes .operator .steps .ConfigMapAfterStep ;
66
67
import oracle .kubernetes .operator .work .Component ;
67
68
import oracle .kubernetes .operator .work .Container ;
@@ -207,13 +208,12 @@ private static void begin() {
207
208
208
209
Step strategy = Step .chain (
209
210
new InitializeNamespacesSecurityStep (configuredDomainNamespaces ),
210
- new NamespaceRulesReviewStep ());
211
+ new NamespaceRulesReviewStep (),
212
+ CrdHelper .createDomainCrdStep (version , productVersion ));
211
213
if (!DomainNamespaceSelectionStrategy .Dedicated .equals (selectionStrategy )) {
212
214
strategy = Step .chain (strategy , readExistingNamespaces (selectionStrategy , configuredDomainNamespaces , false ));
213
215
} else {
214
- strategy = Step .chain (strategy , CrdHelper .createDomainCrdStep (
215
- version , productVersion ,
216
- new StartNamespacesStep (configuredDomainNamespaces , false )));
216
+ strategy = Step .chain (strategy , new StartNamespacesStep (configuredDomainNamespaces , false ));
217
217
}
218
218
runSteps (
219
219
strategy ,
@@ -322,14 +322,13 @@ static Step createDomainRecheckSteps(DateTime now) {
322
322
323
323
Step strategy = Step .chain (
324
324
new InitializeNamespacesSecurityStep (configuredDomainNamespaces ),
325
- new NamespaceRulesReviewStep ());
325
+ new NamespaceRulesReviewStep (),
326
+ CrdHelper .createDomainCrdStep (version , productVersion ));
326
327
if (!DomainNamespaceSelectionStrategy .Dedicated .equals (selectionStrategy )) {
327
328
strategy = Step .chain (strategy , readExistingNamespaces (
328
329
selectionStrategy , configuredDomainNamespaces , isFullRecheck ));
329
330
} else {
330
- strategy = Step .chain (strategy , CrdHelper .createDomainCrdStep (
331
- version , productVersion ,
332
- new StartNamespacesStep (configuredDomainNamespaces , isFullRecheck )));
331
+ strategy = Step .chain (strategy , new StartNamespacesStep (configuredDomainNamespaces , isFullRecheck ));
333
332
}
334
333
return strategy ;
335
334
}
@@ -369,14 +368,31 @@ private static Step readExistingPods(String ns) {
369
368
}
370
369
371
370
static Step readExistingNamespaces (DomainNamespaceSelectionStrategy selectionStrategy ,
372
- Collection <String > domainNamespaces ,
373
- boolean isFullRecheck ) {
371
+ Collection <String > domainNamespaces ,
372
+ boolean isFullRecheck ) {
374
373
CallBuilder builder = new CallBuilder ();
375
374
String selector = selectionStrategy .getLabelSelector ();
376
375
if (selector != null ) {
377
376
builder .withLabelSelectors (selector );
378
377
}
379
- return builder .listNamespaceAsync (new NamespaceListStep (selectionStrategy , domainNamespaces , isFullRecheck ));
378
+ return builder .listNamespaceAsync (
379
+ new ActionResponseStep <V1NamespaceList >(new NamespaceListAfterStep (selectionStrategy )) {
380
+ private Step startNamespaces (Collection <String > namespacesToStart , boolean isFullRecheck ) {
381
+ return new StartNamespacesStep (namespacesToStart , isFullRecheck );
382
+ }
383
+
384
+ @ Override
385
+ protected NextAction onFailureNoRetry (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
386
+ return !selectionStrategy .isRequireList () && isNotAuthorizedOrForbidden (callResponse )
387
+ ? doNext (startNamespaces (domainNamespaces , isFullRecheck ), packet ) :
388
+ super .onFailureNoRetry (packet , callResponse );
389
+ }
390
+
391
+ @ Override
392
+ public Step createSuccessStep (V1NamespaceList result , Step next ) {
393
+ return new NamespaceListStep (result , selectionStrategy , domainNamespaces , isFullRecheck , next );
394
+ }
395
+ });
380
396
}
381
397
382
398
private static ConfigMapAfterStep createConfigMapStep (String ns ) {
@@ -682,7 +698,7 @@ private abstract static class ForEachNamespaceStep extends Step {
682
698
683
699
@ Override
684
700
protected String getDetail () {
685
- return String .join ("," , domainNamespaces );
701
+ return Optional . ofNullable ( domainNamespaces ). map ( d -> String .join ("," , d )). orElse ( null );
686
702
}
687
703
688
704
protected abstract Step action (String ns );
@@ -873,7 +889,8 @@ public NextAction onSuccess(Packet packet, CallResponse<DomainList> callResponse
873
889
domainWatchers .put (
874
890
ns , createDomainWatcher (ns , getResourceVersion (callResponse .getResult ())));
875
891
}
876
- return doNext (packet );
892
+
893
+ return doContinueListOrNext (callResponse , packet );
877
894
}
878
895
879
896
String getResourceVersion (DomainList result ) {
@@ -916,7 +933,8 @@ public NextAction onSuccess(Packet packet, CallResponse<V1ServiceList> callRespo
916
933
if (!serviceWatchers .containsKey (ns )) {
917
934
serviceWatchers .put (ns , createServiceWatcher (ns , getInitialResourceVersion (result )));
918
935
}
919
- return doNext (packet );
936
+
937
+ return doContinueListOrNext (callResponse , packet );
920
938
}
921
939
922
940
private String getInitialResourceVersion (V1ServiceList result ) {
@@ -946,7 +964,8 @@ public NextAction onSuccess(Packet packet, CallResponse<V1EventList> callRespons
946
964
if (!eventWatchers .containsKey (ns )) {
947
965
eventWatchers .put (ns , createEventWatcher (ns , getInitialResourceVersion (result )));
948
966
}
949
- return doNext (packet );
967
+
968
+ return doContinueListOrNext (callResponse , packet );
950
969
}
951
970
952
971
private String getInitialResourceVersion (V1EventList result ) {
@@ -990,103 +1009,86 @@ public NextAction onSuccess(Packet packet, CallResponse<V1PodList> callResponse)
990
1009
if (!podWatchers .containsKey (ns )) {
991
1010
podWatchers .put (ns , createPodWatcher (ns , getInitialResourceVersion (result )));
992
1011
}
993
- return doNext (packet );
1012
+
1013
+ return doContinueListOrNext (callResponse , packet );
994
1014
}
995
1015
996
1016
private String getInitialResourceVersion (V1PodList result ) {
997
1017
return result != null ? result .getMetadata ().getResourceVersion () : "" ;
998
1018
}
999
1019
}
1000
1020
1001
- private static class NamespaceListStep extends ResponseStep <V1NamespaceList > {
1021
+ private static final String ALL_DOMAIN_NAMESPACES = "ALL_DOMAIN_NAMESPACES" ;
1022
+
1023
+ private static class NamespaceListStep extends Step {
1024
+ private final V1NamespaceList list ;
1002
1025
private final DomainNamespaceSelectionStrategy selectionStrategy ;
1003
1026
private final Collection <String > configuredDomainNamespaces ;
1004
1027
private final boolean isFullRecheck ;
1005
1028
1006
- NamespaceListStep (DomainNamespaceSelectionStrategy selectionStrategy ,
1007
- Collection <String > configuredDomainNamespaces ,
1008
- boolean isFullRecheck ) {
1029
+ NamespaceListStep (V1NamespaceList list ,
1030
+ DomainNamespaceSelectionStrategy selectionStrategy ,
1031
+ Collection <String > configuredDomainNamespaces ,
1032
+ boolean isFullRecheck ,
1033
+ Step next ) {
1034
+ super (next );
1035
+ this .list = list ;
1009
1036
this .selectionStrategy = selectionStrategy ;
1010
1037
this .configuredDomainNamespaces = configuredDomainNamespaces ;
1011
1038
this .isFullRecheck = isFullRecheck ;
1012
1039
}
1013
1040
1014
1041
@ Override
1015
- public NextAction onFailure (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
1016
- return callResponse .getStatusCode () == CallBuilder .NOT_FOUND
1017
- ? onSuccess (packet , callResponse )
1018
- : super .onFailure (packet , callResponse );
1019
- }
1020
-
1021
- @ Override
1022
- protected NextAction onFailureNoRetry (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
1023
- return !selectionStrategy .isRequireList () && isNotAuthorizedOrForbidden (callResponse )
1024
- ? doNext (createDomainCrdAndStartNamespaces (configuredDomainNamespaces , isFullRecheck ), packet ) :
1025
- super .onFailureNoRetry (packet , callResponse );
1026
- }
1027
-
1028
- @ Override
1029
- public NextAction onSuccess (Packet packet , CallResponse <V1NamespaceList > callResponse ) {
1030
- V1NamespaceList result = callResponse .getResult ();
1042
+ public NextAction apply (Packet packet ) {
1031
1043
// don't bother processing pre-existing events
1032
- String intialResourceVersion = getInitialResourceVersion (result );
1033
- List <String > nsList = getExistingNamespaces (result );
1044
+ String intialResourceVersion = getInitialResourceVersion (list );
1045
+ List <String > nsPossiblyPartialList = getExistingNamespaces (list );
1034
1046
1035
- Set <String > namespacesToStart ;
1047
+ Set <String > namespacesToStartNow ;
1036
1048
if (selectionStrategy .isRequireList ()) {
1037
- namespacesToStart = new TreeSet <>(nsList );
1049
+ namespacesToStartNow = new TreeSet <>(nsPossiblyPartialList );
1038
1050
String regexp = selectionStrategy .getRegExp ();
1039
1051
if (regexp != null ) {
1040
1052
try {
1041
- namespacesToStart = namespacesToStart .stream ().filter (
1053
+ namespacesToStartNow = namespacesToStartNow .stream ().filter (
1042
1054
Pattern .compile (regexp ).asPredicate ()).collect (Collectors .toSet ());
1043
1055
} catch (PatternSyntaxException pse ) {
1044
1056
LOGGER .severe (MessageKeys .EXCEPTION , pse );
1045
1057
}
1046
1058
}
1047
1059
} else {
1048
- namespacesToStart = new TreeSet <>(configuredDomainNamespaces );
1049
- for (String ns : configuredDomainNamespaces ) {
1050
- if (!nsList .contains (ns )) {
1051
- try (LoggingContext stack = LoggingContext .setThreadContext ().namespace (ns )) {
1052
- LOGGER .warning (MessageKeys .NAMESPACE_IS_MISSING , ns );
1053
- }
1054
- namespacesToStart .remove (ns );
1055
- }
1056
- }
1060
+ namespacesToStartNow = new TreeSet <>(configuredDomainNamespaces );
1061
+ namespacesToStartNow .retainAll (nsPossiblyPartialList );
1057
1062
}
1063
+
1058
1064
Step strategy = null ;
1059
- if (!namespacesToStart .isEmpty ()) {
1060
- strategy = Step .chain (createDomainCrdAndStartNamespaces (namespacesToStart , isFullRecheck ),
1061
- new CreateNamespaceWatcherStep (selectionStrategy , intialResourceVersion ));
1065
+ if (!namespacesToStartNow .isEmpty ()) {
1066
+ strategy = Step .chain (
1067
+ startNamespaces (namespacesToStartNow , isFullRecheck ),
1068
+ new CreateNamespaceWatcherStep (selectionStrategy , intialResourceVersion ),
1069
+ getNext ());
1062
1070
1063
1071
if (configuredDomainNamespaces == null ) {
1064
- strategy = new InitializeNamespacesSecurityStep (namespacesToStart , strategy );
1072
+ strategy = new InitializeNamespacesSecurityStep (namespacesToStartNow , strategy );
1065
1073
}
1066
1074
} else {
1067
- strategy = CrdHelper . createDomainCrdStep (
1068
- version , productVersion ,
1069
- new CreateNamespaceWatcherStep ( selectionStrategy , intialResourceVersion ));
1075
+ strategy = Step . chain (
1076
+ new CreateNamespaceWatcherStep ( selectionStrategy , intialResourceVersion ) ,
1077
+ getNext ( ));
1070
1078
}
1071
1079
1072
- // Check for namespaces that are removed from the operator's
1073
- // domainNamespaces list, or that are deleted from the Kubernetes cluster.
1074
- Set <String > namespacesToStop = new TreeSet <>(namespaceStoppingMap .keySet ());
1075
- for (String ns : namespacesToStart ) {
1076
- // the active namespaces are the ones that will not be stopped
1077
- if (delegate .isNamespaceRunning (ns )) {
1078
- namespacesToStop .remove (ns );
1079
- }
1080
+ Collection <String > allDomainNamespaces = (Collection <String >) packet .get (ALL_DOMAIN_NAMESPACES );
1081
+ if (allDomainNamespaces == null ) {
1082
+ allDomainNamespaces = new HashSet <>();
1083
+ packet .put (ALL_DOMAIN_NAMESPACES , allDomainNamespaces );
1080
1084
}
1081
- stopNamespaces ( namespacesToStart , namespacesToStop );
1085
+ allDomainNamespaces . addAll ( namespacesToStartNow );
1082
1086
1083
1087
return doNext (strategy , packet );
1084
1088
}
1085
-
1086
- private Step createDomainCrdAndStartNamespaces (Collection <String > namespacesToStart , boolean isFullRecheck ) {
1087
- return CrdHelper .createDomainCrdStep (
1088
- version , productVersion ,
1089
- new StartNamespacesStep (namespacesToStart , isFullRecheck ));
1089
+
1090
+ private Step startNamespaces (Collection <String > namespacesToStart , boolean isFullRecheck ) {
1091
+ return new StartNamespacesStep (namespacesToStart , isFullRecheck );
1090
1092
}
1091
1093
1092
1094
private String getInitialResourceVersion (V1NamespaceList result ) {
@@ -1103,7 +1105,48 @@ private List<String> getExistingNamespaces(V1NamespaceList result) {
1103
1105
return namespaces ;
1104
1106
}
1105
1107
}
1106
-
1108
+
1109
+ private static class NamespaceListAfterStep extends Step {
1110
+ private final DomainNamespaceSelectionStrategy selectionStrategy ;
1111
+
1112
+ public NamespaceListAfterStep (DomainNamespaceSelectionStrategy selectionStrategy ) {
1113
+ this .selectionStrategy = selectionStrategy ;
1114
+ }
1115
+
1116
+ @ Override
1117
+ public NextAction apply (Packet packet ) {
1118
+ Collection <String > allDomainNamespaces = (Collection <String >) packet .get (ALL_DOMAIN_NAMESPACES );
1119
+ if (allDomainNamespaces == null ) {
1120
+ allDomainNamespaces = new HashSet <>();
1121
+ }
1122
+
1123
+ Collection <String > configuredDomainNamespaces = selectionStrategy .getConfiguredList ();
1124
+ if (configuredDomainNamespaces != null ) {
1125
+ for (String ns : configuredDomainNamespaces ) {
1126
+ if (!allDomainNamespaces .contains (ns )) {
1127
+ try (LoggingContext stack = LoggingContext .setThreadContext ().namespace (ns )) {
1128
+ LOGGER .warning (MessageKeys .NAMESPACE_IS_MISSING , ns );
1129
+ }
1130
+ }
1131
+ }
1132
+ }
1133
+
1134
+ // Check for namespaces that are removed from the operator's
1135
+ // domainNamespaces list, or that are deleted from the Kubernetes cluster.
1136
+ Set <String > namespacesToStop = new TreeSet <>(namespaceStoppingMap .keySet ());
1137
+ for (String ns : allDomainNamespaces ) {
1138
+ // the active namespaces are the ones that will not be stopped
1139
+ if (delegate .isNamespaceRunning (ns )) {
1140
+ namespacesToStop .remove (ns );
1141
+ }
1142
+ }
1143
+
1144
+ stopNamespaces (allDomainNamespaces , namespacesToStop );
1145
+
1146
+ return doNext (packet );
1147
+ }
1148
+ }
1149
+
1107
1150
private static class CreateNamespaceWatcherStep extends Step {
1108
1151
private final DomainNamespaceSelectionStrategy selectionStrategy ;
1109
1152
private final String initialResourceVersion ;
0 commit comments