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 ¶
- Variables
- func GetIndexedIP(subnet netip.Prefix, index int) (netip.Addr, error)
- func Provide(i *do.Injector)
- func RangeSize(prefix netip.Prefix) int64
- type ErrNotInRange
- type Service
- type Store
- func (s *Store) ExistsByKey(allocatorName string) storage.ExistsOp
- func (s *Store) GetByKey(allocatorName string) storage.GetOp[*StoredSubnetRange]
- func (s *Store) Key(allocatorName string) string
- func (s *Store) Prefix() string
- func (s *Store) Update(item *StoredSubnetRange) storage.PutOp[*StoredSubnetRange]
- type StoredSubnetRange
- type SubnetRange
- func (r *SubnetRange) Allocate(cidr netip.Prefix) error
- func (r *SubnetRange) AllocateNext() (netip.Prefix, error)
- func (r *SubnetRange) CIDR() netip.Prefix
- func (r *SubnetRange) ForEach(fn func(netip.Prefix))
- func (r *SubnetRange) Free() int
- func (r *SubnetRange) Has(subnet netip.Prefix) bool
- func (r *SubnetRange) Release(subnet netip.Prefix) error
- func (r *SubnetRange) Restore(specStr string, data []byte) error
- func (r *SubnetRange) Snapshot() (string, []byte, error)
- func (r *SubnetRange) Used() int
- type SubnetRangeSpec
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func GetIndexedIP ¶
GetIndexedIP returns a net.IP that is subnet.IP + index in the contiguous IP space.
Types ¶
type ErrNotInRange ¶
type ErrNotInRange struct {
ValidRange string
}
func (*ErrNotInRange) Error ¶
func (e *ErrNotInRange) Error() string
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) GetByKey ¶
func (s *Store) GetByKey(allocatorName string) storage.GetOp[*StoredSubnetRange]
func (*Store) Update ¶
func (s *Store) Update(item *StoredSubnetRange) storage.PutOp[*StoredSubnetRange]
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 ¶
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)