Skip to content

Commit

Permalink
Merge pull request #807 from tomdee/host-gw-tidy
Browse files Browse the repository at this point in the history
backend/hostgw: Improve robustness, add logging and comments
  • Loading branch information
gunjan5 authored Sep 6, 2017
2 parents cde204d + 01de65a commit c1b6a06
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 0 additions & 2 deletions backend/hostgw/hostgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 12 additions & 2 deletions backend/hostgw/hostgw_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -200,6 +208,8 @@ func (n *network) checkSubnetExistInRoutes() {
}
}
}
} else {
log.Errorf("Error fetching route list. Will automatically retry: %v", err)
}
}

Expand Down

0 comments on commit c1b6a06

Please sign in to comment.