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-pod/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 NewMemory() *memory
- func NewPostgresStorage(host, port, user, password, dbname, sslmode string) (*sql, error)
- type IP
- type Ipamer
- func (i *Ipamer) AcquireChildPrefix(prefix *Prefix, length int) (*Prefix, error)
- func (i *Ipamer) AcquireIP(prefix *Prefix) (*IP, error)
- func (i *Ipamer) DeletePrefix(cidr string) (*Prefix, 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(prefix *Prefix, ip string) error
- type Prefix
- type Storage
- type Usage
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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. FIXME allow variable child prefix length
func (*Ipamer) DeletePrefix ¶
DeletePrefix delete a Prefix from a string notation.
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)
if err != nil {
panic(err)
}
ip2, err := ipamer.AcquireIP(prefix)
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 FIXME should we change signature to PrefixOverlapping(newPrefix string) only and find all non superPrefixes ourselves that requires that newPrefix was not persisted before and we must implement .IPNet here as well.
func (*Ipamer) ReleaseChildPrefix ¶
ReleaseChildPrefix will mark this child Prefix as available again.
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.
type Storage ¶
type Storage interface {
CreatePrefix(prefix *Prefix) (*Prefix, error)
ReadPrefix(prefix string) (*Prefix, error)
ReadAllPrefixes() ([]*Prefix, error)
UpdatePrefix(prefix *Prefix) (*Prefix, error)
DeletePrefix(prefix *Prefix) (*Prefix, error)
}
Storage is a interface to store ipam objects.