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

@ -0,0 +1,38 @@
package configuration
import (
"fmt"
"net"
)
type BGP struct {
ASN uint32 `mapstructure:"asn"`
RouteReflectors []string `mapstructure:"route_reflectors"`
}
func (b BGP) Enabled() bool {
return b.ASN != 0 || len(b.RouteReflectors) > 0
}
func (b BGP) Reflectors() ([]net.IP, error) {
if b.ASN == 0 {
return nil, fmt.Errorf("bgp.asn must be set")
}
if len(b.RouteReflectors) == 0 {
return nil, fmt.Errorf("bgp.route_reflectors must list at least one address")
}
seen := make(map[string]bool, len(b.RouteReflectors))
ips := make([]net.IP, 0, len(b.RouteReflectors))
for _, rr := range b.RouteReflectors {
ip := net.ParseIP(rr).To4()
if ip == nil {
return nil, fmt.Errorf("bgp.route_reflectors: %q is not an IPv4 address", rr)
}
if seen[ip.String()] {
return nil, fmt.Errorf("bgp.route_reflectors: %s is listed twice", ip)
}
seen[ip.String()] = true
ips = append(ips, ip)
}
return ips, nil
}

View file

@ -0,0 +1,54 @@
package configuration
import (
"strings"
"testing"
)
func TestLoadConfig_BGPAbsentIsDisabled(t *testing.T) {
cfg, err := LoadConfig(writeConfig(t, "database:\n path: /tmp/two\n"))
if err != nil {
t.Fatalf("LoadConfig: %v", err)
}
if cfg.BGP.Enabled() {
t.Errorf("bgp enabled without a bgp section: %+v", cfg.BGP)
}
}
func TestLoadConfig_ReadsTheBGPSection(t *testing.T) {
cfg, err := LoadConfig(writeConfig(t, "bgp:\n asn: 4200000000\n route_reflectors: [10.255.255.1, 10.255.255.2]\n"))
if err != nil {
t.Fatalf("LoadConfig: %v", err)
}
if !cfg.BGP.Enabled() || cfg.BGP.ASN != 4200000000 {
t.Fatalf("bgp = %+v", cfg.BGP)
}
rrs, err := cfg.BGP.Reflectors()
if err != nil {
t.Fatalf("Reflectors: %v", err)
}
if len(rrs) != 2 || rrs[0].String() != "10.255.255.1" || rrs[1].String() != "10.255.255.2" {
t.Errorf("reflectors = %v", rrs)
}
}
func TestBGPReflectors_RejectsAnIncompleteOrInvalidSection(t *testing.T) {
cases := []struct {
bgp BGP
want string
}{
{BGP{RouteReflectors: []string{"10.255.255.1"}}, "bgp.asn must be set"},
{BGP{ASN: 64600}, "at least one address"},
{BGP{ASN: 64600, RouteReflectors: []string{"rr1"}}, `"rr1" is not an IPv4 address`},
{BGP{ASN: 64600, RouteReflectors: []string{"fd00::1"}}, `"fd00::1" is not an IPv4 address`},
{BGP{ASN: 64600, RouteReflectors: []string{"10.255.255.1", "10.255.255.1"}}, "10.255.255.1 is listed twice"},
}
for _, c := range cases {
if !c.bgp.Enabled() {
t.Errorf("%+v: Enabled() = false", c.bgp)
}
if _, err := c.bgp.Reflectors(); err == nil || !strings.Contains(err.Error(), c.want) {
t.Errorf("%+v: erreur %v, attendu %q", c.bgp, err, c.want)
}
}
}

View file

@ -55,6 +55,7 @@ type Config struct {
Enabled bool `mapstructure:"enabled"`
IntervalSeconds int `mapstructure:"interval_seconds"`
} `mapstructure:"watchdog"`
BGP BGP `mapstructure:"bgp"`
DefaultInterface string `mapstructure:"default_interface"`
Interfaces map[string]string `mapstructure:"interfaces"`
}