diff options
author | Valerij Fredriksen <valerijf@oath.com> | 2018-08-29 23:27:31 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerij92@gmail.com> | 2018-08-29 23:38:05 +0200 |
commit | 7cb1dfb0f051e84d7e988a0fa3e10c17f51f16f8 (patch) | |
tree | 48839ba392217b6d01487f7a2e4d4f9308d0b176 /node-admin | |
parent | 1178189e38a66e20c89d399f5898848613194066 (diff) |
Convert to InetAddress without checked exception
Diffstat (limited to 'node-admin')
4 files changed, 92 insertions, 104 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImpl.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImpl.java index 9e7eaa6f8c3..ae7c94db72d 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImpl.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImpl.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.docker; +import com.google.common.net.InetAddresses; import com.yahoo.collections.Pair; import com.yahoo.config.provision.NodeType; import com.yahoo.system.ProcessExecuter; @@ -65,88 +66,85 @@ public class DockerOperationsImpl implements DockerOperations { public void createContainer(ContainerName containerName, NodeSpec node, ContainerData containerData) { PrefixLogger logger = PrefixLogger.getNodeAgentLogger(DockerOperationsImpl.class, containerName); logger.info("Creating container " + containerName); - try { - // IPv6 - Assume always valid - Inet6Address ipV6Address = environment.getIpAddresses().getIPv6Address(node.getHostname()).orElseThrow( - () -> new RuntimeException("Unable to find a valid IPv6 address for " + node.getHostname() + - ". Missing an AAAA DNS entry?")); - - String configServers = String.join(",", environment.getConfigServerHostNames()); - - Docker.CreateContainerCommand command = docker.createContainerCommand( - node.getWantedDockerImage().get(), - ContainerResources.from(node.getMinCpuCores(), node.getMinMainMemoryAvailableGb()), - containerName, - node.getHostname()) - .withManagedBy(MANAGER_NAME) - .withEnvironment("VESPA_CONFIGSERVERS", configServers) - .withEnvironment("CONTAINER_ENVIRONMENT_SETTINGS", - environment.getContainerEnvironmentResolver().createSettings(environment, node)) - .withUlimit("nofile", 262_144, 262_144) - .withUlimit("nproc", 32_768, 409_600) - .withUlimit("core", -1, -1) - .withAddCapability("SYS_PTRACE") // Needed for gcore, pstack etc. - .withAddCapability("SYS_ADMIN"); // Needed for perf - - if (environment.getNodeType() == NodeType.confighost || - environment.getNodeType() == NodeType.proxyhost) { - command.withVolume("/var/lib/sia", "/var/lib/sia"); - } - // TODO When rolling out host-admin on-prem: Always map in /var/zpe from host + make sure zpu is configured on host - if (environment.getCloud().equalsIgnoreCase("yahoo")) { - Path pathInNode = environment.pathInNodeUnderVespaHome("var/zpe"); - command.withVolume(environment.pathInHostFromPathInNode(containerName, pathInNode).toString(), pathInNode.toString()); - } else if (environment.getNodeType() == NodeType.host) { - command.withVolume("/var/zpe", environment.pathInNodeUnderVespaHome("var/zpe").toString()); - } - - if (environment.getNodeType() == NodeType.proxyhost) { - command.withVolume("/opt/yahoo/share/ssl/certs/", "/opt/yahoo/share/ssl/certs/"); - } + // IPv6 - Assume always valid + Inet6Address ipV6Address = environment.getIpAddresses().getIPv6Address(node.getHostname()).orElseThrow( + () -> new RuntimeException("Unable to find a valid IPv6 address for " + node.getHostname() + + ". Missing an AAAA DNS entry?")); + + String configServers = String.join(",", environment.getConfigServerHostNames()); + + Docker.CreateContainerCommand command = docker.createContainerCommand( + node.getWantedDockerImage().get(), + ContainerResources.from(node.getMinCpuCores(), node.getMinMainMemoryAvailableGb()), + containerName, + node.getHostname()) + .withManagedBy(MANAGER_NAME) + .withEnvironment("VESPA_CONFIGSERVERS", configServers) + .withEnvironment("CONTAINER_ENVIRONMENT_SETTINGS", + environment.getContainerEnvironmentResolver().createSettings(environment, node)) + .withUlimit("nofile", 262_144, 262_144) + .withUlimit("nproc", 32_768, 409_600) + .withUlimit("core", -1, -1) + .withAddCapability("SYS_PTRACE") // Needed for gcore, pstack etc. + .withAddCapability("SYS_ADMIN"); // Needed for perf + + if (environment.getNodeType() == NodeType.confighost || + environment.getNodeType() == NodeType.proxyhost) { + command.withVolume("/var/lib/sia", "/var/lib/sia"); + } - if (!docker.networkNATed()) { - command.withIpAddress(ipV6Address); - command.withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME); - command.withVolume("/etc/hosts", "/etc/hosts"); - } else { - InetAddress ipV6Prefix = InetAddress.getByName(IPV6_NPT_PREFIX); - InetAddress ipV6Local = IPAddresses.prefixTranslate(ipV6Address, ipV6Prefix, 8); - command.withIpAddress(ipV6Local); - - // IPv4 - Only present for some containers - Optional<InetAddress> ipV4Local = environment.getIpAddresses().getIPv4Address(node.getHostname()) - .map(ipV4Address -> { - InetAddress ipV4Prefix = uncheck(() -> InetAddress.getByName(IPV4_NPT_PREFIX)); - return IPAddresses.prefixTranslate(ipV4Address, ipV4Prefix, 2); - }); - ipV4Local.ifPresent(command::withIpAddress); - - addEtcHosts(containerData, node.getHostname(), ipV4Local, ipV6Local); - - command.withNetworkMode(DOCKER_CUSTOM_BRIDGE_NETWORK_NAME); - } + // TODO When rolling out host-admin on-prem: Always map in /var/zpe from host + make sure zpu is configured on host + if (environment.getCloud().equalsIgnoreCase("yahoo")) { + Path pathInNode = environment.pathInNodeUnderVespaHome("var/zpe"); + command.withVolume(environment.pathInHostFromPathInNode(containerName, pathInNode).toString(), pathInNode.toString()); + } else if (environment.getNodeType() == NodeType.host) { + command.withVolume("/var/zpe", environment.pathInNodeUnderVespaHome("var/zpe").toString()); + } - for (Path pathInNode : directoriesToMount.keySet()) { - String pathInHost = environment.pathInHostFromPathInNode(containerName, pathInNode).toString(); - command.withVolume(pathInHost, pathInNode.toString()); - } + if (environment.getNodeType() == NodeType.proxyhost) { + command.withVolume("/opt/yahoo/share/ssl/certs/", "/opt/yahoo/share/ssl/certs/"); + } - // TODO: Enforce disk constraints - long minMainMemoryAvailableMb = (long) (node.getMinMainMemoryAvailableGb() * 1024); - if (minMainMemoryAvailableMb > 0) { - // VESPA_TOTAL_MEMORY_MB is used to make any jdisc container think the machine - // only has this much physical memory (overrides total memory reported by `free -m`). - command.withEnvironment("VESPA_TOTAL_MEMORY_MB", Long.toString(minMainMemoryAvailableMb)); - } + if (!docker.networkNATed()) { + command.withIpAddress(ipV6Address); + command.withNetworkMode(DockerImpl.DOCKER_CUSTOM_MACVLAN_NETWORK_NAME); + command.withVolume("/etc/hosts", "/etc/hosts"); + } else { + InetAddress ipV6Prefix = InetAddresses.forString(IPV6_NPT_PREFIX); + InetAddress ipV6Local = IPAddresses.prefixTranslate(ipV6Address, ipV6Prefix, 8); + command.withIpAddress(ipV6Local); + + // IPv4 - Only present for some containers + Optional<InetAddress> ipV4Local = environment.getIpAddresses().getIPv4Address(node.getHostname()) + .map(ipV4Address -> { + InetAddress ipV4Prefix = InetAddresses.forString(IPV4_NPT_PREFIX); + return IPAddresses.prefixTranslate(ipV4Address, ipV4Prefix, 2); + }); + ipV4Local.ifPresent(command::withIpAddress); + + addEtcHosts(containerData, node.getHostname(), ipV4Local, ipV6Local); + + command.withNetworkMode(DOCKER_CUSTOM_BRIDGE_NETWORK_NAME); + } - logger.info("Creating new container with args: " + command); - command.create(); + for (Path pathInNode : directoriesToMount.keySet()) { + String pathInHost = environment.pathInHostFromPathInNode(containerName, pathInNode).toString(); + command.withVolume(pathInHost, pathInNode.toString()); + } - docker.createContainer(command); - } catch (IOException e) { - throw new RuntimeException("Failed to create container " + containerName.asString(), e); + // TODO: Enforce disk constraints + long minMainMemoryAvailableMb = (long) (node.getMinMainMemoryAvailableGb() * 1024); + if (minMainMemoryAvailableMb > 0) { + // VESPA_TOTAL_MEMORY_MB is used to make any jdisc container think the machine + // only has this much physical memory (overrides total memory reported by `free -m`). + command.withEnvironment("VESPA_TOTAL_MEMORY_MB", Long.toString(minMainMemoryAvailableMb)); } + + logger.info("Creating new container with args: " + command); + command.create(); + + docker.createContainer(command); } void addEtcHosts(ContainerData containerData, diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImplTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImplTest.java index 7ba76b592aa..e2db75eb6fb 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImplTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/docker/DockerOperationsImplTest.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.docker; +import com.google.common.net.InetAddresses; import com.yahoo.collections.Pair; import com.yahoo.system.ProcessExecuter; import com.yahoo.vespa.hosted.dockerapi.Container; @@ -16,7 +17,6 @@ import org.mockito.InOrder; import java.io.IOException; import java.net.InetAddress; -import java.net.UnknownHostException; import java.nio.file.Paths; import java.util.Optional; @@ -101,11 +101,11 @@ public class DockerOperationsImplTest { } @Test - public void verifyEtcHosts() throws UnknownHostException { + public void verifyEtcHosts() { ContainerData containerData = mock(ContainerData.class); String hostname = "hostname"; - InetAddress ipV6Local = InetAddress.getByName("::1"); - InetAddress ipV4Local = InetAddress.getByName("127.0.0.1"); + InetAddress ipV6Local = InetAddresses.forString("::1"); + InetAddress ipV4Local = InetAddresses.forString("127.0.0.1"); DockerOperationsImpl dockerOperations = new DockerOperationsImpl( docker, diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesMock.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesMock.java index 8d4108753c0..0e90373d517 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesMock.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesMock.java @@ -1,7 +1,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.network; +import com.google.common.net.InetAddresses; + import java.net.InetAddress; -import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -16,11 +17,7 @@ public class IPAddressesMock implements IPAddresses { public IPAddressesMock addAddress(String hostname, String ip) { List<InetAddress> addresses = otherAddresses.getOrDefault(hostname, new ArrayList<>()); - try { - addresses.add(InetAddress.getByName(ip)); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } + addresses.add(InetAddresses.forString(ip)); otherAddresses.put(hostname, addresses); return this; } diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesTest.java index 71f02364afa..4c4eecfa482 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/network/IPAddressesTest.java @@ -1,11 +1,12 @@ package com.yahoo.vespa.hosted.node.admin.task.util.network; -import org.junit.Assert; +import com.google.common.net.InetAddresses; import org.junit.Test; import java.net.Inet6Address; import java.net.InetAddress; -import java.net.UnknownHostException; + +import static org.junit.Assert.assertEquals; /** * @author smorgrav @@ -21,7 +22,7 @@ public class IPAddressesTest { .addAddress("localhost", "fe80::1") .addAddress("localhost", "2001::1"); - Assert.assertTrue(equals("10.0.2.2", mock.getIPv4Address("localhost").get())); + assertEquals(InetAddresses.forString("10.0.2.2"), mock.getIPv4Address("localhost").get()); } @Test @@ -31,7 +32,7 @@ public class IPAddressesTest { .addAddress("localhost", "fe80::1") .addAddress("localhost", "2001::1"); - Assert.assertTrue(equals("2001::1", mock.getIPv6Address("localhost").get())); + assertEquals(InetAddresses.forString("2001::1"), mock.getIPv6Address("localhost").get()); } @Test(expected = RuntimeException.class) @@ -50,30 +51,22 @@ public class IPAddressesTest { } @Test - public void translator_with_valid_parameters() throws UnknownHostException { + public void translator_with_valid_parameters() { // Test simplest possible address - Inet6Address original = (Inet6Address) InetAddress.getByName("2001:db8::1"); - Inet6Address prefix = (Inet6Address) InetAddress.getByName("fd00::"); + Inet6Address original = (Inet6Address) InetAddresses.forString("2001:db8::1"); + Inet6Address prefix = (Inet6Address) InetAddresses.forString("fd00::"); InetAddress translated = IPAddresses.prefixTranslate(original, prefix, 8); - Assert.assertEquals("fd00:0:0:0:0:0:0:1", translated.getHostAddress()); + assertEquals("fd00:0:0:0:0:0:0:1", translated.getHostAddress()); // Test an actual aws address we use - original = (Inet6Address) InetAddress.getByName("2600:1f16:f34:5300:ccc6:1703:b7c2:369d"); + original = (Inet6Address) InetAddresses.forString("2600:1f16:f34:5300:ccc6:1703:b7c2:369d"); translated = IPAddresses.prefixTranslate(original, prefix, 8); - Assert.assertEquals("fd00:0:0:0:ccc6:1703:b7c2:369d", translated.getHostAddress()); + assertEquals("fd00:0:0:0:ccc6:1703:b7c2:369d", translated.getHostAddress()); // Test different subnet size translated = IPAddresses.prefixTranslate(original, prefix, 6); - Assert.assertEquals("fd00:0:0:5300:ccc6:1703:b7c2:369d", translated.getHostAddress()); - } - - boolean equals(String a, InetAddress b) { - try { - return InetAddress.getByName(a).equals(b); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } + assertEquals("fd00:0:0:5300:ccc6:1703:b7c2:369d", translated.getHostAddress()); } } |