Browse Source

backend/hostgw: Improve robustness, add logging and comments

Ensure that the "desired" list of routes is kept up to date even if
there are errors
Add a few logging and comments
Tom Denham 7 years ago
parent
commit
01de65a0cf
2 changed files with 12 additions and 4 deletions
  1. 0 2
      backend/hostgw/hostgw.go
  2. 12 2
      backend/hostgw/hostgw_network.go

+ 0 - 2
backend/hostgw/hostgw.go

@@ -74,7 +74,5 @@ func (be *HostgwBackend) RegisterNetwork(ctx context.Context, config *subnet.Con
 		return nil, fmt.Errorf("failed to acquire lease: %v", err)
 	}
 
-	/* NB: docker will create the local route to `sn` */
-
 	return n, nil
 }

+ 12 - 2
backend/hostgw/hostgw_network.go

@@ -59,8 +59,11 @@ func (n *network) Run(ctx context.Context) {
 		wg.Done()
 	}()
 
+	// Store a list of routes, initialized to capacity of 10.
 	n.rl = make([]netlink.Route, 0, 10)
 	wg.Add(1)
+
+	// Start a goroutine which periodically checks that the right routes are created
 	go func() {
 		n.routeCheck(ctx)
 		wg.Done()
@@ -96,6 +99,9 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
 				LinkIndex: n.LinkIndex(),
 			}
 
+			// Always add the route to the route list.
+			n.addToRouteList(route)
+
 			// Check if route exists before attempting to add it
 			routeList, err := netlink.RouteListFiltered(netlink.FAMILY_V4, &netlink.Route{
 				Dst: route.Dst,
@@ -119,7 +125,6 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
 				log.Errorf("Error adding route to %v via %v: %v", evt.Lease.Subnet, evt.Lease.Attrs.PublicIP, err)
 				continue
 			}
-			n.addToRouteList(route)
 
 		case subnet.EventRemoved:
 			log.Info("Subnet removed: ", evt.Lease.Subnet)
@@ -134,11 +139,14 @@ func (n *network) handleSubnetEvents(batch []subnet.Event) {
 				Gw:        evt.Lease.Attrs.PublicIP.ToIP(),
 				LinkIndex: n.LinkIndex(),
 			}
+
+			// Always remove the route from the route list.
+			n.removeFromRouteList(route)
+
 			if err := netlink.RouteDel(&route); err != nil {
 				log.Errorf("Error deleting route to %v: %v", evt.Lease.Subnet, err)
 				continue
 			}
-			n.removeFromRouteList(route)
 
 		default:
 			log.Error("Internal error: unknown event type: ", int(evt.Type))
@@ -200,6 +208,8 @@ func (n *network) checkSubnetExistInRoutes() {
 				}
 			}
 		}
+	} else {
+		log.Errorf("Error fetching route list. Will automatically retry: %v", err)
 	}
 }