Skip to content

Commit

Permalink
Merge pull request #1877 from fcrisciani/viplo
Browse files Browse the repository at this point in the history
Add service virtual IP to sandbox's loopback address
  • Loading branch information
mavenugo committed Aug 9, 2017
2 parents 19ac3ea + ea2dcf7 commit 24bb72a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
19 changes: 0 additions & 19 deletions osl/interface_linux.go
Expand Up @@ -26,7 +26,6 @@ type nwIface struct {
mac net.HardwareAddr
address *net.IPNet
addressIPv6 *net.IPNet
ipAliases []*net.IPNet
llAddrs []*net.IPNet
routes []*net.IPNet
bridge bool
Expand Down Expand Up @@ -97,13 +96,6 @@ func (i *nwIface) LinkLocalAddresses() []*net.IPNet {
return i.llAddrs
}

func (i *nwIface) IPAliases() []*net.IPNet {
i.Lock()
defer i.Unlock()

return i.ipAliases
}

func (i *nwIface) Routes() []*net.IPNet {
i.Lock()
defer i.Unlock()
Expand Down Expand Up @@ -337,7 +329,6 @@ func configureInterface(nlh *netlink.Handle, iface netlink.Link, i *nwIface) err
{setInterfaceIPv6, fmt.Sprintf("error setting interface %q IPv6 to %v", ifaceName, i.AddressIPv6())},
{setInterfaceMaster, fmt.Sprintf("error setting interface %q master to %q", ifaceName, i.DstMaster())},
{setInterfaceLinkLocalIPs, fmt.Sprintf("error setting interface %q link local IPs to %v", ifaceName, i.LinkLocalAddresses())},
{setInterfaceIPAliases, fmt.Sprintf("error setting interface %q IP Aliases to %v", ifaceName, i.IPAliases())},
}

for _, config := range ifaceConfigurators {
Expand Down Expand Up @@ -399,16 +390,6 @@ func setInterfaceLinkLocalIPs(nlh *netlink.Handle, iface netlink.Link, i *nwIfac
return nil
}

func setInterfaceIPAliases(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
for _, si := range i.IPAliases() {
ipAddr := &netlink.Addr{IPNet: si}
if err := nlh.AddrAdd(iface, ipAddr); err != nil {
return err
}
}
return nil
}

func setInterfaceName(nlh *netlink.Handle, iface netlink.Link, i *nwIface) error {
return nlh.LinkSetName(iface, i.DstName())
}
Expand Down
16 changes: 16 additions & 0 deletions osl/namespace_linux.go
Expand Up @@ -356,6 +356,22 @@ func (n *networkNamespace) loopbackUp() error {
return n.nlHandle.LinkSetUp(iface)
}

func (n *networkNamespace) AddLoopbackAliasIP(ip *net.IPNet) error {
iface, err := n.nlHandle.LinkByName("lo")
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")
if err != nil {
return err
}
return n.nlHandle.AddrDel(iface, &netlink.Addr{IPNet: ip})
}

func (n *networkNamespace) InvokeFunc(f func()) error {
return nsInvoke(n.nsPath(), func(nsFD int) error { return nil }, func(callerFD int) error {
f()
Expand Down
6 changes: 0 additions & 6 deletions osl/options_linux.go
Expand Up @@ -66,12 +66,6 @@ func (n *networkNamespace) LinkLocalAddresses(list []*net.IPNet) IfaceOption {
}
}

func (n *networkNamespace) IPAliases(list []*net.IPNet) IfaceOption {
return func(i *nwIface) {
i.ipAliases = list
}
}

func (n *networkNamespace) Routes(routes []*net.IPNet) IfaceOption {
return func(i *nwIface) {
i.routes = routes
Expand Down
12 changes: 6 additions & 6 deletions osl/sandbox.go
Expand Up @@ -32,6 +32,12 @@ 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

// RemoveLoopbackAliasIP removes the passed IP address from the sandbox loopback interface
RemoveLoopbackAliasIP(ip *net.IPNet) error

// Add a static route to the sandbox.
AddStaticRoute(*types.StaticRoute) error

Expand Down Expand Up @@ -91,9 +97,6 @@ type IfaceOptionSetter interface {
// LinkLocalAddresses returns an option setter to set the link-local IP addresses.
LinkLocalAddresses([]*net.IPNet) IfaceOption

// IPAliases returns an option setter to set IP address Aliases
IPAliases([]*net.IPNet) IfaceOption

// Master returns an option setter to set the master interface if any for this
// interface. The master interface name should refer to the srcname of a
// previously added interface of type bridge.
Expand Down Expand Up @@ -150,9 +153,6 @@ type Interface interface {
// LinkLocalAddresses returns the link-local IP addresses assigned to the interface.
LinkLocalAddresses() []*net.IPNet

// IPAliases returns the IP address aliases assigned to the interface.
IPAliases() []*net.IPNet

// IP routes for the interface.
Routes() []*net.IPNet

Expand Down
22 changes: 14 additions & 8 deletions sandbox.go
Expand Up @@ -709,8 +709,15 @@ func releaseOSSboxResources(osSbox osl.Sandbox, ep *endpoint) {

ep.Lock()
joinInfo := ep.joinInfo
vip := ep.virtualIP
ep.Unlock()

if len(vip) != 0 {
if err := osSbox.RemoveLoopbackAliasIP(&net.IPNet{IP: vip, Mask: net.CIDRMask(32, 32)}); err != nil {
logrus.Warnf("Remove virtual IP %v failed: %v", vip, err)
}
}

if joinInfo == nil {
return
}
Expand Down Expand Up @@ -767,10 +774,6 @@ func (sb *sandbox) restoreOslSandbox() error {
if len(i.llAddrs) != 0 {
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
}
if len(ep.virtualIP) != 0 {
vipAlias := &net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)}
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().IPAliases([]*net.IPNet{vipAlias}))
}
Ifaces[fmt.Sprintf("%s+%s", i.srcName, i.dstPrefix)] = ifaceOptions
if joinInfo != nil {
routes = append(routes, joinInfo.StaticRoutes...)
Expand Down Expand Up @@ -818,10 +821,6 @@ func (sb *sandbox) populateNetworkResources(ep *endpoint) error {
if len(i.llAddrs) != 0 {
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().LinkLocalAddresses(i.llAddrs))
}
if len(ep.virtualIP) != 0 {
vipAlias := &net.IPNet{IP: ep.virtualIP, Mask: net.CIDRMask(32, 32)}
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().IPAliases([]*net.IPNet{vipAlias}))
}
if i.mac != nil {
ifaceOptions = append(ifaceOptions, sb.osSbox.InterfaceOptions().MacAddress(i.mac))
}
Expand All @@ -831,6 +830,13 @@ 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)})
if err != nil {
return fmt.Errorf("failed to add virtual IP %v: %v", ep.virtualIP, err)
}
}

if joinInfo != nil {
// Set up non-interface routes.
for _, r := range joinInfo.StaticRoutes {
Expand Down

0 comments on commit 24bb72a

Please sign in to comment.