2015-05-06 20:09:12 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-06-12 16:51:57 -04:00
|
|
|
"encoding/json"
|
2015-10-04 23:02:02 -04:00
|
|
|
"fmt"
|
2015-06-20 20:08:36 -04:00
|
|
|
"net"
|
2015-10-26 16:56:56 -04:00
|
|
|
"strings"
|
2015-05-06 20:09:12 -04:00
|
|
|
"sync"
|
2016-10-14 14:12:24 -04:00
|
|
|
"time"
|
2015-05-06 20:09:12 -04:00
|
|
|
|
2016-11-09 21:01:07 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-05-06 20:09:12 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-06-12 04:01:45 -04:00
|
|
|
"github.com/docker/libnetwork/common"
|
2015-05-20 08:20:19 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-06-12 16:51:57 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-06 20:09:12 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-06-20 20:08:36 -04:00
|
|
|
"github.com/docker/libnetwork/etchosts"
|
2015-10-04 23:02:02 -04:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-05-06 20:09:12 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-12-18 14:03:57 -05:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
2016-11-03 02:15:04 -04:00
|
|
|
"github.com/docker/libnetwork/networkdb"
|
2015-05-06 20:09:12 -04:00
|
|
|
"github.com/docker/libnetwork/options"
|
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Network represents a logical connectivity zone that containers may
|
|
|
|
// join using the Link method. A Network is managed by a specific driver.
|
|
|
|
type Network interface {
|
|
|
|
// A user chosen name for this network.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// A system generated id for this network.
|
|
|
|
ID() string
|
|
|
|
|
|
|
|
// The type of network, which corresponds to its managing driver.
|
|
|
|
Type() string
|
|
|
|
|
|
|
|
// Create a new endpoint to this network symbolically identified by the
|
2016-11-03 02:15:04 -04:00
|
|
|
// specified unique name. The options parameter carries driver specific options.
|
2015-05-06 20:09:12 -04:00
|
|
|
CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error)
|
|
|
|
|
|
|
|
// Delete the network.
|
|
|
|
Delete() error
|
|
|
|
|
|
|
|
// Endpoints returns the list of Endpoint(s) in this network.
|
|
|
|
Endpoints() []Endpoint
|
|
|
|
|
|
|
|
// WalkEndpoints uses the provided function to walk the Endpoints
|
|
|
|
WalkEndpoints(walker EndpointWalker)
|
|
|
|
|
|
|
|
// EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
|
|
|
|
EndpointByName(name string) (Endpoint, error)
|
|
|
|
|
|
|
|
// EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
|
|
|
|
EndpointByID(id string) (Endpoint, error)
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
// Return certain operational data belonging to this network
|
|
|
|
Info() NetworkInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkInfo returns some configuration and operational information about the network
|
|
|
|
type NetworkInfo interface {
|
2016-01-08 16:56:01 -05:00
|
|
|
IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf)
|
2015-12-18 14:03:57 -05:00
|
|
|
IpamInfo() ([]*IpamInfo, []*IpamInfo)
|
2015-10-14 23:11:53 -04:00
|
|
|
DriverOptions() map[string]string
|
2015-10-04 23:02:02 -04:00
|
|
|
Scope() string
|
2016-02-10 21:09:33 -05:00
|
|
|
IPv6Enabled() bool
|
2016-01-08 16:56:01 -05:00
|
|
|
Internal() bool
|
2016-11-19 19:03:22 -05:00
|
|
|
Attachable() bool
|
2017-03-14 00:39:56 -04:00
|
|
|
Ingress() bool
|
2017-05-13 22:21:58 -04:00
|
|
|
ConfigFrom() string
|
|
|
|
ConfigOnly() bool
|
2016-03-16 17:52:34 -04:00
|
|
|
Labels() map[string]string
|
2016-06-07 17:28:28 -04:00
|
|
|
Dynamic() bool
|
2016-10-14 14:12:24 -04:00
|
|
|
Created() time.Time
|
2016-11-03 02:15:04 -04:00
|
|
|
// Peers returns a slice of PeerInfo structures which has the information about the peer
|
|
|
|
// nodes participating in the same overlay network. This is currently the per-network
|
|
|
|
// gossip cluster. For non-dynamic overlay networks and bridge networks it returns an
|
|
|
|
// empty slice
|
|
|
|
Peers() []networkdb.PeerInfo
|
2017-03-11 20:22:15 -05:00
|
|
|
//Services returns a map of services keyed by the service name with the details
|
|
|
|
//of all the tasks that belong to the service. Applicable only in swarm mode.
|
|
|
|
Services() map[string]ServiceInfo
|
2015-05-06 20:09:12 -04: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
|
|
|
|
|
2017-01-04 14:42:48 -05:00
|
|
|
// ipInfo is the reverse mapping from IP to service name to serve the PTR query.
|
|
|
|
// extResolver is set if an externl server resolves a service name to this IP.
|
|
|
|
// Its an indication to defer PTR queries also to that external server.
|
|
|
|
type ipInfo struct {
|
|
|
|
name string
|
2017-06-19 22:42:24 -04:00
|
|
|
serviceID string
|
2017-01-04 14:42:48 -05:00
|
|
|
extResolver bool
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
// svcMapEntry is the body of the element into the svcMap
|
|
|
|
// The ip is a string because the SetMatrix does not accept non hashable values
|
|
|
|
type svcMapEntry struct {
|
|
|
|
ip string
|
|
|
|
serviceID string
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
type svcInfo struct {
|
2017-06-19 22:42:24 -04:00
|
|
|
svcMap common.SetMatrix
|
|
|
|
svcIPv6Map common.SetMatrix
|
2017-06-12 04:01:45 -04:00
|
|
|
ipMap common.SetMatrix
|
2016-06-07 17:28:28 -04:00
|
|
|
service map[string][]servicePorts
|
|
|
|
}
|
|
|
|
|
|
|
|
// backing container or host's info
|
|
|
|
type serviceTarget struct {
|
|
|
|
name string
|
|
|
|
ip net.IP
|
|
|
|
port uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type servicePorts struct {
|
|
|
|
portName string
|
|
|
|
proto string
|
|
|
|
target []serviceTarget
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2017-03-11 20:22:15 -05:00
|
|
|
type networkDBTable struct {
|
|
|
|
name string
|
|
|
|
objType driverapi.ObjectType
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// IpamConf contains all the ipam related configurations for a network
|
|
|
|
type IpamConf struct {
|
|
|
|
// The master address pool for containers and network interfaces
|
|
|
|
PreferredPool string
|
|
|
|
// A subset of the master pool. If specified,
|
|
|
|
// this becomes the container pool
|
|
|
|
SubPool string
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 Ipam configuration", 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-05-06 20:09:12 -04:00
|
|
|
type network struct {
|
2015-10-04 23:02:02 -04:00
|
|
|
ctrlr *controller
|
|
|
|
name string
|
|
|
|
networkType string
|
|
|
|
id string
|
2016-10-14 14:12:24 -04:00
|
|
|
created time.Time
|
2017-05-13 22:21:58 -04:00
|
|
|
scope string // network data scope
|
2016-03-16 17:52:34 -04:00
|
|
|
labels map[string]string
|
2015-10-04 23:02:02 -04:00
|
|
|
ipamType string
|
2016-01-08 16:56:01 -05:00
|
|
|
ipamOptions map[string]string
|
2015-10-04 23:02:02 -04:00
|
|
|
addrSpace string
|
|
|
|
ipamV4Config []*IpamConf
|
|
|
|
ipamV6Config []*IpamConf
|
|
|
|
ipamV4Info []*IpamInfo
|
|
|
|
ipamV6Info []*IpamInfo
|
|
|
|
enableIPv6 bool
|
2015-11-11 00:13:25 -05:00
|
|
|
postIPv6 bool
|
2015-10-04 23:02:02 -04:00
|
|
|
epCnt *endpointCnt
|
|
|
|
generic options.Generic
|
|
|
|
dbIndex uint64
|
|
|
|
dbExists bool
|
|
|
|
persist bool
|
|
|
|
stopWatchCh chan struct{}
|
|
|
|
drvOnce *sync.Once
|
2016-09-20 21:21:19 -04:00
|
|
|
resolverOnce sync.Once
|
|
|
|
resolver []Resolver
|
2015-12-18 14:03:57 -05:00
|
|
|
internal bool
|
2016-11-19 19:03:22 -05:00
|
|
|
attachable bool
|
2016-03-16 16:25:44 -04:00
|
|
|
inDelete bool
|
2016-06-07 17:28:28 -04:00
|
|
|
ingress bool
|
2017-03-11 20:22:15 -05:00
|
|
|
driverTables []networkDBTable
|
2016-06-07 17:28:28 -04:00
|
|
|
dynamic bool
|
2017-05-13 22:21:58 -04:00
|
|
|
configOnly bool
|
|
|
|
configFrom string
|
2015-05-06 20:09:12 -04:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Name() string {
|
2015-06-02 14:30:36 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-05-06 20:09:12 -04:00
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ID() string {
|
2015-06-02 14:30:36 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-09-02 19:42:01 -04:00
|
|
|
return n.id
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
2016-10-14 14:12:24 -04:00
|
|
|
func (n *network) Created() time.Time {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.created
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:09:12 -04:00
|
|
|
func (n *network) Type() string {
|
2015-06-02 14:30:36 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
return n.networkType
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
func (n *network) Key() []string {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-09-02 19:42:01 -04:00
|
|
|
return []string{datastore.NetworkKeyPrefix, n.id}
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) KeyPrefix() []string {
|
|
|
|
return []string{datastore.NetworkKeyPrefix}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Value() []byte {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-06-30 17:16:16 -04:00
|
|
|
func (n *network) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, n)
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
func (n *network) Index() uint64 {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) SetIndex(index uint64) {
|
|
|
|
n.Lock()
|
|
|
|
n.dbIndex = index
|
2015-06-30 17:16:16 -04:00
|
|
|
n.dbExists = true
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-06-30 17:16:16 -04:00
|
|
|
func (n *network) Exists() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.dbExists
|
|
|
|
}
|
|
|
|
|
2015-09-25 13:02:31 -04:00
|
|
|
func (n *network) Skip() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return !n.persist
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func (n *network) New() datastore.KVObject {
|
2015-09-25 13:02:31 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
return &network{
|
|
|
|
ctrlr: n.ctrlr,
|
|
|
|
drvOnce: &sync.Once{},
|
2016-01-19 16:49:13 -05:00
|
|
|
scope: n.scope,
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-09-25 13:02:31 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// CopyTo deep copies to the destination IpamConfig
|
|
|
|
func (c *IpamConf) CopyTo(dstC *IpamConf) error {
|
|
|
|
dstC.PreferredPool = c.PreferredPool
|
|
|
|
dstC.SubPool = c.SubPool
|
|
|
|
dstC.Gateway = c.Gateway
|
|
|
|
if c.AuxAddresses != nil {
|
|
|
|
dstC.AuxAddresses = make(map[string]string, len(c.AuxAddresses))
|
|
|
|
for k, v := range c.AuxAddresses {
|
|
|
|
dstC.AuxAddresses[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
func (n *network) validateConfiguration() error {
|
|
|
|
if n.configOnly {
|
|
|
|
// Only supports network specific configurations.
|
|
|
|
// Network operator configurations are not supported.
|
2017-05-18 18:14:12 -04:00
|
|
|
if n.ingress || n.internal || n.attachable || n.scope != "" {
|
2017-05-13 22:21:58 -04:00
|
|
|
return types.ForbiddenErrorf("configuration network can only contain network " +
|
|
|
|
"specific fields. Network operator fields like " +
|
2017-05-18 18:14:12 -04:00
|
|
|
"[ ingress | internal | attachable | scope ] are not supported.")
|
2017-05-13 22:21:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if n.configFrom != "" {
|
|
|
|
if n.configOnly {
|
|
|
|
return types.ForbiddenErrorf("a configuration network cannot depend on another configuration network")
|
|
|
|
}
|
|
|
|
if n.ipamType != "" &&
|
|
|
|
n.ipamType != defaultIpamForNetworkType(n.networkType) ||
|
|
|
|
n.enableIPv6 ||
|
|
|
|
len(n.labels) > 0 || len(n.ipamOptions) > 0 ||
|
|
|
|
len(n.ipamV4Config) > 0 || len(n.ipamV6Config) > 0 {
|
|
|
|
return types.ForbiddenErrorf("user specified configurations are not supported if the network depends on a configuration network")
|
|
|
|
}
|
|
|
|
if len(n.generic) > 0 {
|
|
|
|
if data, ok := n.generic[netlabel.GenericData]; ok {
|
|
|
|
var (
|
|
|
|
driverOptions map[string]string
|
|
|
|
opts interface{}
|
|
|
|
)
|
|
|
|
switch data.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
opts = data.(map[string]interface{})
|
|
|
|
case map[string]string:
|
|
|
|
opts = data.(map[string]string)
|
|
|
|
}
|
|
|
|
ba, err := json.Marshal(opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to validate network configuration: %v", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(ba, &driverOptions); err != nil {
|
|
|
|
return fmt.Errorf("failed to validate network configuration: %v", err)
|
|
|
|
}
|
|
|
|
if len(driverOptions) > 0 {
|
|
|
|
return types.ForbiddenErrorf("network driver options are not supported if the network depends on a configuration network")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applies network specific configurations
|
|
|
|
func (n *network) applyConfigurationTo(to *network) error {
|
|
|
|
to.enableIPv6 = n.enableIPv6
|
|
|
|
if len(n.labels) > 0 {
|
|
|
|
to.labels = make(map[string]string, len(n.labels))
|
|
|
|
for k, v := range n.labels {
|
|
|
|
if _, ok := to.labels[k]; !ok {
|
|
|
|
to.labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-31 19:04:00 -04:00
|
|
|
if len(n.ipamType) != 0 {
|
|
|
|
to.ipamType = n.ipamType
|
|
|
|
}
|
2017-05-13 22:21:58 -04:00
|
|
|
if len(n.ipamOptions) > 0 {
|
|
|
|
to.ipamOptions = make(map[string]string, len(n.ipamOptions))
|
|
|
|
for k, v := range n.ipamOptions {
|
|
|
|
if _, ok := to.ipamOptions[k]; !ok {
|
|
|
|
to.ipamOptions[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(n.ipamV4Config) > 0 {
|
|
|
|
to.ipamV4Config = make([]*IpamConf, 0, len(n.ipamV4Config))
|
|
|
|
for _, v4conf := range n.ipamV4Config {
|
|
|
|
to.ipamV4Config = append(to.ipamV4Config, v4conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(n.ipamV6Config) > 0 {
|
|
|
|
to.ipamV6Config = make([]*IpamConf, 0, len(n.ipamV6Config))
|
|
|
|
for _, v6conf := range n.ipamV6Config {
|
|
|
|
to.ipamV6Config = append(to.ipamV6Config, v6conf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(n.generic) > 0 {
|
|
|
|
to.generic = options.Generic{}
|
|
|
|
for k, v := range n.generic {
|
|
|
|
to.generic[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func (n *network) CopyTo(o datastore.KVObject) error {
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
dstN := o.(*network)
|
|
|
|
dstN.name = n.name
|
|
|
|
dstN.id = n.id
|
2016-10-14 14:12:24 -04:00
|
|
|
dstN.created = n.created
|
2015-10-04 23:02:02 -04:00
|
|
|
dstN.networkType = n.networkType
|
2016-01-19 16:49:13 -05:00
|
|
|
dstN.scope = n.scope
|
2016-07-12 14:27:32 -04:00
|
|
|
dstN.dynamic = n.dynamic
|
2015-10-04 23:02:02 -04:00
|
|
|
dstN.ipamType = n.ipamType
|
|
|
|
dstN.enableIPv6 = n.enableIPv6
|
|
|
|
dstN.persist = n.persist
|
2015-11-11 00:13:25 -05:00
|
|
|
dstN.postIPv6 = n.postIPv6
|
2015-10-04 23:02:02 -04:00
|
|
|
dstN.dbIndex = n.dbIndex
|
|
|
|
dstN.dbExists = n.dbExists
|
|
|
|
dstN.drvOnce = n.drvOnce
|
2015-12-18 14:03:57 -05:00
|
|
|
dstN.internal = n.internal
|
2016-11-19 19:03:22 -05:00
|
|
|
dstN.attachable = n.attachable
|
2016-03-16 16:25:44 -04:00
|
|
|
dstN.inDelete = n.inDelete
|
2016-06-07 17:28:28 -04:00
|
|
|
dstN.ingress = n.ingress
|
2017-05-13 22:21:58 -04:00
|
|
|
dstN.configOnly = n.configOnly
|
|
|
|
dstN.configFrom = n.configFrom
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
// copy labels
|
|
|
|
if dstN.labels == nil {
|
|
|
|
dstN.labels = make(map[string]string, len(n.labels))
|
|
|
|
}
|
|
|
|
for k, v := range n.labels {
|
|
|
|
dstN.labels[k] = v
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:00:35 -04:00
|
|
|
if n.ipamOptions != nil {
|
|
|
|
dstN.ipamOptions = make(map[string]string, len(n.ipamOptions))
|
|
|
|
for k, v := range n.ipamOptions {
|
|
|
|
dstN.ipamOptions[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
for _, v4conf := range n.ipamV4Config {
|
|
|
|
dstV4Conf := &IpamConf{}
|
|
|
|
v4conf.CopyTo(dstV4Conf)
|
|
|
|
dstN.ipamV4Config = append(dstN.ipamV4Config, dstV4Conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v4info := range n.ipamV4Info {
|
|
|
|
dstV4Info := &IpamInfo{}
|
|
|
|
v4info.CopyTo(dstV4Info)
|
|
|
|
dstN.ipamV4Info = append(dstN.ipamV4Info, dstV4Info)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v6conf := range n.ipamV6Config {
|
|
|
|
dstV6Conf := &IpamConf{}
|
|
|
|
v6conf.CopyTo(dstV6Conf)
|
|
|
|
dstN.ipamV6Config = append(dstN.ipamV6Config, dstV6Conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v6info := range n.ipamV6Info {
|
|
|
|
dstV6Info := &IpamInfo{}
|
|
|
|
v6info.CopyTo(dstV6Info)
|
|
|
|
dstN.ipamV6Info = append(dstN.ipamV6Info, dstV6Info)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstN.generic = options.Generic{}
|
|
|
|
for k, v := range n.generic {
|
|
|
|
dstN.generic[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func (n *network) DataScope() string {
|
2017-05-13 22:21:58 -04:00
|
|
|
s := n.Scope()
|
|
|
|
// All swarm scope networks have local datascope
|
|
|
|
if s == datastore.SwarmScope {
|
|
|
|
s = datastore.LocalScope
|
|
|
|
}
|
|
|
|
return s
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func (n *network) getEpCnt() *endpointCnt {
|
2015-06-12 16:51:57 -04:00
|
|
|
n.Lock()
|
2015-10-04 23:02:02 -04:00
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.epCnt
|
2015-06-12 16:51:57 -04: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-09-02 19:42:01 -04:00
|
|
|
netMap["id"] = n.id
|
2016-10-14 14:12:24 -04:00
|
|
|
netMap["created"] = n.created
|
2015-06-12 16:51:57 -04:00
|
|
|
netMap["networkType"] = n.networkType
|
2016-01-19 16:49:13 -05:00
|
|
|
netMap["scope"] = n.scope
|
2016-03-16 17:52:34 -04:00
|
|
|
netMap["labels"] = n.labels
|
2015-10-04 23:02:02 -04:00
|
|
|
netMap["ipamType"] = n.ipamType
|
2016-04-08 22:00:35 -04:00
|
|
|
netMap["ipamOptions"] = n.ipamOptions
|
2015-10-04 23:02:02 -04:00
|
|
|
netMap["addrSpace"] = n.addrSpace
|
2015-06-12 16:51:57 -04:00
|
|
|
netMap["enableIPv6"] = n.enableIPv6
|
2015-10-04 23:02:02 -04:00
|
|
|
if n.generic != nil {
|
|
|
|
netMap["generic"] = n.generic
|
|
|
|
}
|
2015-09-25 13:02:31 -04:00
|
|
|
netMap["persist"] = n.persist
|
2015-11-11 00:13:25 -05:00
|
|
|
netMap["postIPv6"] = n.postIPv6
|
2015-10-04 23:02:02 -04: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-12-18 14:03:57 -05:00
|
|
|
netMap["internal"] = n.internal
|
2016-11-19 19:03:22 -05:00
|
|
|
netMap["attachable"] = n.attachable
|
2016-03-16 16:25:44 -04:00
|
|
|
netMap["inDelete"] = n.inDelete
|
2016-06-07 17:28:28 -04:00
|
|
|
netMap["ingress"] = n.ingress
|
2017-05-13 22:21:58 -04:00
|
|
|
netMap["configOnly"] = n.configOnly
|
|
|
|
netMap["configFrom"] = n.configFrom
|
2015-06-12 16:51:57 -04: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-09-02 19:42:01 -04:00
|
|
|
n.id = netMap["id"].(string)
|
2016-10-14 14:12:24 -04:00
|
|
|
// "created" is not available in older versions
|
|
|
|
if v, ok := netMap["created"]; ok {
|
|
|
|
// n.created is time.Time but marshalled as string
|
|
|
|
if err = n.created.UnmarshalText([]byte(v.(string))); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("failed to unmarshal creation time %v: %v", v, err)
|
2016-10-14 14:12:24 -04:00
|
|
|
n.created = time.Time{}
|
|
|
|
}
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
n.networkType = netMap["networkType"].(string)
|
|
|
|
n.enableIPv6 = netMap["enableIPv6"].(bool)
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
// if we weren't unmarshaling to netMap we could simply set n.labels
|
|
|
|
// unfortunately, we can't because map[string]interface{} != map[string]string
|
|
|
|
if labels, ok := netMap["labels"].(map[string]interface{}); ok {
|
|
|
|
n.labels = make(map[string]string, len(labels))
|
|
|
|
for label, value := range labels {
|
|
|
|
n.labels[label] = value.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 22:00:35 -04:00
|
|
|
if v, ok := netMap["ipamOptions"]; ok {
|
|
|
|
if iOpts, ok := v.(map[string]interface{}); ok {
|
|
|
|
n.ipamOptions = make(map[string]string, len(iOpts))
|
|
|
|
for k, v := range iOpts {
|
|
|
|
n.ipamOptions[k] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
if v, ok := netMap["generic"]; ok {
|
|
|
|
n.generic = v.(map[string]interface{})
|
2015-10-14 23:11:53 -04:00
|
|
|
// Restore opts in their map[string]string form
|
2015-10-04 23:02:02 -04:00
|
|
|
if v, ok := n.generic[netlabel.GenericData]; ok {
|
|
|
|
var lmap map[string]string
|
|
|
|
ba, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(ba, &lmap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.generic[netlabel.GenericData] = lmap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := netMap["persist"]; ok {
|
|
|
|
n.persist = v.(bool)
|
|
|
|
}
|
2015-11-11 00:13:25 -05:00
|
|
|
if v, ok := netMap["postIPv6"]; ok {
|
|
|
|
n.postIPv6 = v.(bool)
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
if v, ok := netMap["ipamType"]; ok {
|
|
|
|
n.ipamType = v.(string)
|
|
|
|
} else {
|
|
|
|
n.ipamType = ipamapi.DefaultIPAM
|
|
|
|
}
|
|
|
|
if v, ok := netMap["addrSpace"]; ok {
|
|
|
|
n.addrSpace = v.(string)
|
|
|
|
}
|
|
|
|
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-06-12 16:51:57 -04:00
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
if v, ok := netMap["ipamV6Info"]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(v.(string)), &n.ipamV6Info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-25 13:02:31 -04:00
|
|
|
}
|
2015-12-18 14:03:57 -05:00
|
|
|
if v, ok := netMap["internal"]; ok {
|
|
|
|
n.internal = v.(bool)
|
|
|
|
}
|
2016-11-19 19:03:22 -05:00
|
|
|
if v, ok := netMap["attachable"]; ok {
|
|
|
|
n.attachable = v.(bool)
|
|
|
|
}
|
2016-01-19 16:49:13 -05:00
|
|
|
if s, ok := netMap["scope"]; ok {
|
|
|
|
n.scope = s.(string)
|
|
|
|
}
|
2016-03-16 16:25:44 -04:00
|
|
|
if v, ok := netMap["inDelete"]; ok {
|
|
|
|
n.inDelete = v.(bool)
|
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
if v, ok := netMap["ingress"]; ok {
|
|
|
|
n.ingress = v.(bool)
|
|
|
|
}
|
2017-05-13 22:21:58 -04:00
|
|
|
if v, ok := netMap["configOnly"]; ok {
|
|
|
|
n.configOnly = v.(bool)
|
|
|
|
}
|
|
|
|
if v, ok := netMap["configFrom"]; ok {
|
|
|
|
n.configFrom = v.(string)
|
|
|
|
}
|
2016-03-22 15:30:39 -04:00
|
|
|
// Reconcile old networks with the recently added `--ipv6` flag
|
|
|
|
if !n.enableIPv6 {
|
|
|
|
n.enableIPv6 = len(n.ipamV6Info) > 0
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:09:33 -05:00
|
|
|
// NetworkOption is an option setter function type used to pass various options to
|
2015-05-06 20:09:12 -04:00
|
|
|
// 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) {
|
2016-02-10 21:09:33 -05:00
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
if val, ok := generic[netlabel.EnableIPv6]; ok {
|
|
|
|
n.enableIPv6 = val.(bool)
|
|
|
|
}
|
|
|
|
if val, ok := generic[netlabel.Internal]; ok {
|
|
|
|
n.internal = val.(bool)
|
|
|
|
}
|
|
|
|
for k, v := range generic {
|
|
|
|
n.generic[k] = v
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// NetworkOptionIngress returns an option setter to indicate if a network is
|
|
|
|
// an ingress network.
|
2017-03-14 00:39:56 -04:00
|
|
|
func NetworkOptionIngress(ingress bool) NetworkOption {
|
2016-06-07 17:28:28 -04:00
|
|
|
return func(n *network) {
|
2017-03-14 00:39:56 -04:00
|
|
|
n.ingress = ingress
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 13:02:31 -04: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:09:33 -05:00
|
|
|
// NetworkOptionEnableIPv6 returns an option setter to explicitly configure IPv6
|
|
|
|
func NetworkOptionEnableIPv6(enableIPv6 bool) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
|
|
|
n.enableIPv6 = enableIPv6
|
|
|
|
n.generic[netlabel.EnableIPv6] = enableIPv6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 14:03:57 -05:00
|
|
|
// NetworkOptionInternalNetwork returns an option setter to config the network
|
|
|
|
// to be internal which disables default gateway service
|
|
|
|
func NetworkOptionInternalNetwork() NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
2016-02-10 21:09:33 -05:00
|
|
|
n.internal = true
|
2015-12-18 14:03:57 -05:00
|
|
|
n.generic[netlabel.Internal] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:03:22 -05:00
|
|
|
// NetworkOptionAttachable returns an option setter to set attachable for a network
|
|
|
|
func NetworkOptionAttachable(attachable bool) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.attachable = attachable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
// NetworkOptionScope returns an option setter to overwrite the network's scope.
|
|
|
|
// By default the network's scope is set to the network driver's datascope.
|
|
|
|
func NetworkOptionScope(scope string) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.scope = scope
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// NetworkOptionIpam function returns an option setter for the ipam configuration for this network
|
2016-01-08 16:56:01 -05:00
|
|
|
func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption {
|
2015-10-04 23:02:02 -04:00
|
|
|
return func(n *network) {
|
|
|
|
if ipamDriver != "" {
|
|
|
|
n.ipamType = ipamDriver
|
2016-11-09 21:01:07 -05:00
|
|
|
if ipamDriver == ipamapi.DefaultIPAM {
|
|
|
|
n.ipamType = defaultIpamForNetworkType(n.Type())
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2016-01-08 16:56:01 -05:00
|
|
|
n.ipamOptions = opts
|
2015-10-04 23:02:02 -04:00
|
|
|
n.addrSpace = addrSpace
|
|
|
|
n.ipamV4Config = ipV4
|
|
|
|
n.ipamV6Config = ipV6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
// NetworkOptionDriverOpts function returns an option setter for any driver parameter described by a map
|
2015-10-14 23:11:53 -04:00
|
|
|
func NetworkOptionDriverOpts(opts map[string]string) NetworkOption {
|
2015-10-04 23:02:02 -04:00
|
|
|
return func(n *network) {
|
|
|
|
if n.generic == nil {
|
|
|
|
n.generic = make(map[string]interface{})
|
|
|
|
}
|
2015-10-14 23:11:53 -04:00
|
|
|
if opts == nil {
|
|
|
|
opts = make(map[string]string)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
// Store the options
|
2015-10-14 23:11:53 -04:00
|
|
|
n.generic[netlabel.GenericData] = opts
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
// NetworkOptionLabels function returns an option setter for labels specific to a network
|
|
|
|
func NetworkOptionLabels(labels map[string]string) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.labels = labels
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
// NetworkOptionDynamic function returns an option setter for dynamic option for a network
|
|
|
|
func NetworkOptionDynamic() NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.dynamic = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-11 00:13:25 -05:00
|
|
|
// NetworkOptionDeferIPv6Alloc instructs the network to defer the IPV6 address allocation until after the endpoint has been created
|
|
|
|
// It is being provided to support the specific docker daemon flags where user can deterministically assign an IPv6 address
|
|
|
|
// to a container as combination of fixed-cidr-v6 + mac-address
|
|
|
|
// TODO: Remove this option setter once we support endpoint ipam options
|
|
|
|
func NetworkOptionDeferIPv6Alloc(enable bool) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.postIPv6 = enable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
// NetworkOptionConfigOnly tells controller this network is
|
|
|
|
// a configuration only network. It serves as a configuration
|
|
|
|
// for other networks.
|
|
|
|
func NetworkOptionConfigOnly() NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.configOnly = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkOptionConfigFrom tells controller to pick the
|
|
|
|
// network configuration from a configuration only network
|
|
|
|
func NetworkOptionConfigFrom(name string) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.configFrom = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 20:09:12 -04:00
|
|
|
func (n *network) processOptions(options ...NetworkOption) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
func (n *network) resolveDriver(name string, load bool) (driverapi.Driver, *driverapi.Capability, error) {
|
2015-10-04 23:02:02 -04:00
|
|
|
c := n.getController()
|
2015-05-06 20:09:12 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// Check if a driver for the specified network type is available
|
2016-05-08 03:32:51 -04:00
|
|
|
d, cap := c.drvRegistry.Driver(name)
|
|
|
|
if d == nil {
|
|
|
|
if load {
|
|
|
|
var err error
|
|
|
|
err = c.loadDriver(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
d, cap = c.drvRegistry.Driver(name)
|
|
|
|
if d == nil {
|
|
|
|
return nil, nil, fmt.Errorf("could not resolve driver %s in registry", name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// don't fail if driver loading is not required
|
|
|
|
return nil, nil, nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
return d, cap, nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
func (n *network) driverScope() string {
|
|
|
|
_, cap, err := n.resolveDriver(n.networkType, true)
|
|
|
|
if err != nil {
|
|
|
|
// If driver could not be resolved simply return an empty string
|
|
|
|
return ""
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
return cap.DataScope
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
func (n *network) driverIsMultihost() bool {
|
|
|
|
_, cap, err := n.resolveDriver(n.networkType, true)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return cap.ConnectivityScope == datastore.GlobalScope
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
func (n *network) driver(load bool) (driverapi.Driver, error) {
|
|
|
|
d, cap, err := n.resolveDriver(n.networkType, load)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
2016-01-19 16:49:13 -05:00
|
|
|
n.Lock()
|
2016-05-08 03:32:51 -04:00
|
|
|
// If load is not required, driver, cap and err may all be nil
|
2017-05-13 22:21:58 -04:00
|
|
|
if n.scope == "" && cap != nil {
|
2016-05-08 03:32:51 -04:00
|
|
|
n.scope = cap.DataScope
|
|
|
|
}
|
2017-05-18 18:14:12 -04:00
|
|
|
if n.dynamic {
|
|
|
|
// If the network is dynamic, then it is swarm
|
|
|
|
// scoped regardless of the backing driver.
|
2017-05-13 22:21:58 -04:00
|
|
|
n.scope = datastore.SwarmScope
|
2016-05-08 03:32:51 -04:00
|
|
|
}
|
2016-01-19 16:49:13 -05:00
|
|
|
n.Unlock()
|
2016-05-08 03:32:51 -04:00
|
|
|
return d, nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Delete() error {
|
2016-03-16 16:25:44 -04:00
|
|
|
return n.delete(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) delete(force bool) error {
|
2015-10-04 23:02:02 -04:00
|
|
|
n.Lock()
|
|
|
|
c := n.ctrlr
|
|
|
|
name := n.name
|
|
|
|
id := n.id
|
|
|
|
n.Unlock()
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
c.networkLocker.Lock(id)
|
|
|
|
defer c.networkLocker.Unlock(id)
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
n, err := c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return &UnknownNetworkError{name: name, id: id}
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:25:44 -04:00
|
|
|
if !force && n.getEpCnt().EndpointCnt() != 0 {
|
2017-05-13 22:21:58 -04:00
|
|
|
if n.configOnly {
|
|
|
|
return types.ForbiddenErrorf("configuration network %q is in use", n.Name())
|
|
|
|
}
|
2015-09-02 19:42:01 -04:00
|
|
|
return &ActiveEndpointsError{name: n.name, id: n.id}
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
2016-03-16 16:25:44 -04:00
|
|
|
// Mark the network for deletion
|
|
|
|
n.inDelete = true
|
|
|
|
if err = c.updateToStore(n); err != nil {
|
|
|
|
return fmt.Errorf("error marking network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2016-03-16 16:25:44 -04:00
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
if n.ConfigFrom() != "" {
|
|
|
|
if t, err := c.getConfigNetwork(n.ConfigFrom()); err == nil {
|
|
|
|
if err := t.getEpCnt().DecEndpointCnt(); err != nil {
|
|
|
|
logrus.Warnf("Failed to update reference count for configuration network %q on removal of network %q: %v",
|
|
|
|
t.Name(), n.Name(), err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Warnf("Could not find configuration network %q during removal of network %q", n.configOnly, n.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.configOnly {
|
|
|
|
goto removeFromStore
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:25:44 -04:00
|
|
|
if err = n.deleteNetwork(); err != nil {
|
|
|
|
if !force {
|
|
|
|
return err
|
2015-09-25 13:02:31 -04:00
|
|
|
}
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("driver failed to delete stale network %s (%s): %v", n.Name(), n.ID(), err)
|
2016-03-16 16:25:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
n.ipamRelease()
|
|
|
|
if err = c.updateToStore(n); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to update store after ipam release for network %s (%s): %v", n.Name(), n.ID(), err)
|
2016-03-16 16:25:44 -04:00
|
|
|
}
|
2015-09-25 13:02:31 -04:00
|
|
|
|
2016-08-23 01:20:48 -04:00
|
|
|
// We are about to delete the network. Leave the gossip
|
|
|
|
// cluster for the network to stop all incoming network
|
|
|
|
// specific gossip updates before cleaning up all the service
|
|
|
|
// bindings for the network. But cleanup service binding
|
|
|
|
// before deleting the network from the store since service
|
|
|
|
// bindings cleanup requires the network in the store.
|
|
|
|
n.cancelDriverWatches()
|
|
|
|
if err = n.leaveCluster(); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Errorf("Failed leaving network %s from the agent cluster: %v", n.Name(), err)
|
2016-08-23 01:20:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.cleanupServiceBindings(n.ID())
|
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
removeFromStore:
|
2015-10-04 23:02:02 -04:00
|
|
|
// deleteFromStore performs an atomic delete operation and the
|
|
|
|
// network.epCnt will help prevent any possible
|
|
|
|
// race between endpoint join and network delete
|
2016-03-16 16:25:44 -04:00
|
|
|
if err = c.deleteFromStore(n.getEpCnt()); err != nil {
|
|
|
|
if !force {
|
|
|
|
return fmt.Errorf("error deleting network endpoint count from store: %v", err)
|
|
|
|
}
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("Error deleting endpoint count from store for stale network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-12-07 18:46:14 -05:00
|
|
|
|
2016-03-16 16:25:44 -04:00
|
|
|
if err = c.deleteFromStore(n); err != nil {
|
2015-10-04 23:02:02 -04:00
|
|
|
return fmt.Errorf("error deleting network from store: %v", err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) deleteNetwork() error {
|
2016-01-19 16:49:13 -05:00
|
|
|
d, err := n.driver(true)
|
2015-10-04 23:02:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed deleting network: %v", err)
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
if err := d.DeleteNetwork(n.ID()); err != nil {
|
2015-06-12 16:51:57 -04:00
|
|
|
// Forbidden Errors should be honored
|
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
if _, ok := err.(types.MaskableError); !ok {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("driver error deleting network %s : %v", n.name, err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-09-20 21:21:19 -04:00
|
|
|
for _, resolver := range n.resolver {
|
|
|
|
resolver.Stop()
|
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) addEndpoint(ep *endpoint) error {
|
2016-01-19 16:49:13 -05:00
|
|
|
d, err := n.driver(true)
|
2015-10-04 23:02:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to add endpoint: %v", err)
|
|
|
|
}
|
2015-05-06 20:09:12 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
err = d.CreateEndpoint(n.id, ep.id, ep.Interface(), ep.generic)
|
2015-06-12 16:51:57 -04:00
|
|
|
if err != nil {
|
2015-10-04 23:02:02 -04:00
|
|
|
return types.InternalErrorf("failed to create endpoint %s on network %s: %v",
|
|
|
|
ep.Name(), n.Name(), err)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
return nil
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
|
2015-06-12 16:51:57 -04:00
|
|
|
var err error
|
2017-02-02 18:22:26 -05:00
|
|
|
if !config.IsValidName(name) {
|
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
if n.ConfigOnly() {
|
|
|
|
return nil, types.ForbiddenErrorf("cannot create endpoint on configuration-only network")
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
if _, err = n.EndpointByName(name); err == nil {
|
2017-01-04 14:42:48 -05:00
|
|
|
return nil, types.ForbiddenErrorf("endpoint with name %s already exists in network %s", name, n.Name())
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
ep := &endpoint{name: name, generic: make(map[string]interface{}), iface: &endpointInterface{}}
|
2015-09-02 19:42:01 -04:00
|
|
|
ep.id = stringid.GenerateRandomID()
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
n.ctrlr.networkLocker.Lock(n.id)
|
|
|
|
defer n.ctrlr.networkLocker.Unlock(n.id)
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// Initialize ep.network with a possibly stale copy of n. We need this to get network from
|
2016-02-10 21:09:33 -05:00
|
|
|
// store. But once we get it from store we will have the most uptodate copy possibly.
|
2015-05-06 20:09:12 -04:00
|
|
|
ep.network = n
|
2016-01-12 14:33:16 -05:00
|
|
|
ep.locator = n.getController().clusterHostID()
|
2015-10-04 23:02:02 -04: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-06 20:09:12 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
ep.processOptions(options...)
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
for _, llIPNet := range ep.Iface().LinkLocalAddresses() {
|
|
|
|
if !llIPNet.IP.IsLinkLocalUnicast() {
|
|
|
|
return nil, types.BadRequestErrorf("invalid link local IP address: %v", llIPNet.IP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 18:44:44 -05:00
|
|
|
if opt, ok := ep.generic[netlabel.MacAddress]; ok {
|
|
|
|
if mac, ok := opt.(net.HardwareAddr); ok {
|
|
|
|
ep.iface.mac = mac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
ipam, cap, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-12-18 14:03:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
if cap.RequiresMACAddress {
|
2015-12-18 14:03:57 -05:00
|
|
|
if ep.iface.mac == nil {
|
|
|
|
ep.iface.mac = netutils.GenerateRandomMAC()
|
|
|
|
}
|
|
|
|
if ep.ipamOptions == nil {
|
|
|
|
ep.ipamOptions = make(map[string]string)
|
|
|
|
}
|
|
|
|
ep.ipamOptions[netlabel.MacAddress] = ep.iface.mac.String()
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
if err = ep.assignAddress(ipam, true, n.enableIPv6 && !n.postIPv6); err != nil {
|
2015-06-12 16:51:57 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-04 23:02:02 -04:00
|
|
|
ep.releaseAddress()
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
}()
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2015-06-12 16:51:57 -04:00
|
|
|
if err = n.addEndpoint(ep); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-01-19 16:49:13 -05:00
|
|
|
if e := ep.deleteEndpoint(false); e != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("cleaning up endpoint failed %s : %v", name, e)
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
if err = ep.assignAddress(ipam, false, n.enableIPv6 && n.postIPv6); err != nil {
|
2015-11-11 00:13:25 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
if err = n.getController().updateToStore(ep); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := n.getController().deleteFromStore(ep); e != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("error rolling back endpoint %s from store: %v", name, e)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-09-25 13:02:31 -04:00
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
}()
|
|
|
|
|
2015-10-26 16:56:56 -04:00
|
|
|
// Watch for service records
|
|
|
|
n.getController().watchSvcRecord(ep)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.getController().unWatchSvcRecord(ep)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
// Increment endpoint count to indicate completion of endpoint addition
|
|
|
|
if err = n.getEpCnt().IncEndpointCnt(); err != nil {
|
|
|
|
return nil, err
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Endpoints() []Endpoint {
|
2015-10-04 23:02:02 -04:00
|
|
|
var list []Endpoint
|
|
|
|
|
|
|
|
endpoints, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Error(err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range endpoints {
|
|
|
|
list = append(list, ep)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) WalkEndpoints(walker EndpointWalker) {
|
|
|
|
for _, e := range n.Endpoints() {
|
|
|
|
if walker(e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) EndpointByName(name string) (Endpoint, error) {
|
|
|
|
if name == "" {
|
2015-05-23 02:00:03 -04:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
var e Endpoint
|
|
|
|
|
|
|
|
s := func(current Endpoint) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
e = current
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
|
|
|
if e == nil {
|
2015-05-23 02:00:03 -04:00
|
|
|
return nil, ErrNoSuchEndpoint(name)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) EndpointByID(id string) (Endpoint, error) {
|
|
|
|
if id == "" {
|
2015-05-23 02:00:03 -04:00
|
|
|
return nil, ErrInvalidID(id)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
ep, err := n.getEndpointFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrNoSuchEndpoint(id)
|
2015-05-06 20:09:12 -04:00
|
|
|
}
|
2015-06-12 16:51:57 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
return ep, nil
|
2015-06-12 16:51:57 -04:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2015-10-04 23:02:02 -04:00
|
|
|
func (n *network) updateSvcRecord(ep *endpoint, localEps []*endpoint, isAdd bool) {
|
2016-03-22 15:30:39 -04:00
|
|
|
var ipv6 net.IP
|
2016-01-08 16:56:01 -05:00
|
|
|
epName := ep.Name()
|
2015-10-04 23:02:02 -04:00
|
|
|
if iface := ep.Iface(); iface.Address() != nil {
|
2016-01-08 16:56:01 -05:00
|
|
|
myAliases := ep.MyAliases()
|
2016-03-22 15:30:39 -04:00
|
|
|
if iface.AddressIPv6() != nil {
|
|
|
|
ipv6 = iface.AddressIPv6().IP
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
serviceID := ep.svcID
|
|
|
|
if serviceID == "" {
|
|
|
|
serviceID = ep.ID()
|
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
if isAdd {
|
2016-03-08 21:47:02 -05:00
|
|
|
// If anonymous endpoint has an alias use the first alias
|
|
|
|
// for ip->name mapping. Not having the reverse mapping
|
|
|
|
// breaks some apps
|
|
|
|
if ep.isAnonymous() {
|
|
|
|
if len(myAliases) > 0 {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.addSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-03-08 21:47:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.addSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-01-14 13:34:00 -05:00
|
|
|
}
|
2016-01-08 16:56:01 -05:00
|
|
|
for _, alias := range myAliases {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.addSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
|
2015-10-26 16:56:56 -04:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
} else {
|
2016-03-08 21:47:02 -05:00
|
|
|
if ep.isAnonymous() {
|
|
|
|
if len(myAliases) > 0 {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.deleteSvcRecords(ep.ID(), myAliases[0], serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-03-08 21:47:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.deleteSvcRecords(ep.ID(), epName, serviceID, iface.Address().IP, ipv6, true, "updateSvcRecord")
|
2016-01-14 13:34:00 -05:00
|
|
|
}
|
2016-01-08 16:56:01 -05:00
|
|
|
for _, alias := range myAliases {
|
2017-06-19 22:42:24 -04:00
|
|
|
n.deleteSvcRecords(ep.ID(), alias, serviceID, iface.Address().IP, ipv6, false, "updateSvcRecord")
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
}
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func addIPToName(ipMap common.SetMatrix, name, serviceID string, ip net.IP) {
|
2016-03-22 15:30:39 -04:00
|
|
|
reverseIP := netutils.ReverseIP(ip.String())
|
2017-06-12 04:01:45 -04:00
|
|
|
ipMap.Insert(reverseIP, ipInfo{
|
2017-06-19 22:42:24 -04:00
|
|
|
name: name,
|
|
|
|
serviceID: serviceID,
|
2017-06-12 04:01:45 -04:00
|
|
|
})
|
2016-03-22 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func delIPToName(ipMap common.SetMatrix, name, serviceID string, ip net.IP) {
|
|
|
|
reverseIP := netutils.ReverseIP(ip.String())
|
|
|
|
ipMap.Remove(reverseIP, ipInfo{
|
|
|
|
name: name,
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
2016-03-22 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func addNameToIP(svcMap common.SetMatrix, name, serviceID string, epIP net.IP) {
|
|
|
|
svcMap.Insert(name, svcMapEntry{
|
|
|
|
ip: epIP.String(),
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
|
|
|
}
|
2016-03-22 15:30:39 -04:00
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func delNameToIP(svcMap common.SetMatrix, name, serviceID string, epIP net.IP) {
|
|
|
|
svcMap.Remove(name, svcMapEntry{
|
|
|
|
ip: epIP.String(),
|
|
|
|
serviceID: serviceID,
|
|
|
|
})
|
2016-03-22 15:30:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func (n *network) addSvcRecords(eID, name, serviceID string, epIP, epIPv6 net.IP, ipMapUpdate bool, method string) {
|
2016-10-03 14:52:17 -04:00
|
|
|
// Do not add service names for ingress network as this is a
|
|
|
|
// routing only network
|
|
|
|
if n.ingress {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
logrus.Debugf("%s (%s).addSvcRecords(%s, %s, %s, %t) %s sid:%s", eID, n.ID()[0:7], name, epIP, epIPv6, ipMapUpdate, method, serviceID)
|
2017-02-24 12:58:36 -05:00
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2017-06-12 04:01:45 -04:00
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
2016-01-08 16:56:01 -05:00
|
|
|
if !ok {
|
|
|
|
sr = svcInfo{
|
2017-06-19 22:42:24 -04:00
|
|
|
svcMap: common.NewSetMatrix(),
|
|
|
|
svcIPv6Map: common.NewSetMatrix(),
|
2017-06-12 04:01:45 -04:00
|
|
|
ipMap: common.NewSetMatrix(),
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
2016-05-08 03:32:51 -04:00
|
|
|
c.svcRecords[n.ID()] = sr
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
if ipMapUpdate {
|
2017-06-19 22:42:24 -04:00
|
|
|
addIPToName(sr.ipMap, name, serviceID, epIP)
|
2016-03-22 15:30:39 -04:00
|
|
|
if epIPv6 != nil {
|
2017-06-19 22:42:24 -04:00
|
|
|
addIPToName(sr.ipMap, name, serviceID, epIPv6)
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
addNameToIP(sr.svcMap, name, serviceID, epIP)
|
2016-03-22 15:30:39 -04:00
|
|
|
if epIPv6 != nil {
|
2017-06-19 22:42:24 -04:00
|
|
|
addNameToIP(sr.svcIPv6Map, name, serviceID, epIPv6)
|
2016-01-08 16:56:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
func (n *network) deleteSvcRecords(eID, name, serviceID string, epIP net.IP, epIPv6 net.IP, ipMapUpdate bool, method string) {
|
2016-10-03 14:52:17 -04:00
|
|
|
// Do not delete service names from ingress network as this is a
|
|
|
|
// routing only network
|
|
|
|
if n.ingress {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
logrus.Debugf("%s (%s).deleteSvcRecords(%s, %s, %s, %t) %s sid:%s ", eID, n.ID()[0:7], name, epIP, epIPv6, ipMapUpdate, method, serviceID)
|
2017-02-24 12:58:36 -05:00
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2017-06-12 04:01:45 -04:00
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
2016-01-08 16:56:01 -05:00
|
|
|
if !ok {
|
2015-06-24 13:23:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
if ipMapUpdate {
|
2017-06-19 22:42:24 -04:00
|
|
|
delIPToName(sr.ipMap, name, serviceID, epIP)
|
2015-10-26 16:56:56 -04:00
|
|
|
|
2016-03-22 15:30:39 -04:00
|
|
|
if epIPv6 != nil {
|
2017-06-19 22:42:24 -04:00
|
|
|
delIPToName(sr.ipMap, name, serviceID, epIPv6)
|
2015-06-20 20:08:36 -04:00
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-06-20 20:08:36 -04:00
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
delNameToIP(sr.svcMap, name, serviceID, epIP)
|
2016-03-22 15:30:39 -04:00
|
|
|
|
|
|
|
if epIPv6 != nil {
|
2017-06-19 22:42:24 -04:00
|
|
|
delNameToIP(sr.svcIPv6Map, name, serviceID, epIPv6)
|
2015-06-20 20:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:56:56 -04:00
|
|
|
func (n *network) getSvcRecords(ep *endpoint) []etchosts.Record {
|
2015-06-20 20:08:36 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2016-06-18 22:16:51 -04:00
|
|
|
if ep == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-20 20:08:36 -04:00
|
|
|
var recs []etchosts.Record
|
2016-07-21 17:27:47 -04:00
|
|
|
|
2016-06-18 22:16:51 -04:00
|
|
|
epName := ep.Name()
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-07-21 17:27:47 -04:00
|
|
|
n.ctrlr.Lock()
|
2016-08-15 20:53:35 -04:00
|
|
|
defer n.ctrlr.Unlock()
|
2017-06-19 22:42:24 -04:00
|
|
|
sr, ok := n.ctrlr.svcRecords[n.id]
|
|
|
|
if !ok || sr.svcMap == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-21 17:27:47 -04:00
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
svcMapKeys := sr.svcMap.Keys()
|
|
|
|
// Loop on service names on this network
|
|
|
|
for _, k := range svcMapKeys {
|
|
|
|
if strings.Split(k, ".")[0] == epName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Get all the IPs associated to this service
|
|
|
|
mapEntryList, ok := sr.svcMap.Get(k)
|
|
|
|
if !ok {
|
|
|
|
// The key got deleted
|
2016-06-18 22:16:51 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-06-19 22:42:24 -04:00
|
|
|
if len(mapEntryList) == 0 {
|
|
|
|
logrus.Warnf("Found empty list of IP addresses for service %s on network %s (%s)", k, n.name, n.id)
|
2015-10-26 16:56:56 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-06-19 22:42:24 -04:00
|
|
|
|
2015-06-20 20:08:36 -04:00
|
|
|
recs = append(recs, etchosts.Record{
|
2017-06-19 22:42:24 -04:00
|
|
|
Hosts: k,
|
|
|
|
IP: mapEntryList[0].(svcMapEntry).ip,
|
2015-06-20 20:08:36 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return recs
|
|
|
|
}
|
2015-09-02 19:42:01 -04:00
|
|
|
|
|
|
|
func (n *network) getController() *controller {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.ctrlr
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
func (n *network) ipamAllocate() error {
|
2016-06-15 18:07:04 -04:00
|
|
|
if n.hasSpecialDriver() {
|
2015-10-04 23:02:02 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-10-04 23:02:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.addrSpace == "" {
|
|
|
|
if n.addrSpace, err = n.deriveAddressSpace(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = n.ipamAllocateVersion(4, ipam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.ipamReleaseVersion(4, ipam)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-22 15:30:39 -04:00
|
|
|
if !n.enableIPv6 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 03:32:51 -04:00
|
|
|
err = n.ipamAllocateVersion(6, ipam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) requestPoolHelper(ipam ipamapi.Ipam, addressSpace, preferredPool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
|
|
|
|
for {
|
|
|
|
poolID, pool, meta, err := ipam.RequestPool(addressSpace, preferredPool, subPool, options, v6)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the network belongs to global scope or the pool was
|
2016-08-11 02:31:01 -04:00
|
|
|
// explicitly chosen or it is invalid, do not perform the overlap check.
|
2016-05-08 03:32:51 -04:00
|
|
|
if n.Scope() == datastore.GlobalScope || preferredPool != "" || !types.IsIPNetValid(pool) {
|
|
|
|
return poolID, pool, meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for overlap and if none found, we have found the right pool.
|
|
|
|
if _, err := netutils.FindAvailableNetwork([]*net.IPNet{pool}); err == nil {
|
|
|
|
return poolID, pool, meta, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pool obtained in this iteration is
|
|
|
|
// overlapping. Hold onto the pool and don't release
|
|
|
|
// it yet, because we don't want ipam to give us back
|
|
|
|
// the same pool over again. But make sure we still do
|
|
|
|
// a deferred release when we have either obtained a
|
|
|
|
// non-overlapping pool or ran out of pre-defined
|
|
|
|
// pools.
|
|
|
|
defer func() {
|
|
|
|
if err := ipam.ReleasePool(poolID); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to release overlapping pool %s while returning from pool request helper for network %s", pool, n.Name())
|
2016-05-08 03:32:51 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// If this is a preferred pool request and the network
|
2016-08-11 02:31:01 -04:00
|
|
|
// is local scope and there is an overlap, we fail the
|
2016-05-08 03:32:51 -04:00
|
|
|
// network creation right here. The pool will be
|
|
|
|
// released in the defer.
|
|
|
|
if preferredPool != "" {
|
|
|
|
return "", nil, nil, fmt.Errorf("requested subnet %s overlaps in the host", preferredPool)
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ipamAllocateVersion(ipVer int, ipam ipamapi.Ipam) error {
|
|
|
|
var (
|
|
|
|
cfgList *[]*IpamConf
|
|
|
|
infoList *[]*IpamInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
cfgList = &n.ipamV4Config
|
|
|
|
infoList = &n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
cfgList = &n.ipamV6Config
|
|
|
|
infoList = &n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return types.InternalErrorf("incorrect ip version passed to ipam allocate: %d", ipVer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(*cfgList) == 0 {
|
2016-02-17 20:07:33 -05:00
|
|
|
*cfgList = []*IpamConf{{}}
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*infoList = make([]*IpamInfo, len(*cfgList))
|
|
|
|
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("Allocating IPv%d pools for network %s (%s)", ipVer, n.Name(), n.ID())
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
for i, cfg := range *cfgList {
|
|
|
|
if err = cfg.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d := &IpamInfo{}
|
|
|
|
(*infoList)[i] = d
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
d.AddressSpace = n.addrSpace
|
2016-05-08 03:32:51 -04:00
|
|
|
d.PoolID, d.Pool, d.Meta, err = n.requestPoolHelper(ipam, n.addrSpace, cfg.PreferredPool, cfg.SubPool, n.ipamOptions, ipVer == 6)
|
2015-10-04 23:02:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to release address pool %s after failure to create network %s (%s)", d.PoolID, n.Name(), n.ID())
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if gws, ok := d.Meta[netlabel.Gateway]; ok {
|
|
|
|
if d.Gateway, err = types.ParseCIDR(gws); err != nil {
|
|
|
|
return 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 {
|
2015-12-07 18:46:14 -05:00
|
|
|
var gatewayOpts = map[string]string{
|
|
|
|
ipamapi.RequestAddressType: netlabel.Gateway,
|
|
|
|
}
|
|
|
|
if d.Gateway, _, err = ipam.RequestAddress(d.PoolID, net.ParseIP(cfg.Gateway), gatewayOpts); err != nil {
|
2015-10-04 23:02:02 -04:00
|
|
|
return types.InternalErrorf("failed to allocate gateway (%v): %v", cfg.Gateway, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auxiliary addresses must be part of the master address pool
|
|
|
|
// If they fall into the container addressable pool, libnetwork will reserve them
|
|
|
|
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 {
|
|
|
|
return types.BadRequestErrorf("non parsable secondary ip address (%s:%s) passed for network %s", k, v, n.Name())
|
|
|
|
}
|
|
|
|
if !d.Pool.Contains(ip) {
|
|
|
|
return 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 types.InternalErrorf("failed to allocate secondary ip address (%s:%s): %v", k, v, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ipamRelease() {
|
2016-06-15 18:07:04 -04:00
|
|
|
if n.hasSpecialDriver() {
|
2015-10-04 23:02:02 -04:00
|
|
|
return
|
|
|
|
}
|
2016-05-08 03:32:51 -04:00
|
|
|
ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
|
2015-10-04 23:02:02 -04:00
|
|
|
if err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to retrieve ipam driver to release address pool(s) on delete of network %s (%s): %v", n.Name(), n.ID(), err)
|
2015-10-04 23:02:02 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
n.ipamReleaseVersion(4, ipam)
|
|
|
|
n.ipamReleaseVersion(6, ipam)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ipamReleaseVersion(ipVer int, ipam ipamapi.Ipam) {
|
2016-03-16 16:25:44 -04:00
|
|
|
var infoList *[]*IpamInfo
|
2015-10-04 23:02:02 -04:00
|
|
|
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
2016-03-16 16:25:44 -04:00
|
|
|
infoList = &n.ipamV4Info
|
2015-10-04 23:02:02 -04:00
|
|
|
case 6:
|
2016-03-16 16:25:44 -04:00
|
|
|
infoList = &n.ipamV6Info
|
2015-10-04 23:02:02 -04:00
|
|
|
default:
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("incorrect ip version passed to ipam release: %d", ipVer)
|
2015-10-04 23:02:02 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-22 15:30:39 -04:00
|
|
|
if len(*infoList) == 0 {
|
2015-10-04 23:02:02 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("releasing IPv%d pools from network %s (%s)", ipVer, n.Name(), n.ID())
|
2015-10-04 23:02:02 -04:00
|
|
|
|
2016-03-16 16:25:44 -04:00
|
|
|
for _, d := range *infoList {
|
2015-10-04 23:02:02 -04:00
|
|
|
if d.Gateway != nil {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, d.Gateway.IP); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to release gateway ip address %s on delete of network %s (%s): %v", d.Gateway.IP, n.Name(), n.ID(), err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.IPAMData.AuxAddresses != nil {
|
|
|
|
for k, nw := range d.IPAMData.AuxAddresses {
|
|
|
|
if d.Pool.Contains(nw.IP) {
|
|
|
|
if err := ipam.ReleaseAddress(d.PoolID, nw.IP); err != nil && err != ipamapi.ErrIPOutOfRange {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.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-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := ipam.ReleasePool(d.PoolID); err != nil {
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Warnf("Failed to release address pool %s on delete of network %s (%s): %v", d.PoolID, n.Name(), n.ID(), err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-16 16:25:44 -04:00
|
|
|
|
|
|
|
*infoList = nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getIPInfo(ipVer int) []*IpamInfo {
|
|
|
|
var info []*IpamInfo
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
info = n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
info = n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := make([]*IpamInfo, 0, len(info))
|
|
|
|
n.Lock()
|
|
|
|
for _, d := range info {
|
|
|
|
l = append(l, d)
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) getIPData(ipVer int) []driverapi.IPAMData {
|
|
|
|
var info []*IpamInfo
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
info = n.ipamV4Info
|
|
|
|
case 6:
|
|
|
|
info = n.ipamV6Info
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := make([]driverapi.IPAMData, 0, len(info))
|
|
|
|
n.Lock()
|
|
|
|
for _, d := range info {
|
|
|
|
l = append(l, d.IPAMData)
|
|
|
|
}
|
|
|
|
n.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) deriveAddressSpace() (string, error) {
|
2016-05-08 03:32:51 -04:00
|
|
|
local, global, err := n.getController().drvRegistry.IPAMDefaultAddressSpaces(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
return "", types.NotFoundErrorf("failed to get default address space: %v", err)
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
if n.DataScope() == datastore.GlobalScope {
|
2016-05-08 03:32:51 -04:00
|
|
|
return global, nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2016-05-08 03:32:51 -04:00
|
|
|
return local, nil
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Info() NetworkInfo {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2016-11-03 02:15:04 -04:00
|
|
|
func (n *network) Peers() []networkdb.PeerInfo {
|
|
|
|
if !n.Dynamic() {
|
|
|
|
return []networkdb.PeerInfo{}
|
|
|
|
}
|
|
|
|
|
2016-11-30 01:53:17 -05:00
|
|
|
agent := n.getController().getAgent()
|
|
|
|
if agent == nil {
|
|
|
|
return []networkdb.PeerInfo{}
|
2016-11-03 02:15:04 -04:00
|
|
|
}
|
|
|
|
|
2016-11-30 01:53:17 -05:00
|
|
|
return agent.networkDB.Peers(n.ID())
|
2016-11-03 02:15:04 -04:00
|
|
|
}
|
|
|
|
|
2015-10-14 23:11:53 -04:00
|
|
|
func (n *network) DriverOptions() map[string]string {
|
2015-10-04 23:02:02 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
if n.generic != nil {
|
|
|
|
if m, ok := n.generic[netlabel.GenericData]; ok {
|
|
|
|
return m.(map[string]string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Scope() string {
|
2016-01-19 16:49:13 -05:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
return n.scope
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
func (n *network) IpamConfig() (string, map[string]string, []*IpamConf, []*IpamConf) {
|
2015-10-04 23:02:02 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
v4L := make([]*IpamConf, len(n.ipamV4Config))
|
|
|
|
v6L := make([]*IpamConf, len(n.ipamV6Config))
|
|
|
|
|
|
|
|
for i, c := range n.ipamV4Config {
|
|
|
|
cc := &IpamConf{}
|
|
|
|
c.CopyTo(cc)
|
|
|
|
v4L[i] = cc
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, c := range n.ipamV6Config {
|
|
|
|
cc := &IpamConf{}
|
|
|
|
c.CopyTo(cc)
|
|
|
|
v6L[i] = cc
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:56:01 -05:00
|
|
|
return n.ipamType, n.ipamOptions, v4L, v6L
|
2015-10-04 23:02:02 -04:00
|
|
|
}
|
2015-12-18 14:03:57 -05:00
|
|
|
|
|
|
|
func (n *network) IpamInfo() ([]*IpamInfo, []*IpamInfo) {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
v4Info := make([]*IpamInfo, len(n.ipamV4Info))
|
|
|
|
v6Info := make([]*IpamInfo, len(n.ipamV6Info))
|
|
|
|
|
|
|
|
for i, info := range n.ipamV4Info {
|
|
|
|
ic := &IpamInfo{}
|
|
|
|
info.CopyTo(ic)
|
|
|
|
v4Info[i] = ic
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, info := range n.ipamV6Info {
|
|
|
|
ic := &IpamInfo{}
|
|
|
|
info.CopyTo(ic)
|
|
|
|
v6Info[i] = ic
|
|
|
|
}
|
|
|
|
|
|
|
|
return v4Info, v6Info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Internal() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.internal
|
|
|
|
}
|
2016-02-10 21:09:33 -05:00
|
|
|
|
2016-11-19 19:03:22 -05:00
|
|
|
func (n *network) Attachable() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.attachable
|
|
|
|
}
|
|
|
|
|
2017-03-14 00:39:56 -04:00
|
|
|
func (n *network) Ingress() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.ingress
|
|
|
|
}
|
|
|
|
|
2016-06-07 17:28:28 -04:00
|
|
|
func (n *network) Dynamic() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.dynamic
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:09:33 -05:00
|
|
|
func (n *network) IPv6Enabled() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.enableIPv6
|
|
|
|
}
|
2016-03-16 17:52:34 -04:00
|
|
|
|
2017-05-13 22:21:58 -04:00
|
|
|
func (n *network) ConfigFrom() string {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.configFrom
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ConfigOnly() bool {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
return n.configOnly
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:52:34 -04:00
|
|
|
func (n *network) Labels() map[string]string {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
var lbls = make(map[string]string, len(n.labels))
|
|
|
|
for k, v := range n.labels {
|
|
|
|
lbls[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return lbls
|
|
|
|
}
|
2016-05-08 03:32:51 -04:00
|
|
|
|
2017-03-11 20:22:15 -05:00
|
|
|
func (n *network) TableEventRegister(tableName string, objType driverapi.ObjectType) error {
|
|
|
|
if !driverapi.IsValidType(objType) {
|
|
|
|
return fmt.Errorf("invalid object type %v in registering table, %s", objType, tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
t := networkDBTable{
|
|
|
|
name: tableName,
|
|
|
|
objType: objType,
|
|
|
|
}
|
2016-05-08 03:32:51 -04:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
2017-03-11 20:22:15 -05:00
|
|
|
n.driverTables = append(n.driverTables, t)
|
2016-05-08 03:32:51 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-15 18:07:04 -04:00
|
|
|
|
|
|
|
// Special drivers are ones which do not need to perform any network plumbing
|
|
|
|
func (n *network) hasSpecialDriver() bool {
|
|
|
|
return n.Type() == "host" || n.Type() == "null"
|
|
|
|
}
|
2016-09-20 21:21:19 -04:00
|
|
|
|
|
|
|
func (n *network) ResolveName(req string, ipType int) ([]net.IP, bool) {
|
|
|
|
var ipv6Miss bool
|
|
|
|
|
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
2017-01-04 14:42:48 -05:00
|
|
|
defer c.Unlock()
|
2016-09-20 21:21:19 -04:00
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
req = strings.TrimSuffix(req, ".")
|
2017-06-19 22:42:24 -04:00
|
|
|
ipSet, ok := sr.svcMap.Get(req)
|
2016-09-20 21:21:19 -04:00
|
|
|
|
|
|
|
if ipType == types.IPv6 {
|
|
|
|
// If the name resolved to v4 address then its a valid name in
|
|
|
|
// the docker network domain. If the network is not v6 enabled
|
|
|
|
// set ipv6Miss to filter the DNS query from going to external
|
|
|
|
// resolvers.
|
|
|
|
if ok && n.enableIPv6 == false {
|
|
|
|
ipv6Miss = true
|
|
|
|
}
|
2017-06-19 22:42:24 -04:00
|
|
|
ipSet, ok = sr.svcIPv6Map.Get(req)
|
2016-09-20 21:21:19 -04:00
|
|
|
}
|
|
|
|
|
2017-06-19 22:42:24 -04:00
|
|
|
if ok && len(ipSet) > 0 {
|
|
|
|
// this map is to avoid IP duplicates, this can happen during a transition period where 2 services are using the same IP
|
|
|
|
noDup := make(map[string]bool)
|
|
|
|
var ipLocal []net.IP
|
|
|
|
for _, ip := range ipSet {
|
|
|
|
if _, dup := noDup[ip.(svcMapEntry).ip]; !dup {
|
|
|
|
noDup[ip.(svcMapEntry).ip] = true
|
|
|
|
ipLocal = append(ipLocal, net.ParseIP(ip.(svcMapEntry).ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ipLocal, ok
|
2016-09-20 21:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ipv6Miss
|
|
|
|
}
|
|
|
|
|
2017-01-04 14:42:48 -05:00
|
|
|
func (n *network) HandleQueryResp(name string, ip net.IP) {
|
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
2016-09-20 21:21:19 -04:00
|
|
|
|
2017-01-04 14:42:48 -05:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ipStr := netutils.ReverseIP(ip.String())
|
2017-06-12 04:01:45 -04:00
|
|
|
// If an object with extResolver == true is already in the set this call will fail
|
|
|
|
// but anyway it means that has already been inserted before
|
|
|
|
if ok, _ := sr.ipMap.Contains(ipStr, ipInfo{name: name}); ok {
|
|
|
|
sr.ipMap.Remove(ipStr, ipInfo{name: name})
|
|
|
|
sr.ipMap.Insert(ipStr, ipInfo{name: name, extResolver: true})
|
2017-01-04 14:42:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ResolveIP(ip string) string {
|
2016-09-20 21:21:19 -04:00
|
|
|
c := n.getController()
|
|
|
|
c.Lock()
|
2017-01-04 14:42:48 -05:00
|
|
|
defer c.Unlock()
|
2016-09-20 21:21:19 -04:00
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
nwName := n.Name()
|
|
|
|
|
2017-06-12 04:01:45 -04:00
|
|
|
elemSet, ok := sr.ipMap.Get(ip)
|
|
|
|
if !ok || len(elemSet) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
// NOTE it is possible to have more than one element in the Set, this will happen
|
2017-06-19 22:42:24 -04:00
|
|
|
// because of interleave of different events from different sources (local container create vs
|
2017-06-12 04:01:45 -04:00
|
|
|
// network db notifications)
|
|
|
|
// In such cases the resolution will be based on the first element of the set, and can vary
|
|
|
|
// during the system stabilitation
|
|
|
|
elem, ok := elemSet[0].(ipInfo)
|
|
|
|
if !ok {
|
|
|
|
setStr, b := sr.ipMap.String(ip)
|
|
|
|
logrus.Errorf("expected set of ipInfo type for key %s set:%t %s", ip, b, setStr)
|
|
|
|
return ""
|
|
|
|
}
|
2016-09-20 21:21:19 -04:00
|
|
|
|
2017-06-12 04:01:45 -04:00
|
|
|
if elem.extResolver {
|
2017-01-04 14:42:48 -05:00
|
|
|
return ""
|
2016-09-20 21:21:19 -04:00
|
|
|
}
|
|
|
|
|
2017-06-12 04:01:45 -04:00
|
|
|
return elem.name + "." + nwName
|
2016-09-20 21:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ResolveService(name string) ([]*net.SRV, []net.IP) {
|
|
|
|
c := n.getController()
|
|
|
|
|
|
|
|
srv := []*net.SRV{}
|
|
|
|
ip := []net.IP{}
|
|
|
|
|
2016-11-09 21:01:07 -05:00
|
|
|
logrus.Debugf("Service name To resolve: %v", name)
|
2016-09-20 21:21:19 -04:00
|
|
|
|
|
|
|
// There are DNS implementaions that allow SRV queries for names not in
|
|
|
|
// the format defined by RFC 2782. Hence specific validations checks are
|
|
|
|
// not done
|
|
|
|
parts := strings.Split(name, ".")
|
|
|
|
if len(parts) < 3 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
portName := parts[0]
|
|
|
|
proto := parts[1]
|
|
|
|
svcName := strings.Join(parts[2:], ".")
|
|
|
|
|
|
|
|
c.Lock()
|
2017-01-04 14:42:48 -05:00
|
|
|
defer c.Unlock()
|
2016-09-20 21:21:19 -04:00
|
|
|
sr, ok := c.svcRecords[n.ID()]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
svcs, ok := sr.service[svcName]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, svc := range svcs {
|
|
|
|
if svc.portName != portName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if svc.proto != proto {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, t := range svc.target {
|
|
|
|
srv = append(srv,
|
|
|
|
&net.SRV{
|
|
|
|
Target: t.name,
|
|
|
|
Port: t.port,
|
|
|
|
})
|
|
|
|
|
|
|
|
ip = append(ip, t.ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv, ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ExecFunc(f func()) error {
|
|
|
|
return types.NotImplementedErrorf("ExecFunc not supported by network")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) NdotsSet() bool {
|
|
|
|
return false
|
|
|
|
}
|
2017-05-13 22:21:58 -04:00
|
|
|
|
|
|
|
// config-only network is looked up by name
|
|
|
|
func (c *controller) getConfigNetwork(name string) (*network, error) {
|
|
|
|
var n Network
|
|
|
|
|
|
|
|
s := func(current Network) bool {
|
|
|
|
if current.Info().ConfigOnly() && current.Name() == name {
|
|
|
|
n = current
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
c.WalkNetworks(s)
|
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
return nil, types.NotFoundErrorf("configuration network %q not found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.(*network), nil
|
|
|
|
}
|