2015-04-29 21:25:01 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-09-09 19:20:54 -04:00
|
|
|
"container/heap"
|
2015-05-31 14:49:11 -04:00
|
|
|
"encoding/json"
|
2015-06-05 16:31:12 -04:00
|
|
|
"fmt"
|
2015-07-02 01:00:48 -04:00
|
|
|
"net"
|
2015-09-16 07:39:46 -04:00
|
|
|
"strings"
|
2015-05-09 00:50:03 -04:00
|
|
|
"sync"
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-06-10 16:27:23 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-31 14:49:11 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-10-03 19:11:50 -04:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-10-05 07:21:15 -04:00
|
|
|
"github.com/docker/libnetwork/options"
|
2015-04-29 21:25:01 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Endpoint represents a logical connection between a network and a sandbox.
|
|
|
|
type Endpoint interface {
|
|
|
|
// A system generated id for this endpoint.
|
|
|
|
ID() string
|
|
|
|
|
|
|
|
// Name returns the name of this endpoint.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Network returns the name of the network to which this endpoint is attached.
|
|
|
|
Network() string
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// Join joins the sandbox to the endpoint and populates into the sandbox
|
|
|
|
// the network resources allocated for the endpoint.
|
|
|
|
Join(sandbox Sandbox, options ...EndpointOption) error
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// Leave detaches the network resources populated in the sandbox.
|
|
|
|
Leave(sandbox Sandbox, options ...EndpointOption) error
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2015-05-14 02:23:45 -04:00
|
|
|
// Return certain operational data belonging to this endpoint
|
|
|
|
Info() EndpointInfo
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2015-05-21 04:08:10 -04:00
|
|
|
// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
|
2015-05-14 02:23:45 -04:00
|
|
|
DriverInfo() (map[string]interface{}, error)
|
2015-05-04 14:49:53 -04:00
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
// Delete and detaches this endpoint from the network.
|
|
|
|
Delete() error
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:11:13 -04:00
|
|
|
// EndpointOption is a option setter function type used to pass varios options to Network
|
|
|
|
// and Endpoint interfaces methods. The various setter functions of type EndpointOption are
|
|
|
|
// provided by libnetwork, they look like <Create|Join|Leave>Option[...](...)
|
|
|
|
type EndpointOption func(ep *endpoint)
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
type endpoint struct {
|
2015-05-09 00:50:03 -04:00
|
|
|
name string
|
2015-07-02 01:00:48 -04:00
|
|
|
id string
|
2015-05-09 00:50:03 -04:00
|
|
|
network *network
|
2015-09-09 19:06:35 -04:00
|
|
|
iface *endpointInterface
|
2015-05-14 02:23:45 -04:00
|
|
|
joinInfo *endpointJoinInfo
|
2015-07-02 01:00:48 -04:00
|
|
|
sandboxID string
|
2015-05-20 16:28:46 -04:00
|
|
|
exposedPorts []types.TransportPort
|
2015-10-20 15:12:16 -04:00
|
|
|
anonymous bool
|
2015-05-09 00:50:03 -04:00
|
|
|
generic map[string]interface{}
|
|
|
|
joinLeaveDone chan struct{}
|
2015-05-31 14:49:11 -04:00
|
|
|
dbIndex uint64
|
2015-06-18 18:13:38 -04:00
|
|
|
dbExists bool
|
2015-05-09 00:50:03 -04:00
|
|
|
sync.Mutex
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (ep *endpoint) MarshalJSON() ([]byte, error) {
|
2015-06-05 16:31:12 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
epMap := make(map[string]interface{})
|
|
|
|
epMap["name"] = ep.name
|
2015-07-02 01:00:48 -04:00
|
|
|
epMap["id"] = ep.id
|
2015-09-09 19:06:35 -04:00
|
|
|
epMap["ep_iface"] = ep.iface
|
2015-05-31 14:49:11 -04:00
|
|
|
epMap["exposed_ports"] = ep.exposedPorts
|
2015-10-03 19:11:50 -04:00
|
|
|
if ep.generic != nil {
|
|
|
|
epMap["generic"] = ep.generic
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
epMap["sandbox"] = ep.sandboxID
|
2015-10-20 15:12:16 -04:00
|
|
|
epMap["anonymous"] = ep.anonymous
|
2015-05-31 14:49:11 -04:00
|
|
|
return json.Marshal(epMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) UnmarshalJSON(b []byte) (err error) {
|
2015-06-05 16:31:12 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
var epMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &epMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ep.name = epMap["name"].(string)
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.id = epMap["id"].(string)
|
2015-05-31 14:49:11 -04:00
|
|
|
|
|
|
|
ib, _ := json.Marshal(epMap["ep_iface"])
|
2015-09-16 16:54:29 -04:00
|
|
|
json.Unmarshal(ib, &ep.iface)
|
2015-05-31 14:49:11 -04:00
|
|
|
|
|
|
|
tb, _ := json.Marshal(epMap["exposed_ports"])
|
|
|
|
var tPorts []types.TransportPort
|
|
|
|
json.Unmarshal(tb, &tPorts)
|
|
|
|
ep.exposedPorts = tPorts
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
cb, _ := json.Marshal(epMap["sandbox"])
|
|
|
|
json.Unmarshal(cb, &ep.sandboxID)
|
2015-06-01 12:43:24 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
if v, ok := epMap["generic"]; ok {
|
|
|
|
ep.generic = v.(map[string]interface{})
|
2015-10-22 11:41:52 -04:00
|
|
|
|
|
|
|
if opt, ok := ep.generic[netlabel.PortMap]; ok {
|
|
|
|
pblist := []types.PortBinding{}
|
|
|
|
|
|
|
|
for i := 0; i < len(opt.([]interface{})); i++ {
|
|
|
|
pb := types.PortBinding{}
|
|
|
|
tmp := opt.([]interface{})[i].(map[string]interface{})
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(bytes, &pb)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
pblist = append(pblist, pb)
|
|
|
|
}
|
|
|
|
ep.generic[netlabel.PortMap] = pblist
|
|
|
|
}
|
|
|
|
|
|
|
|
if opt, ok := ep.generic[netlabel.ExposedPorts]; ok {
|
|
|
|
tplist := []types.TransportPort{}
|
|
|
|
|
|
|
|
for i := 0; i < len(opt.([]interface{})); i++ {
|
|
|
|
tp := types.TransportPort{}
|
|
|
|
tmp := opt.([]interface{})[i].(map[string]interface{})
|
|
|
|
|
|
|
|
bytes, err := json.Marshal(tmp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(bytes, &tp)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tplist = append(tplist, tp)
|
|
|
|
}
|
|
|
|
ep.generic[netlabel.ExposedPorts] = tplist
|
|
|
|
|
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
2015-10-20 15:12:16 -04:00
|
|
|
|
|
|
|
if v, ok := epMap["anonymous"]; ok {
|
|
|
|
ep.anonymous = v.(bool)
|
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (ep *endpoint) New() datastore.KVObject {
|
|
|
|
return &endpoint{network: ep.getNetwork()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) CopyTo(o datastore.KVObject) error {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
dstEp := o.(*endpoint)
|
|
|
|
dstEp.name = ep.name
|
|
|
|
dstEp.id = ep.id
|
|
|
|
dstEp.sandboxID = ep.sandboxID
|
|
|
|
dstEp.dbIndex = ep.dbIndex
|
|
|
|
dstEp.dbExists = ep.dbExists
|
2015-10-20 15:12:16 -04:00
|
|
|
dstEp.anonymous = ep.anonymous
|
2015-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
if ep.iface != nil {
|
|
|
|
dstEp.iface = &endpointInterface{}
|
|
|
|
ep.iface.CopyTo(dstEp.iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
dstEp.exposedPorts = make([]types.TransportPort, len(ep.exposedPorts))
|
|
|
|
copy(dstEp.exposedPorts, ep.exposedPorts)
|
|
|
|
|
|
|
|
dstEp.generic = options.Generic{}
|
|
|
|
for k, v := range ep.generic {
|
|
|
|
dstEp.generic[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
func (ep *endpoint) ID() string {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
return ep.id
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Name() string {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
return ep.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Network() string {
|
2015-10-05 07:21:15 -04:00
|
|
|
if ep.network == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.network.name
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 15:12:16 -04:00
|
|
|
func (ep *endpoint) isAnonymous() bool {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
return ep.anonymous
|
|
|
|
}
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
// endpoint Key structure : endpoint/network-id/endpoint-id
|
2015-05-31 14:49:11 -04:00
|
|
|
func (ep *endpoint) Key() []string {
|
2015-10-05 07:21:15 -04:00
|
|
|
if ep.network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{datastore.EndpointKeyPrefix, ep.network.id, ep.id}
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
func (ep *endpoint) KeyPrefix() []string {
|
2015-10-05 07:21:15 -04:00
|
|
|
if ep.network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{datastore.EndpointKeyPrefix, ep.network.id}
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:39:46 -04:00
|
|
|
func (ep *endpoint) networkIDFromKey(key string) (string, error) {
|
|
|
|
// endpoint Key structure : docker/libnetwork/endpoint/${network-id}/${endpoint-id}
|
|
|
|
// it's an invalid key if the key doesn't have all the 5 key elements above
|
|
|
|
keyElements := strings.Split(key, "/")
|
|
|
|
if !strings.HasPrefix(key, datastore.Key(datastore.EndpointKeyPrefix)) || len(keyElements) < 5 {
|
2015-07-02 01:00:48 -04:00
|
|
|
return "", fmt.Errorf("invalid endpoint key : %v", key)
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
// network-id is placed at index=3. pls refer to endpoint.Key() method
|
|
|
|
return strings.Split(key, "/")[3], nil
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (ep *endpoint) Value() []byte {
|
|
|
|
b, err := json.Marshal(ep)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-06-18 18:13:38 -04:00
|
|
|
func (ep *endpoint) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, ep)
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (ep *endpoint) Index() uint64 {
|
2015-06-05 16:31:12 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
2015-05-31 14:49:11 -04:00
|
|
|
return ep.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetIndex(index uint64) {
|
2015-06-05 16:31:12 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
2015-05-31 14:49:11 -04:00
|
|
|
ep.dbIndex = index
|
2015-06-18 18:13:38 -04:00
|
|
|
ep.dbExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Exists() bool {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
return ep.dbExists
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 10:09:39 -04:00
|
|
|
func (ep *endpoint) Skip() bool {
|
2015-10-07 23:01:38 -04:00
|
|
|
return ep.getNetwork().Skip()
|
2015-09-22 10:09:39 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
func (ep *endpoint) processOptions(options ...EndpointOption) {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (ep *endpoint) getNetwork() *network {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
return ep.network
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) getNetworkFromStore() (*network, error) {
|
|
|
|
if ep.network == nil {
|
|
|
|
return nil, fmt.Errorf("invalid network object in endpoint %s", ep.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.network.ctrlr.getNetworkFromStore(ep.network.id)
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (ep *endpoint) Join(sbox Sandbox, options ...EndpointOption) error {
|
2015-09-18 20:33:55 -04:00
|
|
|
if sbox == nil {
|
|
|
|
return types.BadRequestErrorf("endpoint cannot be joined by nil container")
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
sb.joinLeaveStart()
|
|
|
|
defer sb.joinLeaveEnd()
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
return ep.sbJoin(sbox, options...)
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
|
2015-04-30 01:58:12 -04:00
|
|
|
var err error
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
network, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get network from store during join: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err = network.getEndpointFromStore(ep.ID())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get endpoint from store during join: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
2015-07-02 01:00:48 -04:00
|
|
|
if ep.sandboxID != "" {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Unlock()
|
2015-10-09 04:45:24 -04:00
|
|
|
return types.ForbiddenErrorf("another container is attached to the same network endpoint")
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
ep.Unlock()
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
ep.Lock()
|
|
|
|
ep.network = network
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.sandboxID = sbox.ID()
|
2015-05-14 02:23:45 -04:00
|
|
|
ep.joinInfo = &endpointJoinInfo{}
|
2015-05-09 00:50:03 -04:00
|
|
|
epid := ep.id
|
|
|
|
ep.Unlock()
|
2015-04-30 01:58:12 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-06-01 12:43:24 -04:00
|
|
|
ep.Lock()
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.sandboxID = ""
|
2015-06-01 12:43:24 -04:00
|
|
|
ep.Unlock()
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
network.Lock()
|
|
|
|
nid := network.id
|
|
|
|
network.Unlock()
|
2015-05-03 16:29:43 -04:00
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.processOptions(options...)
|
2015-05-04 01:18:49 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
driver, err := network.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to join endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
err = driver.Join(nid, epid, sbox.Key(), ep, sbox.Labels())
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
2015-05-24 05:41:03 -04:00
|
|
|
return err
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
2015-06-19 21:41:31 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-08-05 20:13:46 -04:00
|
|
|
// Do not alter global err variable, it's needed by the previous defer
|
|
|
|
if err := driver.Leave(nid, epid); err != nil {
|
2015-06-19 21:41:31 -04:00
|
|
|
log.Warnf("driver leave failed while rolling back join: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
address := ""
|
|
|
|
if ip := ep.getFirstInterfaceAddress(); ip != nil {
|
|
|
|
address = ip.String()
|
|
|
|
}
|
2015-10-23 13:09:00 -04:00
|
|
|
if err = sb.updateHostsFile(address, network.getSvcRecords(ep)); err != nil {
|
2015-05-24 05:41:03 -04:00
|
|
|
return err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
// Watch for service records
|
|
|
|
network.getController().watchSvcRecord(ep)
|
|
|
|
|
|
|
|
if err = sb.updateDNS(network.enableIPv6); err != nil {
|
2015-05-24 05:41:03 -04:00
|
|
|
return err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-05-03 16:29:43 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if err = network.getController().updateToStore(ep); err != nil {
|
|
|
|
return err
|
2015-05-04 01:18:49 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
sb.Lock()
|
|
|
|
heap.Push(&sb.endpoints, ep)
|
|
|
|
sb.Unlock()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
for i, e := range sb.getConnectedEndpoints() {
|
|
|
|
if e == ep {
|
|
|
|
sb.Lock()
|
|
|
|
heap.Remove(&sb.endpoints, i)
|
|
|
|
sb.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err = sb.populateNetworkResources(ep); err != nil {
|
2015-06-05 16:31:12 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-06 21:34:50 -04:00
|
|
|
|
|
|
|
if sb.needDefaultGW() {
|
|
|
|
return sb.setupDefaultGW(ep)
|
|
|
|
}
|
|
|
|
return sb.clearDefaultGW()
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-10-22 23:18:25 -04:00
|
|
|
func (ep *endpoint) rename(name string) error {
|
|
|
|
var err error
|
|
|
|
n := ep.getNetwork()
|
|
|
|
if n == nil {
|
|
|
|
return fmt.Errorf("network not connected for ep %q", ep.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
n.getController().Lock()
|
|
|
|
netWatch, ok := n.getController().nmap[n.ID()]
|
|
|
|
n.getController().Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("watch null for network %q", n.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), false)
|
|
|
|
|
|
|
|
oldName := ep.name
|
|
|
|
ep.name = name
|
|
|
|
|
|
|
|
n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), true)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), false)
|
|
|
|
ep.name = oldName
|
|
|
|
n.updateSvcRecord(ep, n.getController().getLocalEps(netWatch), true)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Update the store with the updated name
|
|
|
|
if err = n.getController().updateToStore(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// After the name change do a dummy endpoint count update to
|
|
|
|
// trigger the service record update in the peer nodes
|
|
|
|
|
|
|
|
// Ignore the error because updateStore fail for EpCnt is a
|
|
|
|
// benign error. Besides there is no meaningful recovery that
|
|
|
|
// we can do. When the cluster recovers subsequent EpCnt update
|
|
|
|
// will force the peers to get the correct EP name.
|
2015-10-27 13:14:54 -04:00
|
|
|
n.getEpCnt().updateStore()
|
2015-10-22 23:18:25 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-25 02:24:23 -04:00
|
|
|
func (ep *endpoint) hasInterface(iName string) bool {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 19:06:35 -04:00
|
|
|
return ep.iface != nil && ep.iface.srcName == iName
|
2015-05-25 02:24:23 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (ep *endpoint) Leave(sbox Sandbox, options ...EndpointOption) error {
|
|
|
|
if sbox == nil || sbox.ID() == "" || sbox.Key() == "" {
|
|
|
|
return types.BadRequestErrorf("invalid Sandbox passed to enpoint leave: %v", sbox)
|
|
|
|
}
|
2015-04-30 17:52:46 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-09-18 20:33:55 -04:00
|
|
|
sb.joinLeaveStart()
|
|
|
|
defer sb.joinLeaveEnd()
|
|
|
|
|
|
|
|
return ep.sbLeave(sbox, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) sbLeave(sbox Sandbox, options ...EndpointOption) error {
|
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
n, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get network from store during leave: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err = n.getEndpointFromStore(ep.ID())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get endpoint from store during leave: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
ep.Lock()
|
|
|
|
sid := ep.sandboxID
|
|
|
|
ep.Unlock()
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if sid == "" {
|
|
|
|
return types.ForbiddenErrorf("cannot leave endpoint with no attached sandbox")
|
2015-05-09 00:50:03 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
if sid != sbox.ID() {
|
|
|
|
return types.ForbiddenErrorf("unexpected sandbox ID in leave request. Expected %s. Got %s", ep.sandboxID, sbox.ID())
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.processOptions(options...)
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
d, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to leave endpoint: %v", err)
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
|
2015-10-16 19:11:55 -04:00
|
|
|
ep.Lock()
|
|
|
|
ep.sandboxID = ""
|
|
|
|
ep.network = n
|
|
|
|
ep.Unlock()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
if err := d.Leave(n.id, ep.id); err != nil {
|
2015-10-16 19:11:55 -04:00
|
|
|
if _, ok := err.(types.MaskableError); !ok {
|
|
|
|
log.Warnf("driver error disconnecting container %s : %v", ep.name, err)
|
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-09-06 21:34:50 -04:00
|
|
|
if err := sb.clearNetworkResources(ep); err != nil {
|
2015-10-16 19:11:55 -04:00
|
|
|
log.Warnf("Could not cleanup network resources on container %s disconnect: %v", ep.name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the store about the sandbox detach only after we
|
|
|
|
// have completed sb.clearNetworkresources above to avoid
|
|
|
|
// spurious logs when cleaning up the sandbox when the daemon
|
|
|
|
// ungracefully exits and restarts before completing sandbox
|
|
|
|
// detach but after store has been updated.
|
|
|
|
if err := n.getController().updateToStore(ep); err != nil {
|
2015-09-06 21:34:50 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-24 16:31:01 -04:00
|
|
|
sb.deleteHostsEntries(n.getSvcRecords(ep))
|
|
|
|
|
2015-09-06 21:34:50 -04:00
|
|
|
if sb.needDefaultGW() {
|
|
|
|
ep := sb.getEPwithoutGateway()
|
|
|
|
if ep == nil {
|
|
|
|
return fmt.Errorf("endpoint without GW expected, but not found")
|
|
|
|
}
|
|
|
|
return sb.setupDefaultGW(ep)
|
|
|
|
}
|
|
|
|
return sb.clearDefaultGW()
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Delete() error {
|
2015-06-05 16:31:12 -04:00
|
|
|
var err error
|
2015-10-05 07:21:15 -04:00
|
|
|
n, err := ep.getNetworkFromStore()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get network during Delete: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, err = n.getEndpointFromStore(ep.ID())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get endpoint from store during Delete: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
2015-06-05 16:31:12 -04:00
|
|
|
epid := ep.id
|
|
|
|
name := ep.name
|
2015-11-18 00:00:46 -05:00
|
|
|
sb, _ := n.getController().SandboxByID(ep.sandboxID)
|
|
|
|
if sb != nil {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Unlock()
|
2015-07-02 01:00:48 -04:00
|
|
|
return &ActiveContainerError{name: name, id: epid}
|
2015-06-01 12:43:24 -04:00
|
|
|
}
|
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-23 13:09:00 -04:00
|
|
|
if err = n.getController().deleteFromStore(ep); err != nil {
|
2015-10-05 07:21:15 -04:00
|
|
|
return err
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-23 13:09:00 -04:00
|
|
|
ep.dbExists = false
|
|
|
|
if e := n.getController().updateToStore(ep); e != nil {
|
|
|
|
log.Warnf("failed to recreate endpoint in store %s : %v", name, e)
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-23 13:09:00 -04:00
|
|
|
if err = n.getEpCnt().DecEndpointCnt(); err != nil {
|
2015-06-01 12:43:24 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-23 13:09:00 -04:00
|
|
|
if e := n.getEpCnt().IncEndpointCnt(); e != nil {
|
|
|
|
log.Warnf("failed to update network %s : %v", n.name, e)
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-06-01 12:43:24 -04:00
|
|
|
|
2015-10-23 13:09:00 -04:00
|
|
|
// unwatch for service records
|
|
|
|
n.getController().unWatchSvcRecord(ep)
|
|
|
|
|
2015-06-05 16:31:12 -04:00
|
|
|
if err = ep.deleteEndpoint(); err != nil {
|
2015-06-01 12:43:24 -04:00
|
|
|
return err
|
2015-05-06 16:02:40 -04:00
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
ep.releaseAddress()
|
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2015-06-01 12:43:24 -04:00
|
|
|
func (ep *endpoint) deleteEndpoint() error {
|
|
|
|
ep.Lock()
|
2015-04-29 21:25:01 -04:00
|
|
|
n := ep.network
|
2015-06-01 12:43:24 -04:00
|
|
|
name := ep.name
|
|
|
|
epid := ep.id
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
driver, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete endpoint: %v", err)
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if err := driver.DeleteEndpoint(n.id, epid); err != nil {
|
2015-06-10 16:27:23 -04:00
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
|
|
|
return err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-10-19 18:56:25 -04:00
|
|
|
|
|
|
|
if _, ok := err.(types.MaskableError); !ok {
|
|
|
|
log.Warnf("driver error deleting endpoint %s : %v", name, err)
|
|
|
|
}
|
2015-06-10 16:27:23 -04:00
|
|
|
}
|
2015-06-19 02:40:17 -04:00
|
|
|
|
2015-06-10 16:27:23 -04:00
|
|
|
return nil
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (ep *endpoint) getSandbox() (*sandbox, bool) {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
2015-07-02 01:00:48 -04:00
|
|
|
c := ep.network.getController()
|
|
|
|
sid := ep.sandboxID
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
c.Lock()
|
|
|
|
ps, ok := c.sandboxes[sid]
|
|
|
|
c.Unlock()
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
return ps, ok
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
func (ep *endpoint) getFirstInterfaceAddress() net.IP {
|
2015-05-09 00:50:03 -04:00
|
|
|
ep.Lock()
|
2015-07-02 01:00:48 -04:00
|
|
|
defer ep.Unlock()
|
2015-05-09 00:50:03 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
if ep.iface.addr != nil {
|
2015-09-09 19:06:35 -04:00
|
|
|
return ep.iface.addr.IP
|
2015-05-03 16:29:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-04 01:18:49 -04:00
|
|
|
// EndpointOptionGeneric function returns an option setter for a Generic option defined
|
|
|
|
// in a Dictionary of Key-Value pair
|
|
|
|
func EndpointOptionGeneric(generic map[string]interface{}) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
for k, v := range generic {
|
|
|
|
ep.generic[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 16:46:12 -04:00
|
|
|
// CreateOptionExposedPorts function returns an option setter for the container exposed
|
|
|
|
// ports option to be passed to network.CreateEndpoint() method.
|
2015-05-20 16:28:46 -04:00
|
|
|
func CreateOptionExposedPorts(exposedPorts []types.TransportPort) EndpointOption {
|
2015-05-05 16:46:12 -04:00
|
|
|
return func(ep *endpoint) {
|
|
|
|
// Defensive copy
|
2015-05-20 16:28:46 -04:00
|
|
|
eps := make([]types.TransportPort, len(exposedPorts))
|
2015-05-05 16:46:12 -04:00
|
|
|
copy(eps, exposedPorts)
|
|
|
|
// Store endpoint label and in generic because driver needs it
|
|
|
|
ep.exposedPorts = eps
|
2015-05-06 00:19:57 -04:00
|
|
|
ep.generic[netlabel.ExposedPorts] = eps
|
2015-05-05 16:46:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOptionPortMapping function returns an option setter for the mapping
|
2015-05-01 20:11:13 -04:00
|
|
|
// ports option to be passed to network.CreateEndpoint() method.
|
2015-05-20 16:28:46 -04:00
|
|
|
func CreateOptionPortMapping(portBindings []types.PortBinding) EndpointOption {
|
2015-05-01 20:01:21 -04:00
|
|
|
return func(ep *endpoint) {
|
2015-05-05 02:45:07 -04:00
|
|
|
// Store a copy of the bindings as generic data to pass to the driver
|
2015-05-20 16:28:46 -04:00
|
|
|
pbs := make([]types.PortBinding, len(portBindings))
|
2015-05-05 16:46:12 -04:00
|
|
|
copy(pbs, portBindings)
|
2015-05-06 00:19:57 -04:00
|
|
|
ep.generic[netlabel.PortMap] = pbs
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 15:12:16 -04:00
|
|
|
// CreateOptionAnonymous function returns an option setter for setting
|
|
|
|
// this endpoint as anonymous
|
|
|
|
func CreateOptionAnonymous() EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.anonymous = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// JoinOptionPriority function returns an option setter for priority option to
|
|
|
|
// be passed to the endpoint.Join() method.
|
|
|
|
func JoinOptionPriority(ep Endpoint, prio int) EndpointOption {
|
2015-04-30 17:52:46 -04:00
|
|
|
return func(ep *endpoint) {
|
2015-07-02 01:00:48 -04:00
|
|
|
// ep lock already acquired
|
|
|
|
c := ep.network.getController()
|
|
|
|
c.Lock()
|
|
|
|
sb, ok := c.sandboxes[ep.sandboxID]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("Could not set endpoint priority value during Join to endpoint %s: No sandbox id present in endpoint", ep.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sb.epPriority[ep.id] = prio
|
2015-04-30 17:52:46 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
func (ep *endpoint) DataScope() string {
|
|
|
|
return ep.getNetwork().DataScope()
|
2015-09-16 07:42:35 -04:00
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
|
2015-11-06 02:50:10 -05:00
|
|
|
func (ep *endpoint) assignAddress(assignIPv4, assignIPv6 bool) error {
|
2015-10-03 19:11:50 -04:00
|
|
|
var (
|
|
|
|
ipam ipamapi.Ipam
|
|
|
|
err error
|
|
|
|
)
|
2015-10-05 17:53:25 -04:00
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
n := ep.getNetwork()
|
2015-10-04 22:08:31 -04:00
|
|
|
if n.Type() == "host" || n.Type() == "null" {
|
2015-10-03 19:11:50 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
|
|
|
|
log.Debugf("Assigning addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
ipam, err = n.getController().getIpamDriver(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-06 02:50:10 -05:00
|
|
|
|
|
|
|
if assignIPv4 {
|
|
|
|
if err = ep.assignAddressVersion(4, ipam); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
}
|
2015-11-06 02:50:10 -05:00
|
|
|
|
|
|
|
if assignIPv6 {
|
|
|
|
err = ep.assignAddressVersion(6, ipam)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2015-10-04 00:25:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) assignAddressVersion(ipVer int, ipam ipamapi.Ipam) error {
|
|
|
|
var (
|
|
|
|
poolID *string
|
|
|
|
address **net.IPNet
|
|
|
|
)
|
|
|
|
|
|
|
|
n := ep.getNetwork()
|
|
|
|
switch ipVer {
|
|
|
|
case 4:
|
|
|
|
poolID = &ep.iface.v4PoolID
|
|
|
|
address = &ep.iface.addr
|
|
|
|
case 6:
|
|
|
|
poolID = &ep.iface.v6PoolID
|
|
|
|
address = &ep.iface.addrv6
|
|
|
|
default:
|
|
|
|
return types.InternalErrorf("incorrect ip version number passed: %d", ipVer)
|
|
|
|
}
|
|
|
|
|
|
|
|
ipInfo := n.getIPInfo(ipVer)
|
|
|
|
|
|
|
|
// ipv6 address is not mandatory
|
|
|
|
if len(ipInfo) == 0 && ipVer == 6 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range ipInfo {
|
2015-11-06 02:50:10 -05:00
|
|
|
var prefIP net.IP
|
|
|
|
if *address != nil {
|
|
|
|
prefIP = (*address).IP
|
|
|
|
}
|
|
|
|
addr, _, err := ipam.RequestAddress(d.PoolID, prefIP, nil)
|
2015-10-03 19:11:50 -04:00
|
|
|
if err == nil {
|
|
|
|
ep.Lock()
|
2015-10-04 00:25:57 -04:00
|
|
|
*address = addr
|
|
|
|
*poolID = d.PoolID
|
2015-10-03 19:11:50 -04:00
|
|
|
ep.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != ipamapi.ErrNoAvailableIPs {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
return fmt.Errorf("no available IPv%d addresses on this network's address pools: %s (%s)", ipVer, n.Name(), n.ID())
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) releaseAddress() {
|
|
|
|
n := ep.getNetwork()
|
2015-10-04 22:08:31 -04:00
|
|
|
if n.Type() == "host" || n.Type() == "null" {
|
2015-10-03 19:11:50 -04:00
|
|
|
return
|
|
|
|
}
|
2015-10-05 17:53:25 -04:00
|
|
|
|
|
|
|
log.Debugf("Releasing addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
ipam, err := n.getController().getIpamDriver(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Failed to retrieve ipam driver to release interface address on delete of endpoint %s (%s): %v", ep.Name(), ep.ID(), err)
|
|
|
|
return
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
if err := ipam.ReleaseAddress(ep.iface.v4PoolID, ep.iface.addr.IP); err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
log.Warnf("Failed to release ip address %s on delete of endpoint %s (%s): %v", ep.iface.addr.IP, ep.Name(), ep.ID(), err)
|
|
|
|
}
|
2015-10-04 00:25:57 -04:00
|
|
|
if ep.iface.addrv6 != nil && ep.iface.addrv6.IP.IsGlobalUnicast() {
|
|
|
|
if err := ipam.ReleaseAddress(ep.iface.v6PoolID, ep.iface.addrv6.IP); err != nil {
|
|
|
|
log.Warnf("Failed to release ip address %s on delete of endpoint %s (%s): %v", ep.iface.addrv6.IP, ep.Name(), ep.ID(), err)
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
2015-10-19 16:20:23 -04:00
|
|
|
|
|
|
|
func (c *controller) cleanupLocalEndpoints() {
|
|
|
|
nl, err := c.getNetworksForScope(datastore.LocalScope)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Could not get list of networks during endpoint cleanup: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range nl {
|
|
|
|
epl, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range epl {
|
|
|
|
if err := ep.Delete(); err != nil {
|
|
|
|
log.Warnf("Could not delete local endpoint %s during endpoint cleanup: %v", ep.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|