bypass netlink.LinkByIndex
since we are seeing some cases where it returns "Link not found" when the link is in fact there.
This commit is contained in:
1 parent
b5256f7b71
commit
43419815f4
1 file changed
+11
-10
+11
-10
@@ -10,7 +10,7 @@ import (
|
|||||||
// A network is considered free if it does not overlap any existing
|
// A network is considered free if it does not overlap any existing
|
||||||
// routes on this host. This is the same approach taken by Docker.
|
// routes on this host. This is the same approach taken by Docker.
|
||||||
func CheckNetworkFree(subnet *net.IPNet, ignoreIfaceNames map[string]struct{}) error {
|
func CheckNetworkFree(subnet *net.IPNet, ignoreIfaceNames map[string]struct{}) error {
|
||||||
return forEachRoute(ignoreIfaceNames, func(name string, route netlink.Route) error {
|
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
|
||||||
if route.Dst != nil && overlaps(route.Dst, subnet) {
|
if route.Dst != nil && overlaps(route.Dst, subnet) {
|
||||||
return fmt.Errorf("Network %s overlaps with existing route %s on host.", subnet, route.Dst)
|
return fmt.Errorf("Network %s overlaps with existing route %s on host.", subnet, route.Dst)
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ func overlaps(n1, n2 *net.IPNet) bool {
|
|||||||
// For a specific address, we only care if it is actually *inside* an
|
// For a specific address, we only care if it is actually *inside* an
|
||||||
// existing route, because weave-local traffic never hits IP routing.
|
// existing route, because weave-local traffic never hits IP routing.
|
||||||
func CheckAddressOverlap(addr net.IP, ignoreIfaceNames map[string]struct{}) error {
|
func CheckAddressOverlap(addr net.IP, ignoreIfaceNames map[string]struct{}) error {
|
||||||
return forEachRoute(ignoreIfaceNames, func(name string, route netlink.Route) error {
|
return forEachRoute(ignoreIfaceNames, func(route netlink.Route) error {
|
||||||
if route.Dst != nil && route.Dst.Contains(addr) {
|
if route.Dst != nil && route.Dst.Contains(addr) {
|
||||||
return fmt.Errorf("Address %s overlaps with existing route %s on host.", addr, route.Dst)
|
return fmt.Errorf("Address %s overlaps with existing route %s on host.", addr, route.Dst)
|
||||||
}
|
}
|
||||||
@@ -34,21 +34,22 @@ func CheckAddressOverlap(addr net.IP, ignoreIfaceNames map[string]struct{}) erro
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(name string, r netlink.Route) error) error {
|
func forEachRoute(ignoreIfaceNames map[string]struct{}, check func(r netlink.Route) error) error {
|
||||||
|
ignoreIfaceIndices := make(map[int]struct{})
|
||||||
|
for ifaceName := range ignoreIfaceNames {
|
||||||
|
if iface, err := net.InterfaceByName(ifaceName); err == nil {
|
||||||
|
ignoreIfaceIndices[iface.Index] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
routes, err := netlink.RouteList(nil, netlink.FAMILY_V4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
link, err := netlink.LinkByIndex(route.LinkIndex)
|
if _, found := ignoreIfaceIndices[route.LinkIndex]; found {
|
||||||
if err != nil {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ifaceName := link.Attrs().Name
|
if err := check(route); err != nil {
|
||||||
if _, found := ignoreIfaceNames[ifaceName]; found {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := check(ifaceName, route); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user