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/types"
|
2015-04-13 18:40:42 +00:00
|
|
|
)
|
2015-02-27 17:34:30 +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-28 05:57:36 +00:00
|
|
|
CreateEndpoint(name string, options interface{}) (Endpoint, error)
|
2015-03-20 20:54:49 +00:00
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
// Delete the network.
|
|
|
|
Delete() error
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// Endpoints returns the list of Endpoint(s) in this network.
|
2015-04-18 06:13:29 +00:00
|
|
|
Endpoints() []Endpoint
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// WalkEndpoints uses the provided function to walk the Endpoints
|
|
|
|
WalkEndpoints(walker EndpointWalker)
|
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
// EndpointByName returns the Endpoint which has the passed name, if it exists otherwise nil is returned
|
|
|
|
EndpointByName(name string) Endpoint
|
|
|
|
|
|
|
|
// EndpointByID returns the Endpoint which has the passed id, if it exists otherwise nil is returned
|
|
|
|
EndpointByID(id string) Endpoint
|
2015-03-02 18:17:12 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
// EndpointWalker is a client provided function which will be used to walk the Endpoints.
|
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
type EndpointWalker func(ep Endpoint) bool
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 05:57:36 +00:00
|
|
|
func (n *network) CreateEndpoint(name 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-28 05:57:36 +00:00
|
|
|
sinfo, err := d.CreateEndpoint(n.id, ep.id, options)
|
2015-04-13 18:40:42 +00:00
|
|
|
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()
|
2015-04-20 16:46:11 +00:00
|
|
|
list := make([]Endpoint, 0, len(n.endpoints))
|
2015-04-18 06:13:29 +00:00
|
|
|
for _, e := range n.endpoints {
|
2015-04-20 16:46:11 +00:00
|
|
|
list = append(list, e)
|
2015-04-18 06:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2015-04-20 16:46:11 +00:00
|
|
|
func (n *network) WalkEndpoints(walker EndpointWalker) {
|
|
|
|
for _, e := range n.Endpoints() {
|
|
|
|
if walker(e) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 19:05:33 +00:00
|
|
|
func (n *network) EndpointByName(name string) Endpoint {
|
|
|
|
var e Endpoint
|
|
|
|
|
|
|
|
if name != "" {
|
|
|
|
s := func(current Endpoint) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
e = current
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
n.WalkEndpoints(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) EndpointByID(id string) Endpoint {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
if e, ok := n.endpoints[types.UUID(id)]; ok {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|