From ab44397f6862a789962b295c4253129eeab80c4f Mon Sep 17 00:00:00 2001 From: "David K. Bainbridge" Date: Wed, 22 Jan 2014 16:36:01 -0800 Subject: [PATCH] Add support to resolve docker container names for clustering support. Currently on works when all containers are on a single docker host Change-Id: Ie141e80712e15e037d39ae788a739cb4d51db403 Signed-off-by: David K. Bainbridge --- .../internal/ClusterManager.java | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java b/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java index 83db414400..e34eb32933 100644 --- a/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java +++ b/opendaylight/clustering/services_implementation/src/main/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManager.java @@ -13,6 +13,8 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.EnumSet; import java.util.Enumeration; @@ -95,9 +97,20 @@ public class ClusterManager implements IClusterServices { InetAddress gossipRouterAddress = null; String supernodes_list = System.getProperty("supernodes", loopbackAddress); - StringBuilder sanitized_supernodes_list = new StringBuilder(); + /* + * Check the environment for the "container" variable, if this is set + * and is equal to "lxc", then ODL is running inside an lxc + * container, and address resolution of supernodes will be modified + * accordingly. + */ + boolean inContainer = "lxc".equals(System.getenv("container")); + StringBuffer sanitized_supernodes_list = new StringBuffer(); List myAddresses = new ArrayList(); + if (inContainer) { + logger.trace("DOCKER: Resolving supernode host names using docker container semantics"); + } + StringTokenizer supernodes = new StringTokenizer(supernodes_list, ":"); if (supernodes.hasMoreTokens()) { // Populate the list of my addresses @@ -131,6 +144,34 @@ public class ClusterManager implements IClusterServices { } host = host_port.nextToken(); InetAddress hostAddr; + /* + * If we are in a container and the hostname begins with a '+', this is + * an indication that we should resolve this host name in the context + * of a docker container. + * + * Specifically this means: + * '+self' : self reference and the host will be mapped to the value of + * HOSTNAME in the environment + * '+' : references another container by its name. The docker established + * environment variables will be used to resolve the host to an + * IP address. + */ + if (inContainer && host != null && host.charAt(0) == '+') { + if ("+self".equals(host)) { + host = System.getenv("HOSTNAME"); + } else { + String link = System.getenv(host.substring(1).toUpperCase() + "_PORT"); + if (link != null) { + try { + host = new URI(link).getHost(); + } catch (URISyntaxException e) { + logger.error("DOCKER: Unable to translate container reference ({}) to host IP Address, will attempt using normal host name", + host.substring(1)); + } + } + } + } + try { hostAddr = InetAddress.getByName(host); } catch (UnknownHostException ue) { -- 2.36.6