netns

package
v0.6.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package netns manages the per-host pool of prepaid network namespaces for the containerd engine. Bookkeeping lives in SQLite (container_netns_slots); CNI ADD/DEL and netns creation live in host.go — the same pool.go/host.go split as internal/network/tap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder sequences reserve → realize → adopt with LIFO teardown on failure.

func NewBuilder

func NewBuilder(pool *Pool, host HostManager) *Builder

func (*Builder) Build

func (b *Builder) Build(ctx context.Context, sandboxID string) (*Slot, error)

Build runs the FSM for sandboxID. On failure, teardown runs in reverse order.

type FakeHost

type FakeHost struct {
	// contains filtered or unexported fields
}

FakeHost records realize/remove calls for tests. Like Host, it keys host state by Slot.netnsName, so a test that releases under the wrong identity leaves the realized entry behind exactly as a real node would.

func NewFakeHost

func NewFakeHost() *FakeHost

func (*FakeHost) Realize

func (f *FakeHost) Realize(ctx context.Context, slot Slot) (string, string, error)

func (*FakeHost) RealizedCount

func (f *FakeHost) RealizedCount() int

func (*FakeHost) Remove

func (f *FakeHost) Remove(ctx context.Context, slot Slot) error

func (*FakeHost) Removed added in v0.6.9

func (f *FakeHost) Removed() []string

Removed returns the netns names Remove was called with, in order.

func (*FakeHost) RemovedCount

func (f *FakeHost) RemovedCount() int

func (*FakeHost) SetRealizeError

func (f *FakeHost) SetRealizeError(err error)

func (*FakeHost) SetRemoveError

func (f *FakeHost) SetRemoveError(err error)

type Host

type Host struct {
	Runner    cni.Runner
	NetnsRoot string
	// contains filtered or unexported fields
}

Host is the production HostManager. NetnsRoot is typically /run/netns.

func (*Host) DisableIPv6 added in v0.6.9

func (h *Host) DisableIPv6(_ context.Context, slot Slot) error

DisableIPv6 turns IPv6 off in an already-realized slot's netns. Reconcile runs it on every slot that survives a restart, so a warm slot an older daemon realized is not handed out with IPv6 on, and a running sandbox loses the link-local address it booted with. Idempotent.

func (*Host) IPv4Addrs added in v0.6.9

func (h *Host) IPv4Addrs(_ context.Context, slot Slot) ([]string, error)

IPv4Addrs lists the IPv4 addresses inside an already-realized slot's netns: the address a task started there comes up at. Reconcile compares it with the address the slot's row records before a warm slot that outlived a restart is handed out again.

func (*Host) Realize

func (h *Host) Realize(ctx context.Context, slot Slot) (string, string, error)

func (*Host) Remove

func (h *Host) Remove(ctx context.Context, slot Slot) error

Remove deletes everything Realize created for slot: the CNI attachment (veth, host-local lease, masquerade chain), conntrack entries and the named netns. It uses the same identity Realize used (Slot.netnsName), whoever owns the slot now. Errors are joined, never swallowed: the pool keeps a slot out of circulation until Remove succeeds.

type HostManager

type HostManager interface {
	Realize(ctx context.Context, slot Slot) (netnsPath string, ip string, err error)
	Remove(ctx context.Context, slot Slot) error
}

HostManager realizes reserved slots: create netns, CNI ADD, and inverse on Remove. Idempotent Ensure/Remove mirrors tap.HostManager.

type LiveSandbox

type LiveSandbox func(ctx context.Context, sandboxID string) bool

LiveSandbox reports whether sandboxID still has a live containerd workload.

type NetnsExists

type NetnsExists func(path string) bool

NetnsExists checks whether a prepaid netns path is still present on disk.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool is the policy layer over container_netns_slots.

func New

func New(st *store.Store) *Pool

func (*Pool) Adopt

func (p *Pool) Adopt(ctx context.Context, sandboxID string, now time.Time) (*Slot, error)

Adopt marks the slot adopted once the container task is running.

func (*Pool) ClaimPooled

func (p *Pool) ClaimPooled(ctx context.Context, sandboxID string, now time.Time) (*Slot, bool, error)

ClaimPooled tries the warm queue before a cold build. Returns (slot, true, nil) on hit; (_, false, nil) on miss.

func (*Pool) Get

func (p *Pool) Get(ctx context.Context, sandboxID string) (*Slot, error)

Get returns the slot owned by sandboxID, or nil.

func (*Pool) MarkRealized

func (p *Pool) MarkRealized(ctx context.Context, sandboxID, netnsPath, containerIP string, now time.Time) (*Slot, error)

MarkRealized records CNI output after host realization.

func (*Pool) Prewarm

func (p *Pool) Prewarm(ctx context.Context, host HostManager, now time.Time) error

Prewarm reserves a free slot under its slot_id, runs CNI ADD, and parks it in the pooled warm queue.

func (*Pool) ReassignOwner

func (p *Pool) ReassignOwner(ctx context.Context, fromSandboxID, toSandboxID string, now time.Time) error

ReassignOwner moves an adopted slot from a park slot id to the real sandbox id.

func (*Pool) Reconcile

func (p *Pool) Reconcile(ctx context.Context, host HostManager, live LiveSandbox, exists NetnsExists, now time.Time) (reaped int, err error)

Reconcile tears down orphaned reserved/realized/pooled/adopted rows after a crash or daemon restart, and retries slots parked as releasing. A warm (pooled) slot survives only when its netns is there AND holds the address the row records; otherwise it is rebuilt, never handed out. live==nil treats every adopted/reserved owner as dead, so production wiring must pass a callback that answers "dead" only when the owner's container is confirmed gone: reaping a live owner runs CNI DEL inside its netns and deletes it, cutting a running sandbox off the network.

func (*Pool) Release

func (p *Pool) Release(ctx context.Context, sandboxID string, now time.Time) error

Release returns a sandbox's slot to the pool. Idempotent. It only moves the row; callers that own host state go through teardown.

func (*Pool) Reserve

func (p *Pool) Reserve(ctx context.Context, sandboxID string, now time.Time) (*Slot, error)

Reserve claims a slot for sandboxID. Idempotent for the same sandbox. A create reserves after a warm queue miss, to build inline, so this is where a miss wakes the refiller: only once the create holds its slot. Woken at the miss itself, the refill could take the last free slot first and fail a create that would have fit.

func (*Pool) RetryReleasing added in v0.6.9

func (p *Pool) RetryReleasing(ctx context.Context, host HostManager, now time.Time) (int, error)

RetryReleasing re-runs the host teardown for every slot parked as releasing and frees each one whose teardown now succeeds. Returns how many it freed.

func (*Pool) Seed

func (p *Pool) Seed(ctx context.Context, cfg SeedConfig, now time.Time) error

Seed lays out PoolSize free slots. Idempotent on slot_id PK.

func (*Pool) SetLogger added in v0.6.9

func (p *Pool) SetLogger(logger *slog.Logger)

SetLogger routes the pool's teardown warnings (failed CNI DEL / netns delete) to logger. Nil restores the discard default.

func (*Pool) Stats

func (p *Pool) Stats(ctx context.Context) (Stats, error)

func (*Pool) TargetDepth

func (p *Pool) TargetDepth(ctx context.Context, depth int) (need int, err error)

TargetDepth is the desired count of pooled+warm-ready slots.

type Refiller

type Refiller struct {
	// contains filtered or unexported fields
}

Refiller maintains the pooled warm depth. A create taking a slot wakes it (Pool.claimed); the ticker stays as the backstop for what no claim signals: a slot a release frees while the queue is short, a failed prewarm with no create behind it, and the retry of slots parked as releasing. Every pass runs on Run's one goroutine, so passes never overlap. That single flight is what keeps the queue at or under depth: TargetDepth counts only finished warm slots, so two passes at once would both fill the same gap.

func NewRefiller

func NewRefiller(pool *Pool, host HostManager, depth int, interval time.Duration) *Refiller

func (*Refiller) Run

func (r *Refiller) Run(ctx context.Context)

func (*Refiller) Stop

func (r *Refiller) Stop()

Stop halts the refill loop.

type RuntimeHandoff

type RuntimeHandoff struct {
	// contains filtered or unexported fields
}

RuntimeHandoff adapts the native netns pool to the containerd driver's NetnsHandoff seam: Provision runs the reserve→realize→adopt FSM; Release tears down host resources and returns the slot to the free pool.

func NewRuntimeHandoff

func NewRuntimeHandoff(pool *Pool, host HostManager) *RuntimeHandoff

func (*RuntimeHandoff) Provision

func (h *RuntimeHandoff) Provision(ctx context.Context, sandboxID string) (netnsPath, containerIP string, err error)

func (*RuntimeHandoff) ReassignOwner

func (h *RuntimeHandoff) ReassignOwner(ctx context.Context, fromSandboxID, toSandboxID string) error

ReassignOwner transfers netns slot ownership from a park slot id to the adopted sandbox id (rename-free warm adopt).

func (*RuntimeHandoff) Release

func (h *RuntimeHandoff) Release(ctx context.Context, sandboxID string) error

Release tears down sandboxID's netns and returns the slot to the pool. Idempotent. The host teardown uses the identity CNI ADD used (which is NOT sandboxID for a claimed prewarmed slot or an adopted warm-park slot). If it fails, the slot is parked as releasing and retried by the refiller rather than freed with its lease and NAT chain still on the node; Release still returns nil then, because the failure is owned by the retry from here on.

type SeedConfig

type SeedConfig struct {
	PoolSize int
}

SeedConfig describes how many native netns slots to pre-create at boot.

type Slot

type Slot struct {
	SlotID      string
	NetnsPath   string
	ContainerIP string
	SandboxID   string
	State       string
}

Slot is the pool-level view of a claimed netns slot.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL