ipallocator

package
v0.0.0-...-d7c47dd Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package ipallocator deals with assigning CIDR blocks to environments, and subnets within those blocks for specific networks.

Index

Constants

View Source
const (
	MethodAllocateNetworkBlock      = "AllocateNetworkBlock"
	MethodAllocateSubnetBlock       = "AllocateSubnetBlock"
	MethodReleaseNetworkBlock       = "ReleaseNetworkBlock"
	MethodReleaseSubnetBlock        = "ReleaseSubnetBlock"
	MethodGetNetworkBlock           = "GetNetworkBlock"
	MethodGetAllocatedNetworkBlocks = "GetAllocatedNetworkBlocks"
	MethodHasNetworkBlock           = "HasNetworkBlock"
	MethodGetReservedRanges         = "GetReservedRanges"
	MethodInitialize                = "Initialize"
)

Method names for the IPAllocator service

Variables

This section is empty.

Functions

func AlignToBlockBoundary

func AlignToBlockBoundary(addr netip.Addr, blockSize int) (netip.Addr, error)

AlignToBlockBoundary aligns an IP address to the block boundary for the given block size

func FindNextAvailableBlock

func FindNextAvailableBlock(allowedPrefix netip.Prefix, allocated []NetworkBlock, reserved []ReservedRange, blockSize int) (netip.Addr, error)

FindNextAvailableBlock locates the next available IP block of the requested size

func GetFirstUsableIP

func GetFirstUsableIP(prefix netip.Prefix) netip.Addr

GetFirstUsableIP returns the first usable IP in a subnet (excludes network address)

func GetGatewayIP

func GetGatewayIP(prefix netip.Prefix) netip.Addr

GetGatewayIP typically returns the first usable IP as the gateway

func GetLastUsableIP

func GetLastUsableIP(prefix netip.Prefix) netip.Addr

GetLastUsableIP returns the last usable IP in a subnet (excludes broadcast address)

func GetNextIP

func GetNextIP(ip netip.Addr) netip.Addr

GetNextIP returns the next IP address after the given IP

func GetNextNIPs

func GetNextNIPs(startIP netip.Addr, n int) ([]netip.Addr, error)

GetNextNIPs returns the next n IP addresses after the given IP

func GetSubnetSize

func GetSubnetSize(prefix netip.Prefix) int

GetSubnetSize returns the total number of IPs in a subnet

func GetUsableIPCount

func GetUsableIPCount(prefix netip.Prefix) int

GetUsableIPCount returns the number of usable IPs (excluding network and broadcast)

func IPToUint32

func IPToUint32(ip netip.Addr) (uint32, error)

IPToUint32 converts an IPv4 address to a uint32 integer

func IsSubnetNameOf

func IsSubnetNameOf(subnetName, parentName string) bool

IsSubnetNameOf determines if a subnet name belongs to a parent network

func IsSubnetOf

func IsSubnetOf(subnet, parent netip.Prefix) bool

IsSubnetOf checks if subnet is contained within parent prefix

func RangesOverlap

func RangesOverlap(r1, r2 netipx.IPRange) bool

RangesOverlap checks if two IP ranges overlap

func Uint32ToIP

func Uint32ToIP(n uint32) netip.Addr

Uint32ToIP converts a uint32 integer to an IPv4 address

Types

type AllocationParams

type AllocationParams struct {
	Network string `json:"network"`
	// BlockSize represents a CIDR block, eg 24 = 256.
	BlockSize int `json:"blockSize"`
}

AllocationParams defines parameters for allocating a new network block

type AllocatorState

type AllocatorState struct {
	AllowedCIDR      string          `json:"allowedCIDR"`
	DefaultBlockSize int             `json:"defaultBlockSize"`
	ReservedRanges   []ReservedRange `json:"reservedRanges"`
	AllocatedBlocks  allocatedBlocks `json:"allocatedBlocks"`
}

AllocatorState provides a complete overview of the IP allocator configuration and current state

type IPAllocator

type IPAllocator struct{}

IPAllocator manages IP address allocation for networks and their subnets

func (*IPAllocator) AllocateNetworkBlock

func (i *IPAllocator) AllocateNetworkBlock(ctx restate.ObjectContext, params AllocationParams) (*NetworkBlock, error)

AllocateNetworkBlock allocates a new block within the main subnet for a specific network. If the network already has an allocation, returns the existing allocation.

func (*IPAllocator) AllocateSubnetBlock

func (i *IPAllocator) AllocateSubnetBlock(ctx restate.ObjectContext, params SubnetAllocationParams) (*NetworkBlock, error)

AllocateSubnetBlock allocates a subnet within an existing network block The subnet will be named "parentNetwork-subnetName" and must have a larger prefix size than its parent network

func (*IPAllocator) GetAllocatedBlocks

func (i *IPAllocator) GetAllocatedBlocks(ctx restate.ObjectSharedContext) (allocatedBlocks, error)

GetAllocatedBlocks returns all allocated network blocks

func (*IPAllocator) GetAllocatorState

func (i *IPAllocator) GetAllocatorState(ctx restate.ObjectSharedContext) (*AllocatorState, error)

GetAllocatorState returns comprehensive information about the allocator configuration and current state

func (*IPAllocator) GetNetworkBlock

func (i *IPAllocator) GetNetworkBlock(ctx restate.ObjectSharedContext, network string) (*NetworkBlock, error)

GetNetworkBlock returns a specific network's allocation

func (*IPAllocator) GetReservedRanges

func (i *IPAllocator) GetReservedRanges(ctx restate.ObjectSharedContext) ([]ReservedRange, error)

GetReservedRanges returns the reserved ranges from configuration

func (*IPAllocator) HasNetworkBlock

func (i *IPAllocator) HasNetworkBlock(ctx restate.ObjectSharedContext, network string) (bool, error)

HasNetworkBlock checks if a network has an IP allocation

func (*IPAllocator) Initialize

func (i *IPAllocator) Initialize(ctx restate.ObjectContext, config IPAllocatorConfig) error

Initialize sets up the IP allocator with a given configuration, which defines the main subnet this allocator will provide blocks for.

func (*IPAllocator) ReleaseNetworkBlock

func (i *IPAllocator) ReleaseNetworkBlock(ctx restate.ObjectContext, network string) error

ReleaseNetworkBlock removes a network block allocation and any of its subnets

func (*IPAllocator) ReleaseSubnetBlock

func (i *IPAllocator) ReleaseSubnetBlock(ctx restate.ObjectContext, params struct {
	ParentNetwork string `json:"parentNetwork"`
	SubnetName    string `json:"subnetName"`
},
) error

ReleaseSubnetBlock releases a specific subnet without affecting its parent network

func (*IPAllocator) ResetAllocator

func (i *IPAllocator) ResetAllocator(ctx restate.ObjectContext) error

ResetAllocator clears all state and allows re-initialization

type IPAllocatorConfig

type IPAllocatorConfig struct {
	// The overall IP range to allocate from (e.g., "10.0.0.0/8")
	AllowedCIDR string `json:"allowedCIDR"`
	// Default size of allocated blocks (e.g., 24 for /24 blocks)
	DefaultBlockSize int `json:"defaultBlockSize"`
	// Ranges that should be excluded from allocation
	ReservedRanges []ReservedRange `json:"reservedRanges"`
}

IPAllocatorConfig defines the global configuration for the IP allocator

type IPRangeInfo

type IPRangeInfo struct {
	Network     string `json:"network"`
	Netmask     string `json:"netmask"`
	Gateway     string `json:"gateway"`
	Broadcast   string `json:"broadcast"`
	FirstUsable string `json:"firstUsable"`
	LastUsable  string `json:"lastUsable"`
	TotalIPs    int    `json:"totalIPs"`
	UsableIPs   int    `json:"usableIPs"`
}

IPRangeInfo provides detailed information about an IP range

func GetIPRangeInfo

func GetIPRangeInfo(cidr string) (*IPRangeInfo, error)

GetIPRangeInfo returns detailed information about an IP range

type NetworkBlock

type NetworkBlock struct {
	StartIP   string `json:"startIP"`
	CIDR      string `json:"cidr"`
	BlockSize int    `json:"blockSize"`
}

NetworkBlock represents an allocated IP block for a network

type ReservedRange

type ReservedRange struct {
	CIDR        string `json:"cidr"`
	Description string `json:"description"`
}

ReservedRange represents an IP range that is reserved and should not be allocated

type SubnetAllocationParams

type SubnetAllocationParams struct {
	ParentNetwork string `json:"parentNetwork"`
	SubnetName    string `json:"subnetName"`
	BlockSize     int    `json:"blockSize"`
	Offset        int    `json:"offset"`
}

SubnetAllocationParams defines parameters for allocating a subnet within a parent network

Jump to

Keyboard shortcuts

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