2015-04-30 01:25:01 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-09-09 23:20:54 +00:00
|
|
|
"container/heap"
|
2015-05-31 18:49:11 +00:00
|
|
|
"encoding/json"
|
2015-06-05 20:31:12 +00:00
|
|
|
"fmt"
|
2015-07-02 05:00:48 +00:00
|
|
|
"net"
|
2015-09-16 11:39:46 +00:00
|
|
|
"strings"
|
2015-05-09 04:50:03 +00:00
|
|
|
"sync"
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-06-10 20:27:23 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-31 18:49:11 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-10-03 23:11:50 +00:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-10-05 11:21:15 +00:00
|
|
|
"github.com/docker/libnetwork/options"
|
2015-04-30 01:25:01 +00: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 05:00:48 +00: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-30 01:25:01 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
// Leave detaches the network resources populated in the sandbox.
|
|
|
|
Leave(sandbox Sandbox, options ...EndpointOption) error
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
// Return certain operational data belonging to this endpoint
|
|
|
|
Info() EndpointInfo
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-05-21 08:08:10 +00:00
|
|
|
// DriverInfo returns a collection of driver operational data related to this endpoint retrieved from the driver
|
2015-05-14 06:23:45 +00:00
|
|
|
DriverInfo() (map[string]interface{}, error)
|
2015-05-04 18:49:53 +00:00
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
// Delete and detaches this endpoint from the network.
|
|
|
|
Delete() error
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:11:13 +00: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-30 01:25:01 +00:00
|
|
|
type endpoint struct {
|
2015-05-09 04:50:03 +00:00
|
|
|
name string
|
2015-07-02 05:00:48 +00:00
|
|
|
id string
|
2015-05-09 04:50:03 +00:00
|
|
|
network *network
|
2015-09-09 23:06:35 +00:00
|
|
|
iface *endpointInterface
|
2015-05-14 06:23:45 +00:00
|
|
|
joinInfo *endpointJoinInfo
|
2015-07-02 05:00:48 +00:00
|
|
|
sandboxID string
|
2015-05-20 20:28:46 +00:00
|
|
|
exposedPorts []types.TransportPort
|
2015-05-09 04:50:03 +00:00
|
|
|
generic map[string]interface{}
|
|
|
|
joinLeaveDone chan struct{}
|
2015-05-31 18:49:11 +00:00
|
|
|
dbIndex uint64
|
2015-06-18 22:13:38 +00:00
|
|
|
dbExists bool
|
2015-05-09 04:50:03 +00:00
|
|
|
sync.Mutex
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (ep *endpoint) MarshalJSON() ([]byte, error) {
|
2015-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
epMap := make(map[string]interface{})
|
|
|
|
epMap["name"] = ep.name
|
2015-07-02 05:00:48 +00:00
|
|
|
epMap["id"] = ep.id
|
2015-09-09 23:06:35 +00:00
|
|
|
epMap["ep_iface"] = ep.iface
|
2015-05-31 18:49:11 +00:00
|
|
|
epMap["exposed_ports"] = ep.exposedPorts
|
2015-10-03 23:11:50 +00:00
|
|
|
if ep.generic != nil {
|
|
|
|
epMap["generic"] = ep.generic
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
epMap["sandbox"] = ep.sandboxID
|
2015-05-31 18:49:11 +00:00
|
|
|
return json.Marshal(epMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) UnmarshalJSON(b []byte) (err error) {
|
2015-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
var epMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &epMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ep.name = epMap["name"].(string)
|
2015-07-02 05:00:48 +00:00
|
|
|
ep.id = epMap["id"].(string)
|
2015-05-31 18:49:11 +00:00
|
|
|
|
|
|
|
ib, _ := json.Marshal(epMap["ep_iface"])
|
2015-09-16 20:54:29 +00:00
|
|
|
json.Unmarshal(ib, &ep.iface)
|
2015-05-31 18:49:11 +00:00
|
|
|
|
|
|
|
tb, _ := json.Marshal(epMap["exposed_ports"])
|
|
|
|
var tPorts []types.TransportPort
|
|
|
|
json.Unmarshal(tb, &tPorts)
|
|
|
|
ep.exposedPorts = tPorts
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
cb, _ := json.Marshal(epMap["sandbox"])
|
|
|
|
json.Unmarshal(cb, &ep.sandboxID)
|
2015-06-01 16:43:24 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
if v, ok := epMap["generic"]; ok {
|
|
|
|
ep.generic = v.(map[string]interface{})
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00: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
|
|
|
|
|
|
|
|
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-30 01:25:01 +00:00
|
|
|
func (ep *endpoint) ID() string {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
return ep.id
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Name() string {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
return ep.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Network() string {
|
2015-10-05 11:21:15 +00:00
|
|
|
if ep.network == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return ep.network.name
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
// endpoint Key structure : endpoint/network-id/endpoint-id
|
2015-05-31 18:49:11 +00:00
|
|
|
func (ep *endpoint) Key() []string {
|
2015-10-05 11:21:15 +00:00
|
|
|
if ep.network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{datastore.EndpointKeyPrefix, ep.network.id, ep.id}
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
func (ep *endpoint) KeyPrefix() []string {
|
2015-10-05 11:21:15 +00:00
|
|
|
if ep.network == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{datastore.EndpointKeyPrefix, ep.network.id}
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 11:39:46 +00: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 05:00:48 +00:00
|
|
|
return "", fmt.Errorf("invalid endpoint key : %v", key)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
2015-09-16 11:39:46 +00:00
|
|
|
// network-id is placed at index=3. pls refer to endpoint.Key() method
|
|
|
|
return strings.Split(key, "/")[3], nil
|
2015-06-01 16:43:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (ep *endpoint) Value() []byte {
|
|
|
|
b, err := json.Marshal(ep)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:13:38 +00:00
|
|
|
func (ep *endpoint) SetValue(value []byte) error {
|
|
|
|
return json.Unmarshal(value, ep)
|
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (ep *endpoint) Index() uint64 {
|
2015-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
2015-05-31 18:49:11 +00:00
|
|
|
return ep.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SetIndex(index uint64) {
|
2015-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
2015-05-31 18:49:11 +00:00
|
|
|
ep.dbIndex = index
|
2015-06-18 22:13:38 +00:00
|
|
|
ep.dbExists = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Exists() bool {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
return ep.dbExists
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 14:09:39 +00:00
|
|
|
func (ep *endpoint) Skip() bool {
|
2015-10-08 03:01:38 +00:00
|
|
|
return ep.getNetwork().Skip()
|
2015-09-22 14:09:39 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func (ep *endpoint) processOptions(options ...EndpointOption) {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00: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 04:50:03 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (ep *endpoint) Join(sbox Sandbox, options ...EndpointOption) error {
|
2015-09-19 00:33:55 +00:00
|
|
|
if sbox == nil {
|
|
|
|
return types.BadRequestErrorf("endpoint cannot be joined by nil container")
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 00:33:55 +00:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-09-19 00:33:55 +00:00
|
|
|
sb.joinLeaveStart()
|
|
|
|
defer sb.joinLeaveEnd()
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-09-19 00:33:55 +00:00
|
|
|
return ep.sbJoin(sbox, options...)
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 00:33:55 +00:00
|
|
|
func (ep *endpoint) sbJoin(sbox Sandbox, options ...EndpointOption) error {
|
2015-04-30 05:58:12 +00:00
|
|
|
var err error
|
2015-07-02 05:00:48 +00:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00: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 04:50:03 +00:00
|
|
|
ep.Lock()
|
2015-07-02 05:00:48 +00:00
|
|
|
if ep.sandboxID != "" {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
2015-10-09 08:45:24 +00:00
|
|
|
return types.ForbiddenErrorf("another container is attached to the same network endpoint")
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
ep.Unlock()
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
ep.Lock()
|
|
|
|
ep.network = network
|
2015-07-02 05:00:48 +00:00
|
|
|
ep.sandboxID = sbox.ID()
|
2015-05-14 06:23:45 +00:00
|
|
|
ep.joinInfo = &endpointJoinInfo{}
|
2015-05-09 04:50:03 +00:00
|
|
|
epid := ep.id
|
|
|
|
ep.Unlock()
|
2015-04-30 05:58:12 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-06-01 16:43:24 +00:00
|
|
|
ep.Lock()
|
2015-07-02 05:00:48 +00:00
|
|
|
ep.sandboxID = ""
|
2015-06-01 16:43:24 +00:00
|
|
|
ep.Unlock()
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
network.Lock()
|
|
|
|
nid := network.id
|
|
|
|
network.Unlock()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.processOptions(options...)
|
2015-05-04 05:18:49 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
driver, err := network.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to join endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
err = driver.Join(nid, epid, sbox.Key(), ep, sbox.Labels())
|
2015-04-30 05:58:12 +00:00
|
|
|
if err != nil {
|
2015-05-24 09:41:03 +00:00
|
|
|
return err
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
2015-06-20 01:41:31 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-08-06 00:13:46 +00:00
|
|
|
// Do not alter global err variable, it's needed by the previous defer
|
|
|
|
if err := driver.Leave(nid, epid); err != nil {
|
2015-06-20 01:41:31 +00:00
|
|
|
log.Warnf("driver leave failed while rolling back join: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
address := ""
|
|
|
|
if ip := ep.getFirstInterfaceAddress(); ip != nil {
|
|
|
|
address = ip.String()
|
|
|
|
}
|
|
|
|
if err = sb.updateHostsFile(address, network.getSvcRecords()); err != nil {
|
2015-05-24 09:41:03 +00:00
|
|
|
return err
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// Watch for service records
|
|
|
|
network.getController().watchSvcRecord(ep)
|
|
|
|
|
|
|
|
if err = sb.updateDNS(network.enableIPv6); err != nil {
|
2015-05-24 09:41:03 +00:00
|
|
|
return err
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err = network.getController().updateToStore(ep); err != nil {
|
|
|
|
return err
|
2015-05-04 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:20:54 +00: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 05:00:48 +00:00
|
|
|
if err = sb.populateNetworkResources(ep); err != nil {
|
2015-06-05 20:31:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-07 01:34:50 +00:00
|
|
|
|
|
|
|
if sb.needDefaultGW() {
|
|
|
|
return sb.setupDefaultGW(ep)
|
|
|
|
}
|
|
|
|
return sb.clearDefaultGW()
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 06:24:23 +00:00
|
|
|
func (ep *endpoint) hasInterface(iName string) bool {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-09-09 23:06:35 +00:00
|
|
|
return ep.iface != nil && ep.iface.srcName == iName
|
2015-05-25 06:24:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00: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 21:52:46 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
sb, ok := sbox.(*sandbox)
|
|
|
|
if !ok {
|
|
|
|
return types.BadRequestErrorf("not a valid Sandbox interface")
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-09-19 00:33:55 +00: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 11:21:15 +00: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 05:00:48 +00:00
|
|
|
ep.Lock()
|
|
|
|
sid := ep.sandboxID
|
|
|
|
ep.Unlock()
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
if sid == "" {
|
|
|
|
return types.ForbiddenErrorf("cannot leave endpoint with no attached sandbox")
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
2015-07-02 05:00:48 +00: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...)
|
|
|
|
|
|
|
|
ep.Lock()
|
|
|
|
ep.sandboxID = ""
|
2015-10-05 11:21:15 +00:00
|
|
|
ep.network = n
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err := n.getController().updateToStore(ep); err != nil {
|
|
|
|
ep.Lock()
|
|
|
|
ep.sandboxID = sid
|
|
|
|
ep.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
d, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to leave endpoint: %v", err)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
if err := d.Leave(n.id, ep.id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-09-07 01:34:50 +00:00
|
|
|
if err := sb.clearNetworkResources(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// unwatch for service records
|
|
|
|
n.getController().unWatchSvcRecord(ep)
|
|
|
|
|
2015-09-07 01:34:50 +00: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-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Delete() error {
|
2015-06-05 20:31:12 +00:00
|
|
|
var err error
|
2015-10-05 11:21:15 +00: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 04:50:03 +00:00
|
|
|
ep.Lock()
|
2015-06-05 20:31:12 +00:00
|
|
|
epid := ep.id
|
|
|
|
name := ep.name
|
2015-07-02 05:00:48 +00:00
|
|
|
if ep.sandboxID != "" {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
2015-07-02 05:00:48 +00:00
|
|
|
return &ActiveContainerError{name: name, id: epid}
|
2015-06-01 16:43:24 +00:00
|
|
|
}
|
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-12 05:28:26 +00:00
|
|
|
if err = n.getEpCnt().DecEndpointCnt(); err != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
return err
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-12 05:28:26 +00:00
|
|
|
if e := n.getEpCnt().IncEndpointCnt(); e != nil {
|
2015-10-05 11:21:15 +00:00
|
|
|
log.Warnf("failed to update network %s : %v", n.name, e)
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err = n.getController().deleteFromStore(ep); err != nil {
|
2015-06-01 16:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-05 11:21:15 +00: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 20:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-06-01 16:43:24 +00:00
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if err = ep.deleteEndpoint(); err != nil {
|
2015-06-01 16:43:24 +00:00
|
|
|
return err
|
2015-05-06 20:02:40 +00:00
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
ep.releaseAddress()
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
func (ep *endpoint) deleteEndpoint() error {
|
|
|
|
ep.Lock()
|
2015-04-30 01:25:01 +00:00
|
|
|
n := ep.network
|
2015-06-01 16:43:24 +00:00
|
|
|
name := ep.name
|
|
|
|
epid := ep.id
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
driver, err := n.driver()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to delete endpoint: %v", err)
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err := driver.DeleteEndpoint(n.id, epid); err != nil {
|
2015-06-10 20:27:23 +00:00
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
|
|
|
return err
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-06-10 20:27:23 +00:00
|
|
|
log.Warnf("driver error deleting endpoint %s : %v", name, err)
|
|
|
|
}
|
2015-06-19 06:40:17 +00:00
|
|
|
|
2015-06-10 20:27:23 +00:00
|
|
|
return nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
func (ep *endpoint) getSandbox() (*sandbox, bool) {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
2015-07-02 05:00:48 +00:00
|
|
|
c := ep.network.getController()
|
|
|
|
sid := ep.sandboxID
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
c.Lock()
|
|
|
|
ps, ok := c.sandboxes[sid]
|
|
|
|
c.Unlock()
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
return ps, ok
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
func (ep *endpoint) getFirstInterfaceAddress() net.IP {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
2015-07-02 05:00:48 +00:00
|
|
|
defer ep.Unlock()
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
if ep.iface.addr != nil {
|
2015-09-09 23:06:35 +00:00
|
|
|
return ep.iface.addr.IP
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-04 05:18:49 +00: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 20:46:12 +00:00
|
|
|
// CreateOptionExposedPorts function returns an option setter for the container exposed
|
|
|
|
// ports option to be passed to network.CreateEndpoint() method.
|
2015-05-20 20:28:46 +00:00
|
|
|
func CreateOptionExposedPorts(exposedPorts []types.TransportPort) EndpointOption {
|
2015-05-05 20:46:12 +00:00
|
|
|
return func(ep *endpoint) {
|
|
|
|
// Defensive copy
|
2015-05-20 20:28:46 +00:00
|
|
|
eps := make([]types.TransportPort, len(exposedPorts))
|
2015-05-05 20:46:12 +00:00
|
|
|
copy(eps, exposedPorts)
|
|
|
|
// Store endpoint label and in generic because driver needs it
|
|
|
|
ep.exposedPorts = eps
|
2015-05-06 04:19:57 +00:00
|
|
|
ep.generic[netlabel.ExposedPorts] = eps
|
2015-05-05 20:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOptionPortMapping function returns an option setter for the mapping
|
2015-05-02 00:11:13 +00:00
|
|
|
// ports option to be passed to network.CreateEndpoint() method.
|
2015-05-20 20:28:46 +00:00
|
|
|
func CreateOptionPortMapping(portBindings []types.PortBinding) EndpointOption {
|
2015-05-02 00:01:21 +00:00
|
|
|
return func(ep *endpoint) {
|
2015-05-05 06:45:07 +00:00
|
|
|
// Store a copy of the bindings as generic data to pass to the driver
|
2015-05-20 20:28:46 +00:00
|
|
|
pbs := make([]types.PortBinding, len(portBindings))
|
2015-05-05 20:46:12 +00:00
|
|
|
copy(pbs, portBindings)
|
2015-05-06 04:19:57 +00:00
|
|
|
ep.generic[netlabel.PortMap] = pbs
|
2015-05-02 00:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00: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 21:52:46 +00:00
|
|
|
return func(ep *endpoint) {
|
2015-07-02 05:00:48 +00: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 21:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-16 11:39:46 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
func (ep *endpoint) DataScope() string {
|
|
|
|
return ep.getNetwork().DataScope()
|
2015-09-16 11:42:35 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
|
|
|
|
func (ep *endpoint) assignAddress() error {
|
|
|
|
var (
|
|
|
|
ipam ipamapi.Ipam
|
|
|
|
err error
|
|
|
|
)
|
2015-10-05 21:53:25 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
n := ep.getNetwork()
|
2015-10-05 02:08:31 +00:00
|
|
|
if n.Type() == "host" || n.Type() == "null" {
|
2015-10-03 23:11:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-05 21:53:25 +00:00
|
|
|
|
|
|
|
log.Debugf("Assigning addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
|
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
ipam, err = n.getController().getIpamDriver(n.ipamType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-04 04:25:57 +00:00
|
|
|
err = ep.assignAddressVersion(4, ipam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ep.assignAddressVersion(6, ipam)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
addr, _, err := ipam.RequestAddress(d.PoolID, nil, nil)
|
2015-10-03 23:11:50 +00:00
|
|
|
if err == nil {
|
|
|
|
ep.Lock()
|
2015-10-04 04:25:57 +00:00
|
|
|
*address = addr
|
|
|
|
*poolID = d.PoolID
|
2015-10-03 23:11:50 +00:00
|
|
|
ep.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != ipamapi.ErrNoAvailableIPs {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 04:25:57 +00: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 23:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) releaseAddress() {
|
|
|
|
n := ep.getNetwork()
|
2015-10-05 02:08:31 +00:00
|
|
|
if n.Type() == "host" || n.Type() == "null" {
|
2015-10-03 23:11:50 +00:00
|
|
|
return
|
|
|
|
}
|
2015-10-05 21:53:25 +00:00
|
|
|
|
|
|
|
log.Debugf("Releasing addresses for endpoint %s's interface on network %s", ep.Name(), n.Name())
|
|
|
|
|
2015-10-03 23:11:50 +00: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 04:25:57 +00:00
|
|
|
if err := ipam.ReleaseAddress(ep.iface.v4PoolID, ep.iface.addr.IP); err != nil {
|
2015-10-03 23:11:50 +00: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 04:25:57 +00: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 23:11:50 +00:00
|
|
|
}
|