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 ¶
- Constants
- Variables
- func NewContextWithNamespace(ctx context.Context, namespace string) context.Context
- func PrefixesOverlapping(existingPrefixes []string, newPrefixes []string) error
- type BunPrefix
- type Bundb
- func (s *Bundb) ApplyDbSchema() error
- func (s *Bundb) CreatePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
- func (s *Bundb) DeleteAllPrefixes(ctx context.Context, namespace string) error
- func (s *Bundb) DeleteAllPrefixesFromAllNamespaces(ctx context.Context) error
- func (s *Bundb) DeletePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
- func (s *Bundb) Name() string
- func (s *Bundb) ReadAllPrefixCidrs(ctx context.Context, namespace string) ([]string, error)
- func (s *Bundb) ReadAllPrefixes(ctx context.Context, namespace string) (Prefixes, error)
- func (s *Bundb) ReadPrefix(ctx context.Context, prefix, namespace string) (Prefix, error)
- func (s *Bundb) UpdatePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
- type IP
- type Ipamer
- type MongoConfig
- type Prefix
- type Prefixes
- type SSLMode
- type Storage
- func NewEtcd(ctx context.Context, ip, port string, cert, key []byte, insecureskip bool) (Storage, error)
- func NewLocalFile(ctx context.Context, path string) Storage
- func NewMemory(ctx context.Context) Storage
- func NewMongo(ctx context.Context, config MongoConfig) (Storage, error)
- func NewPostgresStorage(host, port, user, password, dbname string, sslmode SSLMode) (Storage, error)
- func NewRedis(ctx context.Context, ip, port string) (Storage, error)
- type Usage
Constants ¶
const ( // SSLModeAllow I don't care about security // but I will pay the overhead of encryption if the server insists on it SSLModeAllow = SSLMode("allow") // SSLModeDisable I don't care about security // and I don't want to pay the overhead of encryption. SSLModeDisable = SSLMode("disable") // SSLModePrefer I don't care about encryption // but I wish to pay the overhead of encryption if the server supports it. SSLModePrefer = SSLMode("prefer") // SSLModeRequire I want my data to be encrypted and I accept the overhead. // I trust that the network will make sure I always connect to the server I want. SSLModeRequire = SSLMode("require") // SSLModeVerifyCA I want my data encrypted and I accept the overhead. // I want to be sure that I connect to a server that I trust. SSLModeVerifyCA = SSLMode("verify-ca") // SSLModeVerifyFull I want my data encrypted and I accept the overhead. // I want to be sure that I connect to a server I trust, and that it's the one I specify. SSLModeVerifyFull = SSLMode("verify-full") )
Variables ¶
var ( // ErrNotFound is returned if prefix or cidr was not found ErrNotFound = errors.New("NotFound") // ErrNoIPAvailable is returned if no IP is available anymore ErrNoIPAvailable = errors.New("NoIPAvailableError") // ErrAlreadyAllocated is returned if the requested address is not available ErrAlreadyAllocated = errors.New("AlreadyAllocatedError") // ErrOptimisticLockError is returned if insert or update conflicts with the existing data ErrOptimisticLockError = errors.New("OptimisticLockError") // ErrNamespaceDoesNotExist is returned when an operation is perfomed in a namespace that does not exist. ErrNamespaceDoesNotExist = errors.New("NamespaceDoesNotExist") // ErrNameTooLong is returned when a name exceeds the databases max identifier length ErrNameTooLong = errors.New("NameTooLong") // ErrNotImplemented is returned for CreateNamespace, ListNamespaces, DeleteNamespace apis ErrNotImplemented = errors.New("NotImplemented") )
var (
DefaultLocalFilePath string
)
Functions ¶
func NewContextWithNamespace ¶
func PrefixesOverlapping ¶
PrefixesOverlapping will check if one ore more prefix of newPrefixes is overlapping with one of existingPrefixes
Types ¶
type BunPrefix ¶
type BunPrefix struct {
bun.BaseModel `bun:"table:prefixes,alias:bp"`
Cidr string `bun:"cidr,notnull"`
Prefix json.RawMessage `bun:"prefix,type:jsonb"`
Namespace string `bun:"namespace,notnull"`
}
BunPrefix is a bun model of the ipam database table
type Bundb ¶
type Bundb struct {
// contains filtered or unexported fields
}
Bundb represents the bun ipam storage. it has the bun db session, and possibly a user transaction that needs to be used
func NewBunStorage ¶
NewBunStorage will create a new Bundb interface to ipam
func (*Bundb) ApplyDbSchema ¶
ApplyDbSchema will apply the original ipam db schema this schema is idempotent (ie, if schema already exists, no harm applying it again) this function will be incorporated into cloud-db as a bun migration
func (*Bundb) CreatePrefix ¶
CreatePrefix - create the prefix
func (*Bundb) DeleteAllPrefixes ¶
DeleteAllPrefixes will delete all prefixes - used in tests
func (*Bundb) DeleteAllPrefixesFromAllNamespaces ¶
DeleteAllPrefixes will delete all prefixes - used in tests
func (*Bundb) DeletePrefix ¶
DeletePrefix will delete a prefix
func (*Bundb) ReadAllPrefixCidrs ¶
ReadAllPrefixCidrs is cheaper than ReadAllPrefixes because it only returns the Cidrs.
func (*Bundb) ReadAllPrefixes ¶
ReadPrefixes - read all the prefixes for the namespace
func (*Bundb) ReadPrefix ¶
ReadPrefix - read the prefix
type Ipamer ¶
type Ipamer interface {
// SetNamespace to use for separating prefixes which is required to have overlapping prefixes for different purposes.
// Prefixes must still be collision free inside the same namespace.
// if not set, all prefixes/ips belong to a empty default namespace, collisions are not possible.
SetNamespace(namespace string)
// NewPrefix creates a new Prefix from a string notation.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
NewPrefix(ctx context.Context, cidr string) (*Prefix, error)
// DeletePrefix delete a Prefix from a string notation.
// If the Prefix is not found an NotFoundError is returned.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
DeletePrefix(ctx context.Context, cidr string) (*Prefix, error)
// AcquireChildPrefix will return a Prefix with a smaller length from the given Prefix.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
AcquireChildPrefix(ctx context.Context, parentCidr string, length uint8) (*Prefix, error)
// AcquireSpecificChildPrefix will return a Prefix with a smaller length from the given Prefix.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
AcquireSpecificChildPrefix(ctx context.Context, parentCidr, childCidr string) (*Prefix, error)
// ReleaseChildPrefix will mark this child Prefix as available again.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
ReleaseChildPrefix(ctx context.Context, child *Prefix) error
// PrefixFrom will return a known Prefix.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
PrefixFrom(ctx context.Context, cidr string) *Prefix
// 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.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
AcquireSpecificIP(ctx context.Context, prefixCidr, specificIP string) (*IP, error)
// AcquireIP will return the next unused IP from this Prefix.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
AcquireIP(ctx context.Context, prefixCidr string) (*IP, error)
// ReleaseIP will release the given IP for later usage and returns the updated Prefix.
// If the IP is not found an NotFoundError is returned.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
ReleaseIP(ctx context.Context, ip *IP) (*Prefix, error)
// ReleaseIPFromPrefix will release the given IP for later usage.
// If the Prefix or the IP is not found an NotFoundError is returned.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
ReleaseIPFromPrefix(ctx context.Context, prefixCidr, ip string) error
// Dump all stored prefixes as json formatted string
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
Dump(ctx context.Context) (string, error)
// Load a previously created json formatted dump, deletes all prefixes before loading.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
Load(ctx context.Context, dump string) error
// ReadAllPrefixCidrs retrieves all existing Prefix CIDRs from the underlying storage.
// This operation is scoped to the root namespace unless a different namespace is provided in the context.
ReadAllPrefixCidrs(ctx context.Context) ([]string, error)
// CreateNamespace creates a namespace with the given name.
// Any namespace provided in the context is ignored for this operation.
// It is idempotent, so attempts to create a namespace which already exists will not return an error.
CreateNamespace(ctx context.Context, namespace string) error
// ListNamespaces returns a list of all namespaces.
// Any namespace provided in the context is ignored for this operation.
ListNamespaces(ctx context.Context) ([]string, error)
// DeleteNamespaces deletes the namespace with the given name.
// Any namespace provided in the context is ignored for this operation.
// It not idempotent, so attempts to delete a namespace which does not exist will return an error.
DeleteNamespace(ctx context.Context, namespace string) error
// GetSubnetCount will return the subnet count of a given prefix.
GetSubnetCount(ctx context.Context, prefixCidr string) uint64
}
Ipamer can be used to do IPAM stuff.
func NewWithStorage ¶
NewWithStorage allows you to create a Ipamer instance with your Storage implementation. The Storage interface must be implemented.
type MongoConfig ¶
type MongoConfig struct {
DatabaseName string
CollectionName string
MongoClientOptions *options.ClientOptions
}
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
Namespace string // if set overlapping prefixes are possible
// contains filtered or unexported fields
}
Prefix is a expression of a ip with length and forms a classless network.
type SSLMode ¶
type SSLMode string
SSLMode specifies how to configure ssl encryption to the database
type Storage ¶
type Storage interface {
Name() string
CreatePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
ReadPrefix(ctx context.Context, prefix string, namespace string) (Prefix, error)
DeleteAllPrefixes(ctx context.Context, namespace string) error
ReadAllPrefixes(ctx context.Context, namespace string) (Prefixes, error)
ReadAllPrefixCidrs(ctx context.Context, namespace string) ([]string, error)
UpdatePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
DeletePrefix(ctx context.Context, prefix Prefix, namespace string) (Prefix, error)
CreateNamespace(ctx context.Context, namespace string) error
ListNamespaces(ctx context.Context) ([]string, error)
DeleteNamespace(ctx context.Context, namespace string) error
}
Storage is a interface to store ipam objects.
func NewEtcd ¶
func NewEtcd(ctx context.Context, ip, port string, cert, key []byte, insecureskip bool) (Storage, error)
NewEtcd create a etcd storage for ipam
func NewLocalFile ¶
NewLocalFile creates a JSON file storage for ipam
type Usage ¶
type Usage struct {
// AvailableIPs the number of available IPs if this is not a parent prefix
// No more than 2^31 available IPs are reported
AvailableIPs uint64
// AcquiredIPs the number of acquired IPs if this is not a parent prefix
AcquiredIPs uint64
// AvailableSmallestPrefixes is the count of available Prefixes with 2 countable Bits
// No more than 2^31 available Prefixes are reported
AvailableSmallestPrefixes uint64
// AvailablePrefixes is a list of prefixes which are available
AvailablePrefixes []string
// AcquiredPrefixes the number of acquired prefixes if this is a parent prefix
AcquiredPrefixes uint64
}
Usage of ips and child Prefixes of a Prefix