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 (
|
2015-05-08 13:26:35 +00:00
|
|
|
"encoding/json"
|
2015-05-13 21:12:57 +00:00
|
|
|
"strings"
|
2015-04-13 18:40:42 +00:00
|
|
|
"sync"
|
2015-02-27 17:34:30 +00:00
|
|
|
|
2015-06-10 20:27:23 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-04-18 04:37:54 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-05-15 22:23:59 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-04-13 18:40:42 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-05-16 23:02:51 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
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-05-01 00:57:06 +00:00
|
|
|
CreateEndpoint(name string, options ...EndpointOption) (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-05-15 23:04:09 +00:00
|
|
|
// EndpointByName returns the Endpoint which has the passed name. If not found, the error ErrNoSuchEndpoint is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
EndpointByName(name string) (Endpoint, error)
|
2015-04-24 19:05:33 +00:00
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
// EndpointByID returns the Endpoint which has the passed id. If not found, the error ErrNoSuchEndpoint is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
EndpointByID(id string) (Endpoint, error)
|
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-05-06 06:41:20 +00:00
|
|
|
enableIPv6 bool
|
2015-04-15 05:25:42 +00:00
|
|
|
endpoints endpointTable
|
2015-05-01 00:57:06 +00:00
|
|
|
generic options.Generic
|
2015-05-08 13:26:35 +00:00
|
|
|
dbIndex uint64
|
2015-04-13 18:40:42 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Name() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return n.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) ID() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
return string(n.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Type() string {
|
2015-05-28 23:29:21 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
func (n *network) Key() []string {
|
2015-05-15 22:23:59 +00:00
|
|
|
return []string{datastore.NetworkKeyPrefix, string(n.id)}
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
func (n *network) KeyPrefix() []string {
|
|
|
|
return []string{datastore.NetworkKeyPrefix}
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
func (n *network) Value() []byte {
|
|
|
|
b, err := json.Marshal(n)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) Index() uint64 {
|
|
|
|
return n.dbIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) SetIndex(index uint64) {
|
|
|
|
n.dbIndex = index
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
|
|
|
func (n *network) MarshalJSON() ([]byte, error) {
|
|
|
|
netMap := make(map[string]interface{})
|
|
|
|
netMap["name"] = n.name
|
|
|
|
netMap["id"] = string(n.id)
|
|
|
|
netMap["networkType"] = n.networkType
|
|
|
|
netMap["enableIPv6"] = n.enableIPv6
|
|
|
|
netMap["generic"] = n.generic
|
|
|
|
return json.Marshal(netMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : Can be made much more generic with the help of reflection (but has some golang limitations)
|
|
|
|
func (n *network) UnmarshalJSON(b []byte) (err error) {
|
|
|
|
var netMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &netMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.name = netMap["name"].(string)
|
2015-05-13 15:41:45 +00:00
|
|
|
n.id = types.UUID(netMap["id"].(string))
|
2015-05-08 13:26:35 +00:00
|
|
|
n.networkType = netMap["networkType"].(string)
|
|
|
|
n.enableIPv6 = netMap["enableIPv6"].(bool)
|
2015-05-13 15:41:45 +00:00
|
|
|
if netMap["generic"] != nil {
|
|
|
|
n.generic = netMap["generic"].(map[string]interface{})
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
// NetworkOption is a option setter function type used to pass varios options to
|
|
|
|
// NewNetwork method. The various setter functions of type NetworkOption are
|
|
|
|
// provided by libnetwork, they look like NetworkOptionXXXX(...)
|
|
|
|
type NetworkOption func(n *network)
|
|
|
|
|
|
|
|
// NetworkOptionGeneric function returns an option setter for a Generic option defined
|
|
|
|
// in a Dictionary of Key-Value pair
|
|
|
|
func NetworkOptionGeneric(generic map[string]interface{}) NetworkOption {
|
|
|
|
return func(n *network) {
|
|
|
|
n.generic = generic
|
2015-05-06 06:41:20 +00:00
|
|
|
if _, ok := generic[netlabel.EnableIPv6]; ok {
|
|
|
|
n.enableIPv6 = generic[netlabel.EnableIPv6].(bool)
|
|
|
|
}
|
2015-05-01 00:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) processOptions(options ...NetworkOption) {
|
|
|
|
for _, opt := range options {
|
|
|
|
if opt != nil {
|
|
|
|
opt(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 18:40:42 +00:00
|
|
|
func (n *network) Delete() 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
|
|
|
}
|
2015-06-01 16:43:24 +00:00
|
|
|
n.ctrlr.Unlock()
|
|
|
|
|
|
|
|
if err = n.deleteNetwork(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-06-01 16:43:24 +00:00
|
|
|
if err = n.ctrlr.deleteNetworkFromStore(n); err != nil {
|
|
|
|
log.Warnf("Delete network (%s - %v) failed from datastore : %v", n.name, n.id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *network) deleteNetwork() error {
|
|
|
|
var err error
|
|
|
|
n.ctrlr.Lock()
|
2015-04-13 18:40:42 +00:00
|
|
|
delete(n.ctrlr.networks, n.id)
|
|
|
|
n.ctrlr.Unlock()
|
2015-06-10 20:27:23 +00:00
|
|
|
if err := n.driver.DeleteNetwork(n.id); err != nil {
|
|
|
|
// Forbidden Errors should be honored
|
|
|
|
if _, ok := err.(types.ForbiddenError); ok {
|
2015-04-13 18:40:42 +00:00
|
|
|
n.ctrlr.Lock()
|
|
|
|
n.ctrlr.networks[n.id] = n
|
|
|
|
n.ctrlr.Unlock()
|
2015-06-10 20:27:23 +00:00
|
|
|
return err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
2015-06-10 20:27:23 +00:00
|
|
|
log.Warnf("driver error deleting network %s : %v", n.name, err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (n *network) addEndpoint(ep *endpoint) error {
|
|
|
|
var err error
|
|
|
|
n.Lock()
|
|
|
|
n.endpoints[ep.id] = ep
|
|
|
|
d := n.driver
|
|
|
|
n.Unlock()
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
n.Lock()
|
|
|
|
delete(n.endpoints, ep.id)
|
|
|
|
n.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = d.CreateEndpoint(n.id, ep.id, ep, ep.generic)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
|
2015-05-08 02:59:06 +00:00
|
|
|
if name == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-08 02:59:06 +00:00
|
|
|
}
|
2015-06-04 16:41:42 +00:00
|
|
|
|
|
|
|
if _, err := n.EndpointByName(name); err == nil {
|
|
|
|
return nil, types.ForbiddenErrorf("service endpoint with name %s already exists", name)
|
|
|
|
}
|
|
|
|
|
2015-05-14 06:23:45 +00:00
|
|
|
ep := &endpoint{name: name, iFaces: []*endpointInterface{}, generic: make(map[string]interface{})}
|
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-05-01 00:57:06 +00:00
|
|
|
ep.processOptions(options...)
|
2015-04-13 18:40:42 +00:00
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
if err := n.addEndpoint(ep); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := n.ctrlr.addEndpointToStore(ep); err != nil {
|
2015-04-17 22:42:23 +00:00
|
|
|
return nil, err
|
2015-04-13 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
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-05-11 23:13:27 +00:00
|
|
|
func (n *network) EndpointByName(name string) (Endpoint, error) {
|
|
|
|
if name == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
var e Endpoint
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
s := func(current Endpoint) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
e = current
|
|
|
|
return true
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
return false
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
if e == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchEndpoint(name)
|
2015-05-15 23:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
return e, nil
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
func (n *network) EndpointByID(id string) (Endpoint, error) {
|
|
|
|
if id == "" {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidID(id)
|
2015-05-11 23:13:27 +00:00
|
|
|
}
|
2015-04-24 19:05:33 +00:00
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
if e, ok := n.endpoints[types.UUID(id)]; ok {
|
2015-05-11 23:13:27 +00:00
|
|
|
return e, nil
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchEndpoint(id)
|
2015-04-24 19:05:33 +00:00
|
|
|
}
|
2015-05-13 21:12:57 +00:00
|
|
|
|
2015-05-23 04:52:02 +00:00
|
|
|
func isReservedNetwork(name string) bool {
|
2015-05-13 21:12:57 +00:00
|
|
|
reserved := []string{"bridge", "none", "host"}
|
|
|
|
for _, r := range reserved {
|
|
|
|
if strings.EqualFold(r, name) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|