Documentation
¶
Overview ¶
Package ipam is a ip address management library for ip's and prefixes (networks).
It uses either memory or postgresql database to store the ip's and prefixes. You can also bring you own Storage implementation as you need.
Example usage:
import (
"fmt"
goipam "github.com/metal-stack/go-ipam"
)
func main() {
// create a ipamer with in memory storage
ipam := goipam.New()
prefix, err := ipam.NewPrefix("192.168.0.0/24")
if err != nil {
panic(err)
}
ip, err := ipam.AcquireIP(prefix)
if err != nil {
panic(err)
}
fmt.Printf("got IP: %s", ip.IP)
err = ipam.ReleaseIP(ip)
if err != nil {
panic(err)
}
fmt.Printf("IP: %s released.", ip.IP)
}
Index ¶
- func JitterDelay(_ uint, config *retry.Config) time.Duration
- func NewMemory() *memory
- func NewPostgresStorage(host, port, user, password, dbname, sslmode string) (*sql, error)
- type IP
- type Ipamer
- func (i *Ipamer) AcquireChildPrefix(parentCidr string, length int) (*Prefix, error)
- func (i *Ipamer) AcquireIP(prefixCidr string) (*IP, error)
- func (i *Ipamer) AcquireSpecificIP(prefixCidr, specificIP string) (*IP, error)
- func (i *Ipamer) DeletePrefix(cidr string) (*Prefix, error)
- func (i *Ipamer) GetHostAddresses(prefix string) ([]string, error)
- func (i *Ipamer) NewPrefix(cidr string) (*Prefix, error)
- func (i *Ipamer) PrefixFrom(cidr string) *Prefix
- func (i *Ipamer) PrefixesOverlapping(existingPrefixes []string, newPrefixes []string) error
- func (i *Ipamer) ReleaseChildPrefix(child *Prefix) error
- func (i *Ipamer) ReleaseIP(ip *IP) (*Prefix, error)
- func (i *Ipamer) ReleaseIPFromPrefix(prefixCidr, ip string) error
- type NoIPAvailableError
- type NotFoundError
- type OptimisticLockError
- type Prefix
- type Storage
- type Usage
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func JitterDelay ¶
JitterDelay is a DelayType which varies delay in each iterations
func NewPostgresStorage ¶
NewPostgresStorage creates a new Storage which uses postgres.
Types ¶
type Ipamer ¶
type Ipamer struct {
// contains filtered or unexported fields
}
Ipamer can be used to do IPAM stuff.
func New ¶
func New() *Ipamer
New returns a Ipamer with in memory storage for networks, prefixes and ips.
func NewWithStorage ¶
NewWithStorage allows you to create a Ipamer instance with your Storage implementation. The Storage interface must be implemented.
func (*Ipamer) AcquireChildPrefix ¶
AcquireChildPrefix will return a Prefix with a smaller length from the given Prefix.
func (*Ipamer) AcquireSpecificIP ¶
AcquireSpecificIP will acquire given IP and mark this IP as used, if already in use, return nil. If specificIP is empty, the next free IP is returned. If there is no free IP an NoIPAvailableError is returned.
func (*Ipamer) DeletePrefix ¶
DeletePrefix delete a Prefix from a string notation. If the Prefix is not found an NotFoundError is returned.
func (*Ipamer) GetHostAddresses ¶
GetHostAddresses will return all possible ipadresses a host can get in the given prefix. The IPs will be acquired by this method, so that the prefix has no free IPs afterwards.
func (*Ipamer) NewPrefix ¶
NewPrefix create a new Prefix from a string notation.
Example ¶
ipamer := New()
prefix, err := ipamer.NewPrefix("192.168.0.0/24")
if err != nil {
panic(err)
}
ip1, err := ipamer.AcquireIP(prefix.Cidr)
if err != nil {
panic(err)
}
ip2, err := ipamer.AcquireIP(prefix.Cidr)
if err != nil {
panic(err)
}
fmt.Println(prefix)
fmt.Println(ip1.IP.String())
fmt.Println(ip1.ParentPrefix)
fmt.Println(ip2.IP.String())
fmt.Println(ip2.ParentPrefix)
Output: 192.168.0.0/24 192.168.0.1 192.168.0.0/24 192.168.0.2 192.168.0.0/24
func (*Ipamer) PrefixFrom ¶
PrefixFrom will return a known Prefix.
func (*Ipamer) PrefixesOverlapping ¶
PrefixesOverlapping will check if one ore more prefix of newPrefixes is overlapping with one of existingPrefixes
func (*Ipamer) ReleaseChildPrefix ¶
ReleaseChildPrefix will mark this child Prefix as available again.
func (*Ipamer) ReleaseIP ¶
ReleaseIP will release the given IP for later usage and returns the updated Prefix. If the IP is not found an NotFoundError is returned.
func (*Ipamer) ReleaseIPFromPrefix ¶
ReleaseIPFromPrefix will release the given IP for later usage. If the Prefix or the IP is not found an NotFoundError is returned.
type NoIPAvailableError ¶
type NoIPAvailableError struct {
// contains filtered or unexported fields
}
NoIPAvailableError indicates that the acquire-operation could not be executed because the specified prefix has no free IP anymore.
func (NoIPAvailableError) Error ¶
func (o NoIPAvailableError) Error() string
type NotFoundError ¶ added in v1.4.0
type NotFoundError struct {
// contains filtered or unexported fields
}
NotFoundError is raised if the given Prefix or Cidr was not found
func (NotFoundError) Error ¶ added in v1.4.0
func (n NotFoundError) Error() string
type OptimisticLockError ¶
type OptimisticLockError struct {
// contains filtered or unexported fields
}
OptimisticLockError indicates that the operation could not be executed because the dataset to update has changed in the meantime. clients can decide to read the current dataset and retry the operation.
func (OptimisticLockError) Error ¶
func (o OptimisticLockError) Error() string
type Prefix ¶
type Prefix struct {
Cidr string // The Cidr of this prefix
ParentCidr string // if this prefix is a child this is a pointer back
// contains filtered or unexported fields
}
Prefix is a expression of a ip with length and forms a classless network.