Skip to content

Commit

Permalink
bgpv1: use types.NeighborRequest in getPeerConfig
Browse files Browse the repository at this point in the history
types.NeighborRequest contains additional information which can be used
to create Peer request.

Signed-off-by: harsimran pabla <hpabla@isovalent.com>
  • Loading branch information
harsimran-pabla authored and ldelossa committed Jun 27, 2023
1 parent ae68494 commit 2ef6aa7
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions pkg/bgpv1/gobgp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/cilium/cilium/pkg/bgpv1/agent"
"github.com/cilium/cilium/pkg/bgpv1/types"
v2alpha1api "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2alpha1"
)

const (
Expand Down Expand Up @@ -105,7 +104,7 @@ func NewGoBGPServerWithConfig(ctx context.Context, log *logrus.Entry, params typ
// AddNeighbor will add the CiliumBGPNeighbor to the gobgp.BgpServer, creating
// a BGP peering connection.
func (g *GoBGPServer) AddNeighbor(ctx context.Context, n types.NeighborRequest) error {
peer, _, err := g.getPeerConfig(ctx, n.Neighbor, false)
peer, _, err := g.getPeerConfig(ctx, n, false)
if err != nil {
return err
}
Expand All @@ -120,7 +119,7 @@ func (g *GoBGPServer) AddNeighbor(ctx context.Context, n types.NeighborRequest)

// UpdateNeighbor will update the existing CiliumBGPNeighbor in the gobgp.BgpServer.
func (g *GoBGPServer) UpdateNeighbor(ctx context.Context, n types.NeighborRequest) error {
peer, needsHardReset, err := g.getPeerConfig(ctx, n.Neighbor, true)
peer, needsHardReset, err := g.getPeerConfig(ctx, n, true)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,23 +153,23 @@ func (g *GoBGPServer) UpdateNeighbor(ctx context.Context, n types.NeighborReques
}

// getPeerConfig returns GoBGP Peer configuration for the provided CiliumBGPNeighbor.
func (g *GoBGPServer) getPeerConfig(ctx context.Context, n *v2alpha1api.CiliumBGPNeighbor, isUpdate bool) (peer *gobgp.Peer, needsReset bool, err error) {
func (g *GoBGPServer) getPeerConfig(ctx context.Context, n types.NeighborRequest, isUpdate bool) (peer *gobgp.Peer, needsReset bool, err error) {
// cilium neighbor uses prefix string, gobgp neighbor uses IP string, convert.
prefix, err := netip.ParsePrefix(n.PeerAddress)
prefix, err := netip.ParsePrefix(n.Neighbor.PeerAddress)
if err != nil {
// unlikely, we validate this on CR write to k8s api.
return peer, needsReset, fmt.Errorf("failed to parse PeerAddress: %w", err)
}
peerAddr := prefix.Addr()
peerPort := uint32(*n.PeerPort)
peerPort := uint32(*n.Neighbor.PeerPort)

var existingPeer *gobgp.Peer
if isUpdate {
// If this is an update, try retrieving the existing Peer.
// This is necessary as many Peer fields are defaulted internally in GoBGP,
// and if they were not set, the update would always cause BGP peer reset.
// This will not fail if the peer is not found for whatever reason.
existingPeer, err = g.getExistingPeer(ctx, peerAddr, uint32(n.PeerASN))
existingPeer, err = g.getExistingPeer(ctx, peerAddr, uint32(n.Neighbor.PeerASN))
if err != nil {
return peer, needsReset, fmt.Errorf("failed retrieving peer: %w", err)
}
Expand All @@ -189,7 +188,7 @@ func (g *GoBGPServer) getPeerConfig(ctx context.Context, n *v2alpha1api.CiliumBG
peer = &gobgp.Peer{
Conf: &gobgp.PeerConf{
NeighborAddress: peerAddr.String(),
PeerAsn: uint32(n.PeerASN),
PeerAsn: uint32(n.Neighbor.PeerASN),
},
Transport: &gobgp.Transport{
RemotePort: peerPort,
Expand Down Expand Up @@ -221,32 +220,33 @@ func (g *GoBGPServer) getPeerConfig(ctx context.Context, n *v2alpha1api.CiliumBG
}

// Enable multi-hop for eBGP if non-zero TTL is provided
if g.asn != uint32(n.PeerASN) && *n.EBGPMultihopTTL > 1 {
if g.asn != uint32(n.Neighbor.PeerASN) && *n.Neighbor.EBGPMultihopTTL > 1 {
peer.EbgpMultihop = &gobgp.EbgpMultihop{
Enabled: true,
MultihopTtl: uint32(*n.EBGPMultihopTTL),
MultihopTtl: uint32(*n.Neighbor.EBGPMultihopTTL),
}
}

if peer.Timers == nil {
peer.Timers = &gobgp.Timers{}
}
peer.Timers.Config = &gobgp.TimersConfig{
ConnectRetry: uint64(*n.ConnectRetryTimeSeconds),
HoldTime: uint64(*n.HoldTimeSeconds),
KeepaliveInterval: uint64(*n.KeepAliveTimeSeconds),
ConnectRetry: uint64(*n.Neighbor.ConnectRetryTimeSeconds),
HoldTime: uint64(*n.Neighbor.HoldTimeSeconds),
KeepaliveInterval: uint64(*n.Neighbor.KeepAliveTimeSeconds),
IdleHoldTimeAfterReset: idleHoldTimeAfterResetSeconds,
}

// populate graceful restart config
if peer.GracefulRestart == nil {
peer.GracefulRestart = &gobgp.GracefulRestart{}
}
if n.GracefulRestart != nil && n.GracefulRestart.Enabled {
if n.Neighbor.GracefulRestart != nil && n.Neighbor.GracefulRestart.Enabled {
peer.GracefulRestart.Enabled = true
peer.GracefulRestart.RestartTime = uint32(*n.GracefulRestart.RestartTimeSeconds)
peer.GracefulRestart.RestartTime = uint32(*n.Neighbor.GracefulRestart.RestartTimeSeconds)
peer.GracefulRestart.NotificationEnabled = true
}

for _, afiConf := range peer.AfiSafis {
if afiConf.MpGracefulRestart == nil {
afiConf.MpGracefulRestart = &gobgp.MpGracefulRestart{}
Expand Down

0 comments on commit 2ef6aa7

Please sign in to comment.