ipam

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: PostgreSQL Imports: 13 Imported by: 0

Documentation

Overview

This is based on the IP allocator from github.com/cilium/ipam/service/ipallocator, but modified to allocate subnets rather than individual IPs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFull              = errors.New("range is full")
	ErrAllocated         = errors.New("provided subnet is already allocated")
	ErrMismatchedNetwork = errors.New("the provided network does not match the current range")
)

Functions

func GetIndexedIP

func GetIndexedIP(subnet netip.Prefix, index int) (netip.Addr, error)

GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.

func Provide

func Provide(i *do.Injector)

func RangeSize

func RangeSize(prefix netip.Prefix) int64

RangeSize returns the size of a range in valid addresses.

Types

type ErrNotInRange

type ErrNotInRange struct {
	ValidRange string
}

func (*ErrNotInRange) Error

func (e *ErrNotInRange) Error() string

type Service

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

func NewService

func NewService(logger zerolog.Logger, store *Store) *Service

func (*Service) AllocateSubnet

func (s *Service) AllocateSubnet(ctx context.Context, prefix netip.Prefix, bits int) (netip.Prefix, error)

type Store

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

func NewStore

func NewStore(client *clientv3.Client, root string) *Store

func (*Store) ExistsByKey

func (s *Store) ExistsByKey(allocatorName string) storage.ExistsOp

func (*Store) GetByKey

func (s *Store) GetByKey(allocatorName string) storage.GetOp[*StoredSubnetRange]

func (*Store) Key

func (s *Store) Key(allocatorName string) string

func (*Store) Prefix

func (s *Store) Prefix() string

func (*Store) Update

type StoredSubnetRange

type StoredSubnetRange struct {
	storage.StoredValue
	Name     string `json:"name"`
	Spec     string `json:"spec"`
	Snapshot []byte `json:"snapshot"`
}

type SubnetRange

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

SubnetRange is a contiguous block of IP subnets that can be allocated atomically.

The internal structure of the range is:

For CIDR 172.17.96.0/20 with a subnet size of 16:
256 subnets

First subnet                  Last subnet
172.17.96.0/28                172.17.111.255/28
      |                             |
   r.base                    r.base + r.max
      |                             |
offset #0 of r.allocated   last offset of r.allocated

func NewAllocatorSubnetRange

func NewAllocatorSubnetRange(spec SubnetRangeSpec, allocatorFactory allocator.AllocatorFactory) (*SubnetRange, error)

NewAllocatorSubnetRange creates a SubnetRange for the given spec, calling allocatorFactory to construct the backing store.

func NewSubnetRange

func NewSubnetRange(spec SubnetRangeSpec) (*SubnetRange, error)

Helper that wraps NewAllocatorSubnetRange, for creating a range backed by an in-memory store.

func (*SubnetRange) Allocate

func (r *SubnetRange) Allocate(cidr netip.Prefix) error

Allocate attempts to reserve the provided IP. ErrNotInRange or ErrAllocated will be returned if the IP is not valid for this range or has already been reserved. ErrFull will be returned if there are no addresses left.

func (*SubnetRange) AllocateNext

func (r *SubnetRange) AllocateNext() (netip.Prefix, error)

AllocateNext reserves one of the IPs from the pool. ErrFull may be returned if there are no addresses left.

func (*SubnetRange) CIDR

func (r *SubnetRange) CIDR() netip.Prefix

CIDR returns the CIDR covered by the range.

func (*SubnetRange) ForEach

func (r *SubnetRange) ForEach(fn func(netip.Prefix))

ForEach calls the provided function for each allocated IP.

func (*SubnetRange) Free

func (r *SubnetRange) Free() int

Free returns the count of subnets left in the range.

func (*SubnetRange) Has

func (r *SubnetRange) Has(subnet netip.Prefix) bool

Has returns true if the provided IP is already allocated and a call to Allocate(ip) would fail with ErrAllocated.

func (*SubnetRange) Release

func (r *SubnetRange) Release(subnet netip.Prefix) error

Release releases the IP back to the pool. Releasing an unallocated IP or an IP out of the range is a no-op and returns no error.

func (*SubnetRange) Restore

func (r *SubnetRange) Restore(specStr string, data []byte) error

Restore restores the pool to the previously captured state. ErrMismatchedNetwork is returned if the provided IPNet range doesn't exactly match the previous range.

func (*SubnetRange) Snapshot

func (r *SubnetRange) Snapshot() (string, []byte, error)

Snapshot saves the current state of the pool.

func (*SubnetRange) Used

func (r *SubnetRange) Used() int

Used returns the count of subnets used in the range.

type SubnetRangeSpec

type SubnetRangeSpec struct {
	CIDR       netip.Prefix `json:"cidr"`
	SubnetBits int          `json:"subnet_bits"`
}

SubnetRangeSpec is used to identify the allocator in snapshots, any changes to its fields or format will affect the ability to restore older ranges from storage.

func ParseSubnetRangeSpec

func ParseSubnetRangeSpec(str string) (SubnetRangeSpec, error)

func (SubnetRangeSpec) String

func (r SubnetRangeSpec) String() (string, error)

Jump to

Keyboard shortcuts

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