Files
flock/pkg/agent/runtime_linux.go
T
Donavan Fritz eb1f5e0d8d
Build flock Image / build (push) Has been cancelled
M2: netlink, IPAM/handler wiring, BIRD sidecar, CNI installer
Code (Linux build, with no-op stubs for macOS dev):
- pkg/agent/netns_linux.go: ensureVeth → host-side configure (addrgenmode
  none, fe80::1/64, proxy_arp, forwarding) → move peer to pod ns →
  configure pod side (addr, default route via fe80::1, v4 169.254.1.1
  on-link gateway) → host /128 + /32 routes. Idempotent.
- pkg/agent/hostiface.go: deterministic host iface name flock<8hex> from
  FNV-1a-32(containerID).
- pkg/agent/annotations.go: parse flock.fritzlab.net/{ipv6,ipv4,cidr6,
  cidr4,ip-algo,anycast} with design-doc defaults; ParseCNIArgs for the
  K8S_POD_* keys kubelet sets.
- pkg/agent/podinfo.go: shared informer scoped to spec.nodeName==NODE,
  WaitForPod helper for ADD-vs-informer-sync race.
- pkg/agent/handlers.go: PodHandler does
    cache lookup → annotations → IPAM → store(pending) → SetupFunc →
    store(committed) → Result. Idempotent on retry. Del symmetric.
- pkg/routing/bird/config.go: text/template render with stable ordering;
  golden tests for host001 + anycast injection + sort stability.
- pkg/agent/bird.go: writes /etc/flock/bird/bird.conf, debounces 500ms,
  execs `birdc -s /run/flock/bird.ctl configure`. Installs blackhole
  kernel routes for the node summary CIDRs so BIRD's protocol kernel
  imports them.
- pkg/agent/runtime_linux.go: at startup, waits up to 60s for the per-
  node NodeConfig, reconciles committed allocations into IPAM.used,
  garbage-collects pending entries, builds PodHandler, swaps RPC
  handlers in.
- cmd/flock-installer: init-container binary that copies /opt/cni/bin/
  flock and writes 01-flock.conflist (lex-first so kubelet picks it
  over Calico's 10-calico.conflist on flock-labeled nodes).

Deploy:
- Dockerfile: alpine + iproute2 + bird2; multi-binary image.
- deploy/daemonset.yaml: install-cni init container; bird sidecar
  sharing /etc/flock/bird + /run/flock with the agent; ConfigMap-seeded
  bootstrap bird.conf so the sidecar boots before the agent renders.
  Privileged on flock-agent + install-cni; bird sidecar uses
  NET_ADMIN/RAW only.
- RBAC: pods + networkpolicies get/list/watch (the latter is reserved
  for M8 — harmless to grant now).

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-04-24 22:33:48 -05:00

132 lines
3.5 KiB
Go

//go:build linux
package agent
import (
"context"
"fmt"
"net"
"time"
)
// configureRuntime wires Pod informer, IPAM, netlink, and BIRD on a real
// Linux node. Steps:
//
// 1. Wait for NodeConfig (operator-applied per-node CR).
// 2. Reconcile any pre-existing kernel state from allocations.json into
// IPAM.used (so we never re-allocate an in-flight pod's IP).
// 3. Garbage-collect any state==pending entries (partial ADDs from a
// previous agent generation).
// 4. Start the Pod informer (filtered to spec.nodeName == node).
// 5. Build PodHandler and SetHandlers(add, del, check).
// 6. Install BIRD blackhole summary routes + render initial config.
func (s *Server) configureRuntime(ctx context.Context) error {
if err := s.firstAvailableNodeConfig(ctx, 60*time.Second); err != nil {
return err
}
nc := s.NodeConfig.Load()
ipam, err := NewIPAM(nc.Spec.CIDR6, nc.Spec.CIDR4)
if err != nil {
return fmt.Errorf("init ipam: %w", err)
}
// Reconcile committed entries; GC pending entries.
for _, a := range s.Store.Snapshot() {
switch a.State {
case StateCommitted:
if a.IP6 != "" {
ipam.MarkInUse(net.ParseIP(a.IP6))
}
if a.IP4 != "" {
ipam.MarkInUse(net.ParseIP(a.IP4))
}
case StatePending:
s.Logger.Info("GC pending allocation", "container_id", a.ContainerID)
_ = Teardown(a.ContainerID, net.ParseIP(a.IP6), net.ParseIP(a.IP4))
_ = s.Store.Delete(a.ContainerID)
}
}
pods, err := StartPodInformer(ctx, s.restCfg, s.Node, s.Logger)
if err != nil {
return fmt.Errorf("pod informer: %w", err)
}
bird := &BirdManager{
NodeName: s.Node,
ConfigPath: "/etc/flock/bird/bird.conf",
BirdcSocket: "/run/flock/bird.ctl",
Logger: s.Logger,
}
if err := bird.SummaryRoutes(nc); err != nil {
s.Logger.Warn("install summary routes", "err", err)
}
if err := bird.Render(nc, nil, nil, routerIDFromNodeIP(s.restCfg)); err != nil {
s.Logger.Warn("initial bird render", "err", err)
}
// Re-render whenever NodeConfig changes (cheap).
go func() {
t := time.NewTicker(15 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
cur := s.NodeConfig.Load()
if cur == nil {
continue
}
_ = bird.SummaryRoutes(cur)
_ = bird.Render(cur, nil, nil, routerIDFromNodeIP(s.restCfg))
}
}
}()
handler := &PodHandler{
Node: s.Node,
Store: s.Store,
IPAM: ipam,
Pods: pods,
NodeConfig: s.NodeConfig,
SetupFunc: Setup,
TeardownFunc: Teardown,
AfterCommit: func() {
// Future: collect anycast IPs from store snapshot, re-render bird.
},
}
s.RPC.SetHandlers(handler.Add, handler.Del, handler.Check)
s.Logger.Info("runtime ready",
"asn", nc.Spec.BGP.ASN,
"cidr6", nc.Spec.CIDR6,
"cidr4", nc.Spec.CIDR4,
"committed", len(s.Store.Snapshot()),
)
return nil
}
// routerIDFromNodeIP picks a stable IPv4 to use as BIRD router-id. Uses
// the host network for now; falls back to a synthesized value derived
// from the node name if no v4 is reachable.
func routerIDFromNodeIP(_ interface{}) string {
// Best-effort: read the kernel route table for a default-route src.
addrs, err := net.InterfaceAddrs()
if err == nil {
for _, a := range addrs {
ipn, ok := a.(*net.IPNet)
if !ok {
continue
}
v4 := ipn.IP.To4()
if v4 == nil || v4.IsLoopback() || v4.IsLinkLocalUnicast() {
continue
}
return v4.String()
}
}
// Fallback: 127.0.0.1 — bird will accept it but BGP peers won't like a
// duplicate router-id. The agent log will scream above this if it fires.
return "127.0.0.1"
}