From aefdda8ee03fd642afce79b4bf082416e9e362c4 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 25 Oct 2026 10:18:17 -0400 Subject: [PATCH 1/2] fix(router): Open vip_xlat_table once galactic-cni pins it On a node that has just booted, galactic-router can start before galactic-cni pins vip_xlat_table. galactic-router opened the pin once at startup and kept no handle when that failed, so every tap-kind ServiceVIPBinding on the node failed with "vip_xlat_table is not available on this node" for the life of the process, and its VIP stayed unserved there until galactic-router restarted. The GC ticker's vip_xlat_table sweep was skipped for the same lifetime. PinnedVIPTranslationTable opens the pin when a binding or the sweep first needs it, retries on each call while it is missing, and keeps the handle once open. The startup log still reports a missing pin, at Info on a route reflector, where galactic-cni never runs. A map that galactic-cni recreates on a layout change still needs a galactic-router restart, as the router architecture doc says. TestServiceVIPBindingReconciler_TapBindsOncePinAppears reconciles a tap binding before the pin exists, then after, and checks that it binds without a restart and that the handle is opened once and reused. Co-Authored-By: Claude Opus 5.5 --- cmd/galactic-router/root.go | 60 ++++---- docs/agents/ARCHITECTURE-ROUTER.md | 23 +-- internal/controller/vipxlat_pinned.go | 155 +++++++++++++++++++ internal/controller/vipxlat_pinned_test.go | 169 +++++++++++++++++++++ 4 files changed, 369 insertions(+), 38 deletions(-) create mode 100644 internal/controller/vipxlat_pinned.go create mode 100644 internal/controller/vipxlat_pinned_test.go diff --git a/cmd/galactic-router/root.go b/cmd/galactic-router/root.go index 1e62f0bd1..e6959aa60 100644 --- a/cmd/galactic-router/root.go +++ b/cmd/galactic-router/root.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "io" "log" "net" "strings" @@ -147,31 +148,30 @@ func runCmd(cfg *config.RouterConfig) error { // map's own lifetime. // // A missing pin is not fatal. It means this node's datapath has not loaded - // yet, or never will on a control-role node with no CNI attach point. It - // matters only once a tap-kind binding is reconciled here, at which point - // the reconciler reports a clear error rather than silently doing nothing. - // Veth bindings work regardless. + // yet, which is normal right after the node boots, or never will on a + // control-role node with no CNI attach point. The handle is retried each + // time a binding needs it, so a pin that appears later is picked up without + // a restart, and until then the reconciler reports a clear error rather + // than silently doing nothing. // // The same handle backs the GC ticker's vip_xlat_table sweep, which is // skipped while the pin is missing. - var ( - vipTranslationTable controller.VIPTranslationTable - vipXlatSweeper *controller.VIPXlatSweeper - ) - vipXlatTable, vipXlatCloser, vipXlatErr := vipxlatmap.OpenPinnedVipXlatTable(attach.PinDir) - switch { - case vipXlatErr != nil && cfg.Reflector: - // A route reflector never runs galactic-cni, so the pin is never - // created there. That is expected, not an error. - ctrl.Log.Info("vip_xlat_table not available on this route reflector; tap-kind VIP bindings are not served here", - "error", vipXlatErr.Error()) - case vipXlatErr != nil: - ctrl.Log.Error(vipXlatErr, "vip_xlat_table not available on this node; "+ - "tap-kind ServiceVIPBinding objects will fail to bind until the eBPF uSID datapath is loaded") - default: - vipTranslationTable = vipXlatTable - vipXlatSweeper = &controller.VIPXlatSweeper{Client: mgr.GetClient(), NodeName: nodeName, Table: vipXlatTable} - defer vipXlatCloser.Close() //nolint:errcheck // best-effort close of our own fd at shutdown + vipXlat := &controller.PinnedVIPTranslationTable{ + Open: func() (*vipxlatmap.VipXlatTable, io.Closer, error) { + return vipxlatmap.OpenPinnedVipXlatTable(attach.PinDir) + }, + } + defer vipXlat.Close() //nolint:errcheck // best-effort close of our own fd at shutdown + if _, err := vipXlat.Table(); err != nil { + if cfg.Reflector { + // A route reflector never runs galactic-cni, so the pin is never + // created there. That is expected, not an error. + ctrl.Log.Info("vip_xlat_table not available on this route reflector; tap-kind VIP bindings are not served here", + "error", err.Error()) + } else { + ctrl.Log.Error(err, "vip_xlat_table not available on this node yet; "+ + "tap-kind ServiceVIPBinding objects retry opening it until the eBPF uSID datapath is loaded") + } } // Register field indexes. @@ -271,7 +271,7 @@ func runCmd(cfg *config.RouterConfig) error { // Register the ServiceVIPBinding controller and the NetworkService binding // writer that generates its bindings. - if err := setupVIPBindingControllers(mgr, nodeName, vipTranslationTable, cfg.GCInterval); err != nil { + if err := setupVIPBindingControllers(mgr, nodeName, vipXlat, cfg.GCInterval); err != nil { return err } @@ -319,7 +319,8 @@ func runCmd(cfg *config.RouterConfig) error { return fmt.Errorf("setup GC controller: %w", err) } - go runGCTicker(ctx, mgr, cfg.GCInterval, gcRec, vipXlatSweeper) + go runGCTicker(ctx, mgr, cfg.GCInterval, gcRec, + &controller.VIPXlatSweeper{Client: mgr.GetClient(), NodeName: nodeName}, vipXlat) if err := mgr.Start(ctx); err != nil { return fmt.Errorf("manager exited: %w", err) @@ -354,12 +355,14 @@ func runCmd(cfg *config.RouterConfig) error { // cancelled. The first pass waits for informer caches to sync, so it does not // see an empty advertisement list and delete live VRFs. // -// vipXlatSweeper, nil when vip_xlat_table is not open, runs only on ticks, -// never in that first pass, so the ServiceVIPBinding reconciler re-registers -// every live binding before the first sweep. +// vipXlatSweeper runs only on ticks, never in that first pass, so the +// ServiceVIPBinding reconciler re-registers every live binding before the +// first sweep. It sweeps vipXlat's table, and skips a tick while that table +// cannot be opened. func runGCTicker( ctx context.Context, mgr ctrl.Manager, interval time.Duration, gcRec *controller.GCReconciler, vipXlatSweeper *controller.VIPXlatSweeper, + vipXlat *controller.PinnedVIPTranslationTable, ) { ticker := time.NewTicker(interval) defer ticker.Stop() @@ -376,7 +379,8 @@ func runGCTicker( return case <-ticker.C: gcRec.RunGC(ctx) - if vipXlatSweeper != nil { + if table, err := vipXlat.Table(); err == nil { + vipXlatSweeper.Table = table vipXlatSweeper.Sweep(ctx) } } diff --git a/docs/agents/ARCHITECTURE-ROUTER.md b/docs/agents/ARCHITECTURE-ROUTER.md index 3d26d701f..a482757d9 100644 --- a/docs/agents/ARCHITECTURE-ROUTER.md +++ b/docs/agents/ARCHITECTURE-ROUTER.md @@ -155,16 +155,19 @@ lives in `root.go`'s `runCmd`: 5. RBAC pre-flight: `checkWatchPermissions` (in `main.go`) issues a `SelfSubjectAccessReview` for every watched resource type and logs an actionable error if watch RBAC is missing (informer caches would otherwise silently never sync). -6. Open this node's own `vip_xlat_table` handle (`vipxlatmap.OpenPinnedVipXlatTable`) - for `ServiceVIPBindingReconciler`'s tap-kind branch — a missing pin (no - eBPF uSID datapath loaded on this node yet) is logged, not fatal, and - only matters once a tap-kind `ServiceVIPBinding` is actually reconciled. - A route reflector never runs `galactic-cni`, so the pin never exists - there and the reflector logs its absence at Info rather than as an error. - The handle is opened once. When a `galactic-cni` upgrade changes the map's - layout, `attach.Load` unpins and recreates the map, and this process keeps - writing to the old one until it restarts, so restart `galactic-router` - after `galactic-cni` has rolled out such a change. +6. Open this node's own `vip_xlat_table` handle (`controller.PinnedVIPTranslationTable` + over `vipxlatmap.OpenPinnedVipXlatTable`) for `ServiceVIPBindingReconciler` + and the GC ticker's `vip_xlat_table` sweep. A missing pin (no eBPF uSID + datapath loaded on this node yet) is logged, not fatal. Each binding + reconcile and each sweep tick retries the open until it succeeds, so on a + node that has just booted, where `galactic-router` can start before + `galactic-cni` pins the map, bindings bind once the pin appears without a + restart. A route reflector never runs `galactic-cni`, so the pin never + exists there and the reflector logs its absence at Info rather than as an + error. Once open, the handle is kept. When a `galactic-cni` upgrade changes + the map's layout, `attach.Load` unpins and recreates the map, and this + process keeps writing to the old one until it restarts, so restart + `galactic-router` after `galactic-cni` has rolled out such a change. 7. Register field indexes: BGPPeer→secret, BGPPeer→router, BGPPolicy→router, BGPAdvertisement→router, BGPVRFInstance→router, BGPRouter→node, VPCAttachment→`status.vpc`. diff --git a/internal/controller/vipxlat_pinned.go b/internal/controller/vipxlat_pinned.go new file mode 100644 index 000000000..96b1dc6aa --- /dev/null +++ b/internal/controller/vipxlat_pinned.go @@ -0,0 +1,155 @@ +// Copyright 2026 Datum Cloud, Inc. +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package controller + +import ( + "errors" + "fmt" + "io" + "io/fs" + "net" + "sync" + + "go.datum.net/galactic/internal/plumbing/ebpf/vipxlatmap" +) + +// PinnedVIPTranslationTable is a VIPTranslationTable over this node's pinned +// vip_xlat_table that opens the pin when it is first needed rather than once at +// startup. +// +// galactic-cni pins the map when it loads the uSID datapath, and on a node that +// has just booted galactic-router can start first. A handle opened only at +// startup would then stay missing for the life of the process, failing every +// ServiceVIPBinding on the node until galactic-router restarted. Every call +// that finds the table not yet open tries again, so the first reconcile after +// the pin appears opens it, and the handle is kept from then on. +// +// Safe for concurrent use. +type PinnedVIPTranslationTable struct { + // Open opens the pinned map. Production passes + // vipxlatmap.OpenPinnedVipXlatTable over attach.PinDir. + Open func() (*vipxlatmap.VipXlatTable, io.Closer, error) + + mu sync.Mutex + table *vipxlatmap.VipXlatTable + closer io.Closer + closed bool +} + +// errVIPTranslationTableClosed is what Table returns after Close. +var errVIPTranslationTableClosed = errors.New("vip_xlat_table handle is closed") + +// Table returns the open table, opening the pin first if no earlier call has. +func (p *PinnedVIPTranslationTable) Table() (*vipxlatmap.VipXlatTable, error) { + p.mu.Lock() + defer p.mu.Unlock() + if p.closed { + return nil, errVIPTranslationTableClosed + } + if p.table != nil { + return p.table, nil + } + table, closer, err := p.Open() + if errors.Is(err, fs.ErrNotExist) { + return nil, fmt.Errorf("vip_xlat_table is not available on this node (eBPF uSID datapath not loaded): %w", err) + } + if err != nil { + return nil, fmt.Errorf("vip_xlat_table is not available on this node: %w", err) + } + p.table, p.closer = table, closer + return table, nil +} + +// Close closes this process's handle, if one was opened, and makes every later +// call fail rather than open the pin again. The pinned map itself is +// unaffected. Closing twice is a no-op. +func (p *PinnedVIPTranslationTable) Close() error { + p.mu.Lock() + defer p.mu.Unlock() + p.closed = true + if p.closer == nil { + return nil + } + err := p.closer.Close() + p.table, p.closer = nil, nil + return err +} + +// RegisterIngress implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) RegisterIngress(block uint64, argument, slot uint16, proto uint8, + vipAddr net.IP, vipPort uint16, backendAddr net.IP, backendPort uint16, +) error { + t, err := p.Table() + if err != nil { + return err + } + return t.RegisterIngress(block, argument, slot, proto, vipAddr, vipPort, backendAddr, backendPort) +} + +// RegisterEgress implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) RegisterEgress(block uint64, argument uint16, proto uint8, + backendAddr net.IP, backendPort uint16, vipAddr net.IP, vipPort uint16, +) error { + t, err := p.Table() + if err != nil { + return err + } + return t.RegisterEgress(block, argument, proto, backendAddr, backendPort, vipAddr, vipPort) +} + +// UnregisterIngress implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) UnregisterIngress(block uint64, argument, slot uint16, proto uint8, + vipAddr net.IP, vipPort uint16, +) error { + t, err := p.Table() + if err != nil { + return err + } + return t.UnregisterIngress(block, argument, slot, proto, vipAddr, vipPort) +} + +// UnregisterEgress implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) UnregisterEgress(block uint64, argument uint16, proto uint8, + backendAddr net.IP, backendPort uint16, +) error { + t, err := p.Table() + if err != nil { + return err + } + return t.UnregisterEgress(block, argument, proto, backendAddr, backendPort) +} + +// UnregisterEgressTranslated implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) UnregisterEgressTranslated(block uint64, argument uint16, proto uint8, + backendAddr net.IP, backendPort uint16, +) error { + t, err := p.Table() + if err != nil { + return err + } + return t.UnregisterEgressTranslated(block, argument, proto, backendAddr, backendPort) +} + +// UnregisterBinding implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) UnregisterBinding(slot uint16, proto uint8, vipAddr net.IP, vipPort uint16, + backendAddr net.IP, backendPort uint16, +) ([]vipxlatmap.Entry, error) { + t, err := p.Table() + if err != nil { + return nil, err + } + return t.UnregisterBinding(slot, proto, vipAddr, vipPort, backendAddr, backendPort) +} + +// UnregisterBindingAt implements VIPTranslationTable. +func (p *PinnedVIPTranslationTable) UnregisterBindingAt(block uint64, argument, slot uint16, proto uint8, + vipAddr net.IP, vipPort uint16, backendAddr net.IP, backendPort uint16, +) ([]vipxlatmap.Entry, error) { + t, err := p.Table() + if err != nil { + return nil, err + } + return t.UnregisterBindingAt(block, argument, slot, proto, vipAddr, vipPort, backendAddr, backendPort) +} diff --git a/internal/controller/vipxlat_pinned_test.go b/internal/controller/vipxlat_pinned_test.go new file mode 100644 index 000000000..360df0750 --- /dev/null +++ b/internal/controller/vipxlat_pinned_test.go @@ -0,0 +1,169 @@ +// Copyright 2026 Datum Cloud, Inc. +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package controller + +import ( + "context" + "errors" + "io" + "sync" + "sync/atomic" + "testing" + + "k8s.io/apimachinery/pkg/api/meta" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + + bgpv1alpha1 "go.datum.net/galactic/api/network/v1alpha1" + "go.datum.net/galactic/internal/plumbing/ebpf/vipxlatmap" +) + +type countingCloser struct{ closed int } + +func (c *countingCloser) Close() error { + c.closed++ + return nil +} + +// TestServiceVIPBindingReconciler_TapBindsOncePinAppears covers a node that has +// just booted: galactic-router starts before galactic-cni pins vip_xlat_table, +// so a tap-kind binding fails at first. Once the pin exists, the next reconcile +// opens it and binds without a restart, and later reconciles reuse the handle. +func TestServiceVIPBindingReconciler_TapBindsOncePinAppears(t *testing.T) { + scheme := newRuleTestScheme(t) + router, adv, vrf := newBackendFixtures(testVPCRef) + binding := newTestServiceVIPBinding(bgpv1alpha1.ServiceVIPBindingEgressKindTap, testComputeNodeName) + binding.Spec.BackendAddress = testBackendAddr + fakeClient := fake.NewClientBuilder(). + WithScheme(scheme). + WithStatusSubresource(&bgpv1alpha1.ServiceVIPBinding{}). + WithObjects(router, adv, vrf, binding). + Build() + + errNoPin := errors.New("open pinned map: no such file or directory") + mem := newMemTable() + closer := &countingCloser{} + pinned, opens := false, 0 + table := &PinnedVIPTranslationTable{Open: func() (*vipxlatmap.VipXlatTable, io.Closer, error) { + opens++ + if !pinned { + return nil, nil, errNoPin + } + return vipxlatmap.NewVipXlatTable(mem), closer, nil + }} + r := &ServiceVIPBindingReconciler{Client: fakeClient, NodeName: testComputeNodeName, VIPTranslationTable: table} + req := ctrl.Request{NamespacedName: bindingKey()} + + if _, err := r.Reconcile(context.Background(), req); !errors.Is(err, errNoPin) { + t.Fatalf("Reconcile before the pin exists: err = %v, want it to wrap %v", err, errNoPin) + } + + pinned = true + if _, err := r.Reconcile(context.Background(), req); err != nil { + t.Fatalf("Reconcile after the pin appears: %v", err) + } + entries, err := vipxlatmap.NewVipXlatTable(mem).List() + if err != nil { + t.Fatalf("List: %v", err) + } + if len(entries) != 2 { + t.Errorf("vip_xlat_table holds %d rows, want the binding's ingress and egress rows", len(entries)) + } + got := &bgpv1alpha1.ServiceVIPBinding{} + if err := fakeClient.Get(context.Background(), bindingKey(), got); err != nil { + t.Fatalf("get: %v", err) + } + if !meta.IsStatusConditionTrue(got.Status.Conditions, bgpv1alpha1.ConditionTypeBound) { + t.Errorf("Bound condition = %+v, want True", got.Status.Conditions) + } + + if _, err := r.Reconcile(context.Background(), req); err != nil { + t.Fatalf("Reconcile again: %v", err) + } + if opens != 2 { + t.Errorf("Open called %d times, want 2: one failed, then one kept for every later call", opens) + } + + if err := table.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + if closer.closed != 1 { + t.Errorf("handle closed %d times, want 1", closer.closed) + } +} + +// TestPinnedVIPTranslationTable_CloseBeforeOpen: closing a table whose pin never +// opened succeeds. +func TestPinnedVIPTranslationTable_CloseBeforeOpen(t *testing.T) { + table := &PinnedVIPTranslationTable{Open: func() (*vipxlatmap.VipXlatTable, io.Closer, error) { + return nil, nil, errors.New("no pin") + }} + if _, err := table.Table(); err == nil { + t.Fatal("Table: want an error while the pin is missing") + } + if err := table.Close(); err != nil { + t.Errorf("Close: %v", err) + } +} + +// TestPinnedVIPTranslationTable_NoReopenAfterClose: a call that lands after +// Close, such as a GC tick racing shutdown, fails instead of opening a handle +// nothing would close, and a second Close closes nothing more. +func TestPinnedVIPTranslationTable_NoReopenAfterClose(t *testing.T) { + closer := &countingCloser{} + opens := 0 + table := &PinnedVIPTranslationTable{Open: func() (*vipxlatmap.VipXlatTable, io.Closer, error) { + opens++ + return vipxlatmap.NewVipXlatTable(newMemTable()), closer, nil + }} + if _, err := table.Table(); err != nil { + t.Fatalf("Table: %v", err) + } + if err := table.Close(); err != nil { + t.Fatalf("Close: %v", err) + } + if _, err := table.Table(); !errors.Is(err, errVIPTranslationTableClosed) { + t.Errorf("Table after Close: err = %v, want %v", err, errVIPTranslationTableClosed) + } + if err := table.Close(); err != nil { + t.Errorf("second Close: %v", err) + } + if opens != 1 || closer.closed != 1 { + t.Errorf("opens = %d, closes = %d, want 1 each", opens, closer.closed) + } +} + +// TestPinnedVIPTranslationTable_ConcurrentFirstUse: reconciles and the sweep +// asking for the table at once open the pin once and share one handle. +func TestPinnedVIPTranslationTable_ConcurrentFirstUse(t *testing.T) { + var opens atomic.Int32 + table := &PinnedVIPTranslationTable{Open: func() (*vipxlatmap.VipXlatTable, io.Closer, error) { + opens.Add(1) + return vipxlatmap.NewVipXlatTable(newMemTable()), &countingCloser{}, nil + }} + const callers = 16 + got := make([]*vipxlatmap.VipXlatTable, callers) + var wg sync.WaitGroup + for i := range callers { + wg.Add(1) + go func() { + defer wg.Done() + tbl, err := table.Table() + if err != nil { + t.Errorf("Table: %v", err) + } + got[i] = tbl + }() + } + wg.Wait() + if n := opens.Load(); n != 1 { + t.Errorf("Open called %d times, want 1", n) + } + for i := 1; i < callers; i++ { + if got[i] != got[0] { + t.Fatalf("caller %d got a different handle", i) + } + } +} From 67f30713b199552aa032ab8d7bb21fcfcad30742 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 25 Oct 2026 11:28:06 -0400 Subject: [PATCH 2/2] refactor(router): Sweep vip_xlat_table with a per-tick copy The GC ticker set the shared VIPXlatSweeper's Table field on every tick. Only that goroutine touches it today, but nothing stops a later caller from sweeping concurrently with that write. Each tick now sweeps with its own copy of the sweeper. Also trim the PinnedVIPTranslationTable doc to its contract: it opens the pin on first use, retries while the pin is missing, and keeps the handle until Close. --- cmd/galactic-router/root.go | 9 +++++---- internal/controller/vipxlat_pinned.go | 13 ++++--------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/cmd/galactic-router/root.go b/cmd/galactic-router/root.go index e6959aa60..317db6bf8 100644 --- a/cmd/galactic-router/root.go +++ b/cmd/galactic-router/root.go @@ -357,8 +357,8 @@ func runCmd(cfg *config.RouterConfig) error { // // vipXlatSweeper runs only on ticks, never in that first pass, so the // ServiceVIPBinding reconciler re-registers every live binding before the -// first sweep. It sweeps vipXlat's table, and skips a tick while that table -// cannot be opened. +// first sweep. Each tick sweeps vipXlat's table with a copy of +// vipXlatSweeper, and skips the sweep while that table cannot be opened. func runGCTicker( ctx context.Context, mgr ctrl.Manager, interval time.Duration, gcRec *controller.GCReconciler, vipXlatSweeper *controller.VIPXlatSweeper, @@ -380,8 +380,9 @@ func runGCTicker( case <-ticker.C: gcRec.RunGC(ctx) if table, err := vipXlat.Table(); err == nil { - vipXlatSweeper.Table = table - vipXlatSweeper.Sweep(ctx) + sweeper := *vipXlatSweeper + sweeper.Table = table + sweeper.Sweep(ctx) } } } diff --git a/internal/controller/vipxlat_pinned.go b/internal/controller/vipxlat_pinned.go index 96b1dc6aa..480a2329d 100644 --- a/internal/controller/vipxlat_pinned.go +++ b/internal/controller/vipxlat_pinned.go @@ -16,15 +16,10 @@ import ( ) // PinnedVIPTranslationTable is a VIPTranslationTable over this node's pinned -// vip_xlat_table that opens the pin when it is first needed rather than once at -// startup. -// -// galactic-cni pins the map when it loads the uSID datapath, and on a node that -// has just booted galactic-router can start first. A handle opened only at -// startup would then stay missing for the life of the process, failing every -// ServiceVIPBinding on the node until galactic-router restarted. Every call -// that finds the table not yet open tries again, so the first reconcile after -// the pin appears opens it, and the handle is kept from then on. +// vip_xlat_table that opens the pin on first use. galactic-cni creates the pin, +// so it can be missing when galactic-router starts. Every call that finds the +// table not yet open tries again, and the first successful open is kept until +// Close. // // Safe for concurrent use. type PinnedVIPTranslationTable struct {