diff --git a/common/src/main/java/oracle/kubernetes/common/utils/CommonUtils.java b/common/src/main/java/oracle/kubernetes/common/utils/CommonUtils.java index fd39677faf2..287f2764f9b 100644 --- a/common/src/main/java/oracle/kubernetes/common/utils/CommonUtils.java +++ b/common/src/main/java/oracle/kubernetes/common/utils/CommonUtils.java @@ -1,4 +1,4 @@ -// Copyright (c) 2022, Oracle and/or its affiliates. +// Copyright (c) 2022, 2023, Oracle and/or its affiliates. // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. package oracle.kubernetes.common.utils; @@ -6,7 +6,9 @@ import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Collection; import java.util.Optional; +import java.util.stream.Stream; import oracle.kubernetes.common.CommonConstants; @@ -21,6 +23,16 @@ private CommonUtils() { //not called } + /** + * Stream a collection with protection for null collections. + * @param Type + * @param collection Collection + * @return Stream + */ + public static Stream stream(Collection collection) { + return Optional.ofNullable(collection).stream().flatMap(Collection::stream); + } + /** * Returns the image pull policy to use by default, for the specified image. * @param imageName the image name to test diff --git a/operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java b/operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java index 607667d2d3e..ea5ea28e4d6 100644 --- a/operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java +++ b/operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java @@ -90,6 +90,7 @@ import static oracle.kubernetes.common.helpers.AuxiliaryImageEnvVars.AUXILIARY_IMAGE_MOUNT_PATH; import static oracle.kubernetes.common.logging.MessageKeys.CYCLING_POD_EVICTED; import static oracle.kubernetes.common.logging.MessageKeys.CYCLING_POD_SPEC_CHANGED; +import static oracle.kubernetes.common.utils.CommonUtils.stream; import static oracle.kubernetes.operator.DomainStatusUpdater.createKubernetesFailureSteps; import static oracle.kubernetes.operator.IntrospectorConfigMapConstants.NUM_CONFIG_MAPS; import static oracle.kubernetes.operator.KubernetesConstants.DEFAULT_EXPORTER_SIDECAR_PORT; @@ -1253,8 +1254,8 @@ private void convertAuxImagesInitContainerVolumeAndMounts(V1Pod recipe, V1Pod cu private void restoreMetricsExporterSidecarPortTcpMetrics(V1Pod recipe, V1Pod currentPod) { V1PodSpec podSpec = recipe.getSpec(); - podSpec.getContainers().stream().filter(c -> "monitoring-exporter".equals(c.getName())) - .findFirst().flatMap(c -> c.getPorts().stream().filter(p -> "metrics".equals(p.getName())) + stream(podSpec.getContainers()).filter(c -> "monitoring-exporter".equals(c.getName())) + .findFirst().flatMap(c -> stream(c.getPorts()).filter(p -> "metrics".equals(p.getName())) .findFirst()).ifPresent(p -> p.setName("tcp-metrics")); }