10
10
import static oracle .kubernetes .operator .VersionConstants .DEFAULT_DOMAIN_VERSION ;
11
11
import static oracle .kubernetes .operator .helpers .PodCompatibility .asSet ;
12
12
import static oracle .kubernetes .operator .helpers .PodCompatibility .getMissingElements ;
13
+ import static oracle .kubernetes .operator .helpers .PodHelper .AdminPodStepContext .INTERNAL_OPERATOR_CERT_ENV ;
13
14
14
15
import io .kubernetes .client .custom .Quantity ;
15
16
import io .kubernetes .client .models .V1Container ;
21
22
import java .lang .reflect .InvocationTargetException ;
22
23
import java .lang .reflect .Method ;
23
24
import java .util .ArrayList ;
25
+ import java .util .Arrays ;
24
26
import java .util .Collection ;
25
27
import java .util .Collections ;
26
28
import java .util .HashMap ;
@@ -162,7 +164,7 @@ static class ContainerCompatibility extends CollectiveCompatibility {
162
164
add (new EqualResources (expected .getResources (), actual .getResources ()));
163
165
addSets ("volumeMounts" , expected .getVolumeMounts (), actual .getVolumeMounts ());
164
166
addSets ("ports" , expected .getPorts (), actual .getPorts ());
165
- addSets ("env" , expected .getEnv (), actual .getEnv ());
167
+ addSetsIgnoring ("env" , expected .getEnv (), actual .getEnv (), INTERNAL_OPERATOR_CERT_ENV );
166
168
addSets ("envFrom" , expected .getEnvFrom (), actual .getEnvFrom ());
167
169
}
168
170
@@ -275,7 +277,7 @@ static <T> Set<T> asSet(Collection<T> collection) {
275
277
276
278
static <T > Set <T > getMissingElements (Collection <T > expected , Collection <T > actual ) {
277
279
Set <T > missing = asSet (expected );
278
- missing .removeAll (actual );
280
+ if ( actual != null ) missing .removeAll (actual );
279
281
return missing ;
280
282
}
281
283
}
@@ -284,6 +286,10 @@ interface CompatibilityCheck {
284
286
boolean isCompatible ();
285
287
286
288
String getIncompatibility ();
289
+
290
+ default CompatibilityCheck ignoring (String ... keys ) {
291
+ return this ;
292
+ }
287
293
}
288
294
289
295
abstract class CollectiveCompatibility implements CompatibilityCheck {
@@ -316,6 +322,11 @@ <T> void addSets(String description, List<T> expected, List<T> actual) {
316
322
add (CheckFactory .create (description , expected , actual ));
317
323
}
318
324
325
+ @ SuppressWarnings ("SameParameterValue" )
326
+ <T > void addSetsIgnoring (String description , List <T > expected , List <T > actual , String ... keys ) {
327
+ add (CheckFactory .create (description , expected , actual ).ignoring (keys ));
328
+ }
329
+
319
330
protected <T > void add (String description , T expected , T actual ) {
320
331
add (new Equality (description , expected , actual ));
321
332
}
@@ -363,7 +374,7 @@ private static <T> boolean canBeMap(List<T> list) {
363
374
}
364
375
365
376
private static <T > Map <String , T > asMap (List <T > values ) {
366
- if (values == null ) return null ;
377
+ if (values == null ) return Collections . emptyMap () ;
367
378
Map <String , T > result = new HashMap <>();
368
379
for (T value : values ) {
369
380
String key = getKey (value );
@@ -412,6 +423,7 @@ class CompatibleMaps<K, V> implements CompatibilityCheck {
412
423
private final String description ;
413
424
private final Map <K , V > expected ;
414
425
private final Map <K , V > actual ;
426
+ private List <String > ignoredKeys = new ArrayList <>();
415
427
416
428
CompatibleMaps (String description , Map <K , V > expected , Map <K , V > actual ) {
417
429
this .description = description ;
@@ -421,12 +433,22 @@ class CompatibleMaps<K, V> implements CompatibilityCheck {
421
433
422
434
@ Override
423
435
public boolean isCompatible () {
424
- for (K key : expected .keySet ())
425
- if (!actual .containsKey (key ) || !Objects .equals (expected .get (key ), actual .get (key )))
426
- return false ;
436
+ for (K key : expected .keySet ()) if (isKeyToCheck (key ) && isIncompatible (key )) return false ;
427
437
return true ;
428
438
}
429
439
440
+ private boolean isKeyToCheck (K key ) {
441
+ return !ignoredKeys .contains (key .toString ());
442
+ }
443
+
444
+ private boolean isIncompatible (K key ) {
445
+ return !actual .containsKey (key ) || valuesDiffer (key );
446
+ }
447
+
448
+ private boolean valuesDiffer (K key ) {
449
+ return !Objects .equals (expected .get (key ), actual .get (key ));
450
+ }
451
+
430
452
@ Override
431
453
public String getIncompatibility () {
432
454
StringBuilder sb = new StringBuilder ();
@@ -436,12 +458,18 @@ public String getIncompatibility() {
436
458
sb .append (String .format ("actual %s has no entry for '%s'%n" , description , missingKeys ));
437
459
438
460
for (K key : expected .keySet ())
439
- if (actual . containsKey (key ) && ! Objects . equals ( expected . get ( key ), actual . get (key ) ))
461
+ if (isKeyToCheck (key ) && actual . containsKey ( key ) && valuesDiffer (key ))
440
462
sb .append (
441
463
String .format (
442
464
"actual %s has entry '%s' with value '%s' rather than '%s'%n" ,
443
465
description , key , actual .get (key ), expected .get (key )));
444
466
445
467
return sb .toString ();
446
468
}
469
+
470
+ @ Override
471
+ public CompatibilityCheck ignoring (String ... keys ) {
472
+ ignoredKeys .addAll (Arrays .asList (keys ));
473
+ return this ;
474
+ }
447
475
}
0 commit comments