Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions cmd/galactic-router/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"strings"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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. 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,
vipXlat *controller.PinnedVIPTranslationTable,
) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
Expand All @@ -376,8 +379,10 @@ func runGCTicker(
return
case <-ticker.C:
gcRec.RunGC(ctx)
if vipXlatSweeper != nil {
vipXlatSweeper.Sweep(ctx)
if table, err := vipXlat.Table(); err == nil {
sweeper := *vipXlatSweeper
sweeper.Table = table
sweeper.Sweep(ctx)
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions docs/agents/ARCHITECTURE-ROUTER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
150 changes: 150 additions & 0 deletions internal/controller/vipxlat_pinned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 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 {
// 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)
}
Loading
Loading