Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter routes to sync from same HA group #618

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions client/internal/routemanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func newClientNetworkWatcher(ctx context.Context, wgInterface *iface.WGIface, st
return client
}

func getHANetworkID(input *route.Route) string {
return input.NetID + "-" + input.Network.String()
}

func (c *clientNetwork) getRouterPeerStatuses() map[string]routerPeerStatus {
routePeerStatuses := make(map[string]routerPeerStatus)
for _, r := range c.routes {
Expand Down
4 changes: 2 additions & 2 deletions client/internal/routemanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (m *DefaultManager) UpdateRoutes(updateSerial uint64, newRoutes []*route.Ro
ownNetworkIDs := make(map[string]bool)

for _, newRoute := range newRoutes {
networkID := getHANetworkID(newRoute)
networkID := route.GetHAUniqueID(newRoute)
if newRoute.Peer == m.pubKey {
ownNetworkIDs[networkID] = true
// only linux is supported for now
Expand All @@ -164,7 +164,7 @@ func (m *DefaultManager) UpdateRoutes(updateSerial uint64, newRoutes []*route.Ro
}

for _, newRoute := range newRoutes {
networkID := getHANetworkID(newRoute)
networkID := route.GetHAUniqueID(newRoute)
if !ownNetworkIDs[networkID] {
// if prefix is too small, lets assume is a possible default route which is not yet supported
// we skip this route management
Expand Down
42 changes: 32 additions & 10 deletions management/server/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,35 @@ type UserInfo struct {
// getRoutesToSync returns the enabled routes for the peer ID and the routes
// from the ACL peers that have distribution groups associated with the peer ID
func (a *Account) getRoutesToSync(peerID string, aclPeers []*Peer) []*route.Route {
routes := a.getEnabledRoutesByPeer(peerID)
routes, peerDisabledRoutes := a.getEnabledAndDisabledRoutesByPeer(peerID)
peerRoutesMembership := make(lookupMap)
for _, r := range append(routes, peerDisabledRoutes...) {
peerRoutesMembership[route.GetHAUniqueID(r)] = struct{}{}
}

groupListMap := a.getPeerGroups(peerID)
for _, peer := range aclPeers {
activeRoutes := a.getEnabledRoutesByPeer(peer.Key)
filteredRoutes := a.filterRoutesByGroups(activeRoutes, groupListMap)
activeRoutes, _ := a.getEnabledAndDisabledRoutesByPeer(peer.Key)
groupFilteredRoutes := a.filterRoutesByGroups(activeRoutes, groupListMap)
filteredRoutes := a.filterRoutesFromPeersOfSameHAGroup(groupFilteredRoutes, peerRoutesMembership)
routes = append(routes, filteredRoutes...)
}

return routes
}

// filterRoutesByHAMembership filters and returns a list of routes that doesn't share the same HA route membership
func (a *Account) filterRoutesFromPeersOfSameHAGroup(routes []*route.Route, peerMemberships lookupMap) []*route.Route {
var filteredRoutes []*route.Route
for _, r := range routes {
_, found := peerMemberships[route.GetHAUniqueID(r)]
if !found {
filteredRoutes = append(filteredRoutes, r)
}
}
return filteredRoutes
}

// filterRoutesByGroups returns a list with routes that have distribution groups in the group's map
func (a *Account) filterRoutesByGroups(routes []*route.Route, groupListMap lookupMap) []*route.Route {
var filteredRoutes []*route.Route
Expand All @@ -166,17 +184,21 @@ func (a *Account) filterRoutesByGroups(routes []*route.Route, groupListMap looku
return filteredRoutes
}

// getEnabledRoutesByPeer returns a list of routes of a given peer
func (a *Account) getEnabledRoutesByPeer(peerPubKey string) []*route.Route {
// getEnabledAndDisabledRoutesByPeer the enabled and disabled lists of routes that belongs to a peer
func (a *Account) getEnabledAndDisabledRoutesByPeer(peerPubKey string) ([]*route.Route, []*route.Route) {
//TODO Peer.ID migration: we will need to replace search by Peer.ID here
var routes []*route.Route
var enabledRoutes []*route.Route
var disabledRoutes []*route.Route
for _, r := range a.Routes {
if r.Peer == peerPubKey && r.Enabled {
routes = append(routes, r)
continue
if r.Peer == peerPubKey {
if r.Enabled {
enabledRoutes = append(enabledRoutes, r)
continue
}
disabledRoutes = append(disabledRoutes, r)
}
}
return routes
return enabledRoutes, disabledRoutes
}

// GetRoutesByPrefix return list of routes by account and route prefix
Expand Down
18 changes: 17 additions & 1 deletion management/server/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,10 @@ func TestAccount_GetRoutesToSync(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, prefix2, err := route.ParseNetwork("192.168.0.0/24")
if err != nil {
t.Fatal(err)
}
account := &Account{
Peers: map[string]*Peer{
"peer-1": {Key: "peer-1"}, "peer-2": {Key: "peer-2"}, "peer-3": {Key: "peer-1"},
Expand All @@ -1100,6 +1104,18 @@ func TestAccount_GetRoutesToSync(t *testing.T) {
},
"route-2": {
ID: "route-2",
Network: prefix2,
NetID: "network-2",
Description: "network-2",
Peer: "peer-2",
NetworkType: 0,
Masquerade: false,
Metric: 999,
Enabled: true,
Groups: []string{"group1"},
},
"route-3": {
ID: "route-3",
Network: prefix,
NetID: "network-1",
Description: "network-1",
Expand All @@ -1120,8 +1136,8 @@ func TestAccount_GetRoutesToSync(t *testing.T) {
for _, r := range routes {
routeIDs[r.ID] = struct{}{}
}
assert.Contains(t, routeIDs, "route-1")
assert.Contains(t, routeIDs, "route-2")
assert.Contains(t, routeIDs, "route-3")

emptyRoutes := account.getRoutesToSync("peer-3", []*Peer{{Key: "peer-1"}, {Key: "peer-2"}})

Expand Down
5 changes: 5 additions & 0 deletions route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,8 @@ func compareGroupsList(list, other []string) bool {

return true
}

// GetHAUniqueID returns a highly available route ID by combining Network ID and Network range address
func GetHAUniqueID(input *Route) string {
return input.NetID + "-" + input.Network.String()
}