subnets

package
v0.0.0-...-d02b2c3 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 6 Imported by: 15

Documentation

Overview

The subnets package provides a subnet pool from which networks may be dynamically acquired or statically reserved.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInsufficientSubnets is returned by AcquireDynamically if no more subnets can be allocated.
	ErrInsufficientSubnets = errors.New("insufficient subnets remaining in the pool")

	// ErrInsufficientIPs is returned by AcquireDynamically if no more IPs can be allocated.
	ErrInsufficientIPs = errors.New("insufficient IPs remaining in the pool")

	// ErrReleasedUnallocatedNetwork is returned by Release if the subnet is not allocated.
	ErrReleasedUnallocatedSubnet = errors.New("subnet is not allocated")

	// ErrOverlapsExistingSubnet is returned if a recovered subnet overlaps an existing, allocated subnet
	ErrOverlapsExistingSubnet = errors.New("subnet overlaps an existing subnet")

	// ErrInvalidRange is returned by AcquireStatically and by Recover if the subnet range is invalid.
	ErrInvalidRange = errors.New("subnet has invalid range")

	// ErrInvalidIP is returned if a static IP is requested inside a subnet
	// which does not contain that IP
	ErrInvalidIP = errors.New("the requested IP is not within the subnet")

	// ErrIPAlreadyAcquired is returned if a static IP is requested which has already been allocated
	ErrIPAlreadyAcquired = errors.New("the requested IP is already allocated")

	// ErrIpCannotBeNil is returned by Release(..) and Recover(..) if a nil
	// IP address is passed.
	ErrIpCannotBeNil = errors.New("the IP field cannot be empty")

	ErrIPEqualsGateway   = errors.New("a container IP must not equal the gateway IP")
	ErrIPEqualsBroadcast = errors.New("a container IP must not equal the broadcast IP")
)
View Source
var DynamicIPSelector dynamicIPSelector = 0

DynamicIPSelector requests the next available ("dynamic") IP address from a given subnet. Returns an error if no more IP addresses remain in the subnet.

View Source
var DynamicSubnetSelector dynamicSubnetSelector = 0

DynamicSubnetSelector requests the next unallocated ("dynamic") subnet from the dynamic range. Returns an error if there are no remaining subnets in the dynamic range.

Functions

func BroadcastIP

func BroadcastIP(subnet *net.IPNet) net.IP

Returns the broadcast IP of a subnet.

func GatewayIP

func GatewayIP(subnet *net.IPNet) net.IP

Returns the gateway IP of a given subnet, which is always the maximum valid IP

func NetworkIP

func NetworkIP(subnet *net.IPNet) net.IP

Returns the network IP of a subnet.

Types

type IPSelector

type IPSelector interface {
	// Returns an IP address in the given subnet which is not one of the given existing
	// IP addresses. If no such IP address can be found, returns an error.
	SelectIP(subnet *net.IPNet, existing []net.IP) (net.IP, error)
}

IPSelector is a strategy for selecting an IP address in a subnet.

type Pool

type Pool interface {
	// Allocates an IP address and associates it with a subnet. The subnet is selected by the given SubnetSelector.
	// The IP address is selected by the given IPSelector.
	// Returns a subnet, an IP address, and if either selector fails, an error is returned.
	Acquire(lager.Logger, SubnetSelector, IPSelector) (*net.IPNet, net.IP, error)

	// Releases an IP address associated with an allocated subnet. If the subnet has no other IP
	// addresses associated with it, it is deallocated.
	// Returns an error if the given combination is not already in the pool.
	Release(*net.IPNet, net.IP) error

	// Remove an IP address so it appears to be associated with the given subnet.
	Remove(*net.IPNet, net.IP) error

	// Returns the number of /30 subnets which can be Acquired by a DynamicSubnetSelector.
	Capacity() int

	// Run the provided callback if the given subnet is not in use
	RunIfFree(*net.IPNet, func() error) error
}

func NewPool

func NewPool(ipNet *net.IPNet) Pool

type StaticIPSelector

type StaticIPSelector struct {
	net.IP
}

StaticIPSelector requests a specific ("static") IP address. Returns an error if the IP is already allocated, or if it is outside the given subnet.

func (StaticIPSelector) SelectIP

func (s StaticIPSelector) SelectIP(subnet *net.IPNet, existing []net.IP) (net.IP, error)

type StaticSubnetSelector

type StaticSubnetSelector struct {
	*net.IPNet
}

StaticSubnetSelector requests a specific ("static") subnet. Returns an error if the subnet is already allocated.

func (StaticSubnetSelector) SelectSubnet

func (s StaticSubnetSelector) SelectSubnet(dynamic *net.IPNet, existing []*net.IPNet) (*net.IPNet, error)

type SubnetSelector

type SubnetSelector interface {
	// Returns a subnet based on a dynamic range and some existing statically-allocated
	// subnets. If no suitable subnet can be found, returns an error.
	SelectSubnet(dynamic *net.IPNet, existing []*net.IPNet) (*net.IPNet, error)
}

SubnetSelector is a strategy for selecting a subnet.

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

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