Skip to content

Commit

Permalink
network: Reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
Added a new `deleteRoutes()` called from `updateRoutes()` to reduce the
complexity of the latter and keep the linters happy.

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
  • Loading branch information
jodh-intel committed Jun 14, 2018
1 parent cdf9bce commit 5bf3909
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,34 @@ func getInterface(netHandle *netlink.Handle, link netlink.Link) (*pb.Interface,
// Routes //
////////////

func (s *sandbox) deleteRoutes(netHandle *netlink.Handle) error {
if netHandle == nil {
return errNoHandle
}

initRouteList, err := netHandle.RouteList(nil, netlink.FAMILY_ALL)
if err != nil {
return err
}
for _, initRoute := range initRouteList {
// don't delete routes associated with lo:
link, _ := netHandle.LinkByIndex(initRoute.LinkIndex)
if link.Attrs().Name == "lo" || link.Attrs().Name == "::1" {
continue
}

err = netHandle.RouteDel(&initRoute)
if err != nil {
//If there was an error deleting some of the initial routes,
//return the error and the current routes on the system via
//the defer function
return err
}
}

return nil
}

//updateRoutes will take requestedRoutes and create netlink routes, with a goal of creating a final
// state which matches the requested routes. In doing this, preesxisting non-loopback routes will be
// removed from the network. If an error occurs, this function returns the list of routes in
Expand All @@ -304,26 +332,9 @@ func (s *sandbox) updateRoutes(netHandle *netlink.Handle, requestedRoutes *pb.Ro
// is designed to be declarative, so we will attempt to create state matching what is
// requested, and in the event that we fail to do so, will return the error and final state.
//

initRouteList, err := netHandle.RouteList(nil, netlink.FAMILY_ALL)
if err != nil {
if err = s.deleteRoutes(netHandle); err != nil {
return nil, err
}
for _, initRoute := range initRouteList {
// don't delete routes associated with lo:
link, _ := netHandle.LinkByIndex(initRoute.LinkIndex)
if link.Attrs().Name == "lo" || link.Attrs().Name == "::1" {
continue
}

err = netHandle.RouteDel(&initRoute)
if err != nil {
//If there was an error deleting some of the initial routes,
//return the error and the current routes on the system via
//the defer function
return
}
}

//
// Set each of the requested routes
Expand Down

0 comments on commit 5bf3909

Please sign in to comment.