From 78b684a24a97b0bc6d9cc4f8b405ac9613cc385e Mon Sep 17 00:00:00 2001 From: Chris Telfer Date: Mon, 9 Apr 2018 23:58:51 -0400 Subject: [PATCH] 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 --- libnetwork/osl/namespace_linux.go | 12 ++++++++---- libnetwork/osl/sandbox.go | 11 +++++++---- libnetwork/sandbox.go | 6 ++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libnetwork/osl/namespace_linux.go b/libnetwork/osl/namespace_linux.go index 6389215e1a..a55932babe 100644 --- a/libnetwork/osl/namespace_linux.go +++ b/libnetwork/osl/namespace_linux.go @@ -358,16 +358,20 @@ func (n *networkNamespace) loopbackUp() error { 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 { return err } 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 { return err } diff --git a/libnetwork/osl/sandbox.go b/libnetwork/osl/sandbox.go index 6ffc46775c..06149062fb 100644 --- a/libnetwork/osl/sandbox.go +++ b/libnetwork/osl/sandbox.go @@ -32,11 +32,14 @@ type Sandbox interface { // Unset the previously set default IPv6 gateway in the sandbox 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. AddStaticRoute(*types.StaticRoute) error diff --git a/libnetwork/sandbox.go b/libnetwork/sandbox.go index daa0f5e581..79e7f98d11 100644 --- a/libnetwork/sandbox.go +++ b/libnetwork/sandbox.go @@ -744,7 +744,8 @@ func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) { ep.Unlock() 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) } } @@ -862,7 +863,8 @@ func (sb *sandbox) populateNetworkResources(ep *endpoint) error { } 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 { return fmt.Errorf("failed to add virtual IP %v: %v", ep.virtualIP, err) }