2017-02-12 13:03:39 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
2016-09-16 06:27:36 +03:00
|
|
|
|
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2016-12-17 05:03:03 +03:00
|
|
|
"time"
|
2016-09-16 06:27:36 +03:00
|
|
|
|
2016-10-07 00:40:29 +03:00
|
|
|
"github.com/Azure/azure-container-networking/common"
|
|
|
|
"github.com/Azure/azure-container-networking/log"
|
2017-01-21 03:21:41 +03:00
|
|
|
"github.com/Azure/azure-container-networking/platform"
|
2016-10-07 00:40:29 +03:00
|
|
|
"github.com/Azure/azure-container-networking/store"
|
2016-09-16 06:27:36 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IPAM store key.
|
|
|
|
storeKey = "IPAM"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddressManager manages the set of address spaces and pools allocated to containers.
|
|
|
|
type addressManager struct {
|
2016-12-17 05:03:03 +03:00
|
|
|
Version string
|
|
|
|
TimeStamp time.Time
|
2016-09-16 06:27:36 +03:00
|
|
|
AddrSpaces map[string]*addressSpace `json:"AddressSpaces"`
|
|
|
|
store store.KeyValueStore
|
|
|
|
source addressConfigSource
|
2017-01-17 00:56:48 +03:00
|
|
|
netApi common.NetApi
|
2016-09-16 06:27:36 +03:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:48:22 +03:00
|
|
|
// AddressManager API.
|
|
|
|
type AddressManager interface {
|
2016-12-06 22:17:47 +03:00
|
|
|
Initialize(config *common.PluginConfig, options map[string]interface{}) error
|
2016-11-22 22:48:22 +03:00
|
|
|
Uninitialize()
|
|
|
|
|
2017-01-20 03:29:55 +03:00
|
|
|
StartSource(options map[string]interface{}) error
|
|
|
|
StopSource()
|
|
|
|
|
2016-11-22 22:48:22 +03:00
|
|
|
GetDefaultAddressSpaces() (string, string)
|
2017-01-20 03:29:55 +03:00
|
|
|
|
2016-11-22 22:48:22 +03:00
|
|
|
RequestPool(asId, poolId, subPoolId string, options map[string]string, v6 bool) (string, string, error)
|
|
|
|
ReleasePool(asId, poolId string) error
|
2016-12-10 03:53:12 +03:00
|
|
|
GetPoolInfo(asId, poolId string) (*AddressPoolInfo, error)
|
|
|
|
|
2016-11-22 22:48:22 +03:00
|
|
|
RequestAddress(asId, poolId, address string, options map[string]string) (string, error)
|
2017-06-13 02:36:21 +03:00
|
|
|
ReleaseAddress(asId, poolId, address string, options map[string]string) error
|
2016-11-22 22:48:22 +03:00
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
// AddressConfigSource configures the address pools managed by AddressManager.
|
|
|
|
type addressConfigSource interface {
|
|
|
|
start(sink addressConfigSink) error
|
|
|
|
stop()
|
|
|
|
refresh() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressConfigSink interface is used by AddressConfigSources to configure address pools.
|
|
|
|
type addressConfigSink interface {
|
2017-01-25 03:20:34 +03:00
|
|
|
newAddressSpace(id string, scope int) (*addressSpace, error)
|
2016-09-16 06:27:36 +03:00
|
|
|
setAddressSpace(*addressSpace) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new address manager.
|
2016-11-22 22:48:22 +03:00
|
|
|
func NewAddressManager() (AddressManager, error) {
|
2016-09-16 06:27:36 +03:00
|
|
|
am := &addressManager{
|
|
|
|
AddrSpaces: make(map[string]*addressSpace),
|
|
|
|
}
|
|
|
|
|
|
|
|
return am, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize configures address manager.
|
2016-12-06 22:17:47 +03:00
|
|
|
func (am *addressManager) Initialize(config *common.PluginConfig, options map[string]interface{}) error {
|
2016-12-17 05:03:03 +03:00
|
|
|
am.Version = config.Version
|
2016-09-16 06:27:36 +03:00
|
|
|
am.store = config.Store
|
2017-01-17 00:56:48 +03:00
|
|
|
am.netApi = config.NetApi
|
2016-09-16 06:27:36 +03:00
|
|
|
|
|
|
|
// Restore persisted state.
|
|
|
|
err := am.restore()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start source.
|
2017-01-20 03:29:55 +03:00
|
|
|
err = am.StartSource(options)
|
2016-09-16 06:27:36 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninitialize cleans up address manager.
|
|
|
|
func (am *addressManager) Uninitialize() {
|
2017-01-20 03:29:55 +03:00
|
|
|
am.StopSource()
|
2016-09-16 06:27:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Restore reads address manager state from persistent store.
|
|
|
|
func (am *addressManager) restore() error {
|
2016-09-27 02:17:04 +03:00
|
|
|
// Skip if a store is not provided.
|
|
|
|
if am.store == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-22 22:22:47 +03:00
|
|
|
rebooted := false
|
|
|
|
|
|
|
|
// Check if the VM is rebooted.
|
2016-12-17 05:03:03 +03:00
|
|
|
modTime, err := am.store.GetModificationTime()
|
|
|
|
if err == nil {
|
2017-01-21 03:21:41 +03:00
|
|
|
|
|
|
|
rebootTime, err := platform.GetLastRebootTime()
|
2018-06-05 02:47:21 +03:00
|
|
|
log.Printf("[ipam] reboot time %v store mod time %v", rebootTime, modTime)
|
|
|
|
|
2016-12-17 05:03:03 +03:00
|
|
|
if err == nil && rebootTime.After(modTime) {
|
2018-02-22 22:22:47 +03:00
|
|
|
rebooted = true
|
2016-12-17 05:03:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
// Read any persisted state.
|
2016-12-17 05:03:03 +03:00
|
|
|
err = am.store.Read(storeKey, am)
|
2016-09-16 06:27:36 +03:00
|
|
|
if err != nil {
|
|
|
|
if err == store.ErrKeyNotFound {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
log.Printf("[ipam] Failed to restore state, err:%v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate pointers.
|
|
|
|
for _, as := range am.AddrSpaces {
|
|
|
|
for _, ap := range as.Pools {
|
|
|
|
ap.as = as
|
2017-06-07 22:07:53 +03:00
|
|
|
ap.addrsByID = make(map[string]*addressRecord)
|
|
|
|
|
|
|
|
for _, ar := range ap.Addresses {
|
|
|
|
if ar.ID != "" {
|
|
|
|
ap.addrsByID[ar.ID] = ar
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 06:27:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 22:22:47 +03:00
|
|
|
// if rebooted mark the ip as not in use.
|
|
|
|
if rebooted {
|
|
|
|
log.Printf("[ipam] Rehydrating ipam state from persistent store")
|
|
|
|
for _, as := range am.AddrSpaces {
|
|
|
|
for _, ap := range as.Pools {
|
|
|
|
ap.as = as
|
|
|
|
|
|
|
|
for _, ar := range ap.Addresses {
|
|
|
|
ar.InUse = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
log.Printf("[ipam] Restored state, %+v\n", am)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes address manager state to persistent store.
|
|
|
|
func (am *addressManager) save() error {
|
2016-09-27 02:17:04 +03:00
|
|
|
// Skip if a store is not provided.
|
|
|
|
if am.store == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-17 05:03:03 +03:00
|
|
|
// Update time stamp.
|
|
|
|
am.TimeStamp = time.Now()
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
err := am.store.Write(storeKey, am)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[ipam] Save succeeded.\n")
|
|
|
|
} else {
|
|
|
|
log.Printf("[ipam] Save failed, err:%v\n", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts configuration source.
|
2017-01-20 03:29:55 +03:00
|
|
|
func (am *addressManager) StartSource(options map[string]interface{}) error {
|
2016-09-16 06:27:36 +03:00
|
|
|
var err error
|
|
|
|
|
2016-12-06 22:17:47 +03:00
|
|
|
environment, _ := options[common.OptEnvironment].(string)
|
|
|
|
|
2016-10-01 02:26:34 +03:00
|
|
|
switch environment {
|
|
|
|
case common.OptEnvironmentAzure:
|
2016-12-06 22:17:47 +03:00
|
|
|
am.source, err = newAzureSource(options)
|
2016-09-16 06:27:36 +03:00
|
|
|
|
2016-10-01 02:26:34 +03:00
|
|
|
case common.OptEnvironmentMAS:
|
2016-12-06 22:17:47 +03:00
|
|
|
am.source, err = newMasSource(options)
|
2016-09-16 06:27:36 +03:00
|
|
|
|
|
|
|
case "null":
|
|
|
|
am.source, err = newNullSource()
|
|
|
|
|
2016-10-01 02:26:34 +03:00
|
|
|
case "":
|
|
|
|
am.source = nil
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
default:
|
|
|
|
return errInvalidConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
if am.source != nil {
|
2017-01-20 03:29:55 +03:00
|
|
|
log.Printf("[ipam] Starting source %v.", environment)
|
2016-09-16 06:27:36 +03:00
|
|
|
err = am.source.start(am)
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:29:55 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ipam] Failed to start source %v, err:%v.", environment, err)
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stops the configuration source.
|
2017-01-20 03:29:55 +03:00
|
|
|
func (am *addressManager) StopSource() {
|
2016-09-16 06:27:36 +03:00
|
|
|
if am.source != nil {
|
|
|
|
am.source.stop()
|
|
|
|
am.source = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signals configuration source to refresh.
|
|
|
|
func (am *addressManager) refreshSource() {
|
|
|
|
if am.source != nil {
|
2017-03-04 05:01:37 +03:00
|
|
|
log.Printf("[ipam] Refreshing address source.")
|
2016-09-16 06:27:36 +03:00
|
|
|
err := am.source.refresh()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ipam] Source refresh failed, err:%v.\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 01:37:35 +03:00
|
|
|
//
|
|
|
|
// AddressManager API
|
|
|
|
//
|
|
|
|
// Provides atomic stateful wrappers around core IPAM functionality.
|
|
|
|
//
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
// GetDefaultAddressSpaces returns the default local and global address space IDs.
|
|
|
|
func (am *addressManager) GetDefaultAddressSpaces() (string, string) {
|
|
|
|
var localId, globalId string
|
|
|
|
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
am.refreshSource()
|
|
|
|
|
2017-01-25 03:20:34 +03:00
|
|
|
local := am.AddrSpaces[LocalDefaultAddressSpaceId]
|
2016-09-16 06:27:36 +03:00
|
|
|
if local != nil {
|
|
|
|
localId = local.Id
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:20:34 +03:00
|
|
|
global := am.AddrSpaces[GlobalDefaultAddressSpaceId]
|
2016-09-16 06:27:36 +03:00
|
|
|
if global != nil {
|
|
|
|
globalId = global.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
return localId, globalId
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestPool reserves an address pool.
|
|
|
|
func (am *addressManager) RequestPool(asId, poolId, subPoolId string, options map[string]string, v6 bool) (string, string, error) {
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
am.refreshSource()
|
|
|
|
|
|
|
|
as, err := am.getAddressSpace(asId)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := as.requestPool(poolId, subPoolId, options, v6)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.save()
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pool.Id, pool.Subnet.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleasePool releases a previously reserved address pool.
|
|
|
|
func (am *addressManager) ReleasePool(asId string, poolId string) error {
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
am.refreshSource()
|
|
|
|
|
|
|
|
as, err := am.getAddressSpace(asId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = as.releasePool(poolId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.save()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:53:12 +03:00
|
|
|
// GetPoolInfo returns information about the given address pool.
|
|
|
|
func (am *addressManager) GetPoolInfo(asId string, poolId string) (*AddressPoolInfo, error) {
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
as, err := am.getAddressSpace(asId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ap, err := as.getAddressPool(poolId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ap.getInfo(), nil
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
// RequestAddress reserves a new address from the address pool.
|
|
|
|
func (am *addressManager) RequestAddress(asId, poolId, address string, options map[string]string) (string, error) {
|
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
am.refreshSource()
|
|
|
|
|
|
|
|
as, err := am.getAddressSpace(asId)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ap, err := as.getAddressPool(poolId)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := ap.requestAddress(address, options)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.save()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseAddress releases a previously reserved address.
|
2017-06-13 02:36:21 +03:00
|
|
|
func (am *addressManager) ReleaseAddress(asId string, poolId string, address string, options map[string]string) error {
|
2016-09-16 06:27:36 +03:00
|
|
|
am.Lock()
|
|
|
|
defer am.Unlock()
|
|
|
|
|
|
|
|
am.refreshSource()
|
|
|
|
|
|
|
|
as, err := am.getAddressSpace(asId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ap, err := as.getAddressPool(poolId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-13 02:36:21 +03:00
|
|
|
err = ap.releaseAddress(address, options)
|
2016-09-16 06:27:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = am.save()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|