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-06-16 02:09:59 +00:00
|
|
|
networkType := "bridge"
|
|
|
|
|
2015-09-22 15:27:11 +00:00
|
|
|
// Create a new controller instance
|
2015-06-16 02:09:59 +00:00
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
2015-09-22 15:27:11 +00:00
|
|
|
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
|
2015-06-16 02:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a network for containers to join.
|
2015-08-25 16:41:32 +00:00
|
|
|
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make use of
|
2015-06-16 02:09:59 +00: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-09-22 15:27:11 +00:00
|
|
|
// Create the sandbox for the container.
|
|
|
|
// NewSandbox accepts Variadic optional arguments which libnetwork can use.
|
|
|
|
sbx, err := controller.NewSandbox("container1",
|
|
|
|
libnetwork.OptionHostname("test"),
|
|
|
|
libnetwork.OptionDomainname("docker.io"))
|
|
|
|
|
|
|
|
// A sandbox can join the endpoint via the join api.
|
|
|
|
err = ep.Join(sbx)
|
2015-06-16 02:09:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-30 01:25:01 +00:00
|
|
|
*/
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-07-02 05:00:48 +00:00
|
|
|
"container/heap"
|
2015-05-15 22:23:59 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2015-09-18 19:54:08 +00:00
|
|
|
"strings"
|
2015-04-30 01:25:01 +00:00
|
|
|
"sync"
|
|
|
|
|
2015-05-08 13:26:35 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-18 19:54:08 +00:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
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-09-22 20:20:55 +00:00
|
|
|
"github.com/docker/libnetwork/ipamapi"
|
2015-10-13 17:37:53 +00:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-07-02 05:00:48 +00:00
|
|
|
"github.com/docker/libnetwork/osl"
|
2015-04-30 01:25:01 +00:00
|
|
|
"github.com/docker/libnetwork/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
|
|
// networks.
|
|
|
|
type NetworkController interface {
|
2015-09-09 21:24:05 +00:00
|
|
|
// ID provides an unique identity for the controller
|
|
|
|
ID() string
|
|
|
|
|
2015-06-11 13:52:46 +00:00
|
|
|
// Config method returns the bootup configuration for the controller
|
|
|
|
Config() config.Config
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
// Create a new network. The options parameter carries network specific options.
|
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-06-05 18:46:33 +00:00
|
|
|
|
2015-07-02 05:00:48 +00: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-20 01:41:31 +00:00
|
|
|
|
2015-11-02 07:03:26 +00:00
|
|
|
// SandboxDestroy destroys a sandbox given a container ID
|
|
|
|
SandboxDestroy(id string) error
|
|
|
|
|
2015-09-09 21:24:05 +00:00
|
|
|
// Stop network controller
|
|
|
|
Stop()
|
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
|
|
|
|
|
2015-07-02 05:00:48 +00: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 17:21:51 +00:00
|
|
|
type driverData struct {
|
|
|
|
driver driverapi.Driver
|
|
|
|
capability driverapi.Capability
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
type ipamData struct {
|
2015-12-10 02:09:58 +00:00
|
|
|
driver ipamapi.Ipam
|
|
|
|
capability *ipamapi.Capability
|
2015-09-22 20:20:55 +00:00
|
|
|
// default address spaces are provided by ipam driver at registration time
|
|
|
|
defaultLocalAddressSpace, defaultGlobalAddressSpace string
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:51 +00:00
|
|
|
type driverTable map[string]*driverData
|
2015-10-05 11:21:15 +00:00
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
type ipamTable map[string]*ipamData
|
2015-07-02 05:00:48 +00:00
|
|
|
type sandboxTable map[string]*sandbox
|
2015-04-30 01:25:01 +00:00
|
|
|
|
|
|
|
type controller struct {
|
2015-10-25 13:16:09 +00:00
|
|
|
id string
|
2015-10-05 11:21:15 +00:00
|
|
|
drivers driverTable
|
|
|
|
ipamDrivers ipamTable
|
|
|
|
sandboxes sandboxTable
|
|
|
|
cfg *config.Config
|
|
|
|
stores []datastore.DataStore
|
|
|
|
discovery hostdiscovery.HostDiscovery
|
|
|
|
extKeyListener net.Listener
|
|
|
|
watchCh chan *endpoint
|
|
|
|
unWatchCh chan *endpoint
|
2015-12-24 09:51:32 +00:00
|
|
|
svcDb map[string]svcInfo
|
2015-10-23 03:18:25 +00:00
|
|
|
nmap map[string]*netWatch
|
2015-11-01 06:15:15 +00:00
|
|
|
defOsSbox osl.Sandbox
|
|
|
|
sboxOnce sync.Once
|
2015-04-30 01:25:01 +00:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of network controller.
|
2015-06-11 13:52:46 +00:00
|
|
|
func New(cfgOptions ...config.Option) (NetworkController, error) {
|
|
|
|
var cfg *config.Config
|
2015-10-05 11:21:15 +00:00
|
|
|
cfg = &config.Config{
|
|
|
|
Daemon: config.DaemonCfg{
|
|
|
|
DriverCfg: make(map[string]interface{}),
|
|
|
|
},
|
|
|
|
Scopes: make(map[string]*datastore.ScopeCfg),
|
|
|
|
}
|
|
|
|
|
2015-06-11 13:52:46 +00:00
|
|
|
if len(cfgOptions) > 0 {
|
|
|
|
cfg.ProcessOptions(cfgOptions...)
|
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
cfg.LoadDefaultScopes(cfg.Daemon.DataDir)
|
|
|
|
|
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{
|
2015-09-22 20:20:55 +00:00
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
cfg: cfg,
|
|
|
|
sandboxes: sandboxTable{},
|
|
|
|
drivers: driverTable{},
|
2015-10-05 11:21:15 +00:00
|
|
|
ipamDrivers: ipamTable{},
|
2015-12-24 09:51:32 +00:00
|
|
|
svcDb: make(map[string]svcInfo),
|
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
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if err := c.initStores(); err != nil {
|
2015-09-22 20:20:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
if cfg != nil && cfg.Cluster.Watcher != nil {
|
2015-09-18 19:54:08 +00:00
|
|
|
if err := c.initDiscovery(cfg.Cluster.Watcher); err != nil {
|
2015-05-15 22:23:59 +00:00
|
|
|
// Failing to initalize discovery is a bad situation to be in.
|
|
|
|
// But it cannot fail creating the Controller
|
2015-10-26 10:13:34 +00:00
|
|
|
log.Errorf("Failed to Initialize Discovery : %v", err)
|
2015-05-15 22:23:59 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDrivers(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initIpams(c, c.getStore(datastore.LocalScope),
|
|
|
|
c.getStore(datastore.GlobalScope)); err != nil {
|
|
|
|
return nil, err
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 03:01:38 +00:00
|
|
|
c.sandboxCleanup()
|
2015-10-19 20:20:23 +00:00
|
|
|
c.cleanupLocalEndpoints()
|
2015-10-08 03:01:38 +00:00
|
|
|
|
2015-09-09 21:24:05 +00:00
|
|
|
if err := c.startExternalKeyListener(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-13 15:41:45 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:24:05 +00:00
|
|
|
func (c *controller) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2015-06-11 13:52:46 +00:00
|
|
|
func (c *controller) validateHostDiscoveryConfig() bool {
|
|
|
|
if c.cfg == nil || c.cfg.Cluster.Discovery == "" || c.cfg.Cluster.Address == "" {
|
|
|
|
return false
|
2015-05-23 04:52:02 +00:00
|
|
|
}
|
2015-06-11 13:52:46 +00:00
|
|
|
return true
|
2015-05-23 04:52:02 +00:00
|
|
|
}
|
2015-05-08 13:26:35 +00:00
|
|
|
|
2015-09-18 19:54:08 +00:00
|
|
|
func (c *controller) initDiscovery(watcher discovery.Watcher) error {
|
2015-05-15 22:23:59 +00:00
|
|
|
if c.cfg == nil {
|
|
|
|
return fmt.Errorf("discovery initialization requires a valid configuration")
|
|
|
|
}
|
|
|
|
|
2015-09-18 19:54:08 +00:00
|
|
|
c.discovery = hostdiscovery.NewHostDiscovery(watcher)
|
2015-10-22 04:31:43 +00:00
|
|
|
return c.discovery.Watch(c.activeCallback, c.hostJoinCallback, c.hostLeaveCallback)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) activeCallback() {
|
|
|
|
ds := c.getStore(datastore.GlobalScope)
|
|
|
|
if ds != nil && !ds.Active() {
|
|
|
|
ds.RestartWatch()
|
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 19:54:08 +00:00
|
|
|
func (c *controller) hostJoinCallback(nodes []net.IP) {
|
|
|
|
c.processNodeDiscovery(nodes, true)
|
2015-05-15 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 19:54:08 +00:00
|
|
|
func (c *controller) hostLeaveCallback(nodes []net.IP) {
|
|
|
|
c.processNodeDiscovery(nodes, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) processNodeDiscovery(nodes []net.IP, add bool) {
|
|
|
|
c.Lock()
|
|
|
|
drivers := []*driverData{}
|
|
|
|
for _, d := range c.drivers {
|
|
|
|
drivers = append(drivers, d)
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
for _, d := range drivers {
|
|
|
|
c.pushNodeDiscovery(d, nodes, add)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) pushNodeDiscovery(d *driverData, nodes []net.IP, add bool) {
|
|
|
|
var self net.IP
|
|
|
|
if c.cfg != nil {
|
|
|
|
addr := strings.Split(c.cfg.Cluster.Address, ":")
|
|
|
|
self = net.ParseIP(addr[0])
|
|
|
|
}
|
|
|
|
if d == nil || d.capability.DataScope != datastore.GlobalScope || nodes == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, node := range nodes {
|
|
|
|
nodeData := driverapi.NodeDiscoveryData{Address: node.String(), Self: node.Equal(self)}
|
|
|
|
var err error
|
|
|
|
if add {
|
|
|
|
err = d.driver.DiscoverNew(driverapi.NodeDiscovery, nodeData)
|
|
|
|
} else {
|
|
|
|
err = d.driver.DiscoverDelete(driverapi.NodeDiscovery, nodeData)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("discovery notification error : %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 22:23:59 +00:00
|
|
|
}
|
|
|
|
|
2015-06-11 13:52:46 +00:00
|
|
|
func (c *controller) Config() config.Config {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2015-06-13 21:28:34 +00:00
|
|
|
if c.cfg == nil {
|
|
|
|
return config.Config{}
|
|
|
|
}
|
2015-06-11 13:52:46 +00:00
|
|
|
return *c.cfg
|
|
|
|
}
|
|
|
|
|
2015-06-06 17:21:51 +00:00
|
|
|
func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
|
2015-06-14 16:00:27 +00:00
|
|
|
if !config.IsValidName(networkType) {
|
|
|
|
return ErrInvalidName(networkType)
|
|
|
|
}
|
2015-09-11 18:02:16 +00:00
|
|
|
|
|
|
|
c.Lock()
|
2015-05-06 23:57:38 +00:00
|
|
|
if _, ok := c.drivers[networkType]; ok {
|
2015-06-16 02:09:59 +00:00
|
|
|
c.Unlock()
|
2015-05-06 23:57:38 +00:00
|
|
|
return driverapi.ErrActiveRegistration(networkType)
|
|
|
|
}
|
2015-09-18 19:54:08 +00:00
|
|
|
dData := &driverData{driver, capability}
|
|
|
|
c.drivers[networkType] = dData
|
|
|
|
hd := c.discovery
|
2015-06-16 02:09:59 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-09-18 19:54:08 +00:00
|
|
|
if hd != nil {
|
|
|
|
c.pushNodeDiscovery(dData, hd.Fetch(), true)
|
|
|
|
}
|
|
|
|
|
2015-05-06 23:57:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 02:09:58 +00:00
|
|
|
func (c *controller) registerIpamDriver(name string, driver ipamapi.Ipam, caps *ipamapi.Capability) error {
|
2015-09-22 20:20:55 +00:00
|
|
|
if !config.IsValidName(name) {
|
|
|
|
return ErrInvalidName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
2015-10-03 23:11:50 +00:00
|
|
|
_, ok := c.ipamDrivers[name]
|
|
|
|
c.Unlock()
|
|
|
|
if ok {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.ForbiddenErrorf("ipam driver %q already registered", name)
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
locAS, glbAS, err := driver.GetDefaultAddressSpaces()
|
2015-09-22 20:20:55 +00:00
|
|
|
if err != nil {
|
2015-10-21 00:05:01 +00:00
|
|
|
return types.InternalErrorf("ipam driver %q failed to return default address spaces: %v", name, err)
|
2015-09-22 20:20:55 +00:00
|
|
|
}
|
2015-10-03 23:11:50 +00:00
|
|
|
c.Lock()
|
2015-12-10 02:09:58 +00:00
|
|
|
c.ipamDrivers[name] = &ipamData{driver: driver, defaultLocalAddressSpace: locAS, defaultGlobalAddressSpace: glbAS, capability: caps}
|
2015-09-22 20:20:55 +00:00
|
|
|
c.Unlock()
|
|
|
|
|
2015-10-21 00:05:01 +00:00
|
|
|
log.Debugf("Registering ipam driver: %q", name)
|
2015-09-22 20:20:55 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-10 02:09:58 +00:00
|
|
|
func (c *controller) RegisterIpamDriver(name string, driver ipamapi.Ipam) error {
|
|
|
|
return c.registerIpamDriver(name, driver, &ipamapi.Capability{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) RegisterIpamDriverWithCapabilities(name string, driver ipamapi.Ipam, caps *ipamapi.Capability) error {
|
|
|
|
return c.registerIpamDriver(name, driver, caps)
|
|
|
|
}
|
|
|
|
|
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-06-14 16:00:27 +00:00
|
|
|
if !config.IsValidName(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
|
|
|
|
|
|
|
// Construct the network object
|
|
|
|
network := &network{
|
2015-05-30 03:42:23 +00:00
|
|
|
name: name,
|
|
|
|
networkType: networkType,
|
2015-10-13 17:37:53 +00:00
|
|
|
generic: map[string]interface{}{netlabel.GenericData: make(map[string]string)},
|
2015-09-22 20:20:55 +00:00
|
|
|
ipamType: ipamapi.DefaultIPAM,
|
2015-07-02 05:00:48 +00:00
|
|
|
id: stringid.GenerateRandomID(),
|
2015-05-30 03:42:23 +00:00
|
|
|
ctrlr: c,
|
2015-09-22 14:09:39 +00:00
|
|
|
persist: true,
|
2015-10-05 11:21:15 +00:00
|
|
|
drvOnce: &sync.Once{},
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-05-01 00:57:06 +00:00
|
|
|
network.processOptions(options...)
|
2015-05-31 18:49:11 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
// Make sure we have a driver available for this network type
|
|
|
|
// before we allocate anything.
|
|
|
|
if _, err := network.driver(); err != nil {
|
2015-10-03 23:11:50 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-10-04 04:25:57 +00:00
|
|
|
err := network.ipamAllocate()
|
2015-10-03 23:11:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-04 04:25:57 +00:00
|
|
|
network.ipamRelease()
|
2015-10-03 23:11:50 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-10-15 21:41:07 +00:00
|
|
|
if err = c.addNetwork(network); err != nil {
|
2015-04-30 01:25:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-12 05:28:26 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := network.deleteNetwork(); e != nil {
|
|
|
|
log.Warnf("couldn't roll back driver network on network %s creation failure: %v", network.name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-10-03 23:11:50 +00:00
|
|
|
if err = c.updateToStore(network); err != nil {
|
2015-10-12 05:28:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := c.deleteFromStore(network); e != nil {
|
|
|
|
log.Warnf("couldnt rollback from store, network %s on failure (%v): %v", network.name, err, e)
|
|
|
|
}
|
2015-06-05 20:31:12 +00:00
|
|
|
}
|
2015-10-12 05:28:26 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
network.epCnt = &endpointCnt{n: network}
|
|
|
|
if err = c.updateToStore(network.epCnt); err != nil {
|
2015-05-30 03:42:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
return network, nil
|
|
|
|
}
|
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
func (c *controller) addNetwork(n *network) error {
|
2015-10-05 11:21:15 +00:00
|
|
|
d, err := n.driver()
|
|
|
|
if err != nil {
|
2015-10-03 23:11:50 +00:00
|
|
|
return err
|
2015-05-31 18:49:11 +00:00
|
|
|
}
|
2015-05-13 21:12:57 +00:00
|
|
|
|
2015-05-31 18:49:11 +00:00
|
|
|
// Create the network
|
2015-10-04 04:25:57 +00:00
|
|
|
if err := d.CreateNetwork(n.id, n.generic, n.getIPData(4), n.getIPData(6)); err != nil {
|
2015-05-31 18:49:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-13 15:41:45 +00:00
|
|
|
|
2015-05-30 03:42:23 +00:00
|
|
|
return nil
|
2015-05-13 15:41:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 01:25:01 +00:00
|
|
|
func (c *controller) Networks() []Network {
|
2015-10-05 11:21:15 +00:00
|
|
|
var list []Network
|
2015-04-30 01:25:01 +00:00
|
|
|
|
2015-10-05 11:21:15 +00:00
|
|
|
networks, err := c.getNetworksFromStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range networks {
|
2015-04-30 01:25:01 +00:00
|
|
|
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-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
n, err := c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrNoSuchNetwork(id)
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
2015-10-05 11:21:15 +00:00
|
|
|
|
|
|
|
return n, nil
|
2015-04-30 01:25:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00: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")
|
|
|
|
}
|
|
|
|
|
2015-11-02 07:03:26 +00:00
|
|
|
var sb *sandbox
|
|
|
|
c.Lock()
|
|
|
|
for _, s := range c.sandboxes {
|
|
|
|
if s.containerID == containerID {
|
|
|
|
// If not a stub, then we already have a complete sandbox.
|
|
|
|
if !s.isStub {
|
|
|
|
c.Unlock()
|
|
|
|
return nil, types.BadRequestErrorf("container %s is already present: %v", containerID, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We already have a stub sandbox from the
|
|
|
|
// store. Make use of it so that we don't lose
|
|
|
|
// the endpoints from store but reset the
|
|
|
|
// isStub flag.
|
|
|
|
sb = s
|
|
|
|
sb.isStub = false
|
|
|
|
break
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
2015-11-02 07:03:26 +00:00
|
|
|
c.Unlock()
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
// Create sandbox and process options first. Key generation depends on an option
|
2015-11-02 07:03:26 +00:00
|
|
|
if sb == nil {
|
|
|
|
sb = &sandbox{
|
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
containerID: containerID,
|
|
|
|
endpoints: epHeap{},
|
|
|
|
epPriority: map[string]int{},
|
|
|
|
config: containerConfig{},
|
|
|
|
controller: c,
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
heap.Init(&sb.endpoints)
|
|
|
|
|
|
|
|
sb.processOptions(options...)
|
|
|
|
|
2015-09-02 01:55:53 +00:00
|
|
|
if err = sb.setupResolutionFiles(); err != nil {
|
2015-07-02 05:00:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-01 06:15:15 +00:00
|
|
|
if sb.config.useDefaultSandBox {
|
|
|
|
c.sboxOnce.Do(func() {
|
|
|
|
c.defOsSbox, err = osl.NewSandbox(sb.Key(), false)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.sboxOnce = sync.Once{}
|
|
|
|
return nil, fmt.Errorf("failed to create default sandbox: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.osSbox = c.defOsSbox
|
|
|
|
}
|
|
|
|
|
2015-09-09 23:20:54 +00:00
|
|
|
if sb.osSbox == nil && !sb.config.useExternalKey {
|
2015-07-02 05:00:48 +00: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 06:15:15 +00:00
|
|
|
c.Lock()
|
2015-07-02 05:00:48 +00:00
|
|
|
c.sandboxes[sb.id] = sb
|
|
|
|
c.Unlock()
|
2015-10-08 03:01:38 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Lock()
|
|
|
|
delete(c.sandboxes, sb.id)
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = sb.storeUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("updating the store state of sandbox failed: %v", err)
|
|
|
|
}
|
2015-07-02 05:00:48 +00:00
|
|
|
|
|
|
|
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 {
|
2015-11-02 07:03:26 +00:00
|
|
|
// Hide stub sandboxes from libnetwork users
|
|
|
|
if s.isStub {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-11-02 07:03:26 +00:00
|
|
|
// SandboxDestroy destroys a sandbox given a container ID
|
|
|
|
func (c *controller) SandboxDestroy(id string) error {
|
|
|
|
var sb *sandbox
|
|
|
|
c.Lock()
|
|
|
|
for _, s := range c.sandboxes {
|
|
|
|
if s.containerID == id {
|
|
|
|
sb = s
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
// It is not an error if sandbox is not available
|
|
|
|
if sb == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.Delete()
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:00:48 +00:00
|
|
|
// 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 17:21:51 +00:00
|
|
|
func (c *controller) loadDriver(networkType string) (*driverData, error) {
|
2015-05-16 01:14:36 +00: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 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()
|
2015-06-06 17:21:51 +00:00
|
|
|
dd, ok := c.drivers[networkType]
|
2015-05-16 01:14:36 +00:00
|
|
|
if !ok {
|
2015-05-14 21:56:15 +00:00
|
|
|
return nil, ErrInvalidNetworkDriver(networkType)
|
2015-05-16 01:14:36 +00:00
|
|
|
}
|
2015-06-06 17:21:51 +00:00
|
|
|
return dd, nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:20:55 +00:00
|
|
|
func (c *controller) loadIpamDriver(name string) (*ipamData, error) {
|
|
|
|
if _, err := plugins.Get(name, ipamapi.PluginEndpointType); err != nil {
|
|
|
|
if err == plugins.ErrNotFound {
|
|
|
|
return nil, types.NotFoundErrorf(err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-18 19:54:08 +00:00
|
|
|
c.Lock()
|
2015-09-22 20:20:55 +00:00
|
|
|
id, ok := c.ipamDrivers[name]
|
|
|
|
c.Unlock()
|
2015-09-18 19:54:08 +00:00
|
|
|
if !ok {
|
2015-10-21 00:05:01 +00:00
|
|
|
return nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
|
2015-09-18 19:54:08 +00:00
|
|
|
}
|
2015-09-22 20:20:55 +00:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) getIPAM(name string) (id *ipamData, err error) {
|
|
|
|
var ok bool
|
|
|
|
c.Lock()
|
|
|
|
id, ok = c.ipamDrivers[name]
|
|
|
|
c.Unlock()
|
|
|
|
if !ok {
|
|
|
|
id, err = c.loadIpamDriver(name)
|
|
|
|
}
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) getIpamDriver(name string) (ipamapi.Ipam, error) {
|
|
|
|
id, err := c.getIPAM(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return id.driver, nil
|
2015-09-18 19:54:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 21:24:05 +00:00
|
|
|
func (c *controller) Stop() {
|
2015-10-05 11:21:15 +00:00
|
|
|
c.closeStores()
|
2015-09-09 21:24:05 +00:00
|
|
|
c.stopExternalKeyListener()
|
2015-07-02 05:00:48 +00:00
|
|
|
osl.GC()
|
2015-06-05 18:46:33 +00:00
|
|
|
}
|