8
8
import static oracle .kubernetes .operator .LabelConstants .DOMAINRESTARTVERSION_LABEL ;
9
9
import static oracle .kubernetes .operator .LabelConstants .SERVERRESTARTVERSION_LABEL ;
10
10
import static oracle .kubernetes .operator .VersionConstants .DEFAULT_DOMAIN_VERSION ;
11
+ import static oracle .kubernetes .operator .helpers .PodCompatibility .asSet ;
12
+ import static oracle .kubernetes .operator .helpers .PodCompatibility .getMissingElements ;
11
13
12
14
import io .kubernetes .client .custom .Quantity ;
13
15
import io .kubernetes .client .models .V1Container ;
31
33
/** A class which defines the compatability rules for existing vs. specified pods. */
32
34
class PodCompatibility extends CollectiveCompatibility {
33
35
PodCompatibility (V1Pod expected , V1Pod actual ) {
36
+ add ("sha256Hash" , AnnotationHelper .getHash (expected ), AnnotationHelper .getHash (actual ));
34
37
add (new PodMetadataCompatibility (expected .getMetadata (), actual .getMetadata ()));
35
38
add (new PodSpecCompatibility (expected .getSpec (), actual .getSpec ()));
36
39
}
@@ -101,7 +104,9 @@ static class PodSpecCompatibility extends CollectiveCompatibility {
101
104
102
105
PodSpecCompatibility (V1PodSpec expected , V1PodSpec actual ) {
103
106
add ("securityContext" , expected .getSecurityContext (), actual .getSecurityContext ());
104
- add (new EqualsMaps <>("nodeSelector" , expected .getNodeSelector (), actual .getNodeSelector ()));
107
+ add (
108
+ new CompatibleMaps <>(
109
+ "nodeSelector" , expected .getNodeSelector (), actual .getNodeSelector ()));
105
110
addSets ("volumes" , expected .getVolumes (), actual .getVolumes ());
106
111
addSets ("imagePullSecrets" , expected .getImagePullSecrets (), actual .getImagePullSecrets ());
107
112
addContainerChecks (expected .getContainers (), actual .getContainers ());
@@ -188,28 +193,6 @@ public String getIncompatibility() {
188
193
}
189
194
}
190
195
191
- static class EqualsMaps <K , V > implements CompatibilityCheck {
192
- private final String description ;
193
- private final Map <K , V > expected ;
194
- private final Map <K , V > actual ;
195
-
196
- EqualsMaps (String description , Map <K , V > expected , Map <K , V > actual ) {
197
- this .description = description ;
198
- this .expected = expected ;
199
- this .actual = actual ;
200
- }
201
-
202
- @ Override
203
- public boolean isCompatible () {
204
- return KubernetesUtils .mapEquals (expected , actual );
205
- }
206
-
207
- @ Override
208
- public String getIncompatibility () {
209
- return description + " expected: " + expected + " but was: " + actual ;
210
- }
211
- }
212
-
213
196
static class Probes implements CompatibilityCheck {
214
197
private final String description ;
215
198
private final V1Probe expected ;
@@ -283,6 +266,16 @@ public String getIncompatibility() {
283
266
return sb .toString ();
284
267
}
285
268
}
269
+
270
+ static <T > Set <T > asSet (Collection <T > collection ) {
271
+ return (collection == null ) ? Collections .emptySet () : new HashSet <>(collection );
272
+ }
273
+
274
+ static <T > Set <T > getMissingElements (Collection <T > expected , Collection <T > actual ) {
275
+ Set <T > missing = asSet (expected );
276
+ missing .removeAll (actual );
277
+ return missing ;
278
+ }
286
279
}
287
280
288
281
interface CompatibilityCheck {
@@ -318,7 +311,7 @@ public String getIncompatibility() {
318
311
}
319
312
320
313
<T > void addSets (String description , List <T > expected , List <T > actual ) {
321
- add (new EqualSets <>(description , expected , actual ));
314
+ add (new CompatibleSets <>(description , expected , actual ));
322
315
}
323
316
324
317
protected <T > void add (String description , T expected , T actual ) {
@@ -356,12 +349,12 @@ public String getIncompatibility() {
356
349
}
357
350
}
358
351
359
- class EqualSets <T > implements CompatibilityCheck {
352
+ class CompatibleSets <T > implements CompatibilityCheck {
360
353
private String description ;
361
354
private final Collection <T > expected ;
362
355
private final Collection <T > actual ;
363
356
364
- EqualSets (String description , Collection <T > expected , Collection <T > actual ) {
357
+ CompatibleSets (String description , Collection <T > expected , Collection <T > actual ) {
365
358
this .description = description ;
366
359
this .expected = expected ;
367
360
this .actual = actual ;
@@ -370,15 +363,50 @@ class EqualSets<T> implements CompatibilityCheck {
370
363
@ Override
371
364
public boolean isCompatible () {
372
365
if (expected == actual ) return true ;
373
- return asSet (expected ). equals (asSet (actual ));
366
+ return asSet (actual ). containsAll (asSet (expected ));
374
367
}
375
368
376
- private static <T > Set <T > asSet (Collection <T > collection ) {
377
- return (collection == null ) ? Collections .emptySet () : new HashSet <>(collection );
369
+ @ Override
370
+ public String getIncompatibility () {
371
+ return String .format (
372
+ "actual %s does not have %s" , description , getMissingElements (expected , actual ));
373
+ }
374
+ }
375
+
376
+ class CompatibleMaps <K , V > implements CompatibilityCheck {
377
+ private final String description ;
378
+ private final Map <K , V > expected ;
379
+ private final Map <K , V > actual ;
380
+
381
+ CompatibleMaps (String description , Map <K , V > expected , Map <K , V > actual ) {
382
+ this .description = description ;
383
+ this .expected = expected ;
384
+ this .actual = actual ;
385
+ }
386
+
387
+ @ Override
388
+ public boolean isCompatible () {
389
+ for (K key : expected .keySet ())
390
+ if (!actual .containsKey (key ) || !Objects .equals (expected .get (key ), actual .get (key )))
391
+ return false ;
392
+ return true ;
378
393
}
379
394
380
395
@ Override
381
396
public String getIncompatibility () {
382
- return String .format ("%s expected: %s but was: %s" , description , expected , actual );
397
+ StringBuilder sb = new StringBuilder ();
398
+
399
+ Set <K > missingKeys = getMissingElements (expected .keySet (), actual .keySet ());
400
+ if (!missingKeys .isEmpty ())
401
+ sb .append (String .format ("%s has no entry for %s%n" , description , missingKeys ));
402
+
403
+ for (K key : expected .keySet ())
404
+ if (actual .containsKey (key ) && !Objects .equals (expected .get (key ), actual .get (key )))
405
+ sb .append (
406
+ String .format (
407
+ "%s has entry %s with value %s rather than %s%n" ,
408
+ description , key , actual .get (key ), expected .get (key )));
409
+
410
+ return sb .toString ();
383
411
}
384
412
}
0 commit comments