7
7
import java .util .ArrayList ;
8
8
import java .util .Arrays ;
9
9
import java .util .Collections ;
10
+ import java .util .HashMap ;
10
11
import java .util .Iterator ;
11
12
import java .util .LinkedHashMap ;
12
13
import java .util .List ;
@@ -45,6 +46,7 @@ public class SchemaConversionUtils {
45
46
private static final String NAME = "name" ;
46
47
private static final String SPEC = "spec" ;
47
48
private static final String STATUS = "status" ;
49
+ private static final String CONDITIONS = "conditions" ;
48
50
private static final String TYPE = "type" ;
49
51
private static final String CLUSTERS = "clusters" ;
50
52
private static final String CLUSTER_NAME = "clusterName" ;
@@ -59,6 +61,7 @@ public class SchemaConversionUtils {
59
61
private static final String DOLLAR_SPEC = "$.spec" ;
60
62
private static final String DOLLAR_SPEC_SERVERPOD = "$.spec.serverPod" ;
61
63
private static final String DOLLAR_SPEC_AS_SERVERPOD = "$.spec.adminServer.serverPod" ;
64
+ private static final String DOLLAR_STATUS = "$.status" ;
62
65
63
66
public static final String INTERNAL = "Internal" ;
64
67
@@ -70,6 +73,14 @@ public class SchemaConversionUtils {
70
73
"Aborted" , INTERNAL , "TopologyMismatch" , "ReplicasTooHigh" ,
71
74
"ServerPod" , "Kubernetes" , "Introspection" , "DomainInvalid" );
72
75
76
+ private static final String PROGRESSING = "Progressing" ;
77
+ private static final String COMPLETED = "Completed" ;
78
+ private static final String AVAILABLE = "Available" ;
79
+ private static final String FAILED = "Failed" ;
80
+
81
+ private static final List <String > STATUS_CONDITION_TYPES_V8 = List .of (
82
+ PROGRESSING , AVAILABLE , "ConfigChangesPendingRestart" , FAILED );
83
+
73
84
private static final String VOLUME_MOUNTS = "volumeMounts" ;
74
85
private static final String VOLUMES = "volumes" ;
75
86
private static final String VOLUME = "volume" ;
@@ -139,6 +150,13 @@ public Resources convertDomainSchema(Map<String, Object> domain, ResourceLookup
139
150
try {
140
151
Map <String , Object > toBePreserved = new TreeMap <>();
141
152
removeAndPreserveLogHomeLayout (spec , toBePreserved );
153
+ Map <String , Object > status = getStatus (domain );
154
+ removeAndPreserveConditionsV9 (status , toBePreserved );
155
+ Optional .ofNullable (status ).ifPresent (s -> {
156
+ if (status .isEmpty ()) {
157
+ domain .remove (STATUS );
158
+ }
159
+ });
142
160
143
161
preserveV9 (PRESERVED_V9 , domain , toBePreserved , apiVersion );
144
162
} catch (IOException io ) {
@@ -301,39 +319,45 @@ private void convertDomainStatusTargetV9(Map<String, Object> domain) {
301
319
}
302
320
303
321
private void convertCompletedToProgressing (Map <String , Object > domain ) {
304
- Iterator <Map <String , String >> conditions = getStatusConditions (domain ).iterator ();
305
- while (conditions .hasNext ()) {
306
- Map <String , String > condition = conditions .next ();
307
- if ("Completed" .equals (condition .get (TYPE ))) {
308
- if ("False" .equals (condition .get (STATUS ))) {
309
- condition .put (TYPE , "Progressing" );
310
- condition .put (STATUS , "True" );
311
- } else {
312
- conditions .remove ();
313
- }
314
- }
315
- }
322
+ convertCondition (domain , COMPLETED , "False" , PROGRESSING , "True" );
316
323
}
317
324
318
325
private void convertProgressingToCompleted (Map <String , Object > domain ) {
319
- Iterator <Map <String , String >> conditions = getStatusConditions (domain ).iterator ();
320
- while (conditions .hasNext ()) {
321
- Map <String , String > condition = conditions .next ();
322
- if ("Progressing" .equals (condition .get (TYPE ))) {
323
- if ("True" .equals (condition .get (STATUS ))) {
324
- condition .put (TYPE , "Completed" );
325
- condition .put (STATUS , "False" );
326
- } else {
327
- conditions .remove ();
326
+ convertCondition (domain , PROGRESSING , "True" , COMPLETED , "False" );
327
+ }
328
+
329
+ private void convertCondition (Map <String , Object > domain ,
330
+ String type , String expectedStatus , String repType , String repStatus ) {
331
+ Map <String , Object > status = getStatus (domain );
332
+ Optional .ofNullable (status ).ifPresent (s -> {
333
+ List <Map <String , String >> conditions = (List <Map <String , String >>) status .get (CONDITIONS );
334
+ Optional .ofNullable (conditions ).ifPresent (c -> {
335
+ Iterator <Map <String , String >> it = conditions .iterator ();
336
+ while (it .hasNext ()) {
337
+ Map <String , String > condition = it .next ();
338
+ if (type .equals (condition .get (TYPE ))) {
339
+ if (expectedStatus .equals (condition .get (STATUS ))) {
340
+ condition .put (TYPE , repType );
341
+ condition .put (STATUS , repStatus );
342
+ } else {
343
+ it .remove ();
344
+ }
345
+ }
328
346
}
329
- }
330
- }
347
+ if (conditions .isEmpty ()) {
348
+ status .remove (CONDITIONS );
349
+ }
350
+ if (status .isEmpty ()) {
351
+ domain .remove (STATUS );
352
+ }
353
+ });
354
+ });
331
355
}
332
356
333
357
@ Nonnull
334
358
private List <Map <String ,String >> getStatusConditions (Map <String , Object > domain ) {
335
359
return (List <Map <String ,String >>) Optional .ofNullable (getStatus (domain ))
336
- .map (status -> status .get ("conditions" ))
360
+ .map (status -> status .get (CONDITIONS ))
337
361
.orElse (Collections .emptyList ());
338
362
}
339
363
@@ -346,7 +370,7 @@ private void renameUnsupportedDomainStatusAvailableConditionReasonV8ToV9(Map<Str
346
370
}
347
371
348
372
private void renameFailedReasonIfUnsupported (Map <String , Object > domain , Map <String , String > condition ) {
349
- if ("Failed" .equals (condition .get (TYPE ))) {
373
+ if (FAILED .equals (condition .get (TYPE ))) {
350
374
String currentReason = condition .get (REASON );
351
375
if (isUnsupportedReason (currentReason )) {
352
376
Map <String , Object > meta = getMetadata (domain );
@@ -359,7 +383,7 @@ private void renameFailedReasonIfUnsupported(Map<String, Object> domain, Map<Str
359
383
}
360
384
361
385
private void renameAvailableReasonIfUnsupported (Map <String , Object > domain , Map <String , String > condition ) {
362
- if ("Available" .equals (condition .get (TYPE ))) {
386
+ if (AVAILABLE .equals (condition .get (TYPE ))) {
363
387
String currentReason = condition .get (REASON );
364
388
if (currentReason != null && isUnsupportedReason (currentReason )) {
365
389
Map <String , Object > meta = getMetadata (domain );
@@ -388,13 +412,13 @@ private void renameUnsupportedDomainStatusAvailableConditionReasonV9ToV8(Map<Str
388
412
}
389
413
390
414
private void restoreFailedReason (Map <String , String > condition , String reason ) {
391
- if ("Failed" .equals (condition .get (TYPE ))) {
415
+ if (FAILED .equals (condition .get (TYPE ))) {
392
416
condition .put (REASON , reason );
393
417
}
394
418
}
395
419
396
420
private void restoreAvailableReason (Map <String , String > condition , String reason ) {
397
- if ("Available" .equals (condition .get (TYPE ))) {
421
+ if (AVAILABLE .equals (condition .get (TYPE ))) {
398
422
condition .put (REASON , reason );
399
423
}
400
424
}
@@ -776,10 +800,34 @@ private void adjustLogHomeLayoutDefault(Map<String, Object> spec, String apiVers
776
800
private void removeAndPreserveLogHomeLayout (Map <String , Object > spec , Map <String , Object > toBePreserved ) {
777
801
Object existing = Optional .ofNullable (spec .remove (LHL )).orElse ("ByServers" );
778
802
if (!"Flat" .equals (existing )) {
779
- preserve (toBePreserved , "$.spec" , Map .of (LHL , existing ));
803
+ preserve (toBePreserved , DOLLAR_SPEC , Map .of (LHL , existing ));
780
804
}
781
805
}
782
806
807
+ private void removeAndPreserveConditionsV9 (Map <String , Object > status , Map <String , Object > toBePreserved ) {
808
+ Optional .ofNullable (status ).ifPresent (s -> {
809
+ List <Map <String , String >> conditions = (List <Map <String , String >>) status .get (CONDITIONS );
810
+ List <Map <String , String >> removed = new ArrayList <>();
811
+ if (conditions != null ) {
812
+ List <Map <String , String >> filteredConditions = conditions .stream ().filter (cond -> {
813
+ if (!STATUS_CONDITION_TYPES_V8 .contains (cond .get (TYPE ))) {
814
+ removed .add (cond );
815
+ return false ;
816
+ }
817
+ return true ;
818
+ }).collect (Collectors .toList ());
819
+ if (filteredConditions .isEmpty ()) {
820
+ status .remove (CONDITIONS );
821
+ } else {
822
+ status .put (CONDITIONS , filteredConditions );
823
+ }
824
+ }
825
+ if (!removed .isEmpty ()) {
826
+ preserve (toBePreserved , DOLLAR_STATUS , Map .of (CONDITIONS , removed ));
827
+ }
828
+ });
829
+ }
830
+
783
831
private void removeAndPreserveIstio (Map <String , Object > spec , Map <String , Object > toBePreserved ) {
784
832
Optional .ofNullable (getConfiguration (spec )).ifPresent (configuration -> {
785
833
Object existing = configuration .remove ("istio" );
@@ -904,7 +952,7 @@ private void removeAddedAdminChannelPortForwardingEnabled(Map<String, Object> do
904
952
905
953
@ FunctionalInterface
906
954
interface RestoreValidator {
907
- public boolean validateRestore (Map <String , Object > domain , Map <String , Object > scope , Map <String , Object > value );
955
+ boolean validateRestore (Map <String , Object > domain , Map <String , Object > scope , Map <String , Object > value );
908
956
}
909
957
910
958
@ SuppressWarnings ("java:S112" )
@@ -928,6 +976,9 @@ private void restore(Map<String, Object> domain, Map<String, Object> toBeRestore
928
976
if (toBeRestored != null && !toBeRestored .isEmpty ()) {
929
977
ReadContext context = JsonPath .parse (domain );
930
978
toBeRestored .forEach ((key , value ) -> {
979
+ if (DOLLAR_STATUS .equals (key ) && getStatus (domain ) == null ) {
980
+ domain .put (STATUS , new HashMap <>());
981
+ }
931
982
JsonPath path = JsonPath .compile (key );
932
983
Optional .of (read (context , path )).map (List ::stream )
933
984
.ifPresent (stream -> stream .forEach (item -> {
0 commit comments