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
|
|
|
networkType := "bridge"
|
|
|
|
|
2015-09-22 11:27:11 -04:00
|
|
|
// Create a new controller instance
|
2015-06-15 22:09:59 -04:00
|
|
|
driverOptions := options.Generic{}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = driverOptions
|
2015-09-22 11:27:11 -04:00
|
|
|
controller, err := libnetwork.New(config.OptionDriverConfig(networkType, genericOption))
|
2015-06-15 22:09:59 -04:00
|
|
|
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
|
2016-02-29 14:49:04 -05:00
|
|
|
network, err := controller.NewNetwork(networkType, "network1", "")
|
2015-06-15 22:09:59 -04:00
|
|
|
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 11:27:11 -04: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-15 22:09:59 -04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
*/
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
2015-05-15 18:23:59 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-03-10 06:28:14 -05:00
|
|
|
"path/filepath"
|
2017-11-06 18:42:11 -05:00
|
|
|
"runtime"
|
2015-09-18 15:54:08 -04:00
|
|
|
"strings"
|
2015-04-29 21:25:01 -04:00
|
|
|
"sync"
|
2016-09-20 11:59:32 -04:00
|
|
|
"time"
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/cluster"
|
|
|
|
"github.com/docker/docker/libnetwork/config"
|
|
|
|
"github.com/docker/docker/libnetwork/datastore"
|
|
|
|
"github.com/docker/docker/libnetwork/diagnostic"
|
|
|
|
"github.com/docker/docker/libnetwork/discoverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/driverapi"
|
|
|
|
"github.com/docker/docker/libnetwork/drvregistry"
|
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/docker/libnetwork/options"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/plugingetter"
|
|
|
|
"github.com/docker/docker/pkg/plugins"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-09-11 19:22:29 -04:00
|
|
|
"github.com/moby/locker"
|
2018-06-12 12:26:11 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-04-29 21:25:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetworkController provides the interface for controller instance which manages
|
|
|
|
// networks.
|
|
|
|
type NetworkController interface {
|
2016-05-22 22:55:17 -04:00
|
|
|
// ID provides a unique identity for the controller
|
2015-09-09 17:24:05 -04:00
|
|
|
ID() string
|
|
|
|
|
2016-06-30 08:48:14 -04:00
|
|
|
// BuiltinDrivers returns list of builtin drivers
|
|
|
|
BuiltinDrivers() []string
|
|
|
|
|
2016-12-18 22:56:34 -05:00
|
|
|
// BuiltinIPAMDrivers returns list of builtin ipam drivers
|
|
|
|
BuiltinIPAMDrivers() []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.
|
2016-02-29 14:49:04 -05:00
|
|
|
NewNetwork(networkType, name string, id 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
|
|
|
|
2016-05-22 22:55:17 -04:00
|
|
|
// NewSandbox creates a new network sandbox for the passed container id
|
2015-07-02 01:00:48 -04:00
|
|
|
NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error)
|
|
|
|
|
|
|
|
// Sandboxes returns the list of Sandbox(s) managed by this controller.
|
|
|
|
Sandboxes() []Sandbox
|
|
|
|
|
2016-07-29 14:21:10 -04:00
|
|
|
// WalkSandboxes uses the provided function to walk the Sandbox(s) managed by this controller.
|
2015-07-02 01:00:48 -04:00
|
|
|
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-11-02 02:03:26 -05:00
|
|
|
// SandboxDestroy destroys a sandbox given a container ID
|
|
|
|
SandboxDestroy(id string) error
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
// Stop network controller
|
|
|
|
Stop()
|
2016-02-16 18:19:18 -05:00
|
|
|
|
2017-05-21 22:25:52 -04:00
|
|
|
// ReloadConfiguration updates the controller configuration
|
2016-02-16 18:19:18 -05:00
|
|
|
ReloadConfiguration(cfgOptions ...config.Option) error
|
2016-05-24 19:17:19 -04:00
|
|
|
|
|
|
|
// SetClusterProvider sets cluster provider
|
2017-05-25 13:45:38 -04:00
|
|
|
SetClusterProvider(provider cluster.Provider)
|
2016-06-05 22:37:13 -04:00
|
|
|
|
|
|
|
// Wait for agent initialization complete in libnetwork controller
|
|
|
|
AgentInitWait()
|
2016-06-05 01:48:10 -04:00
|
|
|
|
2017-04-05 13:38:24 -04:00
|
|
|
// Wait for agent to stop if running
|
|
|
|
AgentStopWait()
|
|
|
|
|
2016-06-05 01:48:10 -04:00
|
|
|
// SetKeys configures the encryption key for gossip and overlay data path
|
|
|
|
SetKeys(keys []*types.EncryptionKey) error
|
2017-12-01 13:13:01 -05:00
|
|
|
|
2017-12-06 14:21:51 -05:00
|
|
|
// StartDiagnostic start the network diagnostic mode
|
|
|
|
StartDiagnostic(port int)
|
|
|
|
// StopDiagnostic start the network diagnostic mode
|
|
|
|
StopDiagnostic()
|
|
|
|
// IsDiagnosticEnabled returns true if the diagnostic is enabled
|
|
|
|
IsDiagnosticEnabled() bool
|
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
|
|
|
|
|
|
|
|
type sandboxTable map[string]*sandbox
|
2015-04-29 21:25:01 -04:00
|
|
|
|
|
|
|
type controller struct {
|
2021-05-27 20:15:56 -04:00
|
|
|
id string
|
|
|
|
drvRegistry *drvregistry.DrvRegistry
|
|
|
|
sandboxes sandboxTable
|
|
|
|
cfg *config.Config
|
|
|
|
stores []datastore.DataStore
|
|
|
|
extKeyListener net.Listener
|
|
|
|
watchCh chan *endpoint
|
|
|
|
unWatchCh chan *endpoint
|
|
|
|
svcRecords map[string]svcInfo
|
|
|
|
nmap map[string]*netWatch
|
|
|
|
serviceBindings map[serviceKey]*service
|
|
|
|
defOsSbox osl.Sandbox
|
|
|
|
ingressSandbox *sandbox
|
|
|
|
sboxOnce sync.Once
|
|
|
|
agent *agent
|
|
|
|
networkLocker *locker.Locker
|
|
|
|
agentInitDone chan struct{}
|
|
|
|
agentStopDone chan struct{}
|
|
|
|
keys []*types.EncryptionKey
|
|
|
|
DiagnosticServer *diagnostic.Server
|
2015-04-29 21:25:01 -04:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
type initializer struct {
|
|
|
|
fn drvregistry.InitFunc
|
|
|
|
ntype string
|
|
|
|
}
|
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
// New creates a new instance of network controller.
|
2015-06-11 09:52:46 -04:00
|
|
|
func New(cfgOptions ...config.Option) (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 08:46:29 -04:00
|
|
|
c := &controller{
|
2017-12-06 14:21:51 -05:00
|
|
|
id: stringid.GenerateRandomID(),
|
|
|
|
cfg: config.ParseConfigOptions(cfgOptions...),
|
|
|
|
sandboxes: sandboxTable{},
|
|
|
|
svcRecords: make(map[string]svcInfo),
|
|
|
|
serviceBindings: make(map[serviceKey]*service),
|
|
|
|
agentInitDone: make(chan struct{}),
|
|
|
|
networkLocker: locker.New(),
|
|
|
|
DiagnosticServer: diagnostic.New(),
|
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
|
|
|
}
|
2017-12-06 14:21:51 -05:00
|
|
|
c.DiagnosticServer.Init()
|
2015-05-08 09:26:35 -04:00
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
if err := c.initStores(); err != nil {
|
2015-09-22 16:20:55 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-27 16:54:25 -04:00
|
|
|
drvRegistry, err := drvregistry.New(c.getStore(datastore.LocalScope), c.getStore(datastore.GlobalScope), c.RegisterDriver, nil, c.cfg.PluginGetter)
|
2016-02-29 21:17:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-13 16:22:55 -05:00
|
|
|
for _, i := range getInitializers(c.cfg.Daemon.Experimental) {
|
2016-02-29 21:17:04 -05:00
|
|
|
var dcfg map[string]interface{}
|
|
|
|
|
|
|
|
// External plugins don't need config passed through daemon. They can
|
|
|
|
// bootstrap themselves
|
|
|
|
if i.ntype != "remote" {
|
|
|
|
dcfg = c.makeDriverConfig(i.ntype)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := drvRegistry.AddDriver(i.ntype, i.fn, dcfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2016-07-05 16:49:31 -04:00
|
|
|
|
2018-01-18 17:30:51 -05:00
|
|
|
if err = initIPAMDrivers(drvRegistry, nil, c.getStore(datastore.GlobalScope), c.cfg.Daemon.DefaultAddressPool); err != nil {
|
2016-07-05 16:49:31 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
c.drvRegistry = drvRegistry
|
|
|
|
|
2016-06-15 00:34:44 -04:00
|
|
|
c.WalkNetworks(populateSpecial)
|
|
|
|
|
2016-06-10 20:32:19 -04:00
|
|
|
// Reserve pools first before doing cleanup. Otherwise the
|
|
|
|
// cleanups of endpoint/network and sandbox below will
|
|
|
|
// generate many unnecessary warnings
|
2016-06-11 22:19:16 -04:00
|
|
|
c.reservePools()
|
|
|
|
|
|
|
|
// Cleanup resources
|
2016-06-10 20:32:19 -04:00
|
|
|
c.sandboxCleanup(c.cfg.ActiveSandboxes)
|
2015-10-19 16:20:23 -04:00
|
|
|
c.cleanupLocalEndpoints()
|
2016-03-05 05:00:31 -05:00
|
|
|
c.networkCleanup()
|
2015-10-07 23:01:38 -04:00
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
if err := c.startExternalKeyListener(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:54:49 -04:00
|
|
|
setupArrangeUserFilterRule(c)
|
2015-05-13 11:41:45 -04:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-05-25 13:45:38 -04:00
|
|
|
func (c *controller) SetClusterProvider(provider cluster.Provider) {
|
2017-05-03 14:18:33 -04:00
|
|
|
var sameProvider bool
|
2016-06-10 00:38:29 -04:00
|
|
|
c.Lock()
|
2017-05-03 14:18:33 -04:00
|
|
|
// Avoids to spawn multiple goroutine for the same cluster provider
|
|
|
|
if c.cfg.Daemon.ClusterProvider == provider {
|
|
|
|
// If the cluster provider is already set, there is already a go routine spawned
|
|
|
|
// that is listening for events, so nothing to do here
|
|
|
|
sameProvider = true
|
2016-06-10 00:38:29 -04:00
|
|
|
} else {
|
2017-05-03 14:18:33 -04:00
|
|
|
c.cfg.Daemon.ClusterProvider = provider
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if provider == nil || sameProvider {
|
|
|
|
return
|
2016-06-10 00:38:29 -04:00
|
|
|
}
|
2017-05-03 14:18:33 -04:00
|
|
|
// We don't want to spawn a new go routine if the previous one did not exit yet
|
|
|
|
c.AgentStopWait()
|
|
|
|
go c.clusterAgentInit()
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:48:10 -04:00
|
|
|
// libnetwork side of agent depends on the keys. On the first receipt of
|
|
|
|
// keys setup the agent. For subsequent key set handle the key change
|
|
|
|
func (c *controller) SetKeys(keys []*types.EncryptionKey) error {
|
2016-08-03 20:58:24 -04:00
|
|
|
subsysKeys := make(map[string]int)
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Subsystem != subsysGossip &&
|
|
|
|
key.Subsystem != subsysIPSec {
|
|
|
|
return fmt.Errorf("key received for unrecognized subsystem")
|
|
|
|
}
|
|
|
|
subsysKeys[key.Subsystem]++
|
|
|
|
}
|
|
|
|
for s, count := range subsysKeys {
|
|
|
|
if count != keyringSize {
|
2016-12-27 06:42:32 -05:00
|
|
|
return fmt.Errorf("incorrect number of keys for subsystem %v", s)
|
2016-08-03 20:58:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:18:33 -04:00
|
|
|
agent := c.getAgent()
|
|
|
|
|
2016-06-10 00:38:29 -04:00
|
|
|
if agent == nil {
|
|
|
|
c.Lock()
|
2016-06-05 01:48:10 -04:00
|
|
|
c.keys = keys
|
2016-06-10 00:38:29 -04:00
|
|
|
c.Unlock()
|
2016-06-05 01:48:10 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.handleKeyChange(keys)
|
|
|
|
}
|
|
|
|
|
2016-11-22 02:38:03 -05:00
|
|
|
func (c *controller) getAgent() *agent {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
return c.agent
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
func (c *controller) clusterAgentInit() {
|
|
|
|
clusterProvider := c.cfg.Daemon.ClusterProvider
|
2017-05-03 14:18:33 -04:00
|
|
|
var keysAvailable bool
|
2016-05-24 19:17:19 -04:00
|
|
|
for {
|
2017-05-03 14:18:33 -04:00
|
|
|
eventType := <-clusterProvider.ListenClusterEvents()
|
2017-05-25 13:45:38 -04:00
|
|
|
// The events: EventSocketChange, EventNodeReady and EventNetworkKeysAvailable are not ordered
|
2017-05-03 14:18:33 -04:00
|
|
|
// when all the condition for the agent initialization are met then proceed with it
|
|
|
|
switch eventType {
|
2017-05-25 13:45:38 -04:00
|
|
|
case cluster.EventNetworkKeysAvailable:
|
2017-05-03 14:18:33 -04:00
|
|
|
// Validates that the keys are actually available before starting the initialization
|
|
|
|
// This will handle old spurious messages left on the channel
|
|
|
|
c.Lock()
|
|
|
|
keysAvailable = c.keys != nil
|
|
|
|
c.Unlock()
|
|
|
|
fallthrough
|
2017-05-25 13:45:38 -04:00
|
|
|
case cluster.EventSocketChange, cluster.EventNodeReady:
|
2017-05-03 14:18:33 -04:00
|
|
|
if keysAvailable && !c.isDistributedControl() {
|
|
|
|
c.agentOperationStart()
|
|
|
|
if err := c.agentSetup(clusterProvider); err != nil {
|
|
|
|
c.agentStopComplete()
|
|
|
|
} else {
|
|
|
|
c.agentInitComplete()
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-25 13:45:38 -04:00
|
|
|
case cluster.EventNodeLeave:
|
2017-05-03 14:18:33 -04:00
|
|
|
c.agentOperationStart()
|
2016-06-10 00:38:29 -04:00
|
|
|
c.Lock()
|
2016-08-16 20:37:33 -04:00
|
|
|
c.keys = nil
|
2016-06-10 00:38:29 -04:00
|
|
|
c.Unlock()
|
2016-08-17 16:48:51 -04:00
|
|
|
|
2016-08-19 20:50:37 -04:00
|
|
|
// We are leaving the cluster. Make sure we
|
|
|
|
// close the gossip so that we stop all
|
|
|
|
// incoming gossip updates before cleaning up
|
|
|
|
// any remaining service bindings. But before
|
|
|
|
// deleting the networks since the networks
|
|
|
|
// should still be present when cleaning up
|
|
|
|
// service bindings
|
|
|
|
c.agentClose()
|
2017-10-13 00:41:29 -04:00
|
|
|
c.cleanupServiceDiscovery("")
|
2016-08-19 20:50:37 -04:00
|
|
|
c.cleanupServiceBindings("")
|
2017-04-05 13:38:24 -04:00
|
|
|
|
2017-05-03 14:18:33 -04:00
|
|
|
c.agentStopComplete()
|
2017-04-05 13:38:24 -04:00
|
|
|
|
2016-06-10 00:38:29 -04:00
|
|
|
return
|
2016-05-24 19:17:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:18:33 -04:00
|
|
|
// AgentInitWait waits for agent initialization to be completed in the controller.
|
2016-06-05 22:37:13 -04:00
|
|
|
func (c *controller) AgentInitWait() {
|
2016-08-24 12:51:02 -04:00
|
|
|
c.Lock()
|
|
|
|
agentInitDone := c.agentInitDone
|
|
|
|
c.Unlock()
|
|
|
|
|
|
|
|
if agentInitDone != nil {
|
|
|
|
<-agentInitDone
|
|
|
|
}
|
2016-06-05 22:37:13 -04:00
|
|
|
}
|
|
|
|
|
2017-05-03 14:18:33 -04:00
|
|
|
// AgentStopWait waits for the Agent stop to be completed in the controller
|
2017-04-05 13:38:24 -04:00
|
|
|
func (c *controller) AgentStopWait() {
|
|
|
|
c.Lock()
|
|
|
|
agentStopDone := c.agentStopDone
|
|
|
|
c.Unlock()
|
|
|
|
if agentStopDone != nil {
|
|
|
|
<-agentStopDone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 14:18:33 -04:00
|
|
|
// agentOperationStart marks the start of an Agent Init or Agent Stop
|
|
|
|
func (c *controller) agentOperationStart() {
|
|
|
|
c.Lock()
|
|
|
|
if c.agentInitDone == nil {
|
|
|
|
c.agentInitDone = make(chan struct{})
|
|
|
|
}
|
|
|
|
if c.agentStopDone == nil {
|
|
|
|
c.agentStopDone = make(chan struct{})
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// agentInitComplete notifies the successful completion of the Agent initialization
|
|
|
|
func (c *controller) agentInitComplete() {
|
|
|
|
c.Lock()
|
|
|
|
if c.agentInitDone != nil {
|
|
|
|
close(c.agentInitDone)
|
|
|
|
c.agentInitDone = nil
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// agentStopComplete notifies the successful completion of the Agent stop
|
|
|
|
func (c *controller) agentStopComplete() {
|
|
|
|
c.Lock()
|
|
|
|
if c.agentStopDone != nil {
|
|
|
|
close(c.agentStopDone)
|
|
|
|
c.agentStopDone = nil
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (c *controller) makeDriverConfig(ntype string) map[string]interface{} {
|
|
|
|
if c.cfg == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := make(map[string]interface{})
|
|
|
|
|
|
|
|
for _, label := range c.cfg.Daemon.Labels {
|
|
|
|
if !strings.HasPrefix(netlabel.Key(label), netlabel.DriverPrefix+"."+ntype) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
config[netlabel.Key(label)] = netlabel.Value(label)
|
|
|
|
}
|
|
|
|
|
|
|
|
drvCfg, ok := c.cfg.Daemon.DriverCfg[ntype]
|
|
|
|
if ok {
|
|
|
|
for k, v := range drvCfg.(map[string]interface{}) {
|
|
|
|
config[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range c.cfg.Scopes {
|
|
|
|
if !v.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
config[netlabel.MakeKVClient(k)] = discoverapi.DatastoreConfigData{
|
|
|
|
Scope: k,
|
|
|
|
Provider: v.Client.Provider,
|
|
|
|
Address: v.Client.Address,
|
|
|
|
Config: v.Client.Config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2016-02-16 18:19:18 -05:00
|
|
|
var procReloadConfig = make(chan (bool), 1)
|
|
|
|
|
|
|
|
func (c *controller) ReloadConfiguration(cfgOptions ...config.Option) error {
|
|
|
|
procReloadConfig <- true
|
|
|
|
defer func() { <-procReloadConfig }()
|
|
|
|
|
|
|
|
// For now we accept the configuration reload only as a mean to provide a global store config after boot.
|
|
|
|
// Refuse the configuration if it alters an existing datastore client configuration.
|
|
|
|
update := false
|
|
|
|
cfg := config.ParseConfigOptions(cfgOptions...)
|
2016-03-30 17:42:58 -04:00
|
|
|
|
2016-02-16 18:19:18 -05:00
|
|
|
for s := range c.cfg.Scopes {
|
|
|
|
if _, ok := cfg.Scopes[s]; !ok {
|
|
|
|
return types.ForbiddenErrorf("cannot accept new configuration because it removes an existing datastore client")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for s, nSCfg := range cfg.Scopes {
|
|
|
|
if eSCfg, ok := c.cfg.Scopes[s]; ok {
|
|
|
|
if eSCfg.Client.Provider != nSCfg.Client.Provider ||
|
|
|
|
eSCfg.Client.Address != nSCfg.Client.Address {
|
|
|
|
return types.ForbiddenErrorf("cannot accept new configuration because it modifies an existing datastore client")
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-25 04:10:03 -04:00
|
|
|
if err := c.initScopedStore(s, nSCfg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-16 18:19:18 -05:00
|
|
|
update = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !update {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-21 15:09:07 -04:00
|
|
|
c.Lock()
|
|
|
|
c.cfg = cfg
|
|
|
|
c.Unlock()
|
|
|
|
|
2016-02-16 18:19:18 -05:00
|
|
|
var dsConfig *discoverapi.DatastoreConfigData
|
|
|
|
for scope, sCfg := range cfg.Scopes {
|
|
|
|
if scope == datastore.LocalScope || !sCfg.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dsConfig = &discoverapi.DatastoreConfigData{
|
|
|
|
Scope: scope,
|
|
|
|
Provider: sCfg.Client.Provider,
|
|
|
|
Address: sCfg.Client.Address,
|
|
|
|
Config: sCfg.Client.Config,
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if dsConfig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
c.drvRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
|
|
|
|
err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
|
2016-02-16 18:19:18 -05:00
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Failed to set datastore in driver %s: %v", name, err)
|
2016-02-16 18:19:18 -05:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
return false
|
|
|
|
})
|
2016-02-16 18:19:18 -05:00
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
|
|
|
|
err := driver.DiscoverNew(discoverapi.DatastoreConfig, *dsConfig)
|
2016-02-16 18:19:18 -05:00
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Failed to set datastore in driver %s: %v", name, err)
|
2016-02-16 18:19:18 -05:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
return false
|
|
|
|
})
|
2016-02-16 18:19:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
func (c *controller) ID() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
2016-06-30 08:48:14 -04:00
|
|
|
func (c *controller) BuiltinDrivers() []string {
|
|
|
|
drivers := []string{}
|
2016-12-18 22:56:34 -05:00
|
|
|
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
|
|
|
|
if driver.IsBuiltIn() {
|
|
|
|
drivers = append(drivers, name)
|
2016-06-30 08:48:14 -04:00
|
|
|
}
|
2016-12-18 22:56:34 -05:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
return drivers
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) BuiltinIPAMDrivers() []string {
|
|
|
|
drivers := []string{}
|
|
|
|
c.drvRegistry.WalkIPAMs(func(name string, driver ipamapi.Ipam, cap *ipamapi.Capability) bool {
|
|
|
|
if driver.IsBuiltIn() {
|
|
|
|
drivers = append(drivers, name)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2016-06-30 08:48:14 -04:00
|
|
|
return drivers
|
|
|
|
}
|
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
func (c *controller) processNodeDiscovery(nodes []net.IP, add bool) {
|
2016-02-29 21:17:04 -05:00
|
|
|
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
|
|
|
|
c.pushNodeDiscovery(driver, capability, nodes, add)
|
|
|
|
return false
|
|
|
|
})
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (c *controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capability, nodes []net.IP, add bool) {
|
2015-09-18 15:54:08 -04:00
|
|
|
var self net.IP
|
2021-12-22 04:42:13 -05:00
|
|
|
// try swarm-mode config
|
|
|
|
if agent := c.getAgent(); agent != nil {
|
|
|
|
self = net.ParseIP(agent.advertiseAddr)
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
|
2017-04-07 16:31:44 -04:00
|
|
|
if d == nil || cap.ConnectivityScope != datastore.GlobalScope || nodes == nil {
|
2015-09-18 15:54:08 -04:00
|
|
|
return
|
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
|
2015-09-18 15:54:08 -04:00
|
|
|
for _, node := range nodes {
|
2016-01-28 14:54:03 -05:00
|
|
|
nodeData := discoverapi.NodeDiscoveryData{Address: node.String(), Self: node.Equal(self)}
|
2015-09-18 15:54:08 -04:00
|
|
|
var err error
|
|
|
|
if add {
|
2016-02-29 21:17:04 -05:00
|
|
|
err = d.DiscoverNew(discoverapi.NodeDiscovery, nodeData)
|
2015-09-18 15:54:08 -04:00
|
|
|
} else {
|
2016-02-29 21:17:04 -05:00
|
|
|
err = d.DiscoverDelete(discoverapi.NodeDiscovery, nodeData)
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
if err != nil {
|
2016-12-27 06:42:32 -05:00
|
|
|
logrus.Debugf("discovery notification error: %v", err)
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-15 18:23:59 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
func (c *controller) isManager() bool {
|
2016-07-22 17:21:28 -04:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2016-05-24 19:17:19 -04:00
|
|
|
if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.cfg.Daemon.ClusterProvider.IsManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) isAgent() bool {
|
2016-07-22 17:21:28 -04:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2016-05-24 19:17:19 -04:00
|
|
|
if c.cfg == nil || c.cfg.Daemon.ClusterProvider == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.cfg.Daemon.ClusterProvider.IsAgent()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *controller) isDistributedControl() bool {
|
|
|
|
return !c.isManager() && !c.isAgent()
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:58:10 -04:00
|
|
|
func (c *controller) GetPluginGetter() plugingetter.PluginGetter {
|
2016-09-27 16:54:25 -04:00
|
|
|
return c.drvRegistry.GetPluginGetter()
|
|
|
|
}
|
|
|
|
|
2015-06-06 13:21:51 -04:00
|
|
|
func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, capability driverapi.Capability) error {
|
2016-03-30 17:42:58 -04:00
|
|
|
c.agentDriverNotify(driver)
|
2015-09-22 16:20:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:04:31 -04:00
|
|
|
// XXX This should be made driver agnostic. See comment below.
|
|
|
|
const overlayDSROptionString = "dsr"
|
|
|
|
|
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.
|
2016-02-29 14:49:04 -05:00
|
|
|
func (c *controller) NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) {
|
2019-05-07 18:36:32 -04:00
|
|
|
var (
|
2019-07-08 16:52:31 -04:00
|
|
|
cap *driverapi.Capability
|
|
|
|
err error
|
|
|
|
t *network
|
|
|
|
skipCfgEpCount bool
|
2019-05-07 18:36:32 -04:00
|
|
|
)
|
|
|
|
|
2016-09-12 18:19:22 -04:00
|
|
|
if id != "" {
|
|
|
|
c.networkLocker.Lock(id)
|
2021-05-27 20:15:56 -04:00
|
|
|
defer c.networkLocker.Unlock(id) // nolint:errcheck
|
2016-09-12 18:19:22 -04:00
|
|
|
|
2019-05-07 18:36:32 -04:00
|
|
|
if _, err = c.NetworkByID(id); err == nil {
|
2016-09-12 18:19:22 -04:00
|
|
|
return nil, NetworkNameError(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 21:17:29 -05:00
|
|
|
if !config.IsValidName(name) {
|
|
|
|
return nil, ErrInvalidName(name)
|
2015-05-07 22:59:06 -04:00
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2016-02-29 14:49:04 -05:00
|
|
|
if id == "" {
|
|
|
|
id = stringid.GenerateRandomID()
|
|
|
|
}
|
|
|
|
|
2016-10-12 19:55:20 -04:00
|
|
|
defaultIpam := defaultIpamForNetworkType(networkType)
|
2015-04-29 21:25:01 -04:00
|
|
|
// Construct the network object
|
|
|
|
network := &network{
|
2018-10-09 10:04:31 -04:00
|
|
|
name: name,
|
|
|
|
networkType: networkType,
|
|
|
|
generic: map[string]interface{}{netlabel.GenericData: make(map[string]string)},
|
|
|
|
ipamType: defaultIpam,
|
|
|
|
id: id,
|
|
|
|
created: time.Now(),
|
|
|
|
ctrlr: c,
|
|
|
|
persist: true,
|
|
|
|
drvOnce: &sync.Once{},
|
|
|
|
loadBalancerMode: loadBalancerModeDefault,
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
|
|
|
|
2015-04-30 20:57:06 -04:00
|
|
|
network.processOptions(options...)
|
2019-05-07 18:36:32 -04:00
|
|
|
if err = network.validateConfiguration(); err != nil {
|
2017-04-07 13:51:39 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset network types, force local scope and skip allocation and
|
|
|
|
// plumbing for configuration networks. Reset of the config-only
|
|
|
|
// network drivers is needed so that this special network is not
|
|
|
|
// usable by old engine versions.
|
|
|
|
if network.configOnly {
|
|
|
|
network.scope = datastore.LocalScope
|
|
|
|
network.networkType = "null"
|
|
|
|
goto addToStore
|
|
|
|
}
|
2015-05-31 14:49:11 -04:00
|
|
|
|
2017-04-07 13:51:39 -04:00
|
|
|
_, cap, err = network.resolveDriver(network.networkType, true)
|
2016-05-24 19:17:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:31:44 -04:00
|
|
|
if network.scope == datastore.LocalScope && cap.DataScope == datastore.GlobalScope {
|
|
|
|
return nil, types.ForbiddenErrorf("cannot downgrade network scope for %s networks", networkType)
|
|
|
|
|
|
|
|
}
|
2017-03-01 22:09:39 -05:00
|
|
|
if network.ingress && cap.DataScope != datastore.GlobalScope {
|
|
|
|
return nil, types.ForbiddenErrorf("Ingress network can only be global scope network")
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:31:44 -04:00
|
|
|
// At this point the network scope is still unknown if not set by user
|
|
|
|
if (cap.DataScope == datastore.GlobalScope || network.scope == datastore.SwarmScope) &&
|
|
|
|
!c.isDistributedControl() && !network.dynamic {
|
2016-05-24 19:17:19 -04:00
|
|
|
if c.isManager() {
|
|
|
|
// For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
|
|
|
|
return nil, ManagerRedirectError(name)
|
|
|
|
}
|
|
|
|
return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
|
|
|
|
}
|
|
|
|
|
2017-05-18 11:59:52 -04:00
|
|
|
if network.scope == datastore.SwarmScope && c.isDistributedControl() {
|
|
|
|
return nil, types.ForbiddenErrorf("cannot create a swarm scoped network when swarm is not active")
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:21:15 -04:00
|
|
|
// Make sure we have a driver available for this network type
|
|
|
|
// before we allocate anything.
|
2016-01-16 17:24:44 -05:00
|
|
|
if _, err := network.driver(true); err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-07 13:51:39 -04:00
|
|
|
// From this point on, we need the network specific configuration,
|
|
|
|
// which may come from a configuration-only network
|
|
|
|
if network.configFrom != "" {
|
2019-05-07 18:36:32 -04:00
|
|
|
t, err = c.getConfigNetwork(network.configFrom)
|
2017-04-07 13:51:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, types.NotFoundErrorf("configuration network %q does not exist", network.configFrom)
|
|
|
|
}
|
2019-05-07 18:36:32 -04:00
|
|
|
if err = t.applyConfigurationTo(network); err != nil {
|
2017-04-07 13:51:39 -04:00
|
|
|
return nil, types.InternalErrorf("Failed to apply configuration: %v", err)
|
|
|
|
}
|
2019-06-28 09:47:00 -04:00
|
|
|
network.generic[netlabel.Internal] = network.internal
|
2017-04-07 13:51:39 -04:00
|
|
|
defer func() {
|
2019-07-08 16:52:31 -04:00
|
|
|
if err == nil && !skipCfgEpCount {
|
2017-04-07 13:51:39 -04:00
|
|
|
if err := t.getEpCnt().IncEndpointCnt(); err != nil {
|
|
|
|
logrus.Warnf("Failed to update reference count for configuration network %q on creation of network %q: %v",
|
|
|
|
t.Name(), network.Name(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-05-24 19:17:19 -04:00
|
|
|
err = network.ipamAllocate()
|
2015-10-03 19:11:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-10-04 00:25:57 -04:00
|
|
|
network.ipamRelease()
|
2015-10-03 19:11:50 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-03-30 17:42:58 -04:00
|
|
|
err = c.addNetwork(network)
|
|
|
|
if err != nil {
|
2021-05-27 20:15:56 -04:00
|
|
|
if _, ok := err.(types.MaskableError); ok { // nolint:gosimple
|
2019-07-08 16:52:31 -04:00
|
|
|
// This error can be ignored and set this boolean
|
|
|
|
// value to skip a refcount increment for configOnly networks
|
|
|
|
skipCfgEpCount = true
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-10-12 01:28:26 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := network.deleteNetwork(); e != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("couldn't roll back driver network on network %s creation failure: %v", network.name, err)
|
2015-10-12 01:28:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2018-10-09 10:04:31 -04:00
|
|
|
// XXX If the driver type is "overlay" check the options for DSR
|
|
|
|
// being set. If so, set the network's load balancing mode to DSR.
|
|
|
|
// This should really be done in a network option, but due to
|
|
|
|
// time pressure to get this in without adding changes to moby,
|
|
|
|
// swarm and CLI, it is being implemented as a driver-specific
|
|
|
|
// option. Unfortunately, drivers can't influence the core
|
|
|
|
// "libnetwork.network" data type. Hence we need this hack code
|
|
|
|
// to implement in this manner.
|
|
|
|
if gval, ok := network.generic[netlabel.GenericData]; ok && network.networkType == "overlay" {
|
|
|
|
optMap := gval.(map[string]string)
|
|
|
|
if _, ok := optMap[overlayDSROptionString]; ok {
|
|
|
|
network.loadBalancerMode = loadBalancerModeDSR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 13:51:39 -04:00
|
|
|
addToStore:
|
2016-03-05 05:00:31 -05:00
|
|
|
// First store the endpoint count, then the network. To avoid to
|
|
|
|
// end up with a datastore containing a network and not an epCnt,
|
|
|
|
// in case of an ungraceful shutdown during this function call.
|
|
|
|
epCnt := &endpointCnt{n: network}
|
|
|
|
if err = c.updateToStore(epCnt); err != nil {
|
2015-10-12 01:28:26 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-03-05 05:00:31 -05:00
|
|
|
if e := c.deleteFromStore(epCnt); e != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("could not rollback from store, epCnt %v on failure (%v): %v", epCnt, err, e)
|
2015-10-12 01:28:26 -04:00
|
|
|
}
|
2015-06-05 16:31:12 -04:00
|
|
|
}
|
2015-10-12 01:28:26 -04:00
|
|
|
}()
|
|
|
|
|
2016-03-05 05:00:31 -05:00
|
|
|
network.epCnt = epCnt
|
|
|
|
if err = c.updateToStore(network); err != nil {
|
2015-05-29 23:42:23 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-04 16:58:54 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if e := c.deleteFromStore(network); e != nil {
|
|
|
|
logrus.Warnf("could not rollback from store, network %v on failure (%v): %v", network, err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-04-07 13:51:39 -04:00
|
|
|
if network.configOnly {
|
|
|
|
return network, nil
|
|
|
|
}
|
2015-05-29 23:42:23 -04:00
|
|
|
|
2017-11-30 03:22:30 -05:00
|
|
|
joinCluster(network)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
network.cancelDriverWatches()
|
|
|
|
if e := network.leaveCluster(); e != nil {
|
|
|
|
logrus.Warnf("Failed to leave agent cluster on network %s on failure (%v): %v", network.name, err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-04-10 12:34:41 -04:00
|
|
|
if network.hasLoadBalancerEndpoint() {
|
2017-11-04 16:58:54 -04:00
|
|
|
if err = network.createLoadBalancerSandbox(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 13:54:18 -04:00
|
|
|
if !c.isDistributedControl() {
|
2016-11-23 17:24:17 -05:00
|
|
|
c.Lock()
|
2016-08-15 13:54:18 -04:00
|
|
|
arrangeIngressFilterRule()
|
2016-11-23 17:24:17 -05:00
|
|
|
c.Unlock()
|
2016-08-15 13:54:18 -04:00
|
|
|
}
|
2019-10-13 00:54:49 -04:00
|
|
|
arrangeUserFilterRule()
|
2017-03-13 03:46:46 -04:00
|
|
|
|
2015-04-29 21:25:01 -04:00
|
|
|
return network, nil
|
|
|
|
}
|
|
|
|
|
2016-07-12 20:35:32 -04:00
|
|
|
var joinCluster NetworkWalker = func(nw Network) bool {
|
|
|
|
n := nw.(*network)
|
2017-04-07 13:51:39 -04:00
|
|
|
if n.configOnly {
|
|
|
|
return false
|
|
|
|
}
|
2016-07-12 20:35:32 -04:00
|
|
|
if err := n.joinCluster(); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Errorf("Failed to join network %s (%s) into agent cluster: %v", n.Name(), n.ID(), err)
|
2016-07-12 20:35:32 -04:00
|
|
|
}
|
|
|
|
n.addDriverWatches()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:11:36 -04:00
|
|
|
func (c *controller) reservePools() {
|
|
|
|
networks, err := c.getNetworksForScope(datastore.LocalScope)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Could not retrieve networks from local store during ipam allocation for existing networks: %v", err)
|
2016-04-18 18:11:36 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range networks {
|
2017-04-07 13:51:39 -04:00
|
|
|
if n.configOnly {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-18 18:11:36 -04:00
|
|
|
if !doReplayPoolReserve(n) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Construct pseudo configs for the auto IP case
|
|
|
|
autoIPv4 := (len(n.ipamV4Config) == 0 || (len(n.ipamV4Config) == 1 && n.ipamV4Config[0].PreferredPool == "")) && len(n.ipamV4Info) > 0
|
|
|
|
autoIPv6 := (len(n.ipamV6Config) == 0 || (len(n.ipamV6Config) == 1 && n.ipamV6Config[0].PreferredPool == "")) && len(n.ipamV6Info) > 0
|
|
|
|
if autoIPv4 {
|
|
|
|
n.ipamV4Config = []*IpamConf{{PreferredPool: n.ipamV4Info[0].Pool.String()}}
|
|
|
|
}
|
|
|
|
if n.enableIPv6 && autoIPv6 {
|
|
|
|
n.ipamV6Config = []*IpamConf{{PreferredPool: n.ipamV6Info[0].Pool.String()}}
|
|
|
|
}
|
|
|
|
// Account current network gateways
|
|
|
|
for i, c := range n.ipamV4Config {
|
|
|
|
if c.Gateway == "" && n.ipamV4Info[i].Gateway != nil {
|
|
|
|
c.Gateway = n.ipamV4Info[i].Gateway.IP.String()
|
|
|
|
}
|
|
|
|
}
|
2016-09-15 14:21:33 -04:00
|
|
|
if n.enableIPv6 {
|
|
|
|
for i, c := range n.ipamV6Config {
|
|
|
|
if c.Gateway == "" && n.ipamV6Info[i].Gateway != nil {
|
|
|
|
c.Gateway = n.ipamV6Info[i].Gateway.IP.String()
|
|
|
|
}
|
2016-04-18 18:11:36 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-11 12:30:34 -04:00
|
|
|
// Reserve pools
|
2016-04-18 18:11:36 -04:00
|
|
|
if err := n.ipamAllocate(); err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to allocate ipam pool(s) for network %q (%s): %v", n.Name(), n.ID(), err)
|
2016-04-18 18:11:36 -04:00
|
|
|
}
|
2016-06-11 12:30:34 -04:00
|
|
|
// Reserve existing endpoints' addresses
|
|
|
|
ipam, _, err := n.getController().getIPAMDriver(n.ipamType)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to retrieve ipam driver for network %q (%s) during address reservation", n.Name(), n.ID())
|
2016-06-11 12:30:34 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
epl, err := n.getEndpointsFromStore()
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to retrieve list of current endpoints on network %q (%s)", n.Name(), n.ID())
|
2016-06-11 12:30:34 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, ep := range epl {
|
2020-04-03 00:21:47 -04:00
|
|
|
if ep.Iface() == nil {
|
|
|
|
logrus.Warnf("endpoint interface is empty for %q (%s)", ep.Name(), ep.ID())
|
|
|
|
continue
|
|
|
|
}
|
2016-06-11 12:30:34 -04:00
|
|
|
if err := ep.assignAddress(ipam, true, ep.Iface().AddressIPv6() != nil); err != nil {
|
2016-11-28 13:44:45 -05:00
|
|
|
logrus.Warnf("Failed to reserve current address for endpoint %q (%s) on network %q (%s)",
|
2016-06-11 12:30:34 -04:00
|
|
|
ep.Name(), ep.ID(), n.Name(), n.ID())
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 18:11:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doReplayPoolReserve(n *network) bool {
|
|
|
|
_, caps, err := n.getController().getIPAMDriver(n.ipamType)
|
|
|
|
if err != nil {
|
2016-11-01 00:26:14 -04:00
|
|
|
logrus.Warnf("Failed to retrieve ipam driver for network %q (%s): %v", n.Name(), n.ID(), err)
|
2016-04-18 18:11:36 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return caps.RequiresRequestReplay
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
func (c *controller) addNetwork(n *network) error {
|
2016-01-16 17:24:44 -05:00
|
|
|
d, err := n.driver(true)
|
2015-10-05 07:21:15 -04:00
|
|
|
if err != nil {
|
2015-10-03 19:11:50 -04:00
|
|
|
return err
|
2015-05-31 14:49:11 -04:00
|
|
|
}
|
2015-05-13 17:12:57 -04:00
|
|
|
|
2015-05-31 14:49:11 -04:00
|
|
|
// Create the network
|
2016-03-30 17:42:58 -04:00
|
|
|
if err := d.CreateNetwork(n.id, n.generic, n, n.getIPData(4), n.getIPData(6)); err != nil {
|
2015-05-31 14:49:11 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-13 11:41:45 -04:00
|
|
|
|
2016-09-19 18:48:06 -04:00
|
|
|
n.startResolver()
|
|
|
|
|
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 {
|
2015-10-05 07:21:15 -04:00
|
|
|
var list []Network
|
2015-04-29 21:25:01 -04:00
|
|
|
|
2020-05-26 04:39:38 -04:00
|
|
|
for _, n := range c.getNetworksFromStore() {
|
2016-03-05 05:00:31 -05:00
|
|
|
if n.inDelete {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-29 21:25:01 -04: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 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-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
n, err := c.getNetworkFromStore(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrNoSuchNetwork(id)
|
2015-04-29 21:25:01 -04:00
|
|
|
}
|
2015-10-05 07:21:15 -04:00
|
|
|
|
|
|
|
return n, nil
|
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
|
2017-06-12 14:30:30 -04:00
|
|
|
func (c *controller) NewSandbox(containerID string, options ...SandboxOption) (Sandbox, error) {
|
2015-07-02 01:00:48 -04:00
|
|
|
if containerID == "" {
|
|
|
|
return nil, types.BadRequestErrorf("invalid container ID")
|
|
|
|
}
|
|
|
|
|
2015-11-02 02:03:26 -05: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 {
|
2016-10-04 18:40:47 -04:00
|
|
|
sbID := s.ID()
|
2015-11-02 02:03:26 -05:00
|
|
|
c.Unlock()
|
2016-10-04 18:40:47 -04:00
|
|
|
return nil, types.ForbiddenErrorf("container %s is already present in sandbox %s", containerID, sbID)
|
2015-11-02 02:03:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 01:00:48 -04:00
|
|
|
}
|
2015-11-02 02:03:26 -05:00
|
|
|
c.Unlock()
|
2015-07-02 01:00:48 -04:00
|
|
|
|
2017-11-06 18:42:11 -05:00
|
|
|
sandboxID := stringid.GenerateRandomID()
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
sandboxID = containerID
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04:00
|
|
|
// Create sandbox and process options first. Key generation depends on an option
|
2015-11-02 02:03:26 -05:00
|
|
|
if sb == nil {
|
|
|
|
sb = &sandbox{
|
2017-11-06 18:42:11 -05:00
|
|
|
id: sandboxID,
|
2016-06-15 17:00:48 -04:00
|
|
|
containerID: containerID,
|
2018-02-27 11:15:31 -05:00
|
|
|
endpoints: []*endpoint{},
|
2016-06-15 17:00:48 -04:00
|
|
|
epPriority: map[string]int{},
|
|
|
|
populatedEndpoints: map[string]struct{}{},
|
|
|
|
config: containerConfig{},
|
|
|
|
controller: c,
|
2016-12-18 21:27:13 -05:00
|
|
|
extDNS: []extDNSEntry{},
|
2015-11-02 02:03:26 -05:00
|
|
|
}
|
2015-07-02 01:00:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sb.processOptions(options...)
|
|
|
|
|
2016-05-31 02:55:51 -04:00
|
|
|
c.Lock()
|
|
|
|
if sb.ingress && c.ingressSandbox != nil {
|
2016-06-04 12:37:16 -04:00
|
|
|
c.Unlock()
|
2016-07-13 00:02:10 -04:00
|
|
|
return nil, types.ForbiddenErrorf("ingress sandbox already present")
|
2016-05-31 02:55:51 -04:00
|
|
|
}
|
|
|
|
|
2016-06-07 22:49:36 -04:00
|
|
|
if sb.ingress {
|
|
|
|
c.ingressSandbox = sb
|
2017-03-10 06:28:14 -05:00
|
|
|
sb.config.hostsPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/hosts")
|
|
|
|
sb.config.resolvConfPath = filepath.Join(c.cfg.Daemon.DataDir, "/network/files/resolv.conf")
|
2016-09-15 18:22:57 -04:00
|
|
|
sb.id = "ingress_sbox"
|
2018-07-23 17:18:16 -04:00
|
|
|
} else if sb.loadBalancerNID != "" {
|
|
|
|
sb.id = "lb_" + sb.loadBalancerNID
|
2016-06-07 22:49:36 -04:00
|
|
|
}
|
2016-05-31 02:55:51 -04:00
|
|
|
c.Unlock()
|
2017-06-12 14:30:30 -04:00
|
|
|
|
|
|
|
var err error
|
2016-05-31 02:55:51 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Lock()
|
|
|
|
if sb.ingress {
|
|
|
|
c.ingressSandbox = nil
|
|
|
|
}
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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-11-01 01:15:15 -05:00
|
|
|
if sb.config.useDefaultSandBox {
|
|
|
|
c.sboxOnce.Do(func() {
|
2016-06-10 20:32:19 -04:00
|
|
|
c.defOsSbox, err = osl.NewSandbox(sb.Key(), false, false)
|
2015-11-01 01:15:15 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
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 19:20:54 -04:00
|
|
|
if sb.osSbox == nil && !sb.config.useExternalKey {
|
2016-06-10 20:32:19 -04:00
|
|
|
if sb.osSbox, err = osl.NewSandbox(sb.Key(), !sb.config.useDefaultSandBox, false); err != nil {
|
2015-07-02 01:00:48 -04:00
|
|
|
return nil, fmt.Errorf("failed to create new osl sandbox: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:10:14 -04:00
|
|
|
if sb.osSbox != nil {
|
|
|
|
// Apply operating specific knobs on the load balancer sandbox
|
2022-01-11 23:13:39 -05:00
|
|
|
err := sb.osSbox.InvokeFunc(func() {
|
|
|
|
sb.osSbox.ApplyOSTweaks(sb.oslTypes)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to apply performance tuning sysctls to the sandbox: %v", err)
|
|
|
|
}
|
|
|
|
// Keep this just so performance is not changed
|
2018-05-18 17:10:14 -04:00
|
|
|
sb.osSbox.ApplyOSTweaks(sb.oslTypes)
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:15:15 -05:00
|
|
|
c.Lock()
|
2015-07-02 01:00:48 -04:00
|
|
|
c.sandboxes[sb.id] = sb
|
|
|
|
c.Unlock()
|
2015-10-07 23:01:38 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Lock()
|
|
|
|
delete(c.sandboxes, sb.id)
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = sb.storeUpdate()
|
|
|
|
if err != nil {
|
2016-12-27 06:42:32 -05:00
|
|
|
return nil, fmt.Errorf("failed to update the store state of sandbox: %v", err)
|
2015-10-07 23:01:38 -04:00
|
|
|
}
|
2015-07-02 01:00:48 -04: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 02:03:26 -05:00
|
|
|
// Hide stub sandboxes from libnetwork users
|
|
|
|
if s.isStub {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:00:48 -04: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 02:03:26 -05: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 01:00:48 -04: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (c *controller) loadDriver(networkType string) error {
|
2016-10-17 17:35:38 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if pg := c.GetPluginGetter(); pg != nil {
|
2017-01-10 16:17:15 -05:00
|
|
|
_, err = pg.Get(networkType, driverapi.NetworkPluginEndpointType, plugingetter.Lookup)
|
2016-10-17 17:35:38 -04:00
|
|
|
} else {
|
|
|
|
_, err = plugins.Get(networkType, driverapi.NetworkPluginEndpointType)
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:14:36 -04:00
|
|
|
if err != nil {
|
2018-06-12 12:26:11 -04:00
|
|
|
if errors.Cause(err) == plugins.ErrNotFound {
|
2016-02-29 21:17:04 -05:00
|
|
|
return types.NotFoundErrorf(err.Error())
|
2015-05-14 17:56:15 -04:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
return err
|
2015-05-15 21:14:36 -04:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
|
|
|
|
return nil
|
2015-06-06 13:21:51 -04:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (c *controller) loadIPAMDriver(name string) error {
|
2016-10-17 17:35:38 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if pg := c.GetPluginGetter(); pg != nil {
|
2017-01-10 16:17:15 -05:00
|
|
|
_, err = pg.Get(name, ipamapi.PluginEndpointType, plugingetter.Lookup)
|
2016-10-17 17:35:38 -04:00
|
|
|
} else {
|
|
|
|
_, err = plugins.Get(name, ipamapi.PluginEndpointType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-04-10 09:20:56 -04:00
|
|
|
if errors.Cause(err) == plugins.ErrNotFound {
|
2016-02-29 21:17:04 -05:00
|
|
|
return types.NotFoundErrorf(err.Error())
|
2015-09-22 16:20:55 -04:00
|
|
|
}
|
2016-02-29 21:17:04 -05:00
|
|
|
return err
|
2015-09-18 15:54:08 -04:00
|
|
|
}
|
2015-09-22 16:20:55 -04:00
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
return nil
|
2015-09-22 16:20:55 -04:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
func (c *controller) getIPAMDriver(name string) (ipamapi.Ipam, *ipamapi.Capability, error) {
|
|
|
|
id, cap := c.drvRegistry.IPAM(name)
|
|
|
|
if id == nil {
|
|
|
|
// Might be a plugin name. Try loading it
|
|
|
|
if err := c.loadIPAMDriver(name); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-18 15:54:08 -04:00
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
// Now that we resolved the plugin, try again looking up the registry
|
|
|
|
id, cap = c.drvRegistry.IPAM(name)
|
|
|
|
if id == nil {
|
|
|
|
return nil, nil, types.BadRequestErrorf("invalid ipam driver: %q", name)
|
|
|
|
}
|
2016-02-16 18:19:18 -05:00
|
|
|
}
|
|
|
|
|
2016-02-29 21:17:04 -05:00
|
|
|
return id, cap, nil
|
2016-02-16 18:19:18 -05:00
|
|
|
}
|
|
|
|
|
2015-09-09 17:24:05 -04:00
|
|
|
func (c *controller) Stop() {
|
2015-10-05 07:21:15 -04:00
|
|
|
c.closeStores()
|
2015-09-09 17:24:05 -04:00
|
|
|
c.stopExternalKeyListener()
|
2015-07-02 01:00:48 -04:00
|
|
|
osl.GC()
|
2015-06-05 14:46:33 -04:00
|
|
|
}
|
2017-12-01 13:13:01 -05:00
|
|
|
|
2017-12-06 14:21:51 -05:00
|
|
|
// StartDiagnostic start the network dias mode
|
|
|
|
func (c *controller) StartDiagnostic(port int) {
|
2017-12-01 13:13:01 -05:00
|
|
|
c.Lock()
|
2017-12-06 14:21:51 -05:00
|
|
|
if !c.DiagnosticServer.IsDiagnosticEnabled() {
|
|
|
|
c.DiagnosticServer.EnableDiagnostic("127.0.0.1", port)
|
2017-12-01 13:13:01 -05:00
|
|
|
}
|
2017-12-05 12:19:17 -05:00
|
|
|
c.Unlock()
|
2017-12-01 13:13:01 -05:00
|
|
|
}
|
|
|
|
|
2017-12-06 14:21:51 -05:00
|
|
|
// StopDiagnostic start the network dias mode
|
|
|
|
func (c *controller) StopDiagnostic() {
|
2017-12-01 13:13:01 -05:00
|
|
|
c.Lock()
|
2017-12-06 14:21:51 -05:00
|
|
|
if c.DiagnosticServer.IsDiagnosticEnabled() {
|
|
|
|
c.DiagnosticServer.DisableDiagnostic()
|
2017-12-01 13:13:01 -05:00
|
|
|
}
|
2017-12-05 12:19:17 -05:00
|
|
|
c.Unlock()
|
2017-12-01 13:13:01 -05:00
|
|
|
}
|
|
|
|
|
2017-12-06 14:21:51 -05:00
|
|
|
// IsDiagnosticEnabled returns true if the dias is enabled
|
|
|
|
func (c *controller) IsDiagnosticEnabled() bool {
|
2017-12-01 13:13:01 -05:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2017-12-06 14:21:51 -05:00
|
|
|
return c.DiagnosticServer.IsDiagnosticEnabled()
|
2017-12-01 13:13:01 -05:00
|
|
|
}
|
2019-10-13 00:54:49 -04:00
|
|
|
|
|
|
|
func (c *controller) iptablesEnabled() bool {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.cfg == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// parse map cfg["bridge"]["generic"]["EnableIPTable"]
|
|
|
|
cfgBridge, ok := c.cfg.Daemon.DriverCfg["bridge"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cfgGeneric, ok := cfgBridge[netlabel.GenericData].(options.Generic)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
enabled, ok := cfgGeneric["EnableIPTables"].(bool)
|
|
|
|
if !ok {
|
|
|
|
// unless user explicitly stated, assume iptable is enabled
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
return enabled
|
|
|
|
}
|