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 ¶
- Variables
- func DeriveIPv6(ulaPrefix, ipv4Subnet string) (string, error)
- type NetworkProvisioner
- type Service
- func (s *Service) Allocate(dockerName string, nodeID uint, kind string) (*models.NetworkAllocation, error)
- func (s *Service) Capacity() int
- func (s *Service) EnsureManaged(ctx context.Context, dc NetworkProvisioner, spec docker.NetworkSpec, ...) (id, subnet string, err error)
- func (s *Service) ImportExisting(ctx context.Context, dc NetworkProvisioner, nodeID uint) error
- func (s *Service) PublishStats()
- func (s *Service) Release(dockerName string) error
- func (s *Service) SetIPv6(enabled bool, ulaPrefix string)
- func (s *Service) Stats() (used int, total int)
Constants ¶
This section is empty.
Variables ¶
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
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) 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 ¶
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 ¶
Release frees the allocation for a removed network so its subnet returns to the pool. Idempotent.