From 05b2bbeb357d4fa03e71f2bfd5d8eeb0ea6c3f60 Mon Sep 17 00:00:00 2001 From: Billie Rinaldi Date: Mon, 27 Aug 2018 11:34:33 -0700 Subject: [PATCH] YARN-8675. Remove default hostname for docker containers when net=host. Contributed by Suma Shivaprasad --- .../runtime/DockerLinuxContainerRuntime.java | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java index 1872830533..00771ff465 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/linux/runtime/DockerLinuxContainerRuntime.java @@ -134,8 +134,8 @@ *
  • * {@code YARN_CONTAINER_RUNTIME_DOCKER_CONTAINER_HOSTNAME} sets the * hostname to be used by the Docker container. If not specified, a - * hostname will be derived from the container ID. This variable is - * ignored if the network is 'host' and Registry DNS is not enabled. + * hostname will be derived from the container ID and set as default + * hostname for networks other than 'host'. *
  • *
  • * {@code YARN_CONTAINER_RUNTIME_DOCKER_RUN_PRIVILEGED_CONTAINER} @@ -549,22 +549,34 @@ public static void validateHostname(String hostname) throws } } - /** Set a DNS friendly hostname. */ - private void setHostname(DockerRunCommand runCommand, String - containerIdStr, String name) + /** Set a DNS friendly hostname. + * Only add hostname if network is not host or if hostname is + * specified via YARN_CONTAINER_RUNTIME_DOCKER_CONTAINER_HOSTNAME + * in host network mode + */ + private void setHostname(DockerRunCommand runCommand, + String containerIdStr, String network, String name) throws ContainerExecutionException { - if (name == null || name.isEmpty()) { - name = RegistryPathUtils.encodeYarnID(containerIdStr); - String domain = conf.get(RegistryConstants.KEY_DNS_DOMAIN); - if (domain != null) { - name += ("." + domain); + if (network.equalsIgnoreCase("host")) { + if (name != null && !name.isEmpty()) { + LOG.info("setting hostname in container to: " + name); + runCommand.setHostname(name); } - validateHostname(name); - } + } else { + //get default hostname + if (name == null || name.isEmpty()) { + name = RegistryPathUtils.encodeYarnID(containerIdStr); - LOG.info("setting hostname in container to: " + name); - runCommand.setHostname(name); + String domain = conf.get(RegistryConstants.KEY_DNS_DOMAIN); + if (domain != null) { + name += ("." + domain); + } + validateHostname(name); + } + LOG.info("setting hostname in container to: " + name); + runCommand.setHostname(name); + } } /** @@ -823,12 +835,9 @@ public void launchContainer(ContainerRuntimeContext ctx) DockerRunCommand runCommand = new DockerRunCommand(containerIdStr, dockerRunAsUser, imageName) .setNetworkType(network); - // Only add hostname if network is not host or if Registry DNS is enabled. - if (!network.equalsIgnoreCase("host") || - conf.getBoolean(RegistryConstants.KEY_DNS_ENABLED, - RegistryConstants.DEFAULT_DNS_ENABLED)) { - setHostname(runCommand, containerIdStr, hostname); - } + + setHostname(runCommand, containerIdStr, network, hostname); + runCommand.setCapabilities(capabilities); runCommand.addAllReadWriteMountLocations(containerLogDirs);