1010import static oracle .kubernetes .operator .VersionConstants .DEFAULT_DOMAIN_VERSION ;
1111import static oracle .kubernetes .operator .helpers .PodCompatibility .asSet ;
1212import static oracle .kubernetes .operator .helpers .PodCompatibility .getMissingElements ;
13+ import static oracle .kubernetes .operator .helpers .PodHelper .AdminPodStepContext .INTERNAL_OPERATOR_CERT_ENV ;
1314
1415import io .kubernetes .client .custom .Quantity ;
1516import io .kubernetes .client .models .V1Container ;
2122import java .lang .reflect .InvocationTargetException ;
2223import java .lang .reflect .Method ;
2324import java .util .ArrayList ;
25+ import java .util .Arrays ;
2426import java .util .Collection ;
2527import java .util .Collections ;
2628import java .util .HashMap ;
@@ -162,7 +164,7 @@ static class ContainerCompatibility extends CollectiveCompatibility {
162164 add (new EqualResources (expected .getResources (), actual .getResources ()));
163165 addSets ("volumeMounts" , expected .getVolumeMounts (), actual .getVolumeMounts ());
164166 addSets ("ports" , expected .getPorts (), actual .getPorts ());
165- addSets ("env" , expected .getEnv (), actual .getEnv ());
167+ addSetsIgnoring ("env" , expected .getEnv (), actual .getEnv (), INTERNAL_OPERATOR_CERT_ENV );
166168 addSets ("envFrom" , expected .getEnvFrom (), actual .getEnvFrom ());
167169 }
168170
@@ -275,7 +277,7 @@ static <T> Set<T> asSet(Collection<T> collection) {
275277
276278 static <T > Set <T > getMissingElements (Collection <T > expected , Collection <T > actual ) {
277279 Set <T > missing = asSet (expected );
278- missing .removeAll (actual );
280+ if ( actual != null ) missing .removeAll (actual );
279281 return missing ;
280282 }
281283}
@@ -284,6 +286,10 @@ interface CompatibilityCheck {
284286 boolean isCompatible ();
285287
286288 String getIncompatibility ();
289+
290+ default CompatibilityCheck ignoring (String ... keys ) {
291+ return this ;
292+ }
287293}
288294
289295abstract class CollectiveCompatibility implements CompatibilityCheck {
@@ -316,6 +322,11 @@ <T> void addSets(String description, List<T> expected, List<T> actual) {
316322 add (CheckFactory .create (description , expected , actual ));
317323 }
318324
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+
319330 protected <T > void add (String description , T expected , T actual ) {
320331 add (new Equality (description , expected , actual ));
321332 }
@@ -363,7 +374,7 @@ private static <T> boolean canBeMap(List<T> list) {
363374 }
364375
365376 private static <T > Map <String , T > asMap (List <T > values ) {
366- if (values == null ) return null ;
377+ if (values == null ) return Collections . emptyMap () ;
367378 Map <String , T > result = new HashMap <>();
368379 for (T value : values ) {
369380 String key = getKey (value );
@@ -412,6 +423,7 @@ class CompatibleMaps<K, V> implements CompatibilityCheck {
412423 private final String description ;
413424 private final Map <K , V > expected ;
414425 private final Map <K , V > actual ;
426+ private List <String > ignoredKeys = new ArrayList <>();
415427
416428 CompatibleMaps (String description , Map <K , V > expected , Map <K , V > actual ) {
417429 this .description = description ;
@@ -421,12 +433,22 @@ class CompatibleMaps<K, V> implements CompatibilityCheck {
421433
422434 @ Override
423435 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 ;
427437 return true ;
428438 }
429439
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+
430452 @ Override
431453 public String getIncompatibility () {
432454 StringBuilder sb = new StringBuilder ();
@@ -436,12 +458,18 @@ public String getIncompatibility() {
436458 sb .append (String .format ("actual %s has no entry for '%s'%n" , description , missingKeys ));
437459
438460 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 ))
440462 sb .append (
441463 String .format (
442464 "actual %s has entry '%s' with value '%s' rather than '%s'%n" ,
443465 description , key , actual .get (key ), expected .get (key )));
444466
445467 return sb .toString ();
446468 }
469+
470+ @ Override
471+ public CompatibilityCheck ignoring (String ... keys ) {
472+ ignoredKeys .addAll (Arrays .asList (keys ));
473+ return this ;
474+ }
447475}
0 commit comments