-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
269 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package bgp | ||
|
||
import ( | ||
"github.com/cloudnativelabs/kube-router/v2/pkg" | ||
gobgpapi "github.com/osrg/gobgp/v3/api" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
func PathChanged(path *gobgpapi.Path, pl PeerLister, rs pkg.RouteSyncer, tc pkg.TunnelCleaner) error { | ||
klog.V(2).Infof("Path Looks Like: %s", path.String()) | ||
dst, nextHop, err := ParsePath(path) | ||
if err != nil { | ||
return err | ||
} | ||
tunnelName := tc.GenerateTunnelName(nextHop.String()) | ||
|
||
// If we've made it this far, then it is likely that the node is holding a destination route for this path already. | ||
// If the path we've received from GoBGP is a withdrawal, we should clean up any lingering routes that may exist | ||
// on the host (rather than creating a new one or updating an existing one), and then return. | ||
if path.IsWithdraw { | ||
klog.V(2).Infof("Removing route: '%s via %s' from peer in the routing table", dst, nextHop) | ||
|
||
// The path might be withdrawn because the peer became unestablished or it may be withdrawn because just the | ||
// path was withdrawn. Check to see if the peer is still established before deciding whether to clean the | ||
// tunnel and tunnel routes or whether to just delete the destination route. | ||
peerEstablished, err := IsPeerEstablished(pl, nextHop.String()) | ||
if err != nil { | ||
klog.Errorf("encountered error while checking peer status: %v", err) | ||
} | ||
if err == nil && !peerEstablished { | ||
klog.V(1).Infof("Peer '%s' was not found any longer, removing tunnel and routes", | ||
nextHop.String()) | ||
// Also delete route from state map so that it doesn't get re-synced after deletion | ||
rs.DelInjectedRoute(dst) | ||
tc.CleanupTunnel(dst, tunnelName) | ||
return nil | ||
} | ||
|
||
// Also delete route from state map so that it doesn't get re-synced after deletion | ||
rs.DelInjectedRoute(dst) | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package bgp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
api "github.com/osrg/gobgp/v3/api" | ||
) | ||
|
||
type PeerLister interface { | ||
ListPeer(ctx context.Context, r *api.ListPeerRequest, fn func(*api.Peer)) error | ||
} | ||
|
||
func IsPeerEstablished(pl PeerLister, peerIP string) (bool, error) { | ||
var peerConnected bool | ||
peerFunc := func(peer *api.Peer) { | ||
if peer.Conf.NeighborAddress == peerIP && peer.State.SessionState == api.PeerState_ESTABLISHED { | ||
peerConnected = true | ||
} | ||
} | ||
err := pl.ListPeer(context.Background(), &api.ListPeerRequest{Address: peerIP}, peerFunc) | ||
if err != nil { | ||
return false, fmt.Errorf("unable to list peers to see if tunnel & routes need to be removed: %v", err) | ||
} | ||
|
||
return peerConnected, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.