From cd0b6bac4ce1c341cd921fb82bd2b151a9f184b7 Mon Sep 17 00:00:00 2001 From: Santhosh Manohar Date: Wed, 10 Aug 2016 17:44:33 -0700 Subject: [PATCH] For service name DNS resolution prioritize IP on user overlay network Signed-off-by: Santhosh Manohar --- libnetwork/sandbox.go | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index a0667b46aa..ef082f2380 100644 --- a/libnetwork/sandbox.go +++ b/libnetwork/sandbox.go @@ -498,6 +498,38 @@ func (sb *sandbox) ResolveService(name string) ([]*net.SRV, []net.IP, error) { return srv, ip, nil } +func getDynamicNwEndpoints(epList []*endpoint) []*endpoint { + eps := []*endpoint{} + for _, ep := range epList { + n := ep.getNetwork() + if n.dynamic && !n.ingress { + eps = append(eps, ep) + } + } + return eps +} + +func getIngressNwEndpoint(epList []*endpoint) *endpoint { + for _, ep := range epList { + n := ep.getNetwork() + if n.ingress { + return ep + } + } + return nil +} + +func getLocalNwEndpoints(epList []*endpoint) []*endpoint { + eps := []*endpoint{} + for _, ep := range epList { + n := ep.getNetwork() + if !n.dynamic && !n.ingress { + eps = append(eps, ep) + } + } + return eps +} + func (sb *sandbox) ResolveName(name string, ipType int) ([]net.IP, bool) { // Embedded server owns the docker network domain. Resolution should work // for both container_name and container_name.network_name @@ -528,6 +560,18 @@ func (sb *sandbox) ResolveName(name string, ipType int) ([]net.IP, bool) { } epList := sb.getConnectedEndpoints() + + // In swarm mode services with exposed ports are connected to user overlay + // network, ingress network and docker_gwbridge network. Name resolution + // should prioritize returning the VIP/IPs on user overlay network. + newList := []*endpoint{} + if !sb.controller.isDistributedControl() { + newList = append(newList, getDynamicNwEndpoints(epList)...) + newList = append(newList, getIngressNwEndpoint(epList)) + newList = append(newList, getLocalNwEndpoints(epList)...) + epList = newList + } + for i := 0; i < len(reqName); i++ { // First check for local container alias