2015-04-29 21:25:01 -04:00
|
|
|
/*
|
|
|
|
Package libnetwork provides the basic functionality and extension points to
|
|
|
|
create network namespaces and allocate interfaces for containers to use.
|
|
|
|
|
2015-06-15 22:09:59 -04:00
|
|
|
// Create a new controller instance
|
|
|
|
controller, _err := libnetwork.New(nil)
|
|
|
|
|
|
|
|
// Select and configure the network driver
|
|
|
|
networkType := "bridge"
|
|
|
|
|
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
|
|
|
err := controller.ConfigureNetworkDriver(networkType, genericOption)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a network for containers to join.
|
2015-08-25 12:41:32 -04:00
|
|
|
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make use of
|
2015-06-15 22:09:59 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-25 12:41:32 -04:00
|
|
|
// A container can join the endpoint by providing the container ID to the join api.
|
2015-06-26 06:01:23 -04:00
|
|
|
// Join accepts Variadic arguments which will be made use of by libnetwork and Drivers
|
2015-06-15 22:09:59 -04:00
|
|
|
err = ep.Join("container1",
|
|
|
|
libnetwork.JoinOptionHostname("test"),
|
|
|
|
libnetwork.JoinOptionDomainname("docker.io"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
*/
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-07-02 01:00:48 -04:00
|
|
|
"container/heap"
|
2015-05-15 18:23:59 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-04-29 21:25:01 -04:00
|
|
|
"sync"
|
|
|
|
|
2015-05-08 09:26:35 -04:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-15 21:14:36 -04:00
|
|
|
"github.com/docker/docker/pkg/plugins"
|
2015-04-29 21:25:01 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-05-23 00:52:02 -04:00
|
|
|
"github.com/docker/libnetwork/config"
|
2015-05-08 09:26:35 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-05-06 19:57:38 -04:00
|
|
|
"github.com/docker/libnetwork/driverapi"
|
2015-05-15 18:23:59 -04:00
|
|
|
"github.com/docker/libnetwork/hostdiscovery"
|
2015-07-02 01:00:48 -04:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-04-29 21:25:01 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
|
|
// networks.
|
|
|
|
type NetworkController interface {
|
2015-09-09 17:24:05 -04:00
|
|
|
// ID provides an unique identity for the controller
|
|
|
|
ID() string
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
// Config method returns the bootup configuration for the controller
|
|
|
|
Config() config.Config
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
// Create a new network. The options parameter carries network specific options.
|
|
|
|
// Labels support will be added in the near future.
|
2015-04-30 20:57:06 -04:00
|
|
|
NewNetwork(networkType, name string, options ...NetworkOption) (Network, error)
|
2015-04-29 21:25:01 -04: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 19:04:09 -04:00
|
|
|
// NetworkByName returns the Network which has the passed name. If not found, the error ErrNoSuchNetwork is returned.
|
2015-05-11 19:13:27 -04:00
|
|
|
NetworkByName(name string) (Network, error)
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2015-05-15 19:04:09 -04:00
|
|
|
// NetworkByID returns the Network which has the passed id. If not found, the error ErrNoSuchNetwork is returned.
|
2015-05-11 19:13:27 -04:00
|
|
|
NetworkByID(id string) (Network, error)
|
2015-06-05 14:46:33 -04:00
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// NewSandbox cretes a new network sandbox for the passed container id
|
|
|
|
NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error)
|
|
|
|
|
|
|
|
// Sandboxes returns the list of Sandbox(s) managed by this controller.
|
|
|
|
Sandboxes() []Sandbox
|
|
|
|
|
|
|
|
// WlakSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
|
|
|
|
WalkSandboxes(walker SandboxWalker)
|
|
|
|
|
|
|
|
// SandboxByID returns the Sandbox which has the passed id. If not found, a types.NotFoundError is returned.
|
|
|
|
SandboxByID(id string) (Sandbox, error)
|
2015-06-19 21:41:31 -04:00
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
// Stop network controller
|
|
|
|
Stop()
|
2015-04-29 21:25:01 -04: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
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// SandboxWalker is a client provided function which will be used to walk the Sandboxes.
|
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
type SandboxWalker func(sb Sandbox) bool
|
|
|
|
|
2015-06-06 13:21:51 -04:00
|
|
|
type driverData struct {
|
|
|
|
driver driverapi.Driver
|
|
|
|
capability driverapi.Capability
|
|
|
|
}
|
|
|
|
|
|
|
|
type driverTable map[string]*driverData
|
2015-07-02 01:00:48 -04:00
|
|
|
type networkTable map[string]*network
|
|
|
|
type endpointTable map[string]*endpoint
|
|
|
|
type sandboxTable map[string]*sandbox
|
2015-04-29 21:25:01 -04:00
|
|
|
|
|
|
|
type controller struct {
|
2015-09-16 07:39:46 -04:00
|
|
|
id string
|
|
|
|
networks networkTable
|
|
|
|
drivers driverTable
|
|
|
|
sandboxes sandboxTable
|
|
|
|
cfg *config.Config
|
|
|
|
globalStore, localStore datastore.DataStore
|
|
|
|
extKeyListener net.Listener
|
2015-04-29 21:25:01 -04:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of network controller.
|
2015-06-11 09:52:46 -04:00
|
|
|
func New(cfgOptions ...config.Option) (NetworkController, error) {
|
|
|
|
var cfg *config.Config
|
|
|
|
if len(cfgOptions) > 0 {
|
2015-09-18 17:00:36 -04:00
|
|
|
cfg = &config.Config{
|
|
|
|
Daemon: config.DaemonCfg{
|
|
|
|
DriverCfg: make(map[string]interface{}),
|
|
|
|
},
|
|
|
|
}
|
2015-06-11 09:52:46 -04:00
|
|
|
cfg.ProcessOptions(cfgOptions...)
|
|
|
|
}
|
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 08:46:29 -04:00
|
|
|
c := &controller{
|
2015-09-09 17:24:05 -04:00
|
|
|
id: stringid.GenerateRandomID(),
|
2015-06-11 09:52:46 -04:00
|
|
|
cfg: cfg,
|
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 08:46:29 -04:00
|
|
|
networks: networkTable{},
|
|
|
|
sandboxes: sandboxTable{},
|
|
|
|
drivers: driverTable{}}
|
|
|
|
if err := initDrivers(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-08 09:26:35 -04:00
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
if cfg != nil {
|
2015-09-16 07:39:46 -04:00
|
|
|
if err := c.initGlobalStore(); err != nil {
|
2015-05-23 00:52:02 -04:00
|
|
|
// Failing to initalize datastore is a bad situation to be in.
|
|
|
|
// But it cannot fail creating the Controller
|
2015-06-11 09:52:46 -04:00
|
|
|
log.Debugf("Failed to Initialize Datastore due to %v. Operating in non-clustered mode", err)
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
2015-05-15 18:23:59 -04: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
|
2015-06-11 09:52:46 -04:00
|
|
|
log.Debugf("Failed to Initialize Discovery : %v", err)
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
2015-09-16 07:39:46 -04:00
|
|
|
if err := c.initLocalStore(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to Initialize LocalDatastore due to %v.", err)
|
|
|
|
}
|
2015-05-13 11:41:45 -04:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
if err := c.startExternalKeyListener(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-13 11:41:45 -04:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
func (c *controller) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2015-06-11 09:52:46 -04:00
|
|
|
func (c *controller) validateHostDiscoveryConfig() bool {
|
|
|
|
if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" {
|
|
|
|
return false
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
2015-06-11 09:52:46 -04:00
|
|
|
return true
|
2015-05-23 00:52:02 -04:00
|
|
|
}
|
2015-05-08 09:26:35 -04:00
|
|
|
|
2015-05-15 18:23:59 -04: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-06-11 09:52:46 -04:00
|
|
|
func (c *controller) Config() config.Config {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2015-06-13 17:28:34 -04:00
|
|
|
if c.cfg == nil {
|
|
|
|
return config.Config{}
|
|
|
|
}
|
2015-06-11 09:52:46 -04:00
|
|
|
return *c.cfg
|
|
|
|
}
|
|
|
|
|
2015-06-06 13:21:51 -04:00
|
|
|
func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
|
2015-06-14 12:00:27 -04:00
|
|
|
if !config.IsValidName(networkType) {
|
|
|
|
return ErrInvalidName(networkType)
|
|
|
|
}
|
2015-09-11 14:02:16 -04:00
|
|
|
|
|
|
|
c.Lock()
|
2015-05-06 19:57:38 -04:00
|
|
|
if _, ok := c.drivers[networkType]; ok {
|
2015-06-15 22:09:59 -04:00
|
|
|
c.Unlock()
|
2015-05-06 19:57:38 -04:00
|
|
|
return driverapi.ErrActiveRegistration(networkType)
|
|
|
|
}
|
2015-06-06 13:21:51 -04:00
|
|
|
c.drivers[networkType] = &driverData{driver, capability}
|
2015-06-15 22:09:59 -04:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-05-06 19:57:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
// NewNetwork creates a new network of the specified network type. The options
|
|
|
|
// are network specific and modeled in a generic way.
|
2015-04-30 20:57:06 -04:00
|
|
|
func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
|
2015-06-14 12:00:27 -04:00
|
|
|
if !config.IsValidName(name) {
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-07 22:59:06 -04:00
|
|
|
}
|
2015-04-29 21:25:01 -04: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{
|
2015-05-29 23:42:23 -04:00
|
|
|
name: name,
|
|
|
|
networkType: networkType,
|
2015-07-02 01:00:48 -04:00
|
|
|
id: stringid.GenerateRandomID(),
|
2015-05-29 23:42:23 -04:00
|
|
|
ctrlr: c,
|
|
|
|
endpoints: endpointTable{},
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
network.processOptions(options...)
|
2015-05-31 14:49:11 -04:00
|
|
|
|
|
|
|
if err := c.addNetwork(network); err != nil {
|
2015-04-29 21:25:01 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:42:35 -04:00
|
|
|
if err := c.updateToStore(network); err != nil {
|
2015-06-18 18:13:38 -04:00
|
|
|
log.Warnf("couldnt create network %s: %v", network.name, err)
|
2015-06-05 16:31:12 -04:00
|
|
|
if e := network.Delete(); e != nil {
|
|
|
|
log.Warnf("couldnt cleanup network %s: %v", network.name, err)
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
return network, nil
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (c *controller) addNetwork(n *network) error {
|
2015-05-13 11:41:45 -04:00
|
|
|
|
2015-05-15 18:23:59 -04:00
|
|
|
c.Lock()
|
2015-05-31 14:49:11 -04:00
|
|
|
// Check if a driver for the specified network type is available
|
2015-06-06 13:21:51 -04:00
|
|
|
dd, ok := c.drivers[n.networkType]
|
2015-05-15 18:23:59 -04:00
|
|
|
c.Unlock()
|
2015-05-29 23:42:23 -04:00
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
if !ok {
|
|
|
|
var err error
|
2015-06-06 13:21:51 -04:00
|
|
|
dd, err = c.loadDriver(n.networkType)
|
2015-05-31 14:49:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
|
2015-06-06 13:21:51 -04:00
|
|
|
n.Lock()
|
2015-06-19 02:40:17 -04:00
|
|
|
n.svcRecords = svcMap{}
|
2015-06-06 13:21:51 -04:00
|
|
|
n.driver = dd.driver
|
2015-09-16 07:39:46 -04:00
|
|
|
n.dataScope = dd.capability.DataScope
|
2015-06-06 13:21:51 -04:00
|
|
|
d := n.driver
|
|
|
|
n.Unlock()
|
2015-05-13 17:12:57 -04:00
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
// Create the network
|
|
|
|
if err := d.CreateNetwork(n.id, n.generic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-16 07:42:35 -04:00
|
|
|
if n.isGlobalScoped() {
|
|
|
|
if err := n.watchEndpoints(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-17 12:13:31 -04:00
|
|
|
}
|
2015-05-13 11:41:45 -04:00
|
|
|
c.Lock()
|
2015-05-31 14:49:11 -04:00
|
|
|
c.networks[n.id] = n
|
2015-05-13 11:41:45 -04:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-05-29 23:42:23 -04:00
|
|
|
return nil
|
2015-05-13 11:41:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04: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 19:13:27 -04:00
|
|
|
func (c *controller) NetworkByName(name string) (Network, error) {
|
|
|
|
if name == "" {
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-11 19:13:27 -04:00
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
var n Network
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
s := func(current Network) bool {
|
|
|
|
if current.Name() == name {
|
|
|
|
n = current
|
|
|
|
return true
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-05-11 19:13:27 -04:00
|
|
|
return false
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
c.WalkNetworks(s)
|
|
|
|
|
2015-05-15 19:04:09 -04:00
|
|
|
if n == nil {
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrNoSuchNetwork(name)
|
2015-05-15 19:04:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
return n, nil
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:13:27 -04:00
|
|
|
func (c *controller) NetworkByID(id string) (Network, error) {
|
|
|
|
if id == "" {
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrInvalidID(id)
|
2015-05-11 19:13:27 -04:00
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2015-07-02 01:00:48 -04:00
|
|
|
if n, ok := c.networks[id]; ok {
|
2015-05-11 19:13:27 -04:00
|
|
|
return n, nil
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrNoSuchNetwork(id)
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// NewSandbox creates a new sandbox for the passed container id
|
|
|
|
func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if containerID == "" {
|
|
|
|
return nil, types.BadRequestErrorf("invalid container ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
var existing Sandbox
|
|
|
|
look := SandboxContainerWalker(&existing, containerID)
|
|
|
|
c.WalkSandboxes(look)
|
|
|
|
if existing != nil {
|
|
|
|
return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, existing)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create sandbox and process options first. Key generation depends on an option
|
|
|
|
sb := &sandbox{
|
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
containerID: containerID,
|
|
|
|
endpoints: epHeap{},
|
|
|
|
epPriority: map[string]int{},
|
|
|
|
config: containerConfig{},
|
|
|
|
controller: c,
|
|
|
|
}
|
|
|
|
// This sandbox may be using an existing osl sandbox, sharing it with another sandbox
|
|
|
|
var peerSb Sandbox
|
|
|
|
c.WalkSandboxes(SandboxKeyWalker(&peerSb, sb.Key()))
|
|
|
|
if peerSb != nil {
|
|
|
|
sb.osSbox = peerSb.(*sandbox).osSbox
|
|
|
|
}
|
|
|
|
|
|
|
|
heap.Init(&sb.endpoints)
|
|
|
|
|
|
|
|
sb.processOptions(options...)
|
|
|
|
|
2015-09-01 21:55:53 -04:00
|
|
|
if err = sb.setupResolutionFiles(); err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:20:54 -04:00
|
|
|
if sb.osSbox == nil && !sb.config.useExternalKey {
|
2015-07-02 01:00:48 -04:00
|
|
|
if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
c.sandboxes[sb.id] = sb
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
return sb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) Sandboxes() []Sandbox {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
list := make([]Sandbox, 0, len(c.sandboxes))
|
|
|
|
for _, s := range c.sandboxes {
|
|
|
|
list = append(list, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) WalkSandboxes(walker SandboxWalker) {
|
|
|
|
for _, sb := range c.Sandboxes() {
|
|
|
|
if walker(sb) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) SandboxByID(id string) (Sandbox, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, ErrInvalidID(id)
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
s, ok := c.sandboxes[id]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, types.NotFoundErrorf("sandbox %s not found", id)
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SandboxContainerWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed containerID
|
|
|
|
func SandboxContainerWalker(out *Sandbox, containerID string) SandboxWalker {
|
|
|
|
return func(sb Sandbox) bool {
|
|
|
|
if sb.ContainerID() == containerID {
|
|
|
|
*out = sb
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SandboxKeyWalker returns a Sandbox Walker function which looks for an existing Sandbox with the passed key
|
|
|
|
func SandboxKeyWalker(out *Sandbox, key string) SandboxWalker {
|
|
|
|
return func(sb Sandbox) bool {
|
|
|
|
if sb.Key() == key {
|
|
|
|
*out = sb
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-06 13:21:51 -04:00
|
|
|
func (c *controller) loadDriver(networkType string) (*driverData, error) {
|
2015-05-15 21:14:36 -04:00
|
|
|
// 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 17:56:15 -04:00
|
|
|
if err == plugins.ErrNotFound {
|
|
|
|
return nil, types.NotFoundErrorf(err.Error())
|
|
|
|
}
|
2015-05-15 21:14:36 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2015-06-06 13:21:51 -04:00
|
|
|
dd, ok := c.drivers[networkType]
|
2015-05-15 21:14:36 -04:00
|
|
|
if !ok {
|
2015-05-14 17:56:15 -04:00
|
|
|
return nil, ErrInvalidNetworkDriver(networkType)
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
2015-06-06 13:21:51 -04:00
|
|
|
return dd, nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
func (c *controller) Stop() {
|
|
|
|
c.stopExternalKeyListener()
|
2015-07-02 01:00:48 -04:00
|
|
|
osl.GC()
|
2015-06-05 14:46:33 -04:00
|
|
|
}
|