2015-04-30 01:25:01 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-05-15 21:01:53 +00:00
|
|
|
"bytes"
|
2015-05-31 18:49:11 +00:00
|
|
|
"encoding/json"
|
2015-06-05 20:31:12 +00:00
|
|
|
"fmt"
|
2015-05-03 20:29:43 +00:00
|
|
|
"io/ioutil"
|
2015-04-30 05:58:12 +00:00
|
|
|
"os"
|
2015-05-15 21:01:53 +00:00
|
|
|
"path"
|
2015-04-30 05:58:12 +00:00
|
|
|
"path/filepath"
|
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-15 21:01:53 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-05-31 18:49:11 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/etchosts"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/resolvconf"
|
2015-04-30 01:25:01 +00:00
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
"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
|
|
|
|
|
|
|
|
// Join creates a new sandbox for the given container ID and populates the
|
|
|
|
// network resources allocated for the endpoint and joins the sandbox to
|
2015-05-24 09:41:03 +00:00
|
|
|
// the endpoint.
|
|
|
|
Join(containerID string, options ...EndpointOption) error
|
2015-04-30 01:25:01 +00:00
|
|
|
|
|
|
|
// Leave removes the sandbox associated with container ID and detaches
|
|
|
|
// the network resources populated in the sandbox
|
2015-05-02 00:11:13 +00:00
|
|
|
Leave(containerID string, 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-06-11 01:50:19 +00:00
|
|
|
// ContainerInfo returns the info available at the endpoint about the attached container
|
|
|
|
ContainerInfo() ContainerInfo
|
|
|
|
|
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 05:58:12 +00:00
|
|
|
// ContainerData is a set of data returned when a container joins an endpoint.
|
|
|
|
type ContainerData struct {
|
|
|
|
SandboxKey string
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:20:45 +00:00
|
|
|
// These are the container configs used to customize container /etc/hosts file.
|
|
|
|
type hostsPathConfig struct {
|
|
|
|
hostName string
|
|
|
|
domainName string
|
|
|
|
hostsPath string
|
|
|
|
extraHosts []extraHost
|
|
|
|
parentUpdates []parentUpdate
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are the container configs used to customize container /etc/resolv.conf file.
|
|
|
|
type resolvConfPathConfig struct {
|
2015-05-04 05:18:49 +00:00
|
|
|
resolvConfPath string
|
|
|
|
dnsList []string
|
|
|
|
dnsSearchList []string
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 04:20:45 +00:00
|
|
|
type containerConfig struct {
|
|
|
|
hostsPathConfig
|
|
|
|
resolvConfPathConfig
|
|
|
|
generic map[string]interface{}
|
|
|
|
useDefaultSandBox bool
|
2015-06-02 20:22:28 +00:00
|
|
|
prio int // higher the value, more the priority
|
2015-05-05 04:20:45 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
type extraHost struct {
|
|
|
|
name string
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
|
|
|
type parentUpdate struct {
|
|
|
|
eid string
|
|
|
|
name string
|
|
|
|
ip string
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type containerInfo struct {
|
2015-05-01 20:57:03 +00:00
|
|
|
id string
|
|
|
|
config containerConfig
|
|
|
|
data ContainerData
|
2015-06-05 20:31:12 +00:00
|
|
|
sync.Mutex
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 01:50:19 +00:00
|
|
|
func (ci *containerInfo) ID() string {
|
|
|
|
return ci.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *containerInfo) Labels() map[string]interface{} {
|
|
|
|
return ci.config.generic
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
type endpoint struct {
|
2015-05-09 04:50:03 +00:00
|
|
|
name string
|
|
|
|
id types.UUID
|
|
|
|
network *network
|
2015-05-14 06:23:45 +00:00
|
|
|
iFaces []*endpointInterface
|
|
|
|
joinInfo *endpointJoinInfo
|
2015-05-09 04:50:03 +00:00
|
|
|
container *containerInfo
|
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-05-09 04:50:03 +00:00
|
|
|
sync.Mutex
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
func (ci *containerInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
ci.Lock()
|
|
|
|
defer ci.Unlock()
|
|
|
|
|
|
|
|
// We are just interested in the container ID. This can be expanded to include all of containerInfo if there is a need
|
|
|
|
return json.Marshal(ci.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ci *containerInfo) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
ci.Lock()
|
|
|
|
defer ci.Unlock()
|
|
|
|
|
|
|
|
var id string
|
|
|
|
if err := json.Unmarshal(b, &id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ci.id = id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
epMap["id"] = string(ep.id)
|
|
|
|
epMap["ep_iface"] = ep.iFaces
|
|
|
|
epMap["exposed_ports"] = ep.exposedPorts
|
|
|
|
epMap["generic"] = ep.generic
|
2015-06-01 16:43:24 +00:00
|
|
|
if ep.container != nil {
|
|
|
|
epMap["container"] = ep.container
|
|
|
|
}
|
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)
|
|
|
|
ep.id = types.UUID(epMap["id"].(string))
|
|
|
|
|
|
|
|
ib, _ := json.Marshal(epMap["ep_iface"])
|
|
|
|
var ifaces []endpointInterface
|
|
|
|
json.Unmarshal(ib, &ifaces)
|
|
|
|
ep.iFaces = make([]*endpointInterface, 0)
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
ep.iFaces = append(ep.iFaces, &iface)
|
|
|
|
}
|
|
|
|
|
|
|
|
tb, _ := json.Marshal(epMap["exposed_ports"])
|
|
|
|
var tPorts []types.TransportPort
|
|
|
|
json.Unmarshal(tb, &tPorts)
|
|
|
|
ep.exposedPorts = tPorts
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
epc, ok := epMap["container"]
|
|
|
|
if ok {
|
|
|
|
cb, _ := json.Marshal(epc)
|
|
|
|
var cInfo containerInfo
|
|
|
|
json.Unmarshal(cb, &cInfo)
|
|
|
|
ep.container = &cInfo
|
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
if epMap["generic"] != nil {
|
|
|
|
ep.generic = epMap["generic"].(map[string]interface{})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
const defaultPrefix = "/var/lib/docker/network/files"
|
2015-04-30 05:58:12 +00:00
|
|
|
|
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-04-30 01:25:01 +00:00
|
|
|
return string(ep.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
return ep.network.name
|
|
|
|
}
|
|
|
|
|
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-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
n := ep.network
|
|
|
|
defer ep.Unlock()
|
|
|
|
return []string{datastore.EndpointKeyPrefix, string(n.id), string(ep.id)}
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
func (ep *endpoint) KeyPrefix() []string {
|
2015-06-05 20:31:12 +00:00
|
|
|
ep.Lock()
|
|
|
|
n := ep.network
|
|
|
|
defer ep.Unlock()
|
|
|
|
return []string{datastore.EndpointKeyPrefix, string(n.id)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) networkIDFromKey(key []string) (types.UUID, error) {
|
|
|
|
// endpoint Key structure : endpoint/network-id/endpoint-id
|
|
|
|
// its an invalid key if the key doesnt have all the 3 key elements above
|
|
|
|
if key == nil || len(key) < 3 || key[0] != datastore.EndpointKeyPrefix {
|
|
|
|
return types.UUID(""), fmt.Errorf("invalid endpoint key : %v", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// network-id is placed at index=1. pls refer to endpoint.Key() method
|
|
|
|
return types.UUID(key[1]), 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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-04-30 05:58:12 +00:00
|
|
|
func createBasePath(dir string) error {
|
|
|
|
err := os.MkdirAll(dir, 0644)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
func createFile(path string) error {
|
2015-04-30 05:58:12 +00:00
|
|
|
var f *os.File
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(path)
|
|
|
|
err := createBasePath(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err = os.Create(path)
|
|
|
|
if err == nil {
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
// joinLeaveStart waits to ensure there are no joins or leaves in progress and
|
|
|
|
// marks this join/leave in progress without race
|
|
|
|
func (ep *endpoint) joinLeaveStart() {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
for ep.joinLeaveDone != nil {
|
|
|
|
joinLeaveDone := ep.joinLeaveDone
|
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-joinLeaveDone:
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.joinLeaveDone = make(chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// joinLeaveEnd marks the end of this join/leave operation and
|
|
|
|
// signals the same without race to other join and leave waiters
|
|
|
|
func (ep *endpoint) joinLeaveEnd() {
|
|
|
|
ep.Lock()
|
|
|
|
defer ep.Unlock()
|
|
|
|
|
|
|
|
if ep.joinLeaveDone != nil {
|
|
|
|
close(ep.joinLeaveDone)
|
|
|
|
ep.joinLeaveDone = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 09:41:03 +00:00
|
|
|
func (ep *endpoint) Join(containerID string, options ...EndpointOption) error {
|
2015-04-30 05:58:12 +00:00
|
|
|
var err error
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
if containerID == "" {
|
2015-05-24 09:41:03 +00:00
|
|
|
return InvalidContainerIDError(containerID)
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.joinLeaveStart()
|
2015-06-05 20:31:12 +00:00
|
|
|
defer func() {
|
|
|
|
ep.joinLeaveEnd()
|
|
|
|
if err != nil {
|
|
|
|
if e := ep.Leave(containerID, options...); e != nil {
|
|
|
|
log.Warnf("couldnt leave endpoint : %v", ep.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-09 04:50:03 +00:00
|
|
|
|
|
|
|
ep.Lock()
|
2015-04-30 05:58:12 +00:00
|
|
|
if ep.container != nil {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
2015-05-24 09:41:03 +00:00
|
|
|
return ErrInvalidJoin{}
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
ep.container = &containerInfo{
|
2015-05-09 04:50:03 +00:00
|
|
|
id: containerID,
|
2015-05-03 20:29:43 +00:00
|
|
|
config: containerConfig{
|
2015-05-05 04:20:45 +00:00
|
|
|
hostsPathConfig: hostsPathConfig{
|
|
|
|
extraHosts: []extraHost{},
|
|
|
|
parentUpdates: []parentUpdate{},
|
|
|
|
},
|
2015-05-03 20:29:43 +00:00
|
|
|
}}
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
ep.joinInfo = &endpointJoinInfo{}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
container := ep.container
|
|
|
|
network := ep.network
|
|
|
|
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-04-30 05:58:12 +00:00
|
|
|
ep.container = nil
|
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()
|
|
|
|
driver := network.driver
|
|
|
|
nid := network.id
|
|
|
|
ctrlr := network.ctrlr
|
|
|
|
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-05-03 20:29:43 +00:00
|
|
|
sboxKey := sandbox.GenerateKey(containerID)
|
2015-05-09 04:50:03 +00:00
|
|
|
if container.config.useDefaultSandBox {
|
2015-05-05 04:20:45 +00:00
|
|
|
sboxKey = sandbox.GenerateKey("default")
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
err = driver.Join(nid, epid, sboxKey, ep, container.config.generic)
|
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-05-09 04:50:03 +00:00
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
err = ep.buildHostsFiles()
|
|
|
|
if 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
|
|
|
err = ep.updateParentHosts()
|
2015-04-30 01:25:01 +00:00
|
|
|
if 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-05-04 05:18:49 +00:00
|
|
|
err = ep.setupDNS()
|
|
|
|
if err != nil {
|
2015-05-24 09:41:03 +00:00
|
|
|
return err
|
2015-05-04 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:22:28 +00:00
|
|
|
sb, err := ctrlr.sandboxAdd(sboxKey, !container.config.useDefaultSandBox, ep)
|
2015-04-30 21:52:46 +00:00
|
|
|
if err != nil {
|
2015-05-24 09:41:03 +00:00
|
|
|
return err
|
2015-04-30 21:52:46 +00:00
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-06-02 20:22:28 +00:00
|
|
|
ctrlr.sandboxRm(sboxKey, ep)
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-04-30 21:52:46 +00:00
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if err := network.ctrlr.updateEndpointToStore(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-30 05:58:12 +00:00
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
container.data.SandboxKey = sb.Key()
|
2015-05-24 09:41:03 +00:00
|
|
|
return nil
|
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()
|
|
|
|
|
|
|
|
for _, iface := range ep.iFaces {
|
|
|
|
if iface.srcName == iName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-05-02 00:11:13 +00:00
|
|
|
func (ep *endpoint) Leave(containerID string, options ...EndpointOption) error {
|
2015-05-09 04:50:03 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
ep.joinLeaveStart()
|
|
|
|
defer ep.joinLeaveEnd()
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-05-02 00:11:13 +00:00
|
|
|
ep.processOptions(options...)
|
2015-04-30 21:52:46 +00:00
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
container := ep.container
|
2015-05-01 20:57:03 +00:00
|
|
|
n := ep.network
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if container == nil || container.id == "" || container.data.SandboxKey == "" ||
|
2015-05-09 04:50:03 +00:00
|
|
|
containerID == "" || container.id != containerID {
|
|
|
|
if container == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
err = ErrNoContainer{}
|
2015-05-09 04:50:03 +00:00
|
|
|
} else {
|
|
|
|
err = InvalidContainerIDError(containerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ep.container = nil
|
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
driver := n.driver
|
|
|
|
ctrlr := n.ctrlr
|
|
|
|
n.Unlock()
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if err := ctrlr.updateEndpointToStore(ep); err != nil {
|
|
|
|
ep.Lock()
|
|
|
|
ep.container = container
|
|
|
|
ep.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
err = driver.Leave(n.id, ep.id)
|
2015-05-06 00:33:08 +00:00
|
|
|
|
2015-06-02 20:22:28 +00:00
|
|
|
ctrlr.sandboxRm(container.data.SandboxKey, ep)
|
2015-05-09 04:50:03 +00:00
|
|
|
|
2015-05-01 22:33:29 +00:00
|
|
|
return err
|
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-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
2015-06-05 20:31:12 +00:00
|
|
|
epid := ep.id
|
|
|
|
name := ep.name
|
|
|
|
n := ep.network
|
2015-05-06 20:02:40 +00:00
|
|
|
if ep.container != nil {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
2015-06-05 20:31:12 +00:00
|
|
|
return &ActiveContainerError{name: name, id: string(epid)}
|
2015-06-01 16:43:24 +00:00
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
n.Lock()
|
|
|
|
ctrlr := n.ctrlr
|
|
|
|
n.Unlock()
|
2015-06-01 16:43:24 +00:00
|
|
|
ep.Unlock()
|
|
|
|
|
2015-06-05 20:31:12 +00:00
|
|
|
if err = ctrlr.deleteEndpointFromStore(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ep.SetIndex(0)
|
|
|
|
if e := ctrlr.updateEndpointToStore(ep); e != nil {
|
|
|
|
log.Warnf("failed to recreate endpoint in store %s : %v", name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Update the endpoint count in network and update it in the datastore
|
|
|
|
n.DecEndpointCnt()
|
|
|
|
if err = ctrlr.updateNetworkToStore(n); 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 {
|
|
|
|
n.IncEndpointCnt()
|
|
|
|
if e := ctrlr.updateNetworkToStore(n); e != nil {
|
|
|
|
log.Warnf("failed to update network %s : %v", n.name, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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-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-04-30 01:25:01 +00:00
|
|
|
n.Lock()
|
2015-05-09 04:50:03 +00:00
|
|
|
_, ok := n.endpoints[epid]
|
2015-04-30 01:25:01 +00:00
|
|
|
if !ok {
|
|
|
|
n.Unlock()
|
2015-06-17 16:13:31 +00:00
|
|
|
return nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
nid := n.id
|
|
|
|
driver := n.driver
|
|
|
|
delete(n.endpoints, epid)
|
2015-04-30 01:25:01 +00:00
|
|
|
n.Unlock()
|
2015-06-10 20:27:23 +00:00
|
|
|
|
|
|
|
if err := driver.DeleteEndpoint(nid, epid); err != nil {
|
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
2015-04-30 01:25:01 +00:00
|
|
|
n.Lock()
|
2015-05-09 04:50:03 +00:00
|
|
|
n.endpoints[epid] = ep
|
2015-04-30 01:25:01 +00:00
|
|
|
n.Unlock()
|
2015-06-10 20:27:23 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-04-30 05:58:12 +00:00
|
|
|
|
|
|
|
func (ep *endpoint) buildHostsFiles() error {
|
|
|
|
var extraContent []etchosts.Record
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
container := ep.container
|
|
|
|
joinInfo := ep.joinInfo
|
2015-05-14 06:23:45 +00:00
|
|
|
ifaces := ep.iFaces
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
if container == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return ErrNoContainer{}
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.config.hostsPath == "" {
|
|
|
|
container.config.hostsPath = defaultPrefix + "/" + container.id + "/hosts"
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(container.config.hostsPath)
|
2015-05-03 20:29:43 +00:00
|
|
|
err := createBasePath(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
if joinInfo != nil && joinInfo.hostsPath != "" {
|
|
|
|
content, err := ioutil.ReadFile(joinInfo.hostsPath)
|
2015-05-03 20:29:43 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2015-05-09 04:50:03 +00:00
|
|
|
return ioutil.WriteFile(container.config.hostsPath, content, 0644)
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
for _, extraHost := range container.config.extraHosts {
|
2015-05-03 20:29:43 +00:00
|
|
|
extraContent = append(extraContent,
|
|
|
|
etchosts.Record{Hosts: extraHost.name, IP: extraHost.IP})
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
IP := ""
|
2015-05-14 06:23:45 +00:00
|
|
|
if len(ifaces) != 0 && ifaces[0] != nil {
|
|
|
|
IP = ifaces[0].addr.IP.String()
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
return etchosts.Build(container.config.hostsPath, IP, container.config.hostName,
|
|
|
|
container.config.domainName, extraContent)
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
func (ep *endpoint) updateParentHosts() error {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
container := ep.container
|
|
|
|
network := ep.network
|
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
if container == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return ErrNoContainer{}
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range container.config.parentUpdates {
|
|
|
|
network.Lock()
|
|
|
|
pep, ok := network.endpoints[types.UUID(update.eid)]
|
2015-05-03 20:29:43 +00:00
|
|
|
if !ok {
|
2015-05-09 04:50:03 +00:00
|
|
|
network.Unlock()
|
2015-05-03 20:29:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-05-09 04:50:03 +00:00
|
|
|
network.Unlock()
|
2015-05-03 20:29:43 +00:00
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
pep.Lock()
|
|
|
|
pContainer := pep.container
|
|
|
|
pep.Unlock()
|
|
|
|
|
|
|
|
if pContainer != nil {
|
|
|
|
if err := etchosts.Update(pContainer.config.hostsPath, update.ip, update.name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
func (ep *endpoint) updateDNS(resolvConf []byte) error {
|
2015-05-09 04:50:03 +00:00
|
|
|
ep.Lock()
|
|
|
|
container := ep.container
|
|
|
|
network := ep.network
|
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
if container == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return ErrNoContainer{}
|
2015-05-09 04:50:03 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 12:13:17 +00:00
|
|
|
oldHash := []byte{}
|
2015-05-15 21:01:53 +00:00
|
|
|
hashFile := container.config.resolvConfPath + ".hash"
|
|
|
|
|
|
|
|
resolvBytes, err := ioutil.ReadFile(container.config.resolvConfPath)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-16 12:13:17 +00:00
|
|
|
} else {
|
|
|
|
oldHash, err = ioutil.ReadFile(hashFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldHash = []byte{}
|
|
|
|
}
|
2015-05-15 21:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
curHash, err := ioutils.HashData(bytes.NewReader(resolvBytes))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(oldHash) != "" && curHash != string(oldHash) {
|
|
|
|
// Seems the user has changed the container resolv.conf since the last time
|
|
|
|
// we checked so return without doing anything.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace any localhost/127.* and remove IPv6 nameservers if IPv6 disabled.
|
|
|
|
resolvConf, _ = resolvconf.FilterResolvDNS(resolvConf, network.enableIPv6)
|
|
|
|
|
|
|
|
newHash, err := ioutils.HashData(bytes.NewReader(resolvConf))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// for atomic updates to these files, use temporary files with os.Rename:
|
|
|
|
dir := path.Dir(container.config.resolvConfPath)
|
|
|
|
tmpHashFile, err := ioutil.TempFile(dir, "hash")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpResolvFile, err := ioutil.TempFile(dir, "resolv")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-20 05:40:23 +00:00
|
|
|
// Change the perms to 0644 since ioutil.TempFile creates it by default as 0600
|
|
|
|
if err := os.Chmod(tmpResolvFile.Name(), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
// write the updates to the temp files
|
|
|
|
if err = ioutil.WriteFile(tmpHashFile.Name(), []byte(newHash), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = ioutil.WriteFile(tmpResolvFile.Name(), resolvConf, 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename the temp files for atomic replace
|
|
|
|
if err = os.Rename(tmpHashFile.Name(), hashFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Rename(tmpResolvFile.Name(), container.config.resolvConfPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) setupDNS() error {
|
|
|
|
ep.Lock()
|
|
|
|
container := ep.container
|
|
|
|
ep.Unlock()
|
|
|
|
|
|
|
|
if container == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return ErrNoContainer{}
|
2015-05-15 21:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
if container.config.resolvConfPath == "" {
|
|
|
|
container.config.resolvConfPath = defaultPrefix + "/" + container.id + "/resolv.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, _ := filepath.Split(container.config.resolvConfPath)
|
2015-05-04 05:18:49 +00:00
|
|
|
err := createBasePath(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resolvConf, err := resolvconf.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
if len(container.config.dnsList) > 0 ||
|
|
|
|
len(container.config.dnsSearchList) > 0 {
|
2015-05-04 05:18:49 +00:00
|
|
|
var (
|
|
|
|
dnsList = resolvconf.GetNameservers(resolvConf)
|
|
|
|
dnsSearchList = resolvconf.GetSearchDomains(resolvConf)
|
|
|
|
)
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
if len(container.config.dnsList) > 0 {
|
|
|
|
dnsList = container.config.dnsList
|
2015-05-04 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
if len(container.config.dnsSearchList) > 0 {
|
|
|
|
dnsSearchList = container.config.dnsSearchList
|
2015-05-04 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 04:50:03 +00:00
|
|
|
return resolvconf.Build(container.config.resolvConfPath, dnsList, dnsSearchList)
|
2015-05-04 05:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:01:53 +00:00
|
|
|
return ep.updateDNS(resolvConf)
|
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-06-02 20:22:28 +00:00
|
|
|
// JoinOptionPriority function returns an option setter for priority option to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionPriority(prio int) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.prio = prio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 05:58:12 +00:00
|
|
|
// JoinOptionHostname function returns an option setter for hostname option to
|
|
|
|
// be passed to endpoint Join method.
|
2015-05-02 00:11:13 +00:00
|
|
|
func JoinOptionHostname(name string) EndpointOption {
|
2015-04-30 17:35:46 +00:00
|
|
|
return func(ep *endpoint) {
|
2015-05-01 20:57:03 +00:00
|
|
|
ep.container.config.hostName = name
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionDomainname function returns an option setter for domainname option to
|
|
|
|
// be passed to endpoint Join method.
|
2015-05-02 00:11:13 +00:00
|
|
|
func JoinOptionDomainname(name string) EndpointOption {
|
2015-04-30 17:35:46 +00:00
|
|
|
return func(ep *endpoint) {
|
2015-05-01 20:57:03 +00:00
|
|
|
ep.container.config.domainName = name
|
2015-04-30 05:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
// JoinOptionHostsPath function returns an option setter for hostspath option to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionHostsPath(path string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.hostsPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionExtraHost function returns an option setter for extra /etc/hosts options
|
|
|
|
// which is a name and IP as strings.
|
|
|
|
func JoinOptionExtraHost(name string, IP string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
2015-05-05 04:20:45 +00:00
|
|
|
ep.container.config.extraHosts = append(ep.container.config.extraHosts, extraHost{name: name, IP: IP})
|
2015-05-03 20:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionParentUpdate function returns an option setter for parent container
|
|
|
|
// which needs to update the IP address for the linked container.
|
|
|
|
func JoinOptionParentUpdate(eid string, name, ip string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.parentUpdates = append(ep.container.config.parentUpdates, parentUpdate{eid: eid, name: name, ip: ip})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 05:18:49 +00:00
|
|
|
// JoinOptionResolvConfPath function returns an option setter for resolvconfpath option to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionResolvConfPath(path string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.resolvConfPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionDNS function returns an option setter for dns entry option to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionDNS(dns string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.dnsList = append(ep.container.config.dnsList, dns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionDNSSearch function returns an option setter for dns search entry option to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionDNSSearch(search string) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.dnsSearchList = append(ep.container.config.dnsSearchList, search)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 04:20:45 +00:00
|
|
|
// JoinOptionUseDefaultSandbox function returns an option setter for using default sandbox to
|
|
|
|
// be passed to endpoint Join method.
|
|
|
|
func JoinOptionUseDefaultSandbox() EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.container.config.useDefaultSandBox = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-30 21:52:46 +00:00
|
|
|
// JoinOptionGeneric function returns an option setter for Generic configuration
|
|
|
|
// that is not managed by libNetwork but can be used by the Drivers during the call to
|
|
|
|
// endpoint join method. Container Labels are a good example.
|
2015-05-02 00:11:13 +00:00
|
|
|
func JoinOptionGeneric(generic map[string]interface{}) EndpointOption {
|
2015-04-30 21:52:46 +00:00
|
|
|
return func(ep *endpoint) {
|
2015-05-02 00:01:21 +00:00
|
|
|
ep.container.config.generic = generic
|
2015-04-30 21:52:46 +00:00
|
|
|
}
|
|
|
|
}
|