netalloc

package
v1.10.19 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package netalloc hands out non-overlapping subnets, carved from a single platform pool, for every Docker network Miabi creates. It works around Docker's tiny built-in default-address-pools by allocating an explicit /N per network. A DB ledger is the source of truth, so allocations survive restarts.

Index

Constants

This section is empty.

Variables

View Source
var ErrPoolExhausted = errors.New("network subnet pool exhausted")

ErrPoolExhausted is returned when every subnet in the pool is allocated or reserved. The operator must enlarge MIABI_NETWORK_POOL_CIDR.

Functions

func DeriveIPv6 added in v1.10.2

func DeriveIPv6(ulaPrefix, ipv4Subnet string) (string, error)

DeriveIPv6 maps an allocated IPv4 subnet onto a /64 under a ULA prefix, so the two address spaces line up: 10.64.5.0/24 under fd42:6d69:6162::/48 becomes fd42:6d69:6162:4005::/64, and an operator reading a v6 address can see which v4 network it belongs to.

Deriving rather than letting the daemon assign buys two things: it works on Docker Engine 25, which cannot pick a ULA itself, and the addresses are stable and predictable — the same network gets the same /64 on every node and after every recreate, which is what makes a firewall rule possible to write.

The subnet id is the middle 16 bits of the IPv4 network address (its second and third octets). Across the default pool — 10.64.0.0/12 carved into /24s — those bits are unique per subnet, so no two networks collide. An empty prefix means "let the daemon choose" and returns "".

Types

type NetworkProvisioner

type NetworkProvisioner interface {
	EnsureNetworkSpec(ctx context.Context, spec docker.NetworkSpec) (string, error)
	ListNetworks(ctx context.Context) ([]docker.Network, error)
}

NetworkProvisioner is the subset of docker.Client the allocator drives — kept small so both the full client and narrower service-level interfaces (e.g. the stack service's NetworkManager) satisfy it.

type Service

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

Service allocates subnets from the configured pool.

func NewService

func NewService(repo *repositories.NetworkAllocationRepository, poolCIDR string, subnetPrefix int) (*Service, error)

NewService builds the allocator for an IPv4 pool CIDR and a per-network subnet prefix. Returns an error on an invalid or IPv6 pool, or a prefix that doesn't fit inside it — config.validate catches these first at boot, so this is defence in depth.

func (*Service) Allocate

func (s *Service) Allocate(dockerName string, nodeID uint, kind string) (*models.NetworkAllocation, error)

Allocate reserves a subnet for dockerName idempotently: an existing allocation for the same name is returned unchanged, otherwise the lowest-index free subnet is reserved and persisted. Callers that create the Docker network should prefer EnsureManaged, which also handles host-level overlaps.

func (*Service) Capacity

func (s *Service) Capacity() int

Capacity is the total number of subnets the pool can hold.

func (*Service) EnsureManaged

func (s *Service) EnsureManaged(ctx context.Context, dc NetworkProvisioner, spec docker.NetworkSpec, nodeID uint, kind string) (id, subnet string, err error)

func (*Service) ImportExisting

func (s *Service) ImportExisting(ctx context.Context, dc NetworkProvisioner, nodeID uint) error

ImportExisting reserves any pool subnet already in use by a Docker network on the node, so the allocator never hands out an overlapping one. Best-effort and idempotent — called at boot. Networks Miabi already tracks are skipped.

func (*Service) PublishStats

func (s *Service) PublishStats()

PublishStats updates the Prometheus pool-utilization gauges. Called after every mutation so /metrics reflects the current usage without a background loop.

func (*Service) Release

func (s *Service) Release(dockerName string) error

Release frees the allocation for a removed network so its subnet returns to the pool. Idempotent.

func (*Service) SetIPv6 added in v1.10.2

func (s *Service) SetIPv6(enabled bool, ulaPrefix string)

SetIPv6 records the install's IPv6 policy: whether Miabi-created networks are dual-stack, and whether their /64 is derived from the IPv4 subnet under a ULA prefix rather than chosen by the daemon.

func (*Service) Stats

func (s *Service) Stats() (used int, total int)

Stats reports pool utilization (used includes reserved rows).

Jump to

Keyboard shortcuts

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