2017-02-12 13:03:39 +03:00
|
|
|
// Copyright 2017 Microsoft. All rights reserved.
|
|
|
|
// MIT License
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
package ipam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2017-02-07 03:12:51 +03:00
|
|
|
|
2017-05-04 04:19:52 +03:00
|
|
|
"github.com/Azure/azure-container-networking/platform"
|
2023-10-28 07:38:36 +03:00
|
|
|
"go.uber.org/zap"
|
2016-05-09 14:31:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Default address space IDs.
|
2017-01-25 03:20:34 +03:00
|
|
|
LocalDefaultAddressSpaceId = "local"
|
|
|
|
GlobalDefaultAddressSpaceId = "global"
|
|
|
|
)
|
2016-05-09 14:31:08 +03:00
|
|
|
|
2017-01-25 03:20:34 +03:00
|
|
|
const (
|
2016-05-09 14:31:08 +03:00
|
|
|
// Address space scopes.
|
2017-01-25 03:20:34 +03:00
|
|
|
LocalScope = iota
|
|
|
|
GlobalScope
|
2016-05-09 14:31:08 +03:00
|
|
|
)
|
|
|
|
|
2016-12-10 03:53:12 +03:00
|
|
|
var (
|
|
|
|
// Azure VNET well-known host IDs.
|
|
|
|
defaultGatewayHostId = net.ParseIP("::1")
|
|
|
|
dnsPrimaryHostId = net.ParseIP("::2")
|
|
|
|
dnsSecondaryHostId = net.ParseIP("::3")
|
2017-04-20 21:04:12 +03:00
|
|
|
|
|
|
|
// Azure DNS host proxy well-known address.
|
2017-05-04 04:19:52 +03:00
|
|
|
dnsHostProxyAddress = net.ParseIP("168.63.129.16")
|
2016-12-10 03:53:12 +03:00
|
|
|
)
|
|
|
|
|
2016-05-09 14:31:08 +03:00
|
|
|
// Represents the key to an address pool.
|
|
|
|
type addressPoolId struct {
|
2016-11-22 22:48:22 +03:00
|
|
|
AsId string
|
|
|
|
Subnet string
|
|
|
|
ChildSubnet string
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a set of non-overlapping address pools.
|
|
|
|
type addressSpace struct {
|
2016-09-16 06:27:36 +03:00
|
|
|
Id string
|
2017-01-25 03:20:34 +03:00
|
|
|
Scope int
|
2016-09-16 06:27:36 +03:00
|
|
|
Pools map[string]*addressPool
|
2016-05-09 14:31:08 +03:00
|
|
|
epoch int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a subnet and the set of addresses in it.
|
|
|
|
type addressPool struct {
|
|
|
|
as *addressSpace
|
2016-09-16 06:27:36 +03:00
|
|
|
Id string
|
|
|
|
IfName string
|
|
|
|
Subnet net.IPNet
|
2017-05-05 05:59:21 +03:00
|
|
|
Gateway net.IP
|
2016-09-16 06:27:36 +03:00
|
|
|
Addresses map[string]*addressRecord
|
2017-06-07 22:07:53 +03:00
|
|
|
addrsByID map[string]*addressRecord
|
2016-09-16 06:27:36 +03:00
|
|
|
IsIPv6 bool
|
|
|
|
Priority int
|
2016-12-06 00:23:35 +03:00
|
|
|
RefCount int
|
2016-05-09 14:31:08 +03:00
|
|
|
epoch int
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:53:12 +03:00
|
|
|
// AddressPoolInfo contains information about an address pool.
|
|
|
|
type AddressPoolInfo struct {
|
2017-06-09 04:37:21 +03:00
|
|
|
Subnet net.IPNet
|
|
|
|
Gateway net.IP
|
|
|
|
DnsServers []net.IP
|
|
|
|
UnhealthyAddrs []net.IP
|
|
|
|
IsIPv6 bool
|
|
|
|
Available int
|
|
|
|
Capacity int
|
2016-12-10 03:53:12 +03:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:31:08 +03:00
|
|
|
// Represents an IP address in a pool.
|
|
|
|
type addressRecord struct {
|
2017-06-09 04:37:21 +03:00
|
|
|
ID string
|
|
|
|
Addr net.IP
|
|
|
|
InUse bool
|
|
|
|
unhealthy bool
|
|
|
|
epoch int
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// AddressPoolId
|
|
|
|
//
|
|
|
|
|
|
|
|
// Creates a new address pool ID object.
|
2016-11-22 22:48:22 +03:00
|
|
|
func NewAddressPoolId(asId string, subnet string, childSubnet string) *addressPoolId {
|
2016-05-09 14:31:08 +03:00
|
|
|
return &addressPoolId{
|
2016-11-22 22:48:22 +03:00
|
|
|
AsId: asId,
|
|
|
|
Subnet: subnet,
|
|
|
|
ChildSubnet: childSubnet,
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new pool ID from a string representation.
|
2016-11-22 22:48:22 +03:00
|
|
|
func NewAddressPoolIdFromString(s string) (*addressPoolId, error) {
|
2016-05-09 14:31:08 +03:00
|
|
|
var pid addressPoolId
|
|
|
|
|
|
|
|
p := strings.Split(s, "|")
|
|
|
|
if len(p) > 3 {
|
2022-06-06 18:52:38 +03:00
|
|
|
return nil, errInvalidPoolID
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 22:48:22 +03:00
|
|
|
pid.AsId = p[0]
|
2016-05-09 14:31:08 +03:00
|
|
|
if len(p) >= 2 {
|
2016-11-22 22:48:22 +03:00
|
|
|
pid.Subnet = p[1]
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
if len(p) == 3 {
|
2016-11-22 22:48:22 +03:00
|
|
|
pid.ChildSubnet = p[2]
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return &pid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the string representation of a pool ID.
|
|
|
|
func (pid *addressPoolId) String() string {
|
2016-11-22 22:48:22 +03:00
|
|
|
s := fmt.Sprintf("%s|%s", pid.AsId, pid.Subnet)
|
|
|
|
if pid.ChildSubnet != "" {
|
|
|
|
s = fmt.Sprintf("%s|%s", s, pid.ChildSubnet)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// AddressSpace
|
|
|
|
//
|
|
|
|
|
|
|
|
// Creates a new addressSpace object.
|
2017-01-25 03:20:34 +03:00
|
|
|
func (am *addressManager) newAddressSpace(id string, scope int) (*addressSpace, error) {
|
|
|
|
if scope != LocalScope && scope != GlobalScope {
|
2016-05-09 14:31:08 +03:00
|
|
|
return nil, errInvalidScope
|
|
|
|
}
|
|
|
|
|
|
|
|
return &addressSpace{
|
2016-09-16 06:27:36 +03:00
|
|
|
Id: id,
|
|
|
|
Scope: scope,
|
|
|
|
Pools: make(map[string]*addressPool),
|
2016-05-09 14:31:08 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
// Returns the address space with the given ID.
|
|
|
|
func (am *addressManager) getAddressSpace(id string) (*addressSpace, error) {
|
|
|
|
as := am.AddrSpaces[id]
|
|
|
|
if as == nil {
|
|
|
|
return nil, errInvalidAddressSpace
|
|
|
|
}
|
|
|
|
|
|
|
|
return as, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets a new or updates an existing address space.
|
|
|
|
func (am *addressManager) setAddressSpace(as *addressSpace) error {
|
|
|
|
as1, ok := am.AddrSpaces[as.Id]
|
|
|
|
if !ok {
|
|
|
|
am.AddrSpaces[as.Id] = as
|
|
|
|
} else {
|
2020-08-31 20:18:25 +03:00
|
|
|
// merges the address set refreshed from the source
|
|
|
|
// and the ones we have currently in this address space
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("merging address space")
|
2016-09-16 06:27:36 +03:00
|
|
|
as1.merge(as)
|
|
|
|
}
|
|
|
|
|
2016-09-22 01:37:35 +03:00
|
|
|
// Notify NetPlugin of external interfaces.
|
2016-09-27 02:18:06 +03:00
|
|
|
if am.netApi != nil {
|
|
|
|
for _, ap := range as.Pools {
|
|
|
|
am.netApi.AddExternalInterface(ap.IfName, ap.Subnet.String())
|
|
|
|
}
|
2016-09-22 01:37:35 +03:00
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
am.save()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:31:08 +03:00
|
|
|
// Merges a new address space to an existing one.
|
|
|
|
func (as *addressSpace) merge(newas *addressSpace) {
|
|
|
|
// The new epoch after the merge.
|
2020-08-31 20:18:25 +03:00
|
|
|
// epoch is essentially the count of invocations
|
2020-08-05 07:58:45 +03:00
|
|
|
// used to ensure if certain addresses refreshed from the source
|
|
|
|
// are still relevant
|
2016-05-09 14:31:08 +03:00
|
|
|
as.epoch++
|
|
|
|
|
|
|
|
// Add new pools and addresses.
|
2016-09-16 06:27:36 +03:00
|
|
|
for pk, pv := range newas.Pools {
|
|
|
|
ap := as.Pools[pk]
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
if ap == nil {
|
|
|
|
// This is a new address pool.
|
|
|
|
// Merge it to the existing address space.
|
2016-09-16 06:27:36 +03:00
|
|
|
as.Pools[pk] = pv
|
2016-12-03 04:45:00 +03:00
|
|
|
pv.as = as
|
2016-05-09 14:31:08 +03:00
|
|
|
pv.epoch = as.epoch
|
|
|
|
} else {
|
|
|
|
// This pool already exists.
|
|
|
|
// Compare address records one by one.
|
2016-09-16 06:27:36 +03:00
|
|
|
for ak, av := range pv.Addresses {
|
|
|
|
ar := ap.Addresses[ak]
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
if ar == nil {
|
|
|
|
// This is a new address record.
|
|
|
|
// Merge it to the existing address pool.
|
2016-09-16 06:27:36 +03:00
|
|
|
ap.Addresses[ak] = av
|
2016-05-09 14:31:08 +03:00
|
|
|
av.epoch = as.epoch
|
|
|
|
} else {
|
|
|
|
// This address record already exists.
|
|
|
|
ar.epoch = as.epoch
|
2017-06-09 04:37:21 +03:00
|
|
|
ar.unhealthy = false
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2016-12-03 04:45:00 +03:00
|
|
|
|
|
|
|
delete(pv.Addresses, ak)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
2016-12-03 04:45:00 +03:00
|
|
|
pv.as = nil
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2016-12-03 04:45:00 +03:00
|
|
|
|
|
|
|
delete(newas.Pools, pk)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup stale pools and addresses from the old epoch.
|
2016-09-26 21:06:28 +03:00
|
|
|
// Those currently in use will be deleted after they are released.
|
2016-09-16 06:27:36 +03:00
|
|
|
for pk, pv := range as.Pools {
|
2016-05-09 14:31:08 +03:00
|
|
|
if pv.epoch < as.epoch {
|
2016-12-03 04:45:00 +03:00
|
|
|
// This pool may have stale addresses.
|
2016-09-16 06:27:36 +03:00
|
|
|
for ak, av := range pv.Addresses {
|
2017-06-09 04:37:21 +03:00
|
|
|
if av.epoch == as.epoch {
|
2016-12-03 04:45:00 +03:00
|
|
|
// Pool has at least one valid or in-use address.
|
|
|
|
pv.epoch = as.epoch
|
2017-06-09 04:37:21 +03:00
|
|
|
} else if av.InUse {
|
|
|
|
// Address is no longer valid, but still in use.
|
|
|
|
pv.epoch = as.epoch
|
|
|
|
av.unhealthy = true
|
2016-12-03 04:45:00 +03:00
|
|
|
} else {
|
|
|
|
// This address is no longer available.
|
2016-09-16 06:27:36 +03:00
|
|
|
delete(pv.Addresses, ak)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 04:45:00 +03:00
|
|
|
// Delete the pool if it has no addresses left.
|
2016-12-06 00:23:35 +03:00
|
|
|
if pv.epoch < as.epoch && !pv.isInUse() {
|
2016-12-03 04:45:00 +03:00
|
|
|
pv.as = nil
|
2016-09-16 06:27:36 +03:00
|
|
|
delete(as.Pools, pk)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new addressPool object.
|
2016-06-09 14:03:43 +03:00
|
|
|
func (as *addressSpace) newAddressPool(ifName string, priority int, subnet *net.IPNet) (*addressPool, error) {
|
2016-09-16 06:27:36 +03:00
|
|
|
id := subnet.String()
|
2016-05-09 14:31:08 +03:00
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
pool, ok := as.Pools[id]
|
2016-05-09 14:31:08 +03:00
|
|
|
if ok {
|
|
|
|
return pool, errAddressPoolExists
|
|
|
|
}
|
|
|
|
|
2016-09-27 02:18:06 +03:00
|
|
|
v6 := (subnet.IP.To4() == nil)
|
2016-06-09 14:03:43 +03:00
|
|
|
|
2016-05-09 14:31:08 +03:00
|
|
|
pool = &addressPool{
|
|
|
|
as: as,
|
2016-09-16 06:27:36 +03:00
|
|
|
Id: id,
|
|
|
|
IfName: ifName,
|
|
|
|
Subnet: *subnet,
|
2017-05-05 05:59:21 +03:00
|
|
|
Gateway: platform.GenerateAddress(subnet, defaultGatewayHostId),
|
2016-09-16 06:27:36 +03:00
|
|
|
Addresses: make(map[string]*addressRecord),
|
2017-06-07 22:07:53 +03:00
|
|
|
addrsByID: make(map[string]*addressRecord),
|
2016-09-16 06:27:36 +03:00
|
|
|
IsIPv6: v6,
|
|
|
|
Priority: priority,
|
2016-05-09 14:31:08 +03:00
|
|
|
epoch: as.epoch,
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
as.Pools[id] = pool
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
return pool, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the address pool with the given pool ID.
|
|
|
|
func (as *addressSpace) getAddressPool(poolId string) (*addressPool, error) {
|
2016-09-16 06:27:36 +03:00
|
|
|
ap := as.Pools[poolId]
|
2016-05-09 14:31:08 +03:00
|
|
|
if ap == nil {
|
2022-06-06 18:52:38 +03:00
|
|
|
return nil, fmt.Errorf("Pool id %v not found :%w", poolId, errInvalidPoolID)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests a new address pool from the address space.
|
2016-09-16 06:27:36 +03:00
|
|
|
func (as *addressSpace) requestPool(poolId string, subPoolId string, options map[string]string, v6 bool) (*addressPool, error) {
|
2016-05-09 14:31:08 +03:00
|
|
|
var ap *addressPool
|
2017-02-07 03:12:51 +03:00
|
|
|
var err error
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Requesting pool with", zap.String("poolId", poolId), zap.Any("options", options), zap.Any("v6", v6))
|
2016-05-09 14:31:08 +03:00
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
if poolId != "" {
|
2016-05-09 14:31:08 +03:00
|
|
|
// Return the specific address pool requested.
|
2016-12-06 23:27:16 +03:00
|
|
|
// Note sharing of pools is allowed when specifically requested.
|
2016-09-16 06:27:36 +03:00
|
|
|
ap = as.Pools[poolId]
|
2016-05-09 14:31:08 +03:00
|
|
|
if ap == nil {
|
2017-02-07 03:12:51 +03:00
|
|
|
err = errAddressPoolNotFound
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Return any available address pool.
|
2017-05-05 05:59:21 +03:00
|
|
|
ifName := options[OptInterfaceName]
|
2016-06-09 14:03:43 +03:00
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
for _, pool := range as.Pools {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Checking pool", zap.String("id", pool.Id))
|
2017-02-07 03:12:51 +03:00
|
|
|
|
2016-09-26 21:06:28 +03:00
|
|
|
// Skip if pool is already in use.
|
2016-12-06 00:23:35 +03:00
|
|
|
if pool.isInUse() {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool is in use", zap.String("id", pool.Id))
|
2020-08-05 07:58:45 +03:00
|
|
|
|
2020-08-11 03:10:42 +03:00
|
|
|
// in case the pool is actually not in use,
|
|
|
|
// attempt to release it
|
2021-01-08 04:43:33 +03:00
|
|
|
as.releasePool(pool.Id)
|
|
|
|
if pool.isInUse() {
|
|
|
|
continue
|
2020-08-05 07:58:45 +03:00
|
|
|
}
|
2016-09-26 21:06:28 +03:00
|
|
|
}
|
|
|
|
|
2016-06-09 14:03:43 +03:00
|
|
|
// Pick a pool from the same address family.
|
2016-09-16 06:27:36 +03:00
|
|
|
if pool.IsIPv6 != v6 {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool is of a different address family")
|
2016-06-09 14:03:43 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:20:35 +03:00
|
|
|
// Skip if pool is not on the requested interface.
|
|
|
|
if ifName != "" && ifName != pool.IfName {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool is not on the requested interface")
|
2017-02-07 03:12:51 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool matches requirements", zap.String("id", pool.Id))
|
2017-02-07 03:12:51 +03:00
|
|
|
|
|
|
|
if ap == nil {
|
|
|
|
ap = pool
|
2017-01-20 03:20:35 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:27:16 +03:00
|
|
|
// Prefer the pool with the highest priority.
|
2017-02-07 03:12:51 +03:00
|
|
|
if pool.Priority > ap.Priority {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool is preferred because of priority")
|
2016-06-09 14:03:43 +03:00
|
|
|
ap = pool
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2016-12-06 23:27:16 +03:00
|
|
|
|
|
|
|
// Prefer the pool with the highest number of addresses.
|
2017-02-07 03:12:51 +03:00
|
|
|
if len(pool.Addresses) > len(ap.Addresses) {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool is preferred because of capacity")
|
2016-12-06 23:27:16 +03:00
|
|
|
ap = pool
|
|
|
|
}
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ap == nil {
|
2022-05-21 01:43:34 +03:00
|
|
|
err = ErrNoAvailableAddressPools
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 04:43:33 +03:00
|
|
|
if ap != nil && ap.RefCount == 0 {
|
|
|
|
ap.RefCount = 1
|
2017-02-07 03:12:51 +03:00
|
|
|
}
|
2016-05-09 14:31:08 +03:00
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Pool request completed with pool", zap.Any("ap", ap), zap.Error(err))
|
2017-02-07 03:12:51 +03:00
|
|
|
|
|
|
|
return ap, err
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Releases a previously requested address pool back to its address space.
|
|
|
|
func (as *addressSpace) releasePool(poolId string) error {
|
2020-08-05 08:12:53 +03:00
|
|
|
var addressesInUse bool
|
2017-02-07 03:12:51 +03:00
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
ap, ok := as.Pools[poolId]
|
2016-05-09 14:31:08 +03:00
|
|
|
if !ok {
|
2021-01-08 04:43:33 +03:00
|
|
|
return errAddressPoolNotFound
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 04:43:33 +03:00
|
|
|
if addressesInUse = ap.IsAnyRecordInUse(); addressesInUse {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Skip releasing pool with due to address being in use", zap.String("poolId", poolId))
|
2021-01-08 04:43:33 +03:00
|
|
|
} else {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Releasing pool as there are no allocations", zap.String("poolId", poolId))
|
2021-01-08 04:43:33 +03:00
|
|
|
ap.RefCount = 0
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressPool
|
|
|
|
//
|
2017-06-09 04:37:21 +03:00
|
|
|
// Returns address pool information.
|
2016-12-10 03:53:12 +03:00
|
|
|
func (ap *addressPool) getInfo() *AddressPoolInfo {
|
2017-06-09 04:37:21 +03:00
|
|
|
var available int
|
|
|
|
var unhealthyAddrs []net.IP
|
|
|
|
|
|
|
|
for _, ar := range ap.Addresses {
|
|
|
|
if !ar.InUse {
|
|
|
|
available++
|
|
|
|
}
|
|
|
|
if ar.unhealthy {
|
|
|
|
unhealthyAddrs = append(unhealthyAddrs, ar.Addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:53:12 +03:00
|
|
|
info := &AddressPoolInfo{
|
2017-06-09 04:37:21 +03:00
|
|
|
Subnet: ap.Subnet,
|
|
|
|
Gateway: ap.Gateway,
|
|
|
|
DnsServers: []net.IP{dnsHostProxyAddress},
|
|
|
|
UnhealthyAddrs: unhealthyAddrs,
|
|
|
|
IsIPv6: ap.IsIPv6,
|
|
|
|
Available: available,
|
|
|
|
Capacity: len(ap.Addresses),
|
2016-12-10 03:53:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns if an address pool is currently in use.
|
2016-12-06 00:23:35 +03:00
|
|
|
func (ap *addressPool) isInUse() bool {
|
|
|
|
return ap.RefCount > 0
|
|
|
|
}
|
|
|
|
|
2020-08-05 07:58:45 +03:00
|
|
|
// Returns if any address in the pool is currently in use.
|
|
|
|
func (ap *addressPool) IsAnyRecordInUse() bool {
|
|
|
|
for _, address := range ap.Addresses {
|
|
|
|
if address.InUse || address.ID != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:31:08 +03:00
|
|
|
// Creates a new addressRecord object.
|
|
|
|
func (ap *addressPool) newAddressRecord(addr *net.IP) (*addressRecord, error) {
|
|
|
|
id := addr.String()
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
if !ap.Subnet.Contains(*addr) {
|
2016-05-09 14:31:08 +03:00
|
|
|
return nil, errInvalidAddress
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
ar, ok := ap.Addresses[id]
|
2016-05-09 14:31:08 +03:00
|
|
|
if ok {
|
|
|
|
return ar, errAddressExists
|
|
|
|
}
|
|
|
|
|
|
|
|
ar = &addressRecord{
|
2016-09-16 06:27:36 +03:00
|
|
|
Addr: *addr,
|
2016-05-09 14:31:08 +03:00
|
|
|
epoch: ap.epoch,
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:27:36 +03:00
|
|
|
ap.Addresses[id] = ar
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
return ar, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests a new address from the address pool.
|
|
|
|
func (ap *addressPool) requestAddress(address string, options map[string]string) (string, error) {
|
|
|
|
var ar *addressRecord
|
2017-06-07 22:07:53 +03:00
|
|
|
var addr *net.IPNet
|
|
|
|
id := options[OptAddressID]
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Requesting address with", zap.String("address", address), zap.Any("options", options))
|
2016-05-09 14:31:08 +03:00
|
|
|
|
|
|
|
if address != "" {
|
|
|
|
// Return the specific address requested.
|
2016-09-16 06:27:36 +03:00
|
|
|
ar = ap.Addresses[address]
|
2016-05-09 14:31:08 +03:00
|
|
|
if ar == nil {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Address request failed with", zap.Error(errAddressNotFound))
|
2020-11-16 21:28:16 +03:00
|
|
|
return "", errAddressNotFound
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2016-09-16 06:27:36 +03:00
|
|
|
if ar.InUse {
|
2017-06-08 22:42:19 +03:00
|
|
|
// Return the same address if IDs match.
|
|
|
|
if id == "" || id != ar.ID {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Address request failed with", zap.Error(errAddressInUse))
|
2020-11-16 21:28:16 +03:00
|
|
|
return "", errAddressInUse
|
2017-06-08 22:42:19 +03:00
|
|
|
}
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2017-05-05 05:59:21 +03:00
|
|
|
} else if options[OptAddressType] == OptAddressTypeGateway {
|
|
|
|
// Return the pre-assigned gateway address.
|
|
|
|
ar = &addressRecord{
|
|
|
|
Addr: ap.Gateway,
|
|
|
|
}
|
2017-06-07 22:07:53 +03:00
|
|
|
id = ""
|
|
|
|
} else if id != "" {
|
|
|
|
// Return the address with the matching identifier.
|
|
|
|
ar = ap.addrsByID[id]
|
|
|
|
}
|
|
|
|
|
2017-06-08 22:42:19 +03:00
|
|
|
// If no address was found, return any available address.
|
2017-06-07 22:07:53 +03:00
|
|
|
if ar == nil {
|
2016-09-16 06:27:36 +03:00
|
|
|
for _, ar = range ap.Addresses {
|
2020-08-18 19:04:19 +03:00
|
|
|
if !ar.InUse && ar.ID == "" {
|
2016-05-09 14:31:08 +03:00
|
|
|
break
|
|
|
|
}
|
2016-12-06 23:27:16 +03:00
|
|
|
ar = nil
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ar == nil {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Error("Address request failed with", zap.Error(errNoAvailableAddresses))
|
2016-05-09 14:31:08 +03:00
|
|
|
return "", errNoAvailableAddresses
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 22:07:53 +03:00
|
|
|
if id != "" {
|
|
|
|
ap.addrsByID[id] = ar
|
2018-02-22 22:22:47 +03:00
|
|
|
ar.ID = id
|
2017-06-07 22:07:53 +03:00
|
|
|
}
|
|
|
|
|
2020-08-16 00:54:41 +03:00
|
|
|
ar.InUse = true
|
|
|
|
|
2016-05-13 02:40:58 +03:00
|
|
|
// Return address in CIDR notation.
|
2017-06-07 22:07:53 +03:00
|
|
|
addr = &net.IPNet{
|
2016-09-16 06:27:36 +03:00
|
|
|
IP: ar.Addr,
|
|
|
|
Mask: ap.Subnet.Mask,
|
2016-05-13 02:40:58 +03:00
|
|
|
}
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Address request completed with", zap.Any("address", addr))
|
2020-11-16 21:28:16 +03:00
|
|
|
|
2016-05-13 02:40:58 +03:00
|
|
|
return addr.String(), nil
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Releases a previously requested address back to its address pool.
|
2017-06-13 02:36:21 +03:00
|
|
|
func (ap *addressPool) releaseAddress(address string, options map[string]string) error {
|
|
|
|
var ar *addressRecord
|
|
|
|
var id string
|
2017-06-07 22:07:53 +03:00
|
|
|
var err error
|
|
|
|
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Releasing address with", zap.String("address", address), zap.Any("options", options))
|
|
|
|
defer func() { logger.Error("Address release completed with", zap.String("address", address), zap.Error(err)) }()
|
2017-06-07 22:07:53 +03:00
|
|
|
|
2017-06-13 02:36:21 +03:00
|
|
|
if options != nil {
|
|
|
|
id = options[OptAddressID]
|
|
|
|
}
|
|
|
|
|
|
|
|
if address != "" {
|
|
|
|
// Release the specific address.
|
|
|
|
ar = ap.Addresses[address]
|
|
|
|
|
|
|
|
// Release the pre-assigned gateway address.
|
|
|
|
if ar == nil && address == ap.Gateway.String() {
|
2017-05-05 05:59:21 +03:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-13 02:36:21 +03:00
|
|
|
} else if id != "" {
|
|
|
|
// Release the address with the matching ID.
|
|
|
|
ar = ap.addrsByID[id]
|
2017-06-15 00:34:52 +03:00
|
|
|
if ar != nil {
|
|
|
|
address = ar.Addr.String()
|
|
|
|
}
|
2017-06-13 02:36:21 +03:00
|
|
|
}
|
|
|
|
|
2017-06-15 00:34:52 +03:00
|
|
|
// Fail if an address record with a matching ID is not found.
|
|
|
|
if ar == nil || (id != "" && id != ar.ID) {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Address not found. Not Returning error")
|
2017-10-19 00:59:21 +03:00
|
|
|
return nil
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
2017-05-05 05:59:21 +03:00
|
|
|
|
2020-08-18 19:04:19 +03:00
|
|
|
if !ar.InUse {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Address not in use. Not Returning error")
|
2017-10-19 00:59:21 +03:00
|
|
|
return nil
|
2017-06-07 22:07:53 +03:00
|
|
|
}
|
|
|
|
|
2018-02-22 22:22:47 +03:00
|
|
|
ar.InUse = false
|
|
|
|
|
|
|
|
if id != "" && ar.ID == id {
|
2017-06-07 22:07:53 +03:00
|
|
|
delete(ap.addrsByID, ar.ID)
|
2018-02-22 22:22:47 +03:00
|
|
|
ar.ID = ""
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete address record if it is no longer available.
|
|
|
|
if ar.epoch < ap.as.epoch {
|
2023-10-28 07:38:36 +03:00
|
|
|
logger.Info("Deleting Address record from address pool as metadata doesn't have this address")
|
2016-09-16 06:27:36 +03:00
|
|
|
delete(ap.Addresses, address)
|
2016-05-09 14:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|