Ver Fonte

Alias support

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal há 9 anos atrás
pai
commit
63e20c2f3d
2 ficheiros alterados com 56 adições e 4 exclusões
  1. 11 0
      libnetwork/endpoint.go
  2. 45 4
      libnetwork/sandbox.go

+ 11 - 0
libnetwork/endpoint.go

@@ -64,6 +64,7 @@ type endpoint struct {
 	prefAddress       net.IP
 	prefAddressV6     net.IP
 	ipamOptions       map[string]string
+	aliases           map[string]string
 	dbIndex           uint64
 	dbExists          bool
 	sync.Mutex
@@ -748,6 +749,16 @@ func CreateOptionDisableResolution() EndpointOption {
 	}
 }
 
+//CreateOptionAlias function returns an option setter for setting endpoint alias
+func CreateOptionAlias(name string, alias string) EndpointOption {
+	return func(ep *endpoint) {
+		if ep.aliases == nil {
+			ep.aliases = make(map[string]string)
+		}
+		ep.aliases[alias] = name
+	}
+}
+
 // JoinOptionPriority function returns an option setter for priority option to
 // be passed to the endpoint.Join() method.
 func JoinOptionPriority(ep Endpoint, prio int) EndpointOption {

+ 45 - 4
libnetwork/sandbox.go

@@ -424,26 +424,67 @@ func (sb *sandbox) ResolveName(name string) net.IP {
 	parts := strings.Split(name, ".")
 	log.Debugf("To resolve %v", parts)
 
-	for _, ep := range sb.getConnectedEndpoints() {
+	reqName := parts[0]
+	networkName := ""
+	if len(parts) > 1 {
+		networkName = parts[1]
+	}
+	epList := sb.getConnectedEndpoints()
+	// First check for local container alias
+	ip = sb.resolveName(reqName, networkName, epList, true)
+	if ip != nil {
+		return ip
+	}
+
+	// Resolve the actual container name
+	return sb.resolveName(reqName, networkName, epList, false)
+}
+
+func (sb *sandbox) resolveName(req string, networkName string, epList []*endpoint, alias bool) net.IP {
+	for _, ep := range epList {
+		name := req
 		n := ep.getNetwork()
 
-		if len(parts) > 1 && parts[1] != "" && parts[1] != n.Name() {
+		if networkName != "" && networkName != n.Name() {
 			continue
 		}
 
+		if alias {
+			if ep.aliases == nil {
+				continue
+			}
+
+			var ok bool
+			ep.Lock()
+			name, ok = ep.aliases[req]
+			ep.Unlock()
+			if !ok {
+				continue
+			}
+		} else {
+			// If it is a regular lookup and if the requested name is an alias
+			// dont perform a svc lookup for this endpoint.
+			ep.Lock()
+			if _, ok := ep.aliases[req]; ok {
+				ep.Unlock()
+				continue
+			}
+			ep.Unlock()
+		}
+
 		sr, ok := n.getController().svcDb[n.ID()]
 		if !ok {
 			continue
 		}
 
 		n.Lock()
-		ip, ok = sr.svcMap[parts[0]]
+		ip, ok := sr.svcMap[name]
 		n.Unlock()
 		if ok {
 			return ip
 		}
 	}
-	return ip
+	return nil
 }
 
 func (sb *sandbox) SetKey(basePath string) error {