55package oracle .kubernetes .operator .helpers ;
66
77import static oracle .kubernetes .operator .LabelConstants .forDomainUid ;
8+ import static oracle .kubernetes .operator .VersionConstants .DEFAULT_DOMAIN_VERSION ;
89
910import io .kubernetes .client .custom .IntOrString ;
1011import io .kubernetes .client .models .*;
@@ -270,55 +271,35 @@ private boolean canUseCurrentPod(V1Pod currentPod) {
270271 // Therefore, we'll just compare specific fields
271272 private static boolean isCurrentPodValid (V1Pod build , V1Pod current ) {
272273 List <String > ignoring = getVolumesToIgnore (current );
273- if (!VersionHelper .matchesResourceVersion (
274- current .getMetadata (), VersionConstants .DEFAULT_DOMAIN_VERSION )) {
275- return false ;
276- }
277-
278- if (!isRestartVersionValid (build , current )) {
279- return false ;
280- }
281-
282- if (areUnequal (
283- volumesWithout (current .getSpec ().getVolumes (), ignoring ), build .getSpec ().getVolumes ()))
284- return false ;
285-
286- if (areUnequal (current .getSpec ().getImagePullSecrets (), build .getSpec ().getImagePullSecrets ()))
287- return false ;
288274
289- if (areUnequal (getCustomerLabels (current ), getCustomerLabels (build ))) return false ;
290-
291- if (areUnequal (current .getMetadata ().getAnnotations (), build .getMetadata ().getAnnotations ()))
292- return false ;
293-
294- List <V1Container > buildContainers = build .getSpec ().getContainers ();
295- List <V1Container > currentContainers = current .getSpec ().getContainers ();
275+ return isCurrentPodMetadataValid (build .getMetadata (), current .getMetadata ())
276+ && isCurrentPodSpecValid (build .getSpec (), current .getSpec (), ignoring );
277+ }
296278
297- if (buildContainers != null ) {
298- if (currentContainers == null ) {
299- return false ;
300- }
279+ private static boolean isCurrentPodMetadataValid (V1ObjectMeta build , V1ObjectMeta current ) {
280+ return VersionHelper .matchesResourceVersion (current , DEFAULT_DOMAIN_VERSION )
281+ && isRestartVersionValid (build , current )
282+ && Objects .equals (getCustomerLabels (current ), getCustomerLabels (build ))
283+ && Objects .equals (current .getAnnotations (), build .getAnnotations ());
284+ }
301285
302- for (V1Container bc : buildContainers ) {
303- V1Container fcc = getContainerWithName (currentContainers , bc .getName ());
304- if (fcc == null ) {
305- return false ;
306- }
307- if (!fcc .getImage ().equals (bc .getImage ())
308- || !fcc .getImagePullPolicy ().equals (bc .getImagePullPolicy ())) {
309- return false ;
310- }
286+ private static boolean isCurrentPodSpecValid (
287+ V1PodSpec build , V1PodSpec current , List <String > ignoring ) {
288+ return Objects .equals (current .getSecurityContext (), build .getSecurityContext ())
289+ // && Objects.equals(current.getNodeSelector(), build.getNodeSelector())
290+ && equalSets (volumesWithout (current .getVolumes (), ignoring ), build .getVolumes ())
291+ && equalSets (current .getImagePullSecrets (), build .getImagePullSecrets ())
292+ && areCompatible (build .getContainers (), current .getContainers (), ignoring );
293+ }
311294
312- if (areUnequal (mountsWithout (fcc .getVolumeMounts (), ignoring ), bc .getVolumeMounts ()))
313- return false ;
295+ private static boolean areCompatible (
296+ List <V1Container > build , List <V1Container > current , List <String > ignoring ) {
297+ if (build != null ) {
298+ if (current == null ) return false ;
314299
315- if (areUnequal (fcc .getPorts (), bc .getPorts ())) {
316- return false ;
317- }
318- if (areUnequal (fcc .getEnv (), bc .getEnv ())) {
319- return false ;
320- }
321- if (areUnequal (fcc .getEnvFrom (), bc .getEnvFrom ())) {
300+ for (V1Container bc : build ) {
301+ V1Container fcc = getContainerWithName (current , bc .getName ());
302+ if (fcc == null || !isCompatible (bc , fcc , ignoring )) {
322303 return false ;
323304 }
324305 }
@@ -327,6 +308,33 @@ private static boolean isCurrentPodValid(V1Pod build, V1Pod current) {
327308 return true ;
328309 }
329310
311+ /**
312+ * Compares two pod spec containers for equality
313+ *
314+ * @param build the desired container model
315+ * @param current the current container, obtained from Kubernetes
316+ * @param ignoring a list of volume names to ignore
317+ * @return true if the containers are considered equal
318+ */
319+ private static boolean isCompatible (
320+ V1Container build , V1Container current , List <String > ignoring ) {
321+ return current .getImage ().equals (build .getImage ())
322+ && current .getImagePullPolicy ().equals (build .getImagePullPolicy ())
323+ && Objects .equals (current .getSecurityContext (), build .getSecurityContext ())
324+ && equalSettings (current .getLivenessProbe (), build .getLivenessProbe ())
325+ && equalSettings (current .getReadinessProbe (), build .getReadinessProbe ())
326+ && equalSets (mountsWithout (current .getVolumeMounts (), ignoring ), build .getVolumeMounts ())
327+ && equalSets (current .getPorts (), build .getPorts ())
328+ && equalSets (current .getEnv (), build .getEnv ())
329+ && equalSets (current .getEnvFrom (), build .getEnvFrom ());
330+ }
331+
332+ private static boolean equalSettings (V1Probe probe1 , V1Probe probe2 ) {
333+ return Objects .equals (probe1 .getInitialDelaySeconds (), probe2 .getInitialDelaySeconds ())
334+ && Objects .equals (probe1 .getTimeoutSeconds (), probe2 .getTimeoutSeconds ())
335+ && Objects .equals (probe1 .getPeriodSeconds (), probe2 .getPeriodSeconds ());
336+ }
337+
330338 private static List <V1Volume > volumesWithout (
331339 List <V1Volume > volumeMounts , List <String > volumesToIgnore ) {
332340 List <V1Volume > result = new ArrayList <>(volumeMounts );
@@ -363,9 +371,9 @@ private static List<V1VolumeMount> getVolumeMounts(V1Container container) {
363371 return Optional .ofNullable (container .getVolumeMounts ()).orElse (Collections .emptyList ());
364372 }
365373
366- private static Map <String , String > getCustomerLabels (V1Pod pod ) {
374+ private static Map <String , String > getCustomerLabels (V1ObjectMeta metadata ) {
367375 Map <String , String > result = new HashMap <>();
368- for (Map .Entry <String , String > entry : pod . getMetadata () .getLabels ().entrySet ())
376+ for (Map .Entry <String , String > entry : metadata .getLabels ().entrySet ())
369377 if (!isOperatorLabel (entry )) result .put (entry .getKey (), entry .getValue ());
370378 return result ;
371379 }
@@ -374,12 +382,10 @@ private static boolean isOperatorLabel(Map.Entry<String, String> label) {
374382 return label .getKey ().startsWith ("weblogic." );
375383 }
376384
377- private static boolean isRestartVersionValid (V1Pod build , V1Pod current ) {
378- V1ObjectMeta m1 = build .getMetadata ();
379- V1ObjectMeta m2 = current .getMetadata ();
380- return isLabelSame (m1 , m2 , LabelConstants .DOMAINRESTARTVERSION_LABEL )
381- && isLabelSame (m1 , m2 , LabelConstants .CLUSTERRESTARTVERSION_LABEL )
382- && isLabelSame (m1 , m2 , LabelConstants .SERVERRESTARTVERSION_LABEL );
385+ private static boolean isRestartVersionValid (V1ObjectMeta build , V1ObjectMeta current ) {
386+ return isLabelSame (build , current , LabelConstants .DOMAINRESTARTVERSION_LABEL )
387+ && isLabelSame (build , current , LabelConstants .CLUSTERRESTARTVERSION_LABEL )
388+ && isLabelSame (build , current , LabelConstants .SERVERRESTARTVERSION_LABEL );
383389 }
384390
385391 private static boolean isLabelSame (V1ObjectMeta build , V1ObjectMeta current , String labelName ) {
@@ -395,33 +401,13 @@ private static V1Container getContainerWithName(List<V1Container> containers, St
395401 return null ;
396402 }
397403
398- private static <T > boolean areUnequal (List <T > a , List <T > b ) {
399- if (a == b ) {
400- return false ;
401- }
402-
403- if (a == null ) a = Collections .emptyList ();
404- if (b == null ) b = Collections .emptyList ();
405-
406- if (a .size () != b .size ()) {
407- return true ;
408- }
409-
410- List <T > bprime = new ArrayList <>(b );
411- for (T at : a ) {
412- if (!bprime .remove (at )) {
413- return true ;
414- }
415- }
416- return false ;
417- }
418-
419- private static <K , V > boolean areUnequal (Map <K , V > a , Map <K , V > b ) {
420- return !emptyIfNull (a ).equals (emptyIfNull (b ));
404+ private static <T > boolean equalSets (List <T > first , List <T > second ) {
405+ if (first == second ) return true ;
406+ return asSet (first ).equals (asSet (second ));
421407 }
422408
423- private static <K , V > Map < K , V > emptyIfNull ( Map < K , V > map ) {
424- return map != null ? map : Collections . emptyMap ( );
409+ private static <T > Set < T > asSet ( List < T > first ) {
410+ return ( first == null ) ? Collections . emptySet () : new HashSet <>( first );
425411 }
426412
427413 private class VerifyPodStep extends Step {
@@ -595,8 +581,7 @@ protected V1ObjectMeta createMetadata() {
595581 // Add internal labels. This will overwrite any custom labels that conflict with internal
596582 // labels.
597583 metadata
598- .putLabelsItem (
599- LabelConstants .RESOURCE_VERSION_LABEL , VersionConstants .DEFAULT_DOMAIN_VERSION )
584+ .putLabelsItem (LabelConstants .RESOURCE_VERSION_LABEL , DEFAULT_DOMAIN_VERSION )
600585 .putLabelsItem (LabelConstants .DOMAINUID_LABEL , getDomainUID ())
601586 .putLabelsItem (LabelConstants .DOMAINNAME_LABEL , getDomainName ())
602587 .putLabelsItem (LabelConstants .SERVERNAME_LABEL , getServerName ())
0 commit comments