Skip to content

Commit

Permalink
Protect against streaming null collection of container ports
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeberhard committed Nov 2, 2023
1 parent cd84b19 commit 501b2e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// 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;

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;

Expand All @@ -21,6 +23,16 @@ private CommonUtils() {
//not called
}

/**
* Stream a collection with protection for null collections.
* @param <T> Type
* @param collection Collection
* @return Stream
*/
public static <T> Stream<T> stream(Collection<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}

Expand Down

0 comments on commit 501b2e6

Please sign in to comment.