2015-04-13 18:40:42 +00:00
|
|
|
package libnetwork
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
import (
|
2015-05-08 13:26:35 +00:00
|
|
|
"encoding/json"
|
2015-10-05 11:21:15 +00:00
|
|
|
"fmt"
|
2015-06-19 06:40:17 +00:00
|
|
|
"net"
|
2015-04-13 18:40:42 +00:00
|
|
|
"sync"
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-06-10 20:27:23 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-04-18 04:37:54 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-06-14 16:00:27 +00:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-15 22:23:59 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-06-19 06:40:17 +00:00
|
|
|
"github.com/docker/libnetwork/etchosts"
|
2015-10-07 03:29:30 +00:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
2015-04-20 15:44:06 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-04-13 18:40:42 +00:00
|
|
|
)
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-03-02 18:17:12 +00:00
|
|
|
// A Network represents a logical connectivity zone that containers may
|
2015-04-15 12:42:25 +00:00
|
|
|
// join using the Link method. A Network is managed by a specific driver.
|
2015-03-02 18:17:12 +00:00
|
|
|
type Network interface {
|
|
|
|
// A user chosen name for this network.
|
|
|
|
Name() string
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// A system generated id for this network.
|
|
|
|
ID() string
|
|
|
|
|
2015-03-02 18:17:12 +00:00
|
|
|
// The type of network, which corresponds to its managing driver.
|
|
|
|
Type() string
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// Create a new endpoint to this network symbolically identified by the
|
|
|
|
// specified unique name. The options parameter carry driver specific options.
|
|
|
|
// Labels support will be added in the near future.
|
2015-05-01 00:57:06 +00:00
|
|
|
CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error)
|
2015-03-20 20:54:49 +00:00
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
// Delete the network.
|
|
|
|
Delete() error
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// Endpoints returns the list of Endpoint(s) in this network.
|
2015-04-18 06:13:29 +00:00
|
|
|
Endpoints() []Endpoint
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// WalkEndpoints uses the provided function to walk the Endpoints
|
|
|
|
WalkEndpoints(walker EndpointWalker)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
// EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
EndpointByName(name string) (Endpoint, error)
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
// EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
EndpointByID(id string) (Endpoint, error)
|
2015-03-02 18:17:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// EndpointWalker is a client provided function which will be used to walk the Endpoints.
|
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
type EndpointWalker func(ep Endpoint) bool
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-06-19 06:40:17 +00:00
|
|
|
type svcMap map[string]net.IP
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
// IpamConf contains all the ipam related configurations for a network
|
|
|
|
type IpamConf struct {
|
2015-10-07 03:29:30 +00:00
|
|
|
// The master address pool for containers and network iterfaces
|
2015-10-03 23:11:50 +00:00
|
|
|
PreferredPool string
|
2015-10-07 03:29:30 +00:00
|
|
|
// A subset of the master pool. If specified,
|
|
|
|
// this becomes the container pool
|
|
|
|
SubPool string
|
|
|
|
// Input options for IPAM Driver (optional)
|
|
|
|
Options map[string]string // IPAM input options
|
|
|
|
// IPv6 flag, Needed when no preferred pool is specified
|
|
|
|
IsV6 bool
|
|
|
|
// Preferred Network Gateway address (optional)
|
|
|
|
Gateway string
|
|
|
|
// Auxiliary addresses for network driver. Must be within the master pool.
|
|
|
|
// libnetwork will reserve them if they fall into the container pool
|
|
|
|
AuxAddresses map[string]string
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks whether the configuration is valid
|
|
|
|
func (c *IpamConf) Validate() error {
|
|
|
|
if c.Gateway != "" && nil == net.ParseIP(c.Gateway) {
|
|
|
|
return types.BadRequestErrorf("invalid gateway address %s in IpamConf dtructure", c.Gateway)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IpamInfo contains all the ipam related operational info for a network
|
|
|
|
type IpamInfo struct {
|
|
|
|
PoolID string
|
|
|
|
Meta map[string]string
|
|
|
|
driverapi.IPAMData
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes IpamInfo into json message
|
|
|
|
func (i *IpamInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"PoolID": i.PoolID,
|
|
|
|
}
|
|
|
|
v, err := json.Marshal(&i.IPAMData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m["IPAMData"] = string(v)
|
|
|
|
|
|
|
|
if i.Meta != nil {
|
|
|
|
m["Meta"] = i.Meta
|
|
|
|
}
|
|
|
|
return json.Marshal(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes json message into PoolData
|
|
|
|
func (i *IpamInfo) UnmarshalJSON(data []byte) error {
|
|
|
|
var (
|
|
|
|
m map[string]interface{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if err = json.Unmarshal(data, &m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i.PoolID = m["PoolID"].(string)
|
|
|
|
if v, ok := m["Meta"]; ok {
|
|
|
|
b, _ := json.Marshal(v)
|
|
|
|
if err = json.Unmarshal(b, &i.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := m["IPAMData"]; ok {
|
|
|
|
if err = json.Unmarshal([]byte(v.(string)), &i.IPAMData); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
type network struct {
|
2015-10-03 23:11:50 +00:00
|
|
|
ctrlr *controller
|
|
|
|
name string
|
|
|
|
networkType string
|
|
|
|
id string
|
|
|
|
ipamType string
|
|
|
|
addrSpace string
|
|
|
|
ipamV4Config []*IpamConf
|
|
|
|
ipamV6Config []*IpamConf
|
|
|
|
ipamV4Info []*IpamInfo
|
|
|
|
ipamV6Info []*IpamInfo
|
|
|
|
enableIPv6 bool
|
|
|
|
endpointCnt uint64
|
|
|
|
generic options.Generic
|
|
|
|
dbIndex uint64
|
|
|
|
svcRecords svcMap
|
|
|
|
dbExists bool
|
|
|
|
persist bool
|
|
|
|
stopWatchCh chan struct{}
|
2015-10-05 11:21:15 +00:00
|
|
|
scope string
|
|
|
|
drvOnce *sync.Once
|
2015-04-13 18:40:42 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Name() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ID() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
return n.id
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Type() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
return n.networkType
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
func (n *network) Key() []string {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-07-02 05:00:48 +00:00
|
|
|
return []string{datastore.NetworkKeyPrefix, n.id}
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
func (n *network) KeyPrefix() []string {
|
|
|
|
return []string{datastore.NetworkKeyPrefix}
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
func (n *network) Value() []byte {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-05-08 13:26:35 +00:00
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:13:38 +00:00
|
|
|
func (n *network) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, n)
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
func (n *network) Index() uint64 {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-05-08 13:26:35 +00:00
|
|
|
return n.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) SetIndex(index uint64) {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
2015-05-08 13:26:35 +00:00
|
|
|
n.dbIndex = index
|
2015-06-18 22:13:38 +00:00
|
|
|
n.dbExists = true
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:13:38 +00:00
|
|
|
func (n *network) Exists() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.dbExists
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:09:39 +00:00
|
|
|
func (n *network) Skip() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return !n.persist
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) New() datastore.KVObject {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return &network{
|
|
|
|
ctrlr: n.ctrlr,
|
|
|
|
drvOnce: &sync.Once{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTo deep copies to the destination IpamInfo
|
|
|
|
func (i *IpamInfo) CopyTo(dstI *IpamInfo) error {
|
|
|
|
dstI.PoolID = i.PoolID
|
|
|
|
if i.Meta != nil {
|
|
|
|
dstI.Meta = make(map[string]string)
|
|
|
|
for k, v := range i.Meta {
|
|
|
|
dstI.Meta[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstI.AddressSpace = i.AddressSpace
|
|
|
|
dstI.Pool = types.GetIPNetCopy(i.Pool)
|
|
|
|
dstI.Gateway = types.GetIPNetCopy(i.Gateway)
|
|
|
|
|
|
|
|
if i.AuxAddresses != nil {
|
|
|
|
dstI.AuxAddresses = make(map[string]*net.IPNet)
|
|
|
|
for k, v := range i.AuxAddresses {
|
|
|
|
dstI.AuxAddresses[k] = types.GetIPNetCopy(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) CopyTo(o datastore.KVObject) error {
|
2015-09-16 11:39:46 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
dstN := o.(*network)
|
|
|
|
dstN.name = n.name
|
|
|
|
dstN.id = n.id
|
|
|
|
dstN.networkType = n.networkType
|
|
|
|
dstN.ipamType = n.ipamType
|
|
|
|
dstN.endpointCnt = n.endpointCnt
|
|
|
|
dstN.enableIPv6 = n.enableIPv6
|
|
|
|
dstN.persist = n.persist
|
|
|
|
dstN.dbIndex = n.dbIndex
|
|
|
|
dstN.dbExists = n.dbExists
|
|
|
|
dstN.drvOnce = n.drvOnce
|
|
|
|
|
|
|
|
for _, v4info := range n.ipamV4Info {
|
|
|
|
dstV4Info := &IpamInfo{}
|
|
|
|
v4info.CopyTo(dstV4Info)
|
|
|
|
dstN.ipamV4Info = append(dstN.ipamV4Info, dstV4Info)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstN.generic = options.Generic{}
|
|
|
|
for k, v := range n.generic {
|
|
|
|
dstN.generic[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) DataScope() string {
|
|
|
|
return n.driverScope()
|
2015-09-16 11:39:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
func (n *network) EndpointCnt() uint64 {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.endpointCnt
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) IncEndpointCnt() error {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
n.endpointCnt++
|
|
|
|
n.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return n.getController().updateToStore(n)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) DecEndpointCnt() error {
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
n.endpointCnt--
|
|
|
|
n.Unlock()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return n.getController().updateToStore(n)
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
|
|
|
func (n *network) MarshalJSON() ([]byte, error) {
|
|
|
|
netMap := make(map[string]interface{})
|
|
|
|
netMap["name"] = n.name
|
2015-07-02 05:00:48 +00:00
|
|
|
netMap["id"] = n.id
|
2015-05-08 13:26:35 +00:00
|
|
|
netMap["networkType"] = n.networkType
|
2015-09-22 20:20:55 +00:00
|
|
|
netMap["ipamType"] = n.ipamType
|
2015-10-03 23:11:50 +00:00
|
|
|
netMap["addrSpace"] = n.addrSpace
|
2015-06-05 20:31:12 +00:00
|
|
|
netMap["endpointCnt"] = n.endpointCnt
|
2015-05-08 13:26:35 +00:00
|
|
|
netMap["enableIPv6"] = n.enableIPv6
|
2015-10-03 23:11:50 +00:00
|
|
|
if n.generic != nil {
|
|
|
|
netMap["generic"] = n.generic
|
|
|
|
}
|
2015-09-22 14:09:39 +00:00
|
|
|
netMap["persist"] = n.persist
|
2015-10-03 23:11:50 +00:00
|
|
|
if len(n.ipamV4Config) > 0 {
|
|
|
|
ics, err := json.Marshal(n.ipamV4Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV4Config"] = string(ics)
|
|
|
|
}
|
|
|
|
if len(n.ipamV4Info) > 0 {
|
|
|
|
iis, err := json.Marshal(n.ipamV4Info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV4Info"] = string(iis)
|
|
|
|
}
|
|
|
|
if len(n.ipamV6Config) > 0 {
|
|
|
|
ics, err := json.Marshal(n.ipamV6Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV6Config"] = string(ics)
|
|
|
|
}
|
|
|
|
if len(n.ipamV6Info) > 0 {
|
|
|
|
iis, err := json.Marshal(n.ipamV6Info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
netMap["ipamV6Info"] = string(iis)
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
return json.Marshal(netMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
|
|
|
func (n *network) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
var netMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &netMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.name = netMap["name"].(string)
|
2015-07-02 05:00:48 +00:00
|
|
|
n.id = netMap["id"].(string)
|
2015-09-22 20:20:55 +00:00
|
|
|
n.ipamType = netMap["ipamType"].(string)
|
2015-10-03 23:11:50 +00:00
|
|
|
n.addrSpace = netMap["addrSpace"].(string)
|
2015-05-08 13:26:35 +00:00
|
|
|
n.networkType = netMap["networkType"].(string)
|
2015-06-05 20:31:12 +00:00
|
|
|
n.endpointCnt = uint64(netMap["endpointCnt"].(float64))
|
2015-05-08 13:26:35 +00:00
|
|
|
n.enableIPv6 = netMap["enableIPv6"].(bool)
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := netMap["generic"]; ok {
|
|
|
|
n.generic = v.(map[string]interface{})
|
|
|
|
}
|
|
|
|
if v, ok := netMap["persist"]; ok {
|
|
|
|
n.persist = v.(bool)
|
|
|
|
}
|
|
|
|
if v, ok := netMap["ipamV4Config"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := netMap["ipamV4Info"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV4Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := netMap["ipamV6Config"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := netMap["ipamV6Info"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-22 14:09:39 +00:00
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
// NetworkOption is a option setter function type used to pass varios options to
|
|
|
|
// NewNetwork method. The various setter functions of type NetworkOption are
|
|
|
|
// provided by libnetwork, they look like NetworkOptionXXXX(...)
|
|
|
|
type NetworkOption func(n *network)
|
|
|
|
|
|
|
|
// NetworkOptionGeneric function returns an option setter for a Generic option defined
|
|
|
|
// in a Dictionary of Key-Value pair
|
|
|
|
func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.generic = generic
|
2015-05-06 06:41:20 +00:00
|
|
|
if _, ok := generic[netlabel.EnableIPv6]; ok {
|
|
|
|
n.enableIPv6 = generic[netlabel.EnableIPv6].(bool)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 14:09:39 +00:00
|
|
|
// NetworkOptionPersist returns an option setter to set persistence policy for a network
|
|
|
|
func NetworkOptionPersist(persist bool) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.persist = persist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
// NetworkOptionIpam function returns an option setter for the ipam configuration for this network
|
|
|
|
func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.ipamType = ipamDriver
|
|
|
|
n.addrSpace = addrSpace
|
|
|
|
n.ipamV4Config = ipV4
|
|
|
|
n.ipamV6Config = ipV6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func (n *network) processOptions(options ...NetworkOption) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) driverScope() string {
|
|
|
|
c := n.getController()
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
// Check if a driver for the specified network type is available
|
|
|
|
dd, ok := c.drivers[n.networkType]
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
dd, err = c.loadDriver(n.networkType)
|
|
|
|
if err != nil {
|
|
|
|
// If driver could not be resolved simply return an empty string
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dd.capability.DataScope
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) driver() (driverapi.Driver, error) {
|
|
|
|
c := n.getController()
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
c.Lock()
|
|
|
|
// Check if a driver for the specified network type is available
|
|
|
|
dd, ok := c.drivers[n.networkType]
|
|
|
|
c.Unlock()
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
if !ok {
|
2015-10-05 11:21:15 +00:00
|
|
|
var err error
|
|
|
|
dd, err = c.loadDriver(n.networkType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dd.driver, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Delete() error {
|
|
|
|
n.Lock()
|
|
|
|
c := n.ctrlr
|
|
|
|
name := n.name
|
|
|
|
id := n.id
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
n, err := c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return &UnknownNetworkError{name: name, id: id}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
numEps := n.EndpointCnt()
|
2015-04-13 18:40:42 +00:00
|
|
|
if numEps != 0 {
|
2015-07-02 05:00:48 +00:00
|
|
|
return &ActiveEndpointsError{name: n.name, id: n.id}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-06-01 16:43:24 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err = n.deleteNetwork(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := c.addNetwork(n); e != nil {
|
|
|
|
log.Warnf("failed to rollback deleteNetwork for network %s: %v",
|
|
|
|
n.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// deleteFromStore performs an atomic delete operation and the
|
|
|
|
// network.endpointCnt field will help prevent any possible
|
|
|
|
// race between endpoint join and network delete
|
|
|
|
if err = n.getController().deleteFromStore(n); err != nil {
|
2015-06-11 12:32:15 +00:00
|
|
|
if err == datastore.ErrKeyModified {
|
2015-06-05 20:31:12 +00:00
|
|
|
return types.InternalErrorf("operation in progress. delete failed for network %s. Please try again.")
|
|
|
|
}
|
2015-06-01 16:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-09-16 11:42:35 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.dbExists = false
|
2015-10-05 11:21:15 +00:00
|
|
|
if e := n.getController().updateToStore(n); e != nil {
|
2015-09-16 11:42:35 +00:00
|
|
|
log.Warnf("failed to recreate network in store %s : %v", n.name, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
n.ipamRelease()
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) deleteNetwork() error {
|
2015-10-05 11:21:15 +00:00
|
|
|
d, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed deleting network: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is bridge network type make sure we call the driver about the network
|
|
|
|
// because the network may have been created in some past life of libnetwork.
|
|
|
|
if n.Type() == "bridge" {
|
|
|
|
n.drvOnce.Do(func() {
|
|
|
|
err = n.getController().addNetwork(n)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err := d.DeleteNetwork(n.ID()); err != nil {
|
2015-06-10 20:27:23 +00:00
|
|
|
// Forbidden Errors should be honored
|
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-06-10 20:27:23 +00:00
|
|
|
log.Warnf("driver error deleting network %s : %v", n.name, err)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2015-06-10 20:27:23 +00:00
|
|
|
return nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (n *network) addEndpoint(ep *endpoint) error {
|
2015-10-05 11:21:15 +00:00
|
|
|
d, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to add endpoint: %v", err)
|
|
|
|
}
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// If it is bridge network type make sure we call the driver about the network
|
|
|
|
// because the network may have been created in some past life of libnetwork.
|
|
|
|
if n.Type() == "bridge" {
|
|
|
|
n.drvOnce.Do(func() {
|
|
|
|
err = n.getController().addNetwork(n)
|
|
|
|
})
|
2015-05-31 18:49:11 +00:00
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
return err
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
err = d.CreateEndpoint(n.id, ep.id, ep.Interface(), ep.generic)
|
2015-05-31 18:49:11 +00:00
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
return types.InternalErrorf("failed to create endpoint %s on network %s: %v",
|
|
|
|
ep.Name(), n.Name(), err)
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
|
2015-06-05 20:31:12 +00:00
|
|
|
var err error
|
2015-06-14 16:00:27 +00:00
|
|
|
if !config.IsValidName(name) {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
2015-06-04 16:41:42 +00:00
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if _, err = n.EndpointByName(name); err == nil {
|
2015-06-04 16:41:42 +00:00
|
|
|
return nil, types.ForbiddenErrorf("service endpoint with name %s already exists", name)
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}}
|
2015-07-02 05:00:48 +00:00
|
|
|
ep.id = stringid.GenerateRandomID()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
// Initialize ep.network with a possibly stale copy of n. We need this to get network from
|
|
|
|
// store. But once we get it from store we will have the most uptodate copy possible.
|
2015-04-13 18:40:42 +00:00
|
|
|
ep.network = n
|
2015-10-05 11:21:15 +00:00
|
|
|
ep.network, err = ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get network during CreateEndpoint: %v", err)
|
|
|
|
}
|
|
|
|
n = ep.network
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
ep.processOptions(options...)
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
if err = ep.assignAddress(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ep.releaseAddress()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err = n.addEndpoint(ep); err != nil {
|
2015-05-31 18:49:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
if e := ep.deleteEndpoint(); e != nil {
|
|
|
|
log.Warnf("cleaning up endpoint failed %s : %v", name, e)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
if err = n.getController().updateToStore(ep); err != nil {
|
2015-06-05 20:31:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
if e := n.getController().deleteFromStore(ep); e != nil {
|
|
|
|
log.Warnf("error rolling back endpoint %s from store: %v", name, e)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// Increment endpoint count to indicate completion of endpoint addition
|
|
|
|
if err = n.IncEndpointCnt(); err != nil {
|
|
|
|
return nil, err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 22:42:23 +00:00
|
|
|
return ep, nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 06:13:29 +00:00
|
|
|
func (n *network) Endpoints() []Endpoint {
|
2015-10-05 11:21:15 +00:00
|
|
|
var list []Endpoint
|
|
|
|
|
|
|
|
endpoints, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
list = append(list, ep)
|
2015-04-18 06:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
func (n *network) WalkEndpoints(walker EndpointWalker) {
|
|
|
|
for _, e := range n.Endpoints() {
|
|
|
|
if walker(e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
func (n *network) EndpointByName(name string) (Endpoint, error) {
|
|
|
|
if name == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
var e Endpoint
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
s := func(current Endpoint) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
e = current
|
|
|
|
return true
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
return false
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
if e == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchEndpoint(name)
|
2015-05-15 23:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
return e, nil
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
func (n *network) EndpointByID(id string) (Endpoint, error) {
|
|
|
|
if id == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidID(id)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
ep, err := n.getEndpointFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrNoSuchEndpoint(id)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-13 21:12:57 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
return ep, nil
|
2015-05-13 21:12:57 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool) {
|
|
|
|
c := n.getController()
|
|
|
|
sr, ok := c.svcDb[n.ID()]
|
|
|
|
if !ok {
|
|
|
|
c.svcDb[n.ID()] = svcMap{}
|
|
|
|
sr = c.svcDb[n.ID()]
|
|
|
|
}
|
|
|
|
|
2015-06-19 06:40:17 +00:00
|
|
|
n.Lock()
|
|
|
|
var recs []etchosts.Record
|
2015-10-03 23:11:50 +00:00
|
|
|
if iface := ep.Iface(); iface.Address() != nil {
|
2015-06-19 06:40:17 +00:00
|
|
|
if isAdd {
|
2015-10-05 11:21:15 +00:00
|
|
|
sr[ep.Name()] = iface.Address().IP
|
|
|
|
sr[ep.Name()+"."+n.name] = iface.Address().IP
|
2015-06-19 06:40:17 +00:00
|
|
|
} else {
|
2015-10-05 11:21:15 +00:00
|
|
|
delete(sr, ep.Name())
|
|
|
|
delete(sr, ep.Name()+"."+n.name)
|
2015-06-19 06:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recs = append(recs, etchosts.Record{
|
|
|
|
Hosts: ep.Name(),
|
|
|
|
IP: iface.Address().IP.String(),
|
|
|
|
})
|
|
|
|
|
|
|
|
recs = append(recs, etchosts.Record{
|
|
|
|
Hosts: ep.Name() + "." + n.name,
|
|
|
|
IP: iface.Address().IP.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
|
2015-06-24 18:42:53 +00:00
|
|
|
// If there are no records to add or delete then simply return here
|
|
|
|
if len(recs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
var sbList []*sandbox
|
2015-10-05 11:21:15 +00:00
|
|
|
for _, ep := range localEps {
|
|
|
|
if sb, hasSandbox := ep.getSandbox(); hasSandbox {
|
2015-07-02 05:00:48 +00:00
|
|
|
sbList = append(sbList, sb)
|
2015-06-19 06:40:17 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
for _, sb := range sbList {
|
2015-06-19 06:40:17 +00:00
|
|
|
if isAdd {
|
2015-07-02 05:00:48 +00:00
|
|
|
sb.addHostsEntries(recs)
|
2015-06-19 06:40:17 +00:00
|
|
|
} else {
|
2015-07-02 05:00:48 +00:00
|
|
|
sb.deleteHostsEntries(recs)
|
2015-06-19 06:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getSvcRecords() []etchosts.Record {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
var recs []etchosts.Record
|
2015-10-05 11:21:15 +00:00
|
|
|
sr, _ := n.ctrlr.svcDb[n.id]
|
|
|
|
|
|
|
|
for h, ip := range sr {
|
2015-06-19 06:40:17 +00:00
|
|
|
recs = append(recs, etchosts.Record{
|
|
|
|
Hosts: h,
|
|
|
|
IP: ip.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return recs
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
func (n *network) getController() *controller {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.ctrlr
|
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
|
|
|
|
func (n *network) ipamAllocate() ([]func(), error) {
|
|
|
|
var (
|
|
|
|
cnl []func()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// For now also exclude bridge from using new ipam
|
|
|
|
if n.Type() == "host" || n.Type() == "null" || n.Type() == "bridge" {
|
|
|
|
return cnl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ipam, err := n.getController().getIpamDriver(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.addrSpace == "" {
|
|
|
|
if n.addrSpace, err = n.deriveAddressSpace(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.ipamV4Config == nil {
|
|
|
|
n.ipamV4Config = []*IpamConf{&IpamConf{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
n.ipamV4Info = make([]*IpamInfo, len(n.ipamV4Config))
|
|
|
|
|
|
|
|
for i, cfg := range n.ipamV4Config {
|
|
|
|
if err = cfg.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d := &IpamInfo{}
|
|
|
|
n.ipamV4Info[i] = d
|
|
|
|
|
|
|
|
d.PoolID, d.Pool, d.Meta, err = ipam.RequestPool(n.addrSpace, cfg.PreferredPool, cfg.SubPool, cfg.Options, cfg.IsV6)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
|
|
|
log.Warnf("Failed to release address pool %s after failure to create network %s (%s)", d.PoolID, n.Name(), n.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if gws, ok := d.Meta[netlabel.Gateway]; ok {
|
|
|
|
if d.Gateway, err = types.ParseCIDR(gws); err != nil {
|
|
|
|
return nil, types.BadRequestErrorf("failed to parse gateway address (%v) returned by ipam driver: %v", gws, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user requested a specific gateway, libnetwork will allocate it
|
|
|
|
// irrespective of whether ipam driver returned a gateway already.
|
|
|
|
// If none of the above is true, libnetwork will allocate one.
|
|
|
|
if cfg.Gateway != "" || d.Gateway == nil {
|
|
|
|
if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), nil); err != nil {
|
|
|
|
return nil, types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cnl = append(cnl, func() {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, d.Gateway.IP); err != nil {
|
|
|
|
log.Warnf("Failed to release gw address %s after failure to create network %s (%s)", d.Gateway, n.Name(), n.ID())
|
|
|
|
}
|
|
|
|
})
|
2015-10-07 03:29:30 +00:00
|
|
|
// Auxiliary addresses must be part of the master address pool
|
|
|
|
// If they fall into the container addressable pool, libnetwork will reserve them
|
2015-10-03 23:11:50 +00:00
|
|
|
if cfg.AuxAddresses != nil {
|
|
|
|
var ip net.IP
|
|
|
|
d.IPAMData.AuxAddresses = make(map[string]*net.IPNet, len(cfg.AuxAddresses))
|
|
|
|
for k, v := range cfg.AuxAddresses {
|
|
|
|
if ip = net.ParseIP(v); ip == nil {
|
2015-10-07 03:29:30 +00:00
|
|
|
return nil, types.BadRequestErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
2015-10-07 03:29:30 +00:00
|
|
|
if !d.Pool.Contains(ip) {
|
|
|
|
return cnl, types.ForbiddenErrorf("auxilairy address: (%s:%s) must belong to the master pool: %s", k, v, d.Pool)
|
|
|
|
}
|
|
|
|
// Attempt reservation in the container addressable pool, silent the error if address does not belong to that pool
|
|
|
|
if d.IPAMData.AuxAddresses[k], _, err = ipam.RequestAddress(d.PoolID, ip, nil); err != nil && err != ipamapi.ErrIPOutOfRange {
|
|
|
|
return nil, types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
cnl = append(cnl, func() {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, ip); err != nil {
|
|
|
|
log.Warnf("Failed to release secondary ip address %s(%s) after failure to create network %s (%s)", k, v, ip, n.Name(), n.ID())
|
|
|
|
}
|
|
|
|
})
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cnl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ipamRelease() {
|
|
|
|
// For now also exclude bridge from using new ipam
|
|
|
|
if n.Type() == "host" || n.Type() == "null" || n.Type() == "bridge" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ipam, err := n.getController().getIpamDriver(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to retrieve ipam driver to release address pool(s) on delete of network %s (%s): %v", n.Name(), n.ID(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, d := range n.ipamV4Info {
|
|
|
|
if d.Gateway != nil {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, d.Gateway.IP); err != nil {
|
|
|
|
log.Warnf("Failed to release gateway ip address %s on delete of network %s (%s): %v", d.Gateway.IP, n.Name(), n.ID(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.IPAMData.AuxAddresses != nil {
|
|
|
|
for k, nw := range d.IPAMData.AuxAddresses {
|
2015-10-07 03:29:30 +00:00
|
|
|
if d.Pool.Contains(nw.IP) {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, nw.IP); err != nil && err != ipamapi.ErrIPOutOfRange {
|
|
|
|
log.Warnf("Failed to release secondary ip address %s (%v) on delete of network %s (%s): %v", k, nw.IP, n.Name(), n.ID(), err)
|
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
|
|
|
log.Warnf("Failed to release address pool %s on delete of network %s (%s): %v", d.PoolID, n.Name(), n.ID(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getIPInfo() []*IpamInfo {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
l := make([]*IpamInfo, 0, len(n.ipamV4Info))
|
|
|
|
for _, d := range n.ipamV4Info {
|
|
|
|
l = append(l, d)
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getIPv4Data() []driverapi.IPAMData {
|
|
|
|
l := make([]driverapi.IPAMData, 0, len(n.ipamV4Info))
|
|
|
|
n.Lock()
|
|
|
|
for _, d := range n.ipamV4Info {
|
|
|
|
l = append(l, d.IPAMData)
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getIPv6Data() []driverapi.IPAMData {
|
|
|
|
l := make([]driverapi.IPAMData, 0, len(n.ipamV6Info))
|
|
|
|
n.Lock()
|
|
|
|
for _, d := range n.ipamV6Info {
|
|
|
|
l = append(l, d.IPAMData)
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) deriveAddressSpace() (string, error) {
|
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
|
|
|
ipd, ok := c.ipamDrivers[n.ipamType]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return "", types.NotFoundErrorf("could not find ipam driver %s to get default address space", n.ipamType)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
if n.DataScope() == datastore.GlobalScope {
|
2015-10-03 23:11:50 +00:00
|
|
|
return ipd.defaultGlobalAddressSpace, nil
|
|
|
|
}
|
|
|
|
return ipd.defaultLocalAddressSpace, nil
|
|
|
|
}
|