ipam

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2019 License: MIT Imports: 8 Imported by: 0

README

go-ipam

Build Status GoDoc Go Report Card codecov License

go-ipam is a module to handle IPAddress management. It can operate on Networks, Prefixes and IPs.

IP

Most obvious this library is all about ip management. the main purpose is to acquire and release an ip, or a bunch of ip's from prefixes.

Prefix

A prefix is a network with ip and mask, typically in the form of 192.168.0.0/24. To be able to manage IPs you have to create a prefix first.

example usage:


package main

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)

    prefix, err = ipam.ReleaseIP(ip)
    if err != nil {
        panic(err)
    }
    fmt.Printf("IP: %s released.", ip.IP)
}

Performance

BenchmarkNewPrefixMemory-4                500000              4629 ns/op
BenchmarkNewPrefixPostgres-4                 200           7425393 ns/op
BenchmarkAcquireIPMemory-4                2000000              1110 ns/op
BenchmarkAcquireIPPostgres-4                  100          10508799 ns/op
BenchmarkAcquireChildPrefix1-4             300000              3693 ns/op
BenchmarkAcquireChildPrefix2-4             300000              3727 ns/op
BenchmarkAcquireChildPrefix3-4             300000              4036 ns/op
BenchmarkAcquireChildPrefix4-4             300000              4372 ns/op
BenchmarkAcquireChildPrefix5-4             200000              5448 ns/op
BenchmarkAcquireChildPrefix6-4             300000              3729 ns/op
BenchmarkAcquireChildPrefix7-4             300000              3766 ns/op
BenchmarkAcquireChildPrefix8-4             500000              3986 ns/op
BenchmarkAcquireChildPrefix9-4             300000              3934 ns/op
BenchmarkAcquireChildPrefix10-4            300000              3858 ns/op

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMemory

func NewMemory() *memory

NewMemory create a memory storage for ipam

func NewPostgresStorage

func NewPostgresStorage(host, port, user, password, dbname, sslmode string) (*sql, error)

NewPostgresStorage creates a new Storage which uses postgres.

Types

type IP

type IP struct {
	IP           net.IP
	ParentPrefix string
}

IP is a single ipaddress.

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

func NewWithStorage(storage Storage) *Ipamer

NewWithStorage allows you to create a Ipamer instance with your Storage implementation. The Storage interface must be implemented.

func (*Ipamer) AcquireChildPrefix

func (i *Ipamer) AcquireChildPrefix(prefix *Prefix, length int) (*Prefix, error)

AcquireChildPrefix will return a Prefix with a smaller length from the given Prefix. FIXME allow variable child prefix length

func (*Ipamer) AcquireIP

func (i *Ipamer) AcquireIP(prefix *Prefix) (*IP, error)

AcquireIP will return the next unused IP from this Prefix.

func (*Ipamer) DeletePrefix

func (i *Ipamer) DeletePrefix(cidr string) (*Prefix, error)

DeletePrefix delete a Prefix from a string notation.

func (*Ipamer) NewPrefix

func (i *Ipamer) NewPrefix(cidr string) (*Prefix, error)

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

func (i *Ipamer) PrefixFrom(cidr string) *Prefix

PrefixFrom will return a known Prefix

func (*Ipamer) PrefixesOverlapping

func (i *Ipamer) PrefixesOverlapping(existingPrefixes []string, newPrefixes []string) error

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

func (i *Ipamer) ReleaseChildPrefix(child *Prefix) error

ReleaseChildPrefix will mark this child Prefix as available again.

func (*Ipamer) ReleaseIP

func (i *Ipamer) ReleaseIP(ip *IP) (*Prefix, error)

ReleaseIP will release the given IP for later usage and returns the updated Prefix.

func (*Ipamer) ReleaseIPFromPrefix

func (i *Ipamer) ReleaseIPFromPrefix(prefix *Prefix, ip string) error

ReleaseIPFromPrefix will release the given IP for later usage.

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.

func (*Prefix) IPNet

func (p *Prefix) IPNet() (*net.IPNet, error)

IPNet return the net.IPNet part of the Prefix

func (*Prefix) Network

func (p *Prefix) Network() (net.IP, error)

Network return the net.IP part of the Prefix

func (*Prefix) String

func (p *Prefix) String() string

func (*Prefix) Usage

func (p *Prefix) Usage() Usage

Usage report Prefix usage.

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.

type Usage

type Usage struct {
	AvailableIPs      uint64
	AcquiredIPs       uint64
	AvailablePrefixes uint64
	AcquiredPrefixes  uint64
}

Usage of ips and child Prefixes of a Prefix

func (*Usage) String

func (u *Usage) String() string

Jump to

Keyboard shortcuts

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