Skip to content

Commit

Permalink
Merge pull request #81 from maschmid/maschmid-pod-use-node-external-i…
Browse files Browse the repository at this point in the history
…p-if-name-not-resolvable

use node externalIP if exists and node name is not a resovable hostname
  • Loading branch information
maschmid authored Jun 1, 2018
2 parents 3b019e4 + 2cb2285 commit 932c1fd
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/cz/xtf/docker/DockerContainer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package cz.xtf.docker;

import static cz.xtf.openshift.OpenShiftUtils.admin;

import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.regex.Matcher;
Expand All @@ -16,6 +21,7 @@
import cz.xtf.TestConfiguration;
import cz.xtf.openshift.OpenshiftUtil;

import io.fabric8.kubernetes.api.model.NodeAddress;
import io.fabric8.kubernetes.api.model.Pod;

public class DockerContainer {
Expand Down Expand Up @@ -60,6 +66,17 @@ public static DockerContainer createForPod(Pod pod) {
public static DockerContainer createForPod(Pod pod, String containerLabel) {
String host = pod.getSpec().getNodeName();

try {
// attempt to treat the node name as a hostname
InetAddress.getByName(host);
} catch (UnknownHostException e) {
// try the node external address if exists
Optional<NodeAddress> nodeAddress = admin().client().nodes().withName(host).get().getStatus().getAddresses().stream().filter(addr -> "ExternalIP".equals(addr.getType())).findFirst();
if (nodeAddress.isPresent()) {
host = nodeAddress.get().getAddress();
}
}

String containerId = URI.create(pod.getStatus().getContainerStatuses().stream()
.filter(containerStats -> containerStats.getName().equals(containerLabel))
.findFirst().get().getContainerID()
Expand Down

0 comments on commit 932c1fd

Please sign in to comment.