ipam

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPoolExhausted indicates no contiguous block of the requested size is available.
	ErrPoolExhausted = errors.New("pool exhausted: no contiguous block available")

	// ErrInvalidCIDR indicates an unparseable CIDR string.
	ErrInvalidCIDR = errors.New("invalid CIDR notation")

	// ErrInvalidRange indicates start IP is after end IP.
	ErrInvalidRange = errors.New("invalid IP range: start is after end")

	// ErrOutOfRange indicates the requested range falls outside the pool.
	ErrOutOfRange = errors.New("range falls outside pool boundaries")
)

Functions

func BuildBitmap

func BuildBitmap(pool PoolState) ([]bool, uint32, error)

BuildBitmap creates a bitmap spanning all allocatable ranges. true = used/reserved/gap, false = free. The bitmap spans from the minimum range start to the maximum range end. Inter-range gaps are marked as used so the allocator's free-block scan only finds free space within ranges.

func CountIPsInRange

func CountIPsInRange(start, end string) (int32, error)

CountIPsInRange returns the number of IPs between start and end (inclusive).

func EnumerateIPs

func EnumerateIPs(start, end string) ([]string, error)

EnumerateIPs returns all IPs between start and end (inclusive).

func FormatCIDR

func FormatCIDR(start, end string) string

FormatCIDR returns CIDR notation if the range is power-of-2 aligned, otherwise returns "start-end" format.

func IPToUint32

func IPToUint32(ip net.IP) uint32

IPToUint32 converts a 4-byte IPv4 address to a uint32.

func ParseCIDR

func ParseCIDR(cidr string) (start net.IP, end net.IP, count int32, err error)

ParseCIDR parses a CIDR string and returns the first IP, last IP, and total host count.

func ParseIPRange

func ParseIPRange(rangeStr string) (start, end string, err error)

ParseIPRange parses a range string that could be either CIDR notation or "start-end" format.

func PoolCapacityInfo

func PoolCapacityInfo(pool PoolState) (totalIPs int32, allocatedIPs int32, availableIPs int32, err error)

PoolCapacityInfo computes capacity information for a pool. Total is the sum of all range sizes (not the spanning bitmap size). Reserved and allocated counts are computed only within allocatable ranges.

func SortPoolRefsByPriority

func SortPoolRefsByPriority(refs []PoolRef)

SortPoolRefsByPriority sorts pool references by priority (lower = higher priority). Stable sort preserves order for equal priorities.

func Uint32ToIP

func Uint32ToIP(n uint32) net.IP

Uint32ToIP converts a uint32 to a 4-byte IPv4 address.

func ValidateCIDRContains

func ValidateCIDRContains(outer, inner string) (bool, error)

ValidateCIDRContains checks if the inner CIDR is completely contained within the outer CIDR.

func ValidatePoolSpec

func ValidatePoolSpec(cidr string, ranges []AllocatedRange, reservedCIDRs []string) []string

ValidatePoolSpec validates the internal consistency of a network pool specification. Ranges must be sorted by start IP ascending and non-overlapping.

func ValidateRangeWithinCIDR

func ValidateRangeWithinCIDR(cidr, start, end string) (bool, error)

ValidateRangeWithinCIDR checks if a start-end range is within a CIDR.

Types

type AllocatedRange

type AllocatedRange struct {
	Start string
	End   string
}

AllocatedRange represents a contiguous block of allocated IPs.

type AllocationResult

type AllocationResult struct {
	Start     string
	End       string
	CIDR      string // CIDR notation if power-of-2 aligned, otherwise "start-end"
	Addresses []string
	Count     int32
}

AllocationResult contains the result of a successful allocation.

func AllocatePinnedRange

func AllocatePinnedRange(pool PoolState, start, end string) (*AllocationResult, error)

AllocatePinnedRange validates and allocates a specific IP range. Returns an error if the range is outside the pool, overlaps reserved ranges, or conflicts with existing allocations.

func AllocateRange

func AllocateRange(pool PoolState, requestedCount int32) (*AllocationResult, error)

AllocateRange finds the best-fit contiguous block of the requested size. Best-fit selects the smallest free block that satisfies the request, minimizing fragmentation.

type FragmentationInfo

type FragmentationInfo struct {
	FragmentationPercent int32
	LargestFreeBlock     int32
	FreeBlockCount       int32
	TotalFreeIPs         int32
}

FragmentationInfo contains pool health metrics.

func ComputeFragmentation

func ComputeFragmentation(pool PoolState) (*FragmentationInfo, error)

ComputeFragmentation calculates pool health metrics from a bitmap.

type FreeBlock

type FreeBlock struct {
	StartOffset uint32
	EndOffset   uint32
	Size        uint32
}

FreeBlock represents a contiguous block of free IPs in the pool.

type PoolRef

type PoolRef struct {
	Name     string
	Priority *int32
}

PoolRef is a simplified pool reference for sorting.

type PoolState

type PoolState struct {
	// AllocatableRanges defines the allocatable IP ranges. The bitmap spans
	// from the minimum start to the maximum end, with inter-range gaps
	// marked as used. Ranges must be sorted by Start ascending and
	// non-overlapping.
	AllocatableRanges []AllocatedRange
	// ReservedCIDRs are CIDR ranges excluded from allocation (e.g., management cluster IPs).
	ReservedCIDRs []string
	// ExistingAllocs are currently allocated ranges.
	ExistingAllocs []AllocatedRange
}

PoolState represents the current state of a network pool for allocation purposes. It decouples the allocator from Kubernetes types.

Jump to

Keyboard shortcuts

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