Skip to content

Commit 501b2e6

Browse files
committed
Protect against streaming null collection of container ports
1 parent cd84b19 commit 501b2e6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

common/src/main/java/oracle/kubernetes/common/utils/CommonUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
// Copyright (c) 2022, Oracle and/or its affiliates.
1+
// Copyright (c) 2022, 2023, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package oracle.kubernetes.common.utils;
55

66
import java.nio.charset.StandardCharsets;
77
import java.security.MessageDigest;
88
import java.security.NoSuchAlgorithmException;
9+
import java.util.Collection;
910
import java.util.Optional;
11+
import java.util.stream.Stream;
1012

1113
import oracle.kubernetes.common.CommonConstants;
1214

@@ -21,6 +23,16 @@ private CommonUtils() {
2123
//not called
2224
}
2325

26+
/**
27+
* Stream a collection with protection for null collections.
28+
* @param <T> Type
29+
* @param collection Collection
30+
* @return Stream
31+
*/
32+
public static <T> Stream<T> stream(Collection<T> collection) {
33+
return Optional.ofNullable(collection).stream().flatMap(Collection::stream);
34+
}
35+
2436
/**
2537
* Returns the image pull policy to use by default, for the specified image.
2638
* @param imageName the image name to test

operator/src/main/java/oracle/kubernetes/operator/helpers/PodStepContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
import static oracle.kubernetes.common.helpers.AuxiliaryImageEnvVars.AUXILIARY_IMAGE_MOUNT_PATH;
9191
import static oracle.kubernetes.common.logging.MessageKeys.CYCLING_POD_EVICTED;
9292
import static oracle.kubernetes.common.logging.MessageKeys.CYCLING_POD_SPEC_CHANGED;
93+
import static oracle.kubernetes.common.utils.CommonUtils.stream;
9394
import static oracle.kubernetes.operator.DomainStatusUpdater.createKubernetesFailureSteps;
9495
import static oracle.kubernetes.operator.IntrospectorConfigMapConstants.NUM_CONFIG_MAPS;
9596
import static oracle.kubernetes.operator.KubernetesConstants.DEFAULT_EXPORTER_SIDECAR_PORT;
@@ -1253,8 +1254,8 @@ private void convertAuxImagesInitContainerVolumeAndMounts(V1Pod recipe, V1Pod cu
12531254

12541255
private void restoreMetricsExporterSidecarPortTcpMetrics(V1Pod recipe, V1Pod currentPod) {
12551256
V1PodSpec podSpec = recipe.getSpec();
1256-
podSpec.getContainers().stream().filter(c -> "monitoring-exporter".equals(c.getName()))
1257-
.findFirst().flatMap(c -> c.getPorts().stream().filter(p -> "metrics".equals(p.getName()))
1257+
stream(podSpec.getContainers()).filter(c -> "monitoring-exporter".equals(c.getName()))
1258+
.findFirst().flatMap(c -> stream(c.getPorts()).filter(p -> "metrics".equals(p.getName()))
12581259
.findFirst()).ifPresent(p -> p.setName("tcp-metrics"));
12591260
}
12601261

0 commit comments

Comments
 (0)