2015-04-30 01:25:01 +00:00
|
|
|
/*
|
|
|
|
Package libnetwork provides the basic functionality and extension points to
|
|
|
|
create network namespaces and allocate interfaces for containers to use.
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
// Create a new controller instance
|
2015-05-23 04:52:02 +00:00
|
|
|
controller, _err := libnetwork.New("/etc/default/libnetwork.toml")
|
2015-05-01 00:57:06 +00:00
|
|
|
|
|
|
|
// Select and configure the network driver
|
|
|
|
networkType := "bridge"
|
|
|
|
|
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
2015-05-06 04:19:57 +00:00
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
2015-05-01 00:57:06 +00:00
|
|
|
err := controller.ConfigureNetworkDriver(networkType, genericOption)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a network for containers to join.
|
|
|
|
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make of
|
|
|
|
network, err := controller.NewNetwork(networkType, "network1")
|
|
|
|
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. This info is contained or accessible
|
|
|
|
// from the returned endpoint.
|
|
|
|
ep, err := network.CreateEndpoint("Endpoint1")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// A container can join the endpoint by providing the container ID to the join
|
|
|
|
// api which returns the sandbox key which can be used to access the sandbox
|
|
|
|
// created for the container during join.
|
|
|
|
// Join acceps Variadic arguments which will be made use of by libnetwork and Drivers
|
|
|
|
_, err = ep.Join("container1",
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-30 01:25:01 +00:00
|
|
|
*/
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-05-13 15:41:45 +00:00
|
|
|
"encoding/json"
|
2015-05-15 22:23:59 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-05-23 04:52:02 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-04-30 01:25:01 +00:00
|
|
|
"sync"
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-16 01:14:36 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-04-30 01:25:01 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-05-23 04:52:02 +00:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-08 13:26:35 +00:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-06 23:57:38 +00:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-05-15 22:23:59 +00:00
|
|
|
"github.com/docker/libnetwork/hostdiscovery"
|
2015-04-30 01:25:01 +00:00
|
|
|
"github.com/docker/libnetwork/sandbox"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2015-05-13 15:41:45 +00:00
|
|
|
"github.com/docker/swarm/pkg/store"
|
2015-04-30 01:25:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
|
|
// networks.
|
|
|
|
type NetworkController interface {
|
|
|
|
// ConfigureNetworkDriver applies the passed options to the driver instance for the specified network type
|
2015-05-01 00:57:06 +00:00
|
|
|
ConfigureNetworkDriver(networkType string, options map[string]interface{}) error
|
2015-04-30 01:25:01 +00:00
|
|
|
|
|
|
|
// Create a new network. The options parameter carries network specific options.
|
|
|
|
// Labels support will be added in the near future.
|
2015-05-01 00:57:06 +00:00
|
|
|
NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
|
2015-04-30 01:25:01 +00:00
|
|
|
|
|
|
|
// Networks returns the list of Network(s) managed by this controller.
|
|
|
|
Networks() []Network
|
|
|
|
|
|
|
|
// WalkNetworks uses the provided function to walk the Network(s) managed by this controller.
|
|
|
|
WalkNetworks(walker NetworkWalker)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
// NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
NetworkByName(name string) (Network, error)
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
// NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
|
2015-05-11 23:13:27 +00:00
|
|
|
NetworkByID(id string) (Network, error)
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkWalker is a client provided function which will be used to walk the Networks.
|
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
type NetworkWalker func(nw Network) bool
|
|
|
|
|
|
|
|
type sandboxData struct {
|
|
|
|
sandbox sandbox.Sandbox
|
|
|
|
refCnt int
|
|
|
|
}
|
|
|
|
|
|
|
|
type networkTable map[types.UUID]*network
|
|
|
|
type endpointTable map[types.UUID]*endpoint
|
2015-05-19 16:12:47 +00:00
|
|
|
type sandboxTable map[string]*sandboxData
|
2015-04-30 01:25:01 +00:00
|
|
|
|
|
|
|
type controller struct {
|
|
|
|
networks networkTable
|
|
|
|
drivers driverTable
|
|
|
|
sandboxes sandboxTable
|
2015-05-23 04:52:02 +00:00
|
|
|
cfg *config.Config
|
2015-05-08 13:26:35 +00:00
|
|
|
store datastore.DataStore
|
2015-04-30 01:25:01 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of network controller.
|
2015-05-23 04:52:02 +00:00
|
|
|
func New(configFile string) (NetworkController, error) {
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 12:46:29 +00:00
|
|
|
c := &controller{
|
|
|
|
networks: networkTable{},
|
|
|
|
sandboxes: sandboxTable{},
|
|
|
|
drivers: driverTable{}}
|
|
|
|
if err := initDrivers(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
|
2015-05-23 04:52:02 +00:00
|
|
|
if err := c.initConfig(configFile); err == nil {
|
|
|
|
if err := c.initDataStore(); err != nil {
|
|
|
|
// Failing to initalize datastore is a bad situation to be in.
|
|
|
|
// But it cannot fail creating the Controller
|
|
|
|
log.Warnf("Failed to Initialize Datastore due to %v. Operating in non-clustered mode", err)
|
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
if err := c.initDiscovery(); err != nil {
|
|
|
|
// Failing to initalize discovery is a bad situation to be in.
|
|
|
|
// But it cannot fail creating the Controller
|
|
|
|
log.Warnf("Failed to Initialize Discovery : %v", err)
|
|
|
|
}
|
2015-05-23 04:52:02 +00:00
|
|
|
} else {
|
|
|
|
// Missing Configuration file is not a failure scenario
|
|
|
|
// But without that, datastore cannot be initialized.
|
|
|
|
log.Debugf("Unable to Parse LibNetwork Config file : %v", err)
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-05-23 04:52:02 +00:00
|
|
|
const (
|
|
|
|
cfgFileEnv = "LIBNETWORK_CFG"
|
|
|
|
defaultCfgFile = "/etc/default/libnetwork.toml"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *controller) initConfig(configFile string) error {
|
|
|
|
cfgFile := configFile
|
|
|
|
if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
cfgFile = os.Getenv(cfgFileEnv)
|
|
|
|
if strings.Trim(cfgFile, " ") == "" {
|
|
|
|
cfgFile = defaultCfgFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cfg, err := config.ParseConfig(cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidConfigFile(cfgFile)
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
c.cfg = cfg
|
|
|
|
c.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
|
2015-05-23 04:52:02 +00:00
|
|
|
func (c *controller) initDataStore() error {
|
2015-05-15 22:23:59 +00:00
|
|
|
if c.cfg == nil {
|
|
|
|
return fmt.Errorf("datastore initialization requires a valid configuration")
|
|
|
|
}
|
|
|
|
|
2015-05-23 04:52:02 +00:00
|
|
|
store, err := datastore.NewDataStore(&c.cfg.Datastore)
|
2015-05-08 13:26:35 +00:00
|
|
|
if err != nil {
|
2015-05-13 15:41:45 +00:00
|
|
|
return err
|
2015-05-08 13:26:35 +00:00
|
|
|
}
|
2015-05-13 15:41:45 +00:00
|
|
|
c.Lock()
|
2015-05-08 13:26:35 +00:00
|
|
|
c.store = store
|
2015-05-13 15:41:45 +00:00
|
|
|
c.Unlock()
|
2015-05-23 04:52:02 +00:00
|
|
|
go c.watchNewNetworks()
|
2015-05-08 13:26:35 +00:00
|
|
|
|
2015-05-13 15:41:45 +00:00
|
|
|
return nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:23:59 +00:00
|
|
|
func (c *controller) initDiscovery() error {
|
|
|
|
if c.cfg == nil {
|
|
|
|
return fmt.Errorf("discovery initialization requires a valid configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
hostDiscovery := hostdiscovery.NewHostDiscovery()
|
|
|
|
return hostDiscovery.StartDiscovery(&c.cfg.Cluster, c.hostJoinCallback, c.hostLeaveCallback)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) hostJoinCallback(hosts []net.IP) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) hostLeaveCallback(hosts []net.IP) {
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
func (c *controller) ConfigureNetworkDriver(networkType string, options map[string]interface{}) error {
|
2015-05-06 23:57:38 +00:00
|
|
|
c.Lock()
|
2015-04-30 01:25:01 +00:00
|
|
|
d, ok := c.drivers[networkType]
|
2015-05-06 23:57:38 +00:00
|
|
|
c.Unlock()
|
2015-04-30 01:25:01 +00:00
|
|
|
if !ok {
|
|
|
|
return NetworkTypeError(networkType)
|
|
|
|
}
|
|
|
|
return d.Config(options)
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:57:38 +00:00
|
|
|
func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver) error {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
if _, ok := c.drivers[networkType]; ok {
|
|
|
|
return driverapi.ErrActiveRegistration(networkType)
|
|
|
|
}
|
|
|
|
c.drivers[networkType] = driver
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
// NewNetwork creates a new network of the specified network type. The options
|
|
|
|
// are network specific and modeled in a generic way.
|
2015-05-01 00:57:06 +00:00
|
|
|
func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, 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-04-30 01:25:01 +00:00
|
|
|
// Check if a driver for the specified network type is available
|
2015-05-06 23:57:38 +00:00
|
|
|
c.Lock()
|
2015-04-30 01:25:01 +00:00
|
|
|
d, ok := c.drivers[networkType]
|
2015-05-06 23:57:38 +00:00
|
|
|
c.Unlock()
|
2015-04-30 01:25:01 +00:00
|
|
|
if !ok {
|
2015-05-16 01:14:36 +00:00
|
|
|
var err error
|
|
|
|
d, err = c.loadDriver(networkType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a network already exists with the specified network name
|
|
|
|
c.Lock()
|
|
|
|
for _, n := range c.networks {
|
|
|
|
if n.name == name {
|
|
|
|
c.Unlock()
|
|
|
|
return nil, NetworkNameError(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
// Construct the network object
|
|
|
|
network := &network{
|
|
|
|
name: name,
|
|
|
|
id: types.UUID(stringid.GenerateRandomID()),
|
|
|
|
ctrlr: c,
|
|
|
|
driver: d,
|
|
|
|
endpoints: endpointTable{},
|
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
network.processOptions(options...)
|
2015-05-13 21:12:57 +00:00
|
|
|
if err := c.addNetworkToStore(network); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-30 01:25:01 +00:00
|
|
|
// Create the network
|
2015-05-01 00:57:06 +00:00
|
|
|
if err := d.CreateNetwork(network.id, network.generic); err != nil {
|
2015-04-30 01:25:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the network handler in controller
|
|
|
|
c.Lock()
|
|
|
|
c.networks[network.id] = network
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
return network, nil
|
|
|
|
}
|
|
|
|
|
2015-05-13 15:41:45 +00:00
|
|
|
func (c *controller) newNetworkFromStore(n *network) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if _, ok := c.drivers[n.networkType]; !ok {
|
|
|
|
log.Warnf("Network driver unavailable for type=%s. ignoring network updates for %s", n.Type(), n.Name())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.ctrlr = c
|
|
|
|
n.driver = c.drivers[n.networkType]
|
|
|
|
c.networks[n.id] = n
|
|
|
|
// TODO : Populate n.endpoints back from endpoint dbstore
|
|
|
|
}
|
|
|
|
|
2015-05-13 21:12:57 +00:00
|
|
|
func (c *controller) addNetworkToStore(n *network) error {
|
2015-05-23 04:52:02 +00:00
|
|
|
if isReservedNetwork(n.Name()) {
|
2015-05-13 21:12:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
c.Lock()
|
|
|
|
cs := c.store
|
|
|
|
c.Unlock()
|
|
|
|
if cs == nil {
|
|
|
|
log.Debugf("datastore not initialized. Network %s is not added to the store", n.Name())
|
|
|
|
return nil
|
2015-05-13 21:12:57 +00:00
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
return cs.PutObjectAtomic(n)
|
2015-05-13 21:12:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 15:41:45 +00:00
|
|
|
func (c *controller) watchNewNetworks() {
|
|
|
|
c.Lock()
|
2015-05-15 22:23:59 +00:00
|
|
|
cs := c.store
|
2015-05-13 15:41:45 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-05-15 22:23:59 +00:00
|
|
|
cs.KVStore().WatchRange(datastore.Key(datastore.NetworkKeyPrefix), "", 0, func(kvi []store.KVEntry) {
|
2015-05-13 15:41:45 +00:00
|
|
|
for _, kve := range kvi {
|
|
|
|
var n network
|
|
|
|
err := json.Unmarshal(kve.Value(), &n)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
n.dbIndex = kve.LastIndex()
|
|
|
|
c.Lock()
|
|
|
|
existing, ok := c.networks[n.id]
|
|
|
|
c.Unlock()
|
|
|
|
if ok && existing.dbIndex == n.dbIndex {
|
|
|
|
// Skip any watch notification for a network that has not changed
|
|
|
|
continue
|
2015-05-15 22:23:59 +00:00
|
|
|
} else if ok {
|
|
|
|
// Received an update for an existing network object
|
|
|
|
log.Debugf("Skipping network update for %s (%s)", n.name, n.id)
|
|
|
|
continue
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
|
2015-05-13 15:41:45 +00:00
|
|
|
c.newNetworkFromStore(&n)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
func (c *controller) Networks() []Network {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
list := make([]Network, 0, len(c.networks))
|
|
|
|
for _, n := range c.networks {
|
|
|
|
list = append(list, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) WalkNetworks(walker NetworkWalker) {
|
|
|
|
for _, n := range c.Networks() {
|
|
|
|
if walker(n) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
func (c *controller) NetworkByName(name string) (Network, 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-30 01:25:01 +00:00
|
|
|
var n Network
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
s := func(current Network) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
n = current
|
|
|
|
return true
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-05-11 23:13:27 +00:00
|
|
|
return false
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
c.WalkNetworks(s)
|
|
|
|
|
2015-05-15 23:04:09 +00:00
|
|
|
if n == nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchNetwork(name)
|
2015-05-15 23:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
return n, nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 23:13:27 +00:00
|
|
|
func (c *controller) NetworkByID(id string) (Network, 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-30 01:25:01 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
if n, ok := c.networks[types.UUID(id)]; ok {
|
2015-05-11 23:13:27 +00:00
|
|
|
return n, nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrNoSuchNetwork(id)
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 20:29:43 +00:00
|
|
|
func (c *controller) sandboxAdd(key string, create bool) (sandbox.Sandbox, error) {
|
2015-04-30 01:25:01 +00:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
sData, ok := c.sandboxes[key]
|
|
|
|
if !ok {
|
2015-05-03 20:29:43 +00:00
|
|
|
sb, err := sandbox.NewSandbox(key, create)
|
2015-04-30 01:25:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-19 16:12:47 +00:00
|
|
|
sData = &sandboxData{sandbox: sb, refCnt: 1}
|
2015-04-30 01:25:01 +00:00
|
|
|
c.sandboxes[key] = sData
|
|
|
|
return sData.sandbox, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sData.refCnt++
|
|
|
|
return sData.sandbox, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) sandboxRm(key string) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
sData := c.sandboxes[key]
|
|
|
|
sData.refCnt--
|
|
|
|
|
|
|
|
if sData.refCnt == 0 {
|
|
|
|
sData.sandbox.Destroy()
|
|
|
|
delete(c.sandboxes, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) sandboxGet(key string) sandbox.Sandbox {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
sData, ok := c.sandboxes[key]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return sData.sandbox
|
|
|
|
}
|
2015-05-16 01:14:36 +00:00
|
|
|
|
|
|
|
func (c *controller) loadDriver(networkType string) (driverapi.Driver, error) {
|
|
|
|
// Plugins pkg performs lazy loading of plugins that acts as remote drivers.
|
|
|
|
// As per the design, this Get call will result in remote driver discovery if there is a corresponding plugin available.
|
|
|
|
_, err := plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
|
|
|
|
if err != nil {
|
2015-05-14 21:56:15 +00:00
|
|
|
if err == plugins.ErrNotFound {
|
|
|
|
return nil, types.NotFoundErrorf(err.Error())
|
|
|
|
}
|
2015-05-16 01:14:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
d, ok := c.drivers[networkType]
|
|
|
|
if !ok {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidNetworkDriver(networkType)
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|