Allow multiple --ignore-iface arguments

This commit is contained in:
Bryan Boreham committed 2015-07-07 12:52:29 +01:00
1 parent e621aca77e
commit 6666fd14ac
2 files changed
+35 -21

No files matched your search

+19 -16
View File
@@ -9,21 +9,13 @@ import (
// A network is considered free if it does not overlap any existing
// routes on this host. This is the same approach taken by Docker.
func CheckNetworkFree(subnet *net.IPNet, ignoreIfName string) error {
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return err
}
for _, route := range routes {
func CheckNetworkFree(subnet *net.IPNet, ignoreIfaceNames map[string]struct{}) error {
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
if route.Dst != nil && overlaps(route.Dst, subnet) {
if link, err := netlink.LinkByIndex(route.LinkIndex); err == nil && link.Attrs().Name == ignoreIfName {
continue
}
return fmt.Errorf("Network %s overlaps with existing route %s on host.", subnet, route.Dst)
}
}
return nil
return nil
})
}
// Two networks overlap if the start-point of one is inside the other.
@@ -33,17 +25,28 @@ func overlaps(n1, n2 *net.IPNet) bool {
// For a specific address, we only care if it is actually *inside* an
// existing route, because weave-local traffic never hits IP routing.
func CheckAddressOverlap(addr net.IP, ignoreIfName string) error {
func CheckAddressOverlap(addr net.IP, ignoreIfaceNames map[string]struct{}) error {
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
if route.Dst != nil && route.Dst.Contains(addr) {
return fmt.Errorf("Address %s overlaps with existing route %s on host.", addr, route.Dst)
}
return nil
})
}
func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(netlink.Route) error) error {
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
if err != nil {
return err
}
for _, route := range routes {
if route.Dst != nil && route.Dst.Contains(addr) {
if link, err := netlink.LinkByIndex(route.LinkIndex); err == nil && link.Attrs().Name == ignoreIfName {
if link, err := netlink.LinkByIndex(route.LinkIndex); err == nil {
if _, found := ignoreIfaceNames[link.Attrs().Name]; found {
continue
}
return fmt.Errorf("Address %s overlaps with existing route %s on host.", addr, route.Dst)
}
if err := check(route); err != nil {
return err
}
}
return nil