2015-04-13 18:40:42 +00:00
|
|
|
/*
|
2015-04-15 12:42:25 +00:00
|
|
|
Package libnetwork provides the basic functionality and extension points to
|
2015-04-13 18:40:42 +00:00
|
|
|
create network namespaces and allocate interfaces for containers to use.
|
|
|
|
|
2015-04-15 12:42:25 +00:00
|
|
|
// Create a new controller instance
|
|
|
|
controller := libnetwork.New()
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-04-15 12:42:25 +00:00
|
|
|
// This option is only needed for in-tree drivers. Plugins(in future) will get
|
|
|
|
// their options through plugin infrastructure.
|
|
|
|
option := options.Generic{}
|
2015-04-23 17:49:57 +00:00
|
|
|
driver, err := controller.NewNetworkDriver("bridge", option)
|
2015-04-15 12:42:25 +00:00
|
|
|
if err != nil {
|
2015-04-15 05:25:42 +00:00
|
|
|
return
|
2015-04-15 12:42:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
netOptions := options.Generic{}
|
|
|
|
// Create a network for containers to join.
|
|
|
|
network, err := controller.NewNetwork(driver, "network1", netOptions)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// For a new container: create a sandbox instance (providing a unique key).
|
|
|
|
// For linux it is a filesystem path
|
|
|
|
networkPath := "/var/lib/docker/.../4d23e"
|
|
|
|
networkNamespace, err := sandbox.NewSandbox(networkPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each new container: allocate IP and interfaces. The returned network
|
|
|
|
// settings will be used for container infos (inspect and such), as well as
|
|
|
|
// iptables rules for port publishing.
|
2015-04-23 18:15:15 +00:00
|
|
|
ep, err := network.CreateEndpoint("Endpoint1", networkNamespace.Key(), nil)
|
2015-04-15 12:42:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
*/
|
|
|
|
package libnetwork
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-18 04:37:54 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-04-20 15:44:06 +00:00
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2015-04-13 18:40:42 +00:00
|
|
|
)
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
|
|
// networks.
|
|
|
|
type NetworkController interface {
|
2015-04-22 23:47:07 +00:00
|
|
|
// ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
|
|
|
|
ConfigureNetworkDriver(networkType string, options interface{}) error
|
2015-04-17 22:42:23 +00:00
|
|
|
// Create a new network. The options parameter carries network specific options.
|
2015-04-13 18:40:42 +00:00
|
|
|
// Labels support will be added in the near future.
|
2015-04-22 23:47:07 +00:00
|
|
|
NewNetwork(networkType, name string, options interface{}) (Network, error)
|
2015-02-20 01:21:42 +00:00
|
|
|
}
|
|
|
|
|
2015-03-02 18:17:12 +00:00
|
|
|
// A Network represents a logical connectivity zone that containers may
|
2015-04-15 12:42:25 +00:00
|
|
|
// join using the Link method. A Network is managed by a specific driver.
|
2015-03-02 18:17:12 +00:00
|
|
|
type Network interface {
|
|
|
|
// A user chosen name for this network.
|
|
|
|
Name() string
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// A system generated id for this network.
|
|
|
|
ID() string
|
|
|
|
|
2015-03-02 18:17:12 +00:00
|
|
|
// The type of network, which corresponds to its managing driver.
|
|
|
|
Type() string
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// Create a new endpoint to this network symbolically identified by the
|
|
|
|
// specified unique name. The options parameter carry driver specific options.
|
|
|
|
// Labels support will be added in the near future.
|
2015-04-17 22:42:23 +00:00
|
|
|
CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error)
|
2015-03-20 20:54:49 +00:00
|
|
|
|
2015-04-18 06:13:29 +00:00
|
|
|
// Endpoints returns the list of Endpoint in this network.
|
|
|
|
Endpoints() []Endpoint
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// Delete the network.
|
2015-03-20 20:54:49 +00:00
|
|
|
Delete() error
|
2015-03-02 18:17:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
// Endpoint represents a logical connection between a network and a sandbox.
|
|
|
|
type Endpoint interface {
|
2015-04-17 22:42:23 +00:00
|
|
|
// A system generated id for this endpoint.
|
2015-04-17 14:31:29 +00:00
|
|
|
ID() string
|
|
|
|
|
2015-04-17 22:42:23 +00:00
|
|
|
// Name returns the name of this endpoint.
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Network returns the name of the network to which this endpoint is attached.
|
|
|
|
Network() string
|
|
|
|
|
|
|
|
// SandboxInfo returns the sandbox information for this endpoint.
|
2015-04-20 15:44:06 +00:00
|
|
|
SandboxInfo() *sandbox.Info
|
2015-04-17 22:42:23 +00:00
|
|
|
|
|
|
|
// Delete and detaches this endpoint from the network.
|
2015-04-13 18:40:42 +00:00
|
|
|
Delete() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
name string
|
2015-04-20 15:44:06 +00:00
|
|
|
id types.UUID
|
2015-04-13 18:40:42 +00:00
|
|
|
network *network
|
2015-04-20 15:44:06 +00:00
|
|
|
sandboxInfo *sandbox.Info
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
type network struct {
|
|
|
|
ctrlr *controller
|
|
|
|
name string
|
|
|
|
networkType string
|
2015-04-20 15:44:06 +00:00
|
|
|
id types.UUID
|
2015-04-22 23:47:07 +00:00
|
|
|
driver driverapi.Driver
|
2015-04-15 05:25:42 +00:00
|
|
|
endpoints endpointTable
|
2015-04-13 18:40:42 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:44:06 +00:00
|
|
|
type networkTable map[types.UUID]*network
|
|
|
|
type endpointTable map[types.UUID]*endpoint
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
type controller struct {
|
|
|
|
networks networkTable
|
|
|
|
drivers driverTable
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of network controller.
|
|
|
|
func New() NetworkController {
|
|
|
|
return &controller{networkTable{}, enumerateDrivers(), sync.Mutex{}}
|
2015-02-20 01:21:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
func (c *controller) ConfigureNetworkDriver(networkType string, options interface{}) error {
|
2015-04-13 18:40:42 +00:00
|
|
|
d, ok := c.drivers[networkType]
|
|
|
|
if !ok {
|
2015-04-22 23:47:07 +00:00
|
|
|
return NetworkTypeError(networkType)
|
2015-04-15 05:25:42 +00:00
|
|
|
}
|
2015-04-22 23:47:07 +00:00
|
|
|
return d.Config(options)
|
2015-04-15 05:25:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// NewNetwork creates a new network of the specified network type. The options
|
|
|
|
// are network specific and modeled in a generic way.
|
|
|
|
func (c *controller) NewNetwork(networkType, name string, options interface{}) (Network, error) {
|
|
|
|
// Check if a driver for the specified network type is available
|
|
|
|
d, ok := c.drivers[networkType]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrInvalidNetworkDriver
|
2015-04-16 05:01:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// Check if a network already exists with the specified network name
|
2015-04-16 05:01:29 +00:00
|
|
|
c.Lock()
|
|
|
|
for _, n := range c.networks {
|
|
|
|
if n.name == name {
|
|
|
|
c.Unlock()
|
|
|
|
return nil, NetworkNameError(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// Construct the network object
|
2015-04-15 05:25:42 +00:00
|
|
|
network := &network{
|
2015-04-22 23:47:07 +00:00
|
|
|
name: name,
|
|
|
|
id: types.UUID(stringid.GenerateRandomID()),
|
|
|
|
ctrlr: c,
|
|
|
|
driver: d,
|
|
|
|
endpoints: endpointTable{},
|
2015-04-15 05:25:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// Create the network
|
2015-04-13 18:40:42 +00:00
|
|
|
if err := d.CreateNetwork(network.id, options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
// Store the network handler in controller
|
2015-04-13 18:40:42 +00:00
|
|
|
c.Lock()
|
|
|
|
c.networks[network.id] = network
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
return network, nil
|
2015-02-20 01:21:42 +00:00
|
|
|
}
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
func (n *network) Name() string {
|
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ID() string {
|
|
|
|
return string(n.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Type() string {
|
2015-04-16 05:01:29 +00:00
|
|
|
if n.driver == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
return n.driver.Type()
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Delete() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n.ctrlr.Lock()
|
2015-04-15 05:25:42 +00:00
|
|
|
_, ok := n.ctrlr.networks[n.id]
|
2015-04-13 18:40:42 +00:00
|
|
|
if !ok {
|
|
|
|
n.ctrlr.Unlock()
|
2015-04-16 05:01:29 +00:00
|
|
|
return &UnknownNetworkError{name: n.name, id: string(n.id)}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n.Lock()
|
|
|
|
numEps := len(n.endpoints)
|
|
|
|
n.Unlock()
|
|
|
|
if numEps != 0 {
|
|
|
|
n.ctrlr.Unlock()
|
2015-04-16 05:01:29 +00:00
|
|
|
return &ActiveEndpointsError{name: n.name, id: string(n.id)}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(n.ctrlr.networks, n.id)
|
|
|
|
n.ctrlr.Unlock()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.ctrlr.Lock()
|
|
|
|
n.ctrlr.networks[n.id] = n
|
|
|
|
n.ctrlr.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
err = n.driver.DeleteNetwork(n.id)
|
2015-04-13 18:40:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:42:23 +00:00
|
|
|
func (n *network) CreateEndpoint(name string, sboxKey string, options interface{}) (Endpoint, error) {
|
2015-04-13 18:40:42 +00:00
|
|
|
ep := &endpoint{name: name}
|
2015-04-20 15:44:06 +00:00
|
|
|
ep.id = types.UUID(stringid.GenerateRandomID())
|
2015-04-13 18:40:42 +00:00
|
|
|
ep.network = n
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
d := n.driver
|
2015-04-13 18:40:42 +00:00
|
|
|
sinfo, err := d.CreateEndpoint(n.id, ep.id, sboxKey, options)
|
|
|
|
if err != nil {
|
2015-04-17 22:42:23 +00:00
|
|
|
return nil, err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ep.sandboxInfo = sinfo
|
|
|
|
n.Lock()
|
|
|
|
n.endpoints[ep.id] = ep
|
|
|
|
n.Unlock()
|
2015-04-17 22:42:23 +00:00
|
|
|
return ep, nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-18 06:13:29 +00:00
|
|
|
func (n *network) Endpoints() []Endpoint {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
list := make([]Endpoint, len(n.endpoints))
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
for _, e := range n.endpoints {
|
|
|
|
list[idx] = e
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:31:29 +00:00
|
|
|
func (ep *endpoint) ID() string {
|
|
|
|
return string(ep.id)
|
|
|
|
}
|
|
|
|
|
2015-04-17 22:42:23 +00:00
|
|
|
func (ep *endpoint) Name() string {
|
|
|
|
return ep.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ep *endpoint) Network() string {
|
|
|
|
return ep.network.name
|
|
|
|
}
|
|
|
|
|
2015-04-20 15:44:06 +00:00
|
|
|
func (ep *endpoint) SandboxInfo() *sandbox.Info {
|
2015-04-17 22:42:23 +00:00
|
|
|
if ep.sandboxInfo == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ep.sandboxInfo.GetCopy()
|
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
func (ep *endpoint) Delete() error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
n := ep.network
|
|
|
|
n.Lock()
|
2015-04-15 05:25:42 +00:00
|
|
|
_, ok := n.endpoints[ep.id]
|
2015-04-13 18:40:42 +00:00
|
|
|
if !ok {
|
|
|
|
n.Unlock()
|
2015-04-16 05:01:29 +00:00
|
|
|
return &UnknownEndpointError{name: ep.name, id: string(ep.id)}
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(n.endpoints, ep.id)
|
|
|
|
n.Unlock()
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.Lock()
|
|
|
|
n.endpoints[ep.id] = ep
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-04-22 23:47:07 +00:00
|
|
|
err = n.driver.DeleteEndpoint(n.id, ep.id)
|
2015-04-13 18:40:42 +00:00
|
|
|
return err
|
2015-02-27 17:34:30 +00:00
|
|
|
}
|