2015-04-29 21:25:01 -04:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-04-30 01:58:12 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/etchosts"
|
2015-05-01 20:01:21 -04:00
|
|
|
"github.com/docker/libnetwork/netutils"
|
|
|
|
"github.com/docker/libnetwork/pkg/options"
|
2015-04-29 21:25:01 -04: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
|
|
|
|
// the endpoint. It returns the sandbox key to the caller
|
2015-05-01 20:11:13 -04:00
|
|
|
Join(containerID string, options ...EndpointOption) (*ContainerData, error)
|
2015-04-29 21:25:01 -04:00
|
|
|
|
|
|
|
// Leave removes the sandbox associated with container ID and detaches
|
|
|
|
// the network resources populated in the sandbox
|
2015-05-01 20:11:13 -04:00
|
|
|
Leave(containerID string, options ...EndpointOption) error
|
2015-04-29 21:25:01 -04:00
|
|
|
|
|
|
|
// SandboxInfo returns the sandbox information for this endpoint.
|
|
|
|
SandboxInfo() *sandbox.Info
|
|
|
|
|
2015-05-04 14:49:53 -04:00
|
|
|
// Info returns a collection of operational data related to this endpoint retrieved from the driver
|
|
|
|
Info() (map[string]interface{}, error)
|
|
|
|
|
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-30 01:58:12 -04:00
|
|
|
// ContainerData is a set of data returned when a container joins an endpoint.
|
|
|
|
type ContainerData struct {
|
|
|
|
SandboxKey string
|
|
|
|
HostsPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerConfig struct {
|
2015-05-01 16:57:03 -04:00
|
|
|
hostName string
|
|
|
|
domainName string
|
2015-04-30 17:52:46 -04:00
|
|
|
generic map[string]interface{}
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type containerInfo struct {
|
2015-05-01 16:57:03 -04:00
|
|
|
id string
|
|
|
|
config containerConfig
|
|
|
|
data ContainerData
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
type endpoint struct {
|
2015-05-01 20:01:21 -04:00
|
|
|
name string
|
|
|
|
id types.UUID
|
|
|
|
network *network
|
|
|
|
sandboxInfo *sandbox.Info
|
|
|
|
sandBox sandbox.Sandbox
|
|
|
|
container *containerInfo
|
|
|
|
exposedPorts []netutils.TransportPort
|
|
|
|
generic map[string]interface{}
|
|
|
|
context map[string]interface{}
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
const prefix = "/var/lib/docker/network/files"
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
func (ep *endpoint) ID() string {
|
|
|
|
return string(ep.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Name() string {
|
|
|
|
return ep.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Network() string {
|
|
|
|
return ep.network.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) SandboxInfo() *sandbox.Info {
|
|
|
|
if ep.sandboxInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ep.sandboxInfo.GetCopy()
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:49:53 -04:00
|
|
|
func (ep *endpoint) Info() (map[string]interface{}, error) {
|
|
|
|
return ep.network.driver.EndpointInfo(ep.network.id, ep.id)
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
func (ep *endpoint) processOptions(options ...EndpointOption) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(ep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
func createBasePath(dir string) error {
|
|
|
|
err := os.MkdirAll(dir, 0644)
|
|
|
|
if err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createHostsFile(path string) error {
|
|
|
|
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-01 20:11:13 -04:00
|
|
|
func (ep *endpoint) Join(containerID string, options ...EndpointOption) (*ContainerData, error) {
|
2015-04-30 01:58:12 -04:00
|
|
|
var err error
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
if containerID == "" {
|
2015-04-30 01:58:12 -04:00
|
|
|
return nil, InvalidContainerIDError(containerID)
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:58:12 -04:00
|
|
|
if ep.container != nil {
|
|
|
|
return nil, ErrInvalidJoin
|
|
|
|
}
|
|
|
|
|
|
|
|
ep.container = &containerInfo{}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ep.container = nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-05-01 20:11:13 -04:00
|
|
|
ep.processOptions(options...)
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
ep.container.data.HostsPath = prefix + "/" + containerID + "/hosts"
|
|
|
|
err = createHostsFile(ep.container.data.HostsPath)
|
2015-04-30 01:58:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ep.buildHostsFiles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sboxKey := sandbox.GenerateKey(containerID)
|
|
|
|
sb, err := ep.network.ctrlr.sandboxAdd(sboxKey)
|
|
|
|
if err != nil {
|
2015-04-30 01:58:12 -04:00
|
|
|
return nil, err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
ep.network.ctrlr.sandboxRm(sboxKey)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-30 17:52:46 -04:00
|
|
|
n := ep.network
|
2015-05-01 20:01:21 -04:00
|
|
|
err = n.driver.Join(n.id, ep.id, sboxKey, ep.container.config.generic)
|
2015-04-30 17:52:46 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
sinfo := ep.SandboxInfo()
|
|
|
|
if sinfo != nil {
|
|
|
|
for _, i := range sinfo.Interfaces {
|
|
|
|
err = sb.AddInterface(i)
|
|
|
|
if err != nil {
|
2015-04-30 01:58:12 -04:00
|
|
|
return nil, err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sb.SetGateway(sinfo.Gateway)
|
|
|
|
if err != nil {
|
2015-04-30 01:58:12 -04:00
|
|
|
return nil, err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = sb.SetGatewayIPv6(sinfo.GatewayIPv6)
|
|
|
|
if err != nil {
|
2015-04-30 01:58:12 -04:00
|
|
|
return nil, err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
ep.container.id = containerID
|
|
|
|
ep.container.data.SandboxKey = sb.Key()
|
2015-04-30 01:58:12 -04:00
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
cData := ep.container.data
|
2015-04-30 01:58:12 -04:00
|
|
|
return &cData, nil
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-01 20:11:13 -04:00
|
|
|
func (ep *endpoint) Leave(containerID string, options ...EndpointOption) error {
|
2015-05-01 16:57:03 -04:00
|
|
|
if ep.container == nil || ep.container.id == "" ||
|
|
|
|
containerID == "" || ep.container.id != containerID {
|
2015-04-29 21:25:01 -04:00
|
|
|
return InvalidContainerIDError(containerID)
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:11:13 -04:00
|
|
|
ep.processOptions(options...)
|
2015-04-30 17:52:46 -04:00
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
n := ep.network
|
2015-04-30 17:52:46 -04:00
|
|
|
err := n.driver.Leave(n.id, ep.id, ep.context)
|
2015-05-01 16:57:03 -04:00
|
|
|
ep.network.ctrlr.sandboxRm(ep.container.data.SandboxKey)
|
2015-04-30 01:58:12 -04:00
|
|
|
ep.container = nil
|
2015-04-30 17:52:46 -04:00
|
|
|
ep.context = nil
|
2015-05-01 18:33:29 -04:00
|
|
|
return err
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Delete() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n := ep.network
|
|
|
|
n.Lock()
|
|
|
|
_, ok := n.endpoints[ep.id]
|
|
|
|
if !ok {
|
|
|
|
n.Unlock()
|
|
|
|
return &UnknownEndpointError{name: ep.name, id: string(ep.id)}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(n.endpoints, ep.id)
|
|
|
|
n.Unlock()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.Lock()
|
|
|
|
n.endpoints[ep.id] = ep
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = n.driver.DeleteEndpoint(n.id, ep.id)
|
|
|
|
return err
|
|
|
|
}
|
2015-04-30 01:58:12 -04:00
|
|
|
|
|
|
|
func (ep *endpoint) buildHostsFiles() error {
|
|
|
|
var extraContent []etchosts.Record
|
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
name := ep.container.config.hostName
|
|
|
|
if ep.container.config.domainName != "" {
|
|
|
|
name = name + "." + ep.container.config.domainName
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IP := ""
|
|
|
|
if ep.sandboxInfo != nil && ep.sandboxInfo.Interfaces[0] != nil &&
|
|
|
|
ep.sandboxInfo.Interfaces[0].Address != nil {
|
|
|
|
IP = ep.sandboxInfo.Interfaces[0].Address.IP.String()
|
|
|
|
}
|
|
|
|
|
2015-05-01 16:57:03 -04:00
|
|
|
return etchosts.Build(ep.container.data.HostsPath, IP, ep.container.config.hostName,
|
|
|
|
ep.container.config.domainName, extraContent)
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
|
2015-05-01 20:11:13 -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-04-30 01:58:12 -04:00
|
|
|
// JoinOptionHostname function returns an option setter for hostname option to
|
|
|
|
// be passed to endpoint Join method.
|
2015-05-01 20:11:13 -04:00
|
|
|
func JoinOptionHostname(name string) EndpointOption {
|
2015-04-30 13:35:46 -04:00
|
|
|
return func(ep *endpoint) {
|
2015-05-01 16:57:03 -04:00
|
|
|
ep.container.config.hostName = name
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinOptionDomainname function returns an option setter for domainname option to
|
|
|
|
// be passed to endpoint Join method.
|
2015-05-01 20:11:13 -04:00
|
|
|
func JoinOptionDomainname(name string) EndpointOption {
|
2015-04-30 13:35:46 -04:00
|
|
|
return func(ep *endpoint) {
|
2015-05-01 16:57:03 -04:00
|
|
|
ep.container.config.domainName = name
|
2015-04-30 01:58:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
// CreateOptionPortMapping function returns an option setter for the container exposed
|
2015-05-01 20:11:13 -04:00
|
|
|
// ports option to be passed to network.CreateEndpoint() method.
|
2015-05-01 20:01:21 -04:00
|
|
|
func CreateOptionPortMapping(portBindings []netutils.PortBinding) EndpointOption {
|
|
|
|
return func(ep *endpoint) {
|
|
|
|
// Store endpoint label
|
|
|
|
ep.generic[options.PortMap] = portBindings
|
|
|
|
// Extract exposed ports as this is the only concern of libnetwork endpoint
|
|
|
|
ep.exposedPorts = make([]netutils.TransportPort, 0, len(portBindings))
|
|
|
|
for _, b := range portBindings {
|
|
|
|
ep.exposedPorts = append(ep.exposedPorts, netutils.TransportPort{Proto: b.Proto, Port: b.Port})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-30 17:52:46 -04: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-01 20:11:13 -04:00
|
|
|
func JoinOptionGeneric(generic map[string]interface{}) EndpointOption {
|
2015-04-30 17:52:46 -04:00
|
|
|
return func(ep *endpoint) {
|
2015-05-01 20:01:21 -04:00
|
|
|
ep.container.config.generic = generic
|
2015-04-30 17:52:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveOptionGeneric 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 leave method. Container Labels are a good example.
|
2015-05-01 20:11:13 -04:00
|
|
|
func LeaveOptionGeneric(context map[string]interface{}) EndpointOption {
|
2015-04-30 17:52:46 -04:00
|
|
|
return func(ep *endpoint) {
|
|
|
|
ep.context = context
|
|
|
|
}
|
|
|
|
}
|