f-41: config, frr: base BGP pilotée par l'agent au démarrage #41

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-11 19:52:46 +02:00
commit bf0e83862f
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
12 changed files with 331 additions and 18 deletions

View file

@ -22,9 +22,26 @@ func (c *Client) ApplyBase(b Base) error {
if err := b.validate(); err != nil {
return err
}
running, err := c.runningConfig()
if err != nil {
return err
}
stale, err := b.staleReflectors(running)
if err != nil {
return err
}
if err := c.configure(b.commands()...); err != nil {
return err
}
if len(stale) > 0 {
cmds := []string{fmt.Sprintf("router bgp %d", b.ASN)}
for _, rr := range stale {
cmds = append(cmds, "no neighbor "+rr)
}
if err := c.configure(append(cmds, "exit")...); err != nil {
return err
}
}
return c.save()
}

View file

@ -79,17 +79,6 @@ func TestApplyVRFRefusesAnInvalidVRFWithoutCallingVtysh(t *testing.T) {
}
}
func TestApplyBaseConfiguresThenSaves(t *testing.T) {
f := &fakeVtysh{}
b := Base{ASN: 64600, RouterID: labVRF(t).VTEP, RouteReflectors: []net.IP{net.ParseIP("10.255.255.1")}}
if err := f.client().ApplyBase(b); err != nil {
t.Fatal(err)
}
if len(f.calls) != 2 || f.calls[0][0] != "configure terminal" || f.calls[0][1] != "router bgp 64600" || f.calls[1][0] != "write memory" {
t.Errorf("appels : %q", f.calls)
}
}
func TestExecReportsAnErrorPrintedByVtysh(t *testing.T) {
f := &fakeVtysh{fail: map[string]string{"vni 4001": "% Please unconfigure l3vni 4001\n"}}
err := f.client().ApplyVRF(labVRF(t))
@ -182,3 +171,81 @@ func TestTeardownNeedsOnlyTheASNAndTheID(t *testing.T) {
t.Errorf("démontage sans id : erreur %v, appels %q", err, f.calls)
}
}
const baseRunning = `frr version 10.7.1
router bgp 64600
bgp router-id 192.168.14.13
neighbor fabric peer-group
neighbor fabric remote-as 64600
neighbor 10.255.255.1 peer-group fabric
neighbor 10.255.255.9 peer-group fabric
neighbor 10.0.0.5 peer-group monitoring
!
address-family l2vpn evpn
neighbor fabric activate
advertise-all-vni
exit-address-family
exit
!
router bgp 64600 vrf vrf-a1b2
neighbor 10.255.255.7 peer-group fabric
exit
`
func labBase() Base {
return Base{ASN: 64600, RouterID: net.ParseIP("192.168.14.13"), RouteReflectors: []net.IP{net.ParseIP("10.255.255.1")}}
}
func TestApplyBaseOnAFreshFRRConfiguresThenSaves(t *testing.T) {
f := &fakeVtysh{running: []string{""}}
if err := f.client().ApplyBase(labBase()); err != nil {
t.Fatal(err)
}
want := [][]string{
{"show running-config"},
append([]string{"configure terminal"}, labBase().commands()...),
{"write memory"},
}
if !reflect.DeepEqual(f.calls, want) {
t.Errorf("appels :\n%q\nattendu :\n%q", f.calls, want)
}
}
func TestApplyBaseRemovesReflectorsNoLongerConfiguredAfterAddingTheNewOnes(t *testing.T) {
f := &fakeVtysh{running: []string{baseRunning}}
if err := f.client().ApplyBase(labBase()); err != nil {
t.Fatal(err)
}
want := [][]string{
{"show running-config"},
append([]string{"configure terminal"}, labBase().commands()...),
{"configure terminal", "router bgp 64600", "no neighbor 10.255.255.9", "exit"},
{"write memory"},
}
if !reflect.DeepEqual(f.calls, want) {
t.Errorf("appels :\n%q\nattendu :\n%q", f.calls, want)
}
}
func TestApplyBaseRefusesToChangeTheASN(t *testing.T) {
f := &fakeVtysh{running: []string{baseRunning}}
b := labBase()
b.ASN = 64601
err := f.client().ApplyBase(b)
if err == nil || !strings.Contains(err.Error(), "bgp already runs as AS 64600, not 64601") {
t.Errorf("erreur %v", err)
}
if len(f.calls) != 1 {
t.Errorf("appels après le refus : %q", f.calls)
}
}
func TestApplyBaseStopsWhenTheRunningConfigIsUnreadable(t *testing.T) {
f := &fakeVtysh{}
if err := f.client().ApplyBase(labBase()); err == nil {
t.Fatal("aucune erreur")
}
if f.sent("write memory") || f.sent("router bgp 64600") {
t.Errorf("appels : %q", f.calls)
}
}

View file

@ -3,6 +3,7 @@ package frr
import (
"fmt"
"net"
"strings"
)
const maxVNI = 1<<24 - 1
@ -60,6 +61,31 @@ func (b Base) commands() []string {
)
}
func (b Base) staleReflectors(running string) ([]string, error) {
want := make(map[string]bool, len(b.RouteReflectors))
for _, rr := range b.RouteReflectors {
want[rr.String()] = true
}
var stale []string
in := false
for _, l := range strings.Split(running, "\n") {
l = strings.TrimRight(l, " \r")
f := strings.Fields(l)
switch {
case len(f) == 3 && f[0] == "router" && f[1] == "bgp":
if f[2] != fmt.Sprint(b.ASN) {
return nil, fmt.Errorf("frr: bgp already runs as AS %s, not %d: changing the asn requires removing that instance by hand", f[2], b.ASN)
}
in = true
case in && l == "exit":
in = false
case in && len(f) == 4 && f[0] == "neighbor" && f[2] == "peer-group" && f[3] == "fabric" && !want[f[1]]:
stale = append(stale, f[1])
}
}
return stale, nil
}
func (v VRF) Name() string {
return "vrf-" + v.ID
}