From 26095062c81efa767b148bd6eee027ca170f2cb3 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Sun, 25 Oct 2026 23:36:14 +0100 Subject: [PATCH] f-87: agent: bridge public = interfaces.internet, watchdog du segment #87 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le veth vp--e rejoint cfg.PublicBridge() (interfaces.internet, br-public par défaut, jamais default_interface). Le watchdog signale un bridge public absent, éteint ou sans port VXLAN. config.exemple.yml : internet: br-public. Signed-off-by: GnomeZworc --- conf/agent/config.exemple.yml | 6 +- internal/config/agent/config_test.go | 17 ++++++ internal/config/agent/interfaces.go | 13 ++++ internal/config/agent/interfaces_test.go | 20 ++++++ internal/netif/link.go | 18 ++++++ internal/vpc/create.go | 2 +- internal/watchdog/check_public.go | 39 ++++++++++++ internal/watchdog/check_public_test.go | 77 ++++++++++++++++++++++++ internal/watchdog/watchdog.go | 3 + internal/watchdog/watchdog_test.go | 8 +++ 10 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 internal/config/agent/interfaces.go create mode 100644 internal/config/agent/interfaces_test.go create mode 100644 internal/watchdog/check_public.go create mode 100644 internal/watchdog/check_public_test.go diff --git a/conf/agent/config.exemple.yml b/conf/agent/config.exemple.yml index e03bc6e..09ab413 100644 --- a/conf/agent/config.exemple.yml +++ b/conf/agent/config.exemple.yml @@ -39,10 +39,12 @@ times: # Bridge interface used when the requested iface_type is not found in interfaces default_interface: br-000000 -# Map of logical interface types to physical bridge names on this host +# Map of logical interface types to physical bridge names on this host. +# internet: public bridge, carrying vxlan-public (deploy.sh --public_vni); every VPC +# plugs its vp--e veth into it. br-public when absent, never default_interface. interfaces: vms: br-000000 - internet: br-000000 + internet: br-public admin: br-000000 # MTU, identical on every hypervisor. diff --git a/internal/config/agent/config_test.go b/internal/config/agent/config_test.go index a4d5b22..d99ec7c 100644 --- a/internal/config/agent/config_test.go +++ b/internal/config/agent/config_test.go @@ -145,4 +145,21 @@ func TestLoadConfig_ExempleFourniEstValide(t *testing.T) { if cfg.QEMU.QMPDir == "" { t.Error("config.exemple.yml devrait définir qemu.qmp_dir") } + if got := cfg.PublicBridge(); got != "br-public" { + t.Errorf("config.exemple.yml : bridge public %q, br-public attendu", got) + } +} + +func TestLoadConfig_PublicBridgeWithoutInternetKey(t *testing.T) { + path := filepath.Join(t.TempDir(), "agent.yml") + if err := os.WriteFile(path, []byte("default_interface: br-000000\ninterfaces:\n lan: br-lan\n"), 0o600); err != nil { + t.Fatal(err) + } + cfg, err := LoadConfig(path) + if err != nil { + t.Fatalf("LoadConfig : %v", err) + } + if got := cfg.PublicBridge(); got != "br-public" { + t.Errorf("bridge public %q, br-public attendu (jamais default_interface)", got) + } } diff --git a/internal/config/agent/interfaces.go b/internal/config/agent/interfaces.go new file mode 100644 index 0000000..67e3163 --- /dev/null +++ b/internal/config/agent/interfaces.go @@ -0,0 +1,13 @@ +package configuration + +const ( + InternetInterface = "internet" + DefaultPublicBridge = "br-public" +) + +func (c Config) PublicBridge() string { + if bridge := c.Interfaces[InternetInterface]; bridge != "" { + return bridge + } + return DefaultPublicBridge +} diff --git a/internal/config/agent/interfaces_test.go b/internal/config/agent/interfaces_test.go new file mode 100644 index 0000000..ef5a0a0 --- /dev/null +++ b/internal/config/agent/interfaces_test.go @@ -0,0 +1,20 @@ +package configuration + +import "testing" + +func TestPublicBridge(t *testing.T) { + cases := map[string]struct { + cfg Config + want string + }{ + "clé absente": {Config{DefaultInterface: "br-000000"}, "br-public"}, + "clé vide": {Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"internet": ""}}, "br-public"}, + "clé posée": {Config{Interfaces: map[string]string{"internet": "br-wan"}}, "br-wan"}, + "autres types, sans default_interface en repli": {Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"lan": "br-lan"}}, "br-public"}, + } + for name, c := range cases { + if got := c.cfg.PublicBridge(); got != c.want { + t.Errorf("%s : %q, %q attendu", name, got, c.want) + } + } +} diff --git a/internal/netif/link.go b/internal/netif/link.go index 56fdb87..8c4b2b0 100644 --- a/internal/netif/link.go +++ b/internal/netif/link.go @@ -46,3 +46,21 @@ func LinkMTU(name string) (int, error) { } return link.Attrs().MTU, nil } + +func BridgeVxlanPorts(bridge string) ([]string, error) { + br, err := netlink.LinkByName(bridge) + if err != nil { + return nil, err + } + links, err := netlink.LinkList() + if err != nil { + return nil, err + } + var ports []string + for _, l := range links { + if l.Attrs().MasterIndex == br.Attrs().Index && l.Type() == "vxlan" { + ports = append(ports, l.Attrs().Name) + } + } + return ports, nil +} diff --git a/internal/vpc/create.go b/internal/vpc/create.go index c0377bf..8b31911 100644 --- a/internal/vpc/create.go +++ b/internal/vpc/create.go @@ -32,7 +32,7 @@ func CreateVPC(db *badger.DB, name string, cfg *configuration.Config) error { return err } - if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", "br-public"); err != nil { + if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", cfg.PublicBridge()); err != nil { return err } diff --git a/internal/watchdog/check_public.go b/internal/watchdog/check_public.go new file mode 100644 index 0000000..8752688 --- /dev/null +++ b/internal/watchdog/check_public.go @@ -0,0 +1,39 @@ +package watchdog + +import ( + "fmt" + + configuration "git.g3e.fr/syonad/two/internal/config/agent" + "git.g3e.fr/syonad/two/internal/netif" + "git.g3e.fr/syonad/two/internal/watchdog/notify" +) + +const kindPublic = "public" + +type publicProbe interface { + LinkProblem(name string) string + VxlanPorts(bridge string) ([]string, error) +} + +type systemPublicProbe struct{} + +func (systemPublicProbe) LinkProblem(name string) string { return linkProblem(name) } + +func (systemPublicProbe) VxlanPorts(bridge string) ([]string, error) { + return netif.BridgeVxlanPorts(bridge) +} + +func CheckPublicSegment(cfg *configuration.Config, p publicProbe, n notify.Notifier) { + bridge := cfg.PublicBridge() + if problem := p.LinkProblem(bridge); problem != "" { + n.Notify(kindPublic, bridge, problem+" (public bridge, interfaces.internet): the vpcs of this host have no public segment") + return + } + ports, err := p.VxlanPorts(bridge) + switch { + case err != nil: + n.Notify(kindPublic, bridge, fmt.Sprintf("vxlan ports unreadable: %v", err)) + case len(ports) == 0: + n.Notify(kindPublic, bridge, "no vxlan port: the public segment is unreachable from this host") + } +} diff --git a/internal/watchdog/check_public_test.go b/internal/watchdog/check_public_test.go new file mode 100644 index 0000000..f8c0ea8 --- /dev/null +++ b/internal/watchdog/check_public_test.go @@ -0,0 +1,77 @@ +package watchdog + +import ( + "errors" + "testing" + + configuration "git.g3e.fr/syonad/two/internal/config/agent" +) + +type fakePublicProbe struct { + problems map[string]string + ports []string + err error + asked []string +} + +func (f *fakePublicProbe) LinkProblem(name string) string { + f.asked = append(f.asked, name) + return f.problems[name] +} + +func (f *fakePublicProbe) VxlanPorts(bridge string) ([]string, error) { + f.asked = append(f.asked, "ports:"+bridge) + return f.ports, f.err +} + +func TestCheckPublicSegment_SilentWithAVxlanPort(t *testing.T) { + r := &recorder{} + p := &fakePublicProbe{ports: []string{"vxlan-public"}} + CheckPublicSegment(&configuration.Config{}, p, r) + if len(r.calls) != 0 { + t.Errorf("notifications = %+v, aucune attendue", r.calls) + } + if len(p.asked) != 2 || p.asked[0] != "br-public" || p.asked[1] != "ports:br-public" { + t.Errorf("sondes = %q, br-public attendu par défaut", p.asked) + } +} + +func TestCheckPublicSegment_FollowsInterfacesInternet(t *testing.T) { + r := &recorder{} + p := &fakePublicProbe{ports: []string{"vxlan-public"}} + CheckPublicSegment(&configuration.Config{DefaultInterface: "br-000000", Interfaces: map[string]string{"internet": "br-wan"}}, p, r) + if len(p.asked) != 2 || p.asked[0] != "br-wan" || p.asked[1] != "ports:br-wan" { + t.Errorf("sondes = %q, br-wan attendu", p.asked) + } +} + +func TestCheckPublicSegment_ReportsAMissingBridgeWithoutReadingPorts(t *testing.T) { + r := &recorder{} + p := &fakePublicProbe{problems: map[string]string{"br-public": "interface br-public not found: Link not found"}} + CheckPublicSegment(&configuration.Config{}, p, r) + got := r.forName("br-public") + want := "interface br-public not found: Link not found (public bridge, interfaces.internet): the vpcs of this host have no public segment" + if len(got) != 1 || got[0].kind != "public" || got[0].problem != want { + t.Fatalf("notifications = %+v, une attendue : %q", r.calls, want) + } + if len(p.asked) != 1 { + t.Errorf("ports lus alors que le bridge manque : %q", p.asked) + } +} + +func TestCheckPublicSegment_ReportsABridgeWithoutVxlan(t *testing.T) { + r := &recorder{} + CheckPublicSegment(&configuration.Config{}, &fakePublicProbe{}, r) + got := r.forName("br-public") + if len(got) != 1 || got[0].problem != "no vxlan port: the public segment is unreachable from this host" { + t.Errorf("notifications = %+v", r.calls) + } +} + +func TestCheckPublicSegment_ReportsUnreadablePorts(t *testing.T) { + r := &recorder{} + CheckPublicSegment(&configuration.Config{}, &fakePublicProbe{err: errors.New("netlink: permission denied")}, r) + if !r.hasProblemContaining("vxlan ports unreadable: netlink: permission denied") { + t.Errorf("notifications = %+v", r.calls) + } +} diff --git a/internal/watchdog/watchdog.go b/internal/watchdog/watchdog.go index 7a0101f..b49b12c 100644 --- a/internal/watchdog/watchdog.go +++ b/internal/watchdog/watchdog.go @@ -23,6 +23,7 @@ type Watchdog struct { interval time.Duration dbusDown bool l3 l3Probe + public publicProbe } func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *slog.Logger, interval time.Duration) *Watchdog { @@ -41,6 +42,7 @@ func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *sl logger: logger, interval: interval, l3: systemProbe{frr: frr.NewClient()}, + public: systemPublicProbe{}, } } @@ -74,6 +76,7 @@ func (w *Watchdog) tick() { if err := CheckVMs(w.db, w.cfg, u, w.notifier); err != nil { w.logger.Error("watchdog: vm check failed", "err", err) } + CheckPublicSegment(w.cfg, w.public, w.notifier) CheckBGP(w.cfg, w.l3, w.notifier) if err := CheckL3VPCs(w.db, w.cfg, w.l3, w.notifier); err != nil { w.logger.Error("watchdog: l3 vpc check failed", "err", err) diff --git a/internal/watchdog/watchdog_test.go b/internal/watchdog/watchdog_test.go index eb43975..87fcfb6 100644 --- a/internal/watchdog/watchdog_test.go +++ b/internal/watchdog/watchdog_test.go @@ -74,6 +74,13 @@ func TestNew_IntervalleValideConserve(t *testing.T) { } } +func TestNew_ChecksThePublicSegmentOnTheSystem(t *testing.T) { + w := New(newTestDB(t), testCfg(t), &recorder{}, nil, time.Second) + if _, ok := w.public.(systemPublicProbe); !ok { + t.Errorf("sonde du segment public = %T, systemPublicProbe attendue", w.public) + } +} + func TestNew_LoggerNil(t *testing.T) { w := New(newTestDB(t), testCfg(t), &recorder{}, nil, time.Second) if w.logger == nil { @@ -141,6 +148,7 @@ func TestRun_ErreurDeBaseLogueeEtBoucleContinue(t *testing.T) { n := newChanNotifier() l, buf := testLogger() w := New(db, testCfg(t), n, l, 10*time.Millisecond) + w.public = &fakePublicProbe{ports: []string{"vxlan-public"}} ctx, cancel := context.WithTimeout(context.Background(), 300*time.Millisecond) defer cancel()