Check specific addresses differently to networks
For a network, as used by IPAM, we care if *any* address in the range could possibly conflict with an existing address routed by the host. When given a single address, we only care if *that specific address* conflicts with an existing route.
This commit is contained in:
1 parent
6403e7a11c
commit
f05225da22
2 files changed
+25
-5
No files matched your search
+16
-1
@@ -16,7 +16,7 @@ func CheckNetworkFree(subnet *net.IPNet) error {
|
||||
}
|
||||
for _, route := range routes {
|
||||
if route.Dst != nil && overlaps(route.Dst, subnet) {
|
||||
return fmt.Errorf("network %s would overlap with route %s", subnet, route.Dst)
|
||||
return fmt.Errorf("Network %s would overlap with existing route %s", subnet, route.Dst)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -26,3 +26,18 @@ func CheckNetworkFree(subnet *net.IPNet) error {
|
||||
func overlaps(n1, n2 *net.IPNet) bool {
|
||||
return n1.Contains(n2.IP) || n2.Contains(n1.IP)
|
||||
}
|
||||
|
||||
// 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) 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) {
|
||||
return fmt.Errorf("Address %s would overlap with existing route %s", addr, route.Dst)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user