pkg/route: account for interface churn

When interfaces on the host churn, the kernel will remove routes
associated with those interfaces. This could cause the Kilo route
controller to become out of sync with the routes that really exist. This
commit fixes this behavior.
This commit is contained in:
Lucas Servén Marín 2019-05-07 12:05:40 +02:00
parent 02bd5fa6c0
commit c65627dab0
No known key found for this signature in database
GPG Key ID: 586FEAF680DA74AD
1 changed files with 16 additions and 1 deletions

View File

@ -139,8 +139,23 @@ func (t *Table) Set(routes []*netlink.Route) error {
delete(t.routes, k)
}
}
// When adding routes, we need to compare against what is
// actually on the Linux routing table. This is because
// routes can be deleted by the kernel due to interface churn
// causing a situation where the controller thinks it has a route
// that is not actually there.
existing := make(map[string]*netlink.Route)
existingRoutes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
if err != nil {
return fmt.Errorf("failed to list existing routes: %v", err)
}
for k := range existingRoutes {
existing[routeToString(&existingRoutes[k])] = &existingRoutes[k]
}
for k := range r {
if _, ok := t.routes[k]; !ok {
if _, ok := existing[k]; !ok {
if err := t.add(r[k]); err != nil {
return fmt.Errorf("failed to add route %q: %v", routeToString(r[k]), err)
}