瀏覽代碼

Add ability to alias any interface in a sanbox

New load balancing code will require ability to add aliases to
load-balncer sandboxes.  So this broadens the OSL interface to allow
adding aliases to any interface, along with the facility to get the
loopback interface's name based on the OS.

Signed-off-by: Chris Telfer <ctelfer@docker.com>
Chris Telfer 7 年之前
父節點
當前提交
78b684a24a
共有 3 個文件被更改,包括 19 次插入10 次删除
  1. 8 4
      libnetwork/osl/namespace_linux.go
  2. 7 4
      libnetwork/osl/sandbox.go
  3. 4 2
      libnetwork/sandbox.go

+ 8 - 4
libnetwork/osl/namespace_linux.go

@@ -358,16 +358,20 @@ func (n *networkNamespace) loopbackUp() error {
 	return n.nlHandle.LinkSetUp(iface)
 	return n.nlHandle.LinkSetUp(iface)
 }
 }
 
 
-func (n *networkNamespace) AddLoopbackAliasIP(ip *net.IPNet) error {
-	iface, err := n.nlHandle.LinkByName("lo")
+func (n *networkNamespace) GetLoopbackIfaceName() string {
+	return "lo"
+}
+
+func (n *networkNamespace) AddAliasIP(ifName string, ip *net.IPNet) error {
+	iface, err := n.nlHandle.LinkByName(ifName)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 	return n.nlHandle.AddrAdd(iface, &netlink.Addr{IPNet: ip})
 	return n.nlHandle.AddrAdd(iface, &netlink.Addr{IPNet: ip})
 }
 }
 
 
-func (n *networkNamespace) RemoveLoopbackAliasIP(ip *net.IPNet) error {
-	iface, err := n.nlHandle.LinkByName("lo")
+func (n *networkNamespace) RemoveAliasIP(ifName string, ip *net.IPNet) error {
+	iface, err := n.nlHandle.LinkByName(ifName)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}

+ 7 - 4
libnetwork/osl/sandbox.go

@@ -32,11 +32,14 @@ type Sandbox interface {
 	// Unset the previously set default IPv6 gateway in the sandbox
 	// Unset the previously set default IPv6 gateway in the sandbox
 	UnsetGatewayIPv6() error
 	UnsetGatewayIPv6() error
 
 
-	// AddLoopbackAliasIP adds the passed IP address to the sandbox loopback interface
-	AddLoopbackAliasIP(ip *net.IPNet) error
+	// GetLoopbackIfaceName returns the name of the loopback interface
+	GetLoopbackIfaceName() string
 
 
-	// RemoveLoopbackAliasIP removes the passed IP address from the sandbox loopback interface
-	RemoveLoopbackAliasIP(ip *net.IPNet) error
+	// AddAliasIP adds the passed IP address to the named interface
+	AddAliasIP(ifName string, ip *net.IPNet) error
+
+	// RemoveAliasIP removes the passed IP address from the named interface
+	RemoveAliasIP(ifName string, ip *net.IPNet) error
 
 
 	// Add a static route to the sandbox.
 	// Add a static route to the sandbox.
 	AddStaticRoute(*types.StaticRoute) error
 	AddStaticRoute(*types.StaticRoute) error

+ 4 - 2
libnetwork/sandbox.go

@@ -744,7 +744,8 @@ func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {
 	ep.Unlock()
 	ep.Unlock()
 
 
 	if len(vip) != 0 {
 	if len(vip) != 0 {
-		if err := osSbox.RemoveLoopbackAliasIP(&net.IPNet{IP: vip, Mask: net.CIDRMask(32, 32)}); err != nil {
+		loopName := osSbox.GetLoopbackIfaceName()
+		if err := osSbox.RemoveAliasIP(loopName, &net.IPNet{IP: vip, Mask: net.CIDRMask(32, 32)}); err != nil {
 			logrus.Warnf("Remove virtual IP %v failed: %v", vip, err)
 			logrus.Warnf("Remove virtual IP %v failed: %v", vip, err)
 		}
 		}
 	}
 	}
@@ -862,7 +863,8 @@ func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
 	}
 	}
 
 
 	if len(ep.virtualIP) != 0 {
 	if len(ep.virtualIP) != 0 {
-		err := sb.osSbox.AddLoopbackAliasIP(&net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)})
+		loopName := sb.osSbox.GetLoopbackIfaceName()
+		err := sb.osSbox.AddAliasIP(loopName, &net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)})
 		if err != nil {
 		if err != nil {
 			return fmt.Errorf("failed to add virtual IP %v: %v", ep.virtualIP, err)
 			return fmt.Errorf("failed to add virtual IP %v: %v", ep.virtualIP, err)
 		}
 		}