1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Vendoring libnetwork @b2bc1a6

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch 2017-05-13 19:21:58 -07:00
parent b34d3e730f
commit 46392f2442
29 changed files with 1091 additions and 113 deletions

View file

@ -26,7 +26,7 @@ github.com/imdario/mergo 0.2.1
golang.org/x/sync de49d9dcd27d4f764488181bea099dfe6179bcf0 golang.org/x/sync de49d9dcd27d4f764488181bea099dfe6179bcf0
#get libnetwork packages #get libnetwork packages
github.com/docker/libnetwork 6786135bf7de08ec26a72a6f7e4291d27d113a3f github.com/docker/libnetwork b2bc1a68486ccf8ada503162d9f0df7d31bdd8fb
github.com/docker/go-events 18b43f1bc85d9cdd42c05a6cd2d444c7a200a894 github.com/docker/go-events 18b43f1bc85d9cdd42c05a6cd2d444c7a200a894
github.com/armon/go-radix e39d623f12e8e41c7b5529e9a9dd67a1e2261f80 github.com/armon/go-radix e39d623f12e8e41c7b5529e9a9dd67a1e2261f80
github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec

View file

@ -222,7 +222,7 @@ func (c *controller) agentSetup(clusterProvider cluster.Provider) error {
return err return err
} }
c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool { c.drvRegistry.WalkDrivers(func(name string, driver driverapi.Driver, capability driverapi.Capability) bool {
if capability.DataScope == datastore.GlobalScope { if capability.ConnectivityScope == datastore.GlobalScope {
c.agentDriverNotify(driver) c.agentDriverNotify(driver)
} }
return false return false
@ -507,7 +507,7 @@ func (n *network) Services() map[string]ServiceInfo {
} }
func (n *network) isClusterEligible() bool { func (n *network) isClusterEligible() bool {
if n.driverScope() != datastore.GlobalScope { if n.scope != datastore.SwarmScope || !n.driverIsMultihost() {
return false return false
} }
return n.getController().getAgent() != nil return n.getController().getAgent() != nil

View file

@ -621,7 +621,7 @@ func (c *controller) pushNodeDiscovery(d driverapi.Driver, cap driverapi.Capabil
} }
} }
if d == nil || cap.DataScope != datastore.GlobalScope || nodes == nil { if d == nil || cap.ConnectivityScope != datastore.GlobalScope || nodes == nil {
return return
} }
@ -722,22 +722,46 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
} }
network.processOptions(options...) network.processOptions(options...)
if err := network.validateConfiguration(); err != nil {
return nil, err
}
_, cap, err := network.resolveDriver(networkType, true) var (
cap *driverapi.Capability
err error
)
// 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"
network.ipamType = ""
goto addToStore
}
_, cap, err = network.resolveDriver(network.networkType, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if network.scope == datastore.LocalScope && cap.DataScope == datastore.GlobalScope {
return nil, types.ForbiddenErrorf("cannot downgrade network scope for %s networks", networkType)
}
if network.ingress && cap.DataScope != datastore.GlobalScope { if network.ingress && cap.DataScope != datastore.GlobalScope {
return nil, types.ForbiddenErrorf("Ingress network can only be global scope network") return nil, types.ForbiddenErrorf("Ingress network can only be global scope network")
} }
if cap.DataScope == datastore.GlobalScope && !c.isDistributedControl() && !network.dynamic { // 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 {
if c.isManager() { if c.isManager() {
// For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager // For non-distributed controlled environment, globalscoped non-dynamic networks are redirected to Manager
return nil, ManagerRedirectError(name) 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.") return nil, types.ForbiddenErrorf("Cannot create a multi-host network from a worker node. Please create the network from a manager node.")
} }
@ -747,6 +771,26 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
return nil, err return nil, err
} }
// From this point on, we need the network specific configuration,
// which may come from a configuration-only network
if network.configFrom != "" {
t, err := c.getConfigNetwork(network.configFrom)
if err != nil {
return nil, types.NotFoundErrorf("configuration network %q does not exist", network.configFrom)
}
if err := t.applyConfigurationTo(network); err != nil {
return nil, types.InternalErrorf("Failed to apply configuration: %v", err)
}
defer func() {
if err == nil {
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)
}
}
}()
}
err = network.ipamAllocate() err = network.ipamAllocate()
if err != nil { if err != nil {
return nil, err return nil, err
@ -769,6 +813,7 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
} }
}() }()
addToStore:
// First store the endpoint count, then the network. To avoid to // First store the endpoint count, then the network. To avoid to
// end up with a datastore containing a network and not an epCnt, // end up with a datastore containing a network and not an epCnt,
// in case of an ungraceful shutdown during this function call. // in case of an ungraceful shutdown during this function call.
@ -788,6 +833,9 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
if err = c.updateToStore(network); err != nil { if err = c.updateToStore(network); err != nil {
return nil, err return nil, err
} }
if network.configOnly {
return network, nil
}
joinCluster(network) joinCluster(network)
if !c.isDistributedControl() { if !c.isDistributedControl() {
@ -796,11 +844,18 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ...
c.Unlock() c.Unlock()
} }
c.Lock()
arrangeUserFilterRule()
c.Unlock()
return network, nil return network, nil
} }
var joinCluster NetworkWalker = func(nw Network) bool { var joinCluster NetworkWalker = func(nw Network) bool {
n := nw.(*network) n := nw.(*network)
if n.configOnly {
return false
}
if err := n.joinCluster(); err != nil { if err := n.joinCluster(); err != nil {
logrus.Errorf("Failed to join network %s (%s) into agent cluster: %v", n.Name(), n.ID(), err) logrus.Errorf("Failed to join network %s (%s) into agent cluster: %v", n.Name(), n.ID(), err)
} }
@ -816,6 +871,9 @@ func (c *controller) reservePools() {
} }
for _, n := range networks { for _, n := range networks {
if n.configOnly {
continue
}
if !doReplayPoolReserve(n) { if !doReplayPoolReserve(n) {
continue continue
} }

View file

@ -115,7 +115,10 @@ const (
// LocalScope indicates to store the KV object in local datastore such as boltdb // LocalScope indicates to store the KV object in local datastore such as boltdb
LocalScope = "local" LocalScope = "local"
// GlobalScope indicates to store the KV object in global datastore such as consul/etcd/zookeeper // GlobalScope indicates to store the KV object in global datastore such as consul/etcd/zookeeper
GlobalScope = "global" GlobalScope = "global"
// SwarmScope is not indicating a datastore location. It is defined here
// along with the other two scopes just for consistency.
SwarmScope = "swarm"
defaultPrefix = "/var/lib/docker/network/files" defaultPrefix = "/var/lib/docker/network/files"
) )

View file

@ -161,7 +161,8 @@ type DriverCallback interface {
// Capability represents the high level capabilities of the drivers which libnetwork can make use of // Capability represents the high level capabilities of the drivers which libnetwork can make use of
type Capability struct { type Capability struct {
DataScope string DataScope string
ConnectivityScope string
} }
// IPAMData represents the per-network ip related // IPAMData represents the per-network ip related

View file

@ -153,7 +153,8 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
} }
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
ConnectivityScope: datastore.LocalScope,
} }
return dc.RegisterDriver(networkType, d, c) return dc.RegisterDriver(networkType, d, c)
} }

View file

@ -0,0 +1,88 @@
package brmanager
import (
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/types"
)
const networkType = "bridge"
type driver struct{}
// Init registers a new instance of bridge manager driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{
DataScope: datastore.LocalScope,
ConnectivityScope: datastore.LocalScope,
}
return dc.RegisterDriver(networkType, &driver{}, c)
}
func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DeleteEndpoint(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Leave(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Type() string {
return networkType
}
func (d *driver) IsBuiltIn() bool {
return true
}
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}

View file

@ -53,7 +53,7 @@ func setupIPChains(config *configuration) (*iptables.ChainInfo, *iptables.ChainI
return nil, nil, nil, fmt.Errorf("failed to create FILTER isolation chain: %v", err) return nil, nil, nil, fmt.Errorf("failed to create FILTER isolation chain: %v", err)
} }
if err := addReturnRule(IsolationChain); err != nil { if err := iptables.AddReturnRule(IsolationChain); err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -117,7 +117,7 @@ func (n *bridgeNetwork) setupIPTables(config *networkConfiguration, i *bridgeInt
} }
d.Lock() d.Lock()
err = ensureJumpRule("FORWARD", IsolationChain) err = iptables.EnsureJumpRule("FORWARD", IsolationChain)
d.Unlock() d.Unlock()
if err != nil { if err != nil {
return err return err
@ -280,46 +280,6 @@ func setINC(iface1, iface2 string, enable bool) error {
return nil return nil
} }
func addReturnRule(chain string) error {
var (
table = iptables.Filter
args = []string{"-j", "RETURN"}
)
if iptables.Exists(table, chain, args...) {
return nil
}
err := iptables.RawCombinedOutput(append([]string{"-I", chain}, args...)...)
if err != nil {
return fmt.Errorf("unable to add return rule in %s chain: %s", chain, err.Error())
}
return nil
}
// Ensure the jump rule is on top
func ensureJumpRule(fromChain, toChain string) error {
var (
table = iptables.Filter
args = []string{"-j", toChain}
)
if iptables.Exists(table, fromChain, args...) {
err := iptables.RawCombinedOutput(append([]string{"-D", fromChain}, args...)...)
if err != nil {
return fmt.Errorf("unable to remove jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
}
}
err := iptables.RawCombinedOutput(append([]string{"-I", fromChain}, args...)...)
if err != nil {
return fmt.Errorf("unable to insert jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
}
return nil
}
func removeIPChains() { func removeIPChains() {
for _, chainInfo := range []iptables.ChainInfo{ for _, chainInfo := range []iptables.ChainInfo{
{Name: DockerChain, Table: iptables.Nat}, {Name: DockerChain, Table: iptables.Nat},

View file

@ -19,7 +19,8 @@ type driver struct {
// Init registers a new instance of host driver // Init registers a new instance of host driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
ConnectivityScope: datastore.LocalScope,
} }
return dc.RegisterDriver(networkType, &driver{}, c) return dc.RegisterDriver(networkType, &driver{}, c)
} }

View file

@ -58,7 +58,8 @@ type network struct {
// Init initializes and registers the libnetwork ipvlan driver // Init initializes and registers the libnetwork ipvlan driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{
networks: networkTable{}, networks: networkTable{},

View file

@ -0,0 +1,88 @@
package ivmanager
import (
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/types"
)
const networkType = "ipvlan"
type driver struct{}
// Init registers a new instance of ipvlan manager driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{
DataScope: datastore.LocalScope,
ConnectivityScope: datastore.GlobalScope,
}
return dc.RegisterDriver(networkType, &driver{}, c)
}
func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DeleteEndpoint(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Leave(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Type() string {
return networkType
}
func (d *driver) IsBuiltIn() bool {
return true
}
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}

View file

@ -60,7 +60,8 @@ type network struct {
// Init initializes and registers the libnetwork macvlan driver // Init initializes and registers the libnetwork macvlan driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{
networks: networkTable{}, networks: networkTable{},

View file

@ -0,0 +1,88 @@
package mvmanager
import (
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/driverapi"
"github.com/docker/libnetwork/types"
)
const networkType = "macvlan"
type driver struct{}
// Init registers a new instance of macvlan manager driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{
DataScope: datastore.LocalScope,
ConnectivityScope: datastore.GlobalScope,
}
return dc.RegisterDriver(networkType, &driver{}, c)
}
func (d *driver) NetworkAllocate(id string, option map[string]string, ipV4Data, ipV6Data []driverapi.IPAMData) (map[string]string, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) NetworkFree(id string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EventNotify(etype driverapi.EventType, nid, tableName, key string, value []byte) {
}
func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (string, map[string]string) {
return "", nil
}
func (d *driver) DeleteNetwork(nid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, epOptions map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DeleteEndpoint(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) EndpointOperInfo(nid, eid string) (map[string]interface{}, error) {
return nil, types.NotImplementedErrorf("not implemented")
}
func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Leave(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) Type() string {
return networkType
}
func (d *driver) IsBuiltIn() bool {
return true
}
func (d *driver) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) ProgramExternalConnectivity(nid, eid string, options map[string]interface{}) error {
return types.NotImplementedErrorf("not implemented")
}
func (d *driver) RevokeExternalConnectivity(nid, eid string) error {
return types.NotImplementedErrorf("not implemented")
}

View file

@ -56,7 +56,8 @@ type driver struct {
// Init registers a new instance of overlay driver // Init registers a new instance of overlay driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.GlobalScope, DataScope: datastore.GlobalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{
networks: networkTable{}, networks: networkTable{},

View file

@ -49,7 +49,8 @@ type network struct {
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
var err error var err error
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.GlobalScope, DataScope: datastore.GlobalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{

View file

@ -24,7 +24,8 @@ func (r *Response) GetError() string {
// GetCapabilityResponse is the response of GetCapability request // GetCapabilityResponse is the response of GetCapability request
type GetCapabilityResponse struct { type GetCapabilityResponse struct {
Response Response
Scope string Scope string
ConnectivityScope string
} }
// AllocateNetworkRequest requests allocation of new network by manager // AllocateNetworkRequest requests allocation of new network by manager

View file

@ -74,6 +74,17 @@ func (d *driver) getCapabilities() (*driverapi.Capability, error) {
return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope) return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope)
} }
switch capResp.ConnectivityScope {
case "global":
c.ConnectivityScope = datastore.GlobalScope
case "local":
c.ConnectivityScope = datastore.LocalScope
case "":
c.ConnectivityScope = c.DataScope
default:
return nil, fmt.Errorf("invalid capability: expecting 'local' or 'global', got %s", capResp.Scope)
}
return c, nil return c, nil
} }

View file

@ -159,7 +159,8 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
} }
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.LocalScope, DataScope: datastore.LocalScope,
ConnectivityScope: datastore.LocalScope,
} }
return dc.RegisterDriver(networkType, d, c) return dc.RegisterDriver(networkType, d, c)
} }

View file

@ -57,7 +57,8 @@ type driver struct {
// Init registers a new instance of overlay driver // Init registers a new instance of overlay driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.GlobalScope, DataScope: datastore.GlobalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{
networks: networkTable{}, networks: networkTable{},

View file

@ -36,7 +36,8 @@ type driver struct {
// Init registers a new instance of overlay driver // Init registers a new instance of overlay driver
func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error {
c := driverapi.Capability{ c := driverapi.Capability{
DataScope: datastore.GlobalScope, DataScope: datastore.GlobalScope,
ConnectivityScope: datastore.GlobalScope,
} }
d := &driver{ d := &driver{

View file

@ -41,6 +41,8 @@ type networkConfiguration struct {
DNSSuffix string DNSSuffix string
SourceMac string SourceMac string
NetworkAdapterName string NetworkAdapterName string
dbIndex uint64
dbExists bool
} }
// endpointConfiguration represents the user specified configuration for the sandbox endpoint // endpointConfiguration represents the user specified configuration for the sandbox endpoint
@ -59,17 +61,22 @@ type endpointConnectivity struct {
type hnsEndpoint struct { type hnsEndpoint struct {
id string id string
nid string
profileID string profileID string
Type string
macAddress net.HardwareAddr macAddress net.HardwareAddr
epOption *endpointOption // User specified parameters epOption *endpointOption // User specified parameters
epConnectivity *endpointConnectivity // User specified parameters epConnectivity *endpointConnectivity // User specified parameters
portMapping []types.PortBinding // Operation port bindings portMapping []types.PortBinding // Operation port bindings
addr *net.IPNet addr *net.IPNet
gateway net.IP gateway net.IP
dbIndex uint64
dbExists bool
} }
type hnsNetwork struct { type hnsNetwork struct {
id string id string
created bool
config *networkConfiguration config *networkConfiguration
endpoints map[string]*hnsEndpoint // key: endpoint id endpoints map[string]*hnsEndpoint // key: endpoint id
driver *driver // The network's driver driver *driver // The network's driver
@ -79,9 +86,14 @@ type hnsNetwork struct {
type driver struct { type driver struct {
name string name string
networks map[string]*hnsNetwork networks map[string]*hnsNetwork
store datastore.DataStore
sync.Mutex sync.Mutex
} }
const (
errNotFound = "HNS failed with error : The object identifier does not represent a valid object. "
)
// IsBuiltinWindowsDriver vaidates if network-type is a builtin local-scoped driver // IsBuiltinWindowsDriver vaidates if network-type is a builtin local-scoped driver
func IsBuiltinLocalDriver(networkType string) bool { func IsBuiltinLocalDriver(networkType string) bool {
if "l2bridge" == networkType || "l2tunnel" == networkType || "nat" == networkType || "ics" == networkType || "transparent" == networkType { if "l2bridge" == networkType || "l2tunnel" == networkType || "nat" == networkType || "ics" == networkType || "transparent" == networkType {
@ -103,8 +115,16 @@ func GetInit(networkType string) func(dc driverapi.DriverCallback, config map[st
return types.BadRequestErrorf("Network type not supported: %s", networkType) return types.BadRequestErrorf("Network type not supported: %s", networkType)
} }
return dc.RegisterDriver(networkType, newDriver(networkType), driverapi.Capability{ d := newDriver(networkType)
DataScope: datastore.LocalScope,
err := d.initStore(config)
if err != nil {
return err
}
return dc.RegisterDriver(networkType, d, driverapi.Capability{
DataScope: datastore.LocalScope,
ConnectivityScope: datastore.LocalScope,
}) })
} }
} }
@ -132,7 +152,7 @@ func (n *hnsNetwork) getEndpoint(eid string) (*hnsEndpoint, error) {
} }
func (d *driver) parseNetworkOptions(id string, genericOptions map[string]string) (*networkConfiguration, error) { func (d *driver) parseNetworkOptions(id string, genericOptions map[string]string) (*networkConfiguration, error) {
config := &networkConfiguration{} config := &networkConfiguration{Type: d.name}
for label, value := range genericOptions { for label, value := range genericOptions {
switch label { switch label {
@ -187,6 +207,21 @@ func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (s
return "", nil return "", nil
} }
func (d *driver) createNetwork(config *networkConfiguration) error {
network := &hnsNetwork{
id: config.ID,
endpoints: make(map[string]*hnsEndpoint),
config: config,
driver: d,
}
d.Lock()
d.networks[config.ID] = network
d.Unlock()
return nil
}
// Create a new network // Create a new network
func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error { func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo driverapi.NetworkInfo, ipV4Data, ipV6Data []driverapi.IPAMData) error {
if _, err := d.getNetwork(id); err == nil { if _, err := d.getNetwork(id); err == nil {
@ -209,16 +244,11 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
return err return err
} }
network := &hnsNetwork{ err = d.createNetwork(config)
id: config.ID,
endpoints: make(map[string]*hnsEndpoint),
config: config,
driver: d,
}
d.Lock() if err != nil {
d.networks[config.ID] = network return err
d.Unlock() }
// A non blank hnsid indicates that the network was discovered // A non blank hnsid indicates that the network was discovered
// from HNS. No need to call HNS if this network was discovered // from HNS. No need to call HNS if this network was discovered
@ -293,7 +323,9 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
genData[HNSID] = config.HnsID genData[HNSID] = config.HnsID
} }
return nil n, err := d.getNetwork(id)
n.created = true
return d.storeUpdate(config)
} }
func (d *driver) DeleteNetwork(nid string) error { func (d *driver) DeleteNetwork(nid string) error {
@ -306,21 +338,25 @@ func (d *driver) DeleteNetwork(nid string) error {
config := n.config config := n.config
n.Unlock() n.Unlock()
// Cannot remove network if endpoints are still present if n.created {
if len(n.endpoints) != 0 { _, err = hcsshim.HNSNetworkRequest("DELETE", config.HnsID, "")
return fmt.Errorf("network %s has active endpoint", n.id) if err != nil && err.Error() != errNotFound {
} return types.ForbiddenErrorf(err.Error())
}
_, err = hcsshim.HNSNetworkRequest("DELETE", config.HnsID, "")
if err != nil {
return types.ForbiddenErrorf(err.Error())
} }
d.Lock() d.Lock()
delete(d.networks, nid) delete(d.networks, nid)
d.Unlock() d.Unlock()
return nil // delele endpoints belong to this network
for _, ep := range n.endpoints {
if err := d.storeDelete(ep); err != nil {
logrus.Warnf("Failed to remove bridge endpoint %s from store: %v", ep.id[0:7], err)
}
}
return d.storeDelete(config)
} }
func convertQosPolicies(qosPolicies []types.QosPolicy) ([]json.RawMessage, error) { func convertQosPolicies(qosPolicies []types.QosPolicy) ([]json.RawMessage, error) {
@ -543,6 +579,8 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
// TODO For now the ip mask is not in the info generated by HNS // TODO For now the ip mask is not in the info generated by HNS
endpoint := &hnsEndpoint{ endpoint := &hnsEndpoint{
id: eid, id: eid,
nid: n.id,
Type: d.name,
addr: &net.IPNet{IP: hnsresponse.IPAddress, Mask: hnsresponse.IPAddress.DefaultMask()}, addr: &net.IPNet{IP: hnsresponse.IPAddress, Mask: hnsresponse.IPAddress.DefaultMask()},
macAddress: mac, macAddress: mac,
} }
@ -573,6 +611,10 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo,
ifInfo.SetMacAddress(endpoint.macAddress) ifInfo.SetMacAddress(endpoint.macAddress)
} }
if err = d.storeUpdate(endpoint); err != nil {
return fmt.Errorf("failed to save endpoint %s to store: %v", endpoint.id[0:7], err)
}
return nil return nil
} }
@ -592,10 +634,13 @@ func (d *driver) DeleteEndpoint(nid, eid string) error {
n.Unlock() n.Unlock()
_, err = hcsshim.HNSEndpointRequest("DELETE", ep.profileID, "") _, err = hcsshim.HNSEndpointRequest("DELETE", ep.profileID, "")
if err != nil { if err != nil && err.Error() != errNotFound {
return err return err
} }
if err := d.storeDelete(ep); err != nil {
logrus.Warnf("Failed to remove bridge endpoint %s from store: %v", ep.id[0:7], err)
}
return nil return nil
} }

View file

@ -0,0 +1,335 @@
// +build windows
package windows
import (
"encoding/json"
"fmt"
"net"
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/discoverapi"
"github.com/docker/libnetwork/netlabel"
"github.com/docker/libnetwork/types"
)
const (
windowsPrefix = "windows"
windowsEndpointPrefix = "windows-endpoint"
)
func (d *driver) initStore(option map[string]interface{}) error {
if data, ok := option[netlabel.LocalKVClient]; ok {
var err error
dsc, ok := data.(discoverapi.DatastoreConfigData)
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
d.store, err = datastore.NewDataStoreFromConfig(dsc)
if err != nil {
return types.InternalErrorf("windows driver failed to initialize data store: %v", err)
}
err = d.populateNetworks()
if err != nil {
return err
}
err = d.populateEndpoints()
if err != nil {
return err
}
}
return nil
}
func (d *driver) populateNetworks() error {
kvol, err := d.store.List(datastore.Key(windowsPrefix), &networkConfiguration{Type: d.name})
if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get windows network configurations from store: %v", err)
}
// It's normal for network configuration state to be empty. Just return.
if err == datastore.ErrKeyNotFound {
return nil
}
for _, kvo := range kvol {
ncfg := kvo.(*networkConfiguration)
if ncfg.Type != d.name {
continue
}
if err = d.createNetwork(ncfg); err != nil {
logrus.Warnf("could not create windows network for id %s hnsid %s while booting up from persistent state: %v", ncfg.ID, ncfg.HnsID, err)
}
logrus.Debugf("Network (%s) restored", ncfg.ID[0:7])
}
return nil
}
func (d *driver) populateEndpoints() error {
kvol, err := d.store.List(datastore.Key(windowsEndpointPrefix), &hnsEndpoint{Type: d.name})
if err != nil && err != datastore.ErrKeyNotFound {
return fmt.Errorf("failed to get endpoints from store: %v", err)
}
if err == datastore.ErrKeyNotFound {
return nil
}
for _, kvo := range kvol {
ep := kvo.(*hnsEndpoint)
if ep.Type != d.name {
continue
}
n, ok := d.networks[ep.nid]
if !ok {
logrus.Debugf("Network (%s) not found for restored endpoint (%s)", ep.nid[0:7], ep.id[0:7])
logrus.Debugf("Deleting stale endpoint (%s) from store", ep.id[0:7])
if err := d.storeDelete(ep); err != nil {
logrus.Debugf("Failed to delete stale endpoint (%s) from store", ep.id[0:7])
}
continue
}
n.endpoints[ep.id] = ep
logrus.Debugf("Endpoint (%s) restored to network (%s)", ep.id[0:7], ep.nid[0:7])
}
return nil
}
func (d *driver) storeUpdate(kvObject datastore.KVObject) error {
if d.store == nil {
logrus.Warnf("store not initialized. kv object %s is not added to the store", datastore.Key(kvObject.Key()...))
return nil
}
if err := d.store.PutObjectAtomic(kvObject); err != nil {
return fmt.Errorf("failed to update store for object type %T: %v", kvObject, err)
}
return nil
}
func (d *driver) storeDelete(kvObject datastore.KVObject) error {
if d.store == nil {
logrus.Debugf("store not initialized. kv object %s is not deleted from store", datastore.Key(kvObject.Key()...))
return nil
}
retry:
if err := d.store.DeleteObjectAtomic(kvObject); err != nil {
if err == datastore.ErrKeyModified {
if err := d.store.GetObject(datastore.Key(kvObject.Key()...), kvObject); err != nil {
return fmt.Errorf("could not update the kvobject to latest when trying to delete: %v", err)
}
goto retry
}
return err
}
return nil
}
func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
nMap := make(map[string]interface{})
nMap["ID"] = ncfg.ID
nMap["Type"] = ncfg.Type
nMap["Name"] = ncfg.Name
nMap["HnsID"] = ncfg.HnsID
nMap["VLAN"] = ncfg.VLAN
nMap["VSID"] = ncfg.VSID
nMap["DNSServers"] = ncfg.DNSServers
nMap["DNSSuffix"] = ncfg.DNSSuffix
nMap["SourceMac"] = ncfg.SourceMac
nMap["NetworkAdapterName"] = ncfg.NetworkAdapterName
return json.Marshal(nMap)
}
func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
var (
err error
nMap map[string]interface{}
)
if err = json.Unmarshal(b, &nMap); err != nil {
return err
}
ncfg.ID = nMap["ID"].(string)
ncfg.Type = nMap["Type"].(string)
ncfg.Name = nMap["Name"].(string)
ncfg.HnsID = nMap["HnsID"].(string)
ncfg.VLAN = uint(nMap["VLAN"].(float64))
ncfg.VSID = uint(nMap["VSID"].(float64))
ncfg.DNSServers = nMap["DNSServers"].(string)
ncfg.DNSSuffix = nMap["DNSSuffix"].(string)
ncfg.SourceMac = nMap["SourceMac"].(string)
ncfg.NetworkAdapterName = nMap["NetworkAdapterName"].(string)
return nil
}
func (ncfg *networkConfiguration) Key() []string {
return []string{windowsPrefix + ncfg.Type, ncfg.ID}
}
func (ncfg *networkConfiguration) KeyPrefix() []string {
return []string{windowsPrefix + ncfg.Type}
}
func (ncfg *networkConfiguration) Value() []byte {
b, err := json.Marshal(ncfg)
if err != nil {
return nil
}
return b
}
func (ncfg *networkConfiguration) SetValue(value []byte) error {
return json.Unmarshal(value, ncfg)
}
func (ncfg *networkConfiguration) Index() uint64 {
return ncfg.dbIndex
}
func (ncfg *networkConfiguration) SetIndex(index uint64) {
ncfg.dbIndex = index
ncfg.dbExists = true
}
func (ncfg *networkConfiguration) Exists() bool {
return ncfg.dbExists
}
func (ncfg *networkConfiguration) Skip() bool {
return false
}
func (ncfg *networkConfiguration) New() datastore.KVObject {
return &networkConfiguration{Type: ncfg.Type}
}
func (ncfg *networkConfiguration) CopyTo(o datastore.KVObject) error {
dstNcfg := o.(*networkConfiguration)
*dstNcfg = *ncfg
return nil
}
func (ncfg *networkConfiguration) DataScope() string {
return datastore.LocalScope
}
func (ep *hnsEndpoint) MarshalJSON() ([]byte, error) {
epMap := make(map[string]interface{})
epMap["id"] = ep.id
epMap["nid"] = ep.nid
epMap["Type"] = ep.Type
epMap["profileID"] = ep.profileID
epMap["MacAddress"] = ep.macAddress.String()
epMap["Addr"] = ep.addr.String()
epMap["gateway"] = ep.gateway.String()
epMap["epOption"] = ep.epOption
epMap["epConnectivity"] = ep.epConnectivity
epMap["PortMapping"] = ep.portMapping
return json.Marshal(epMap)
}
func (ep *hnsEndpoint) UnmarshalJSON(b []byte) error {
var (
err error
epMap map[string]interface{}
)
if err = json.Unmarshal(b, &epMap); err != nil {
return fmt.Errorf("Failed to unmarshal to endpoint: %v", err)
}
if v, ok := epMap["MacAddress"]; ok {
if ep.macAddress, err = net.ParseMAC(v.(string)); err != nil {
return types.InternalErrorf("failed to decode endpoint MAC address (%s) after json unmarshal: %v", v.(string), err)
}
}
if v, ok := epMap["Addr"]; ok {
if ep.addr, err = types.ParseCIDR(v.(string)); err != nil {
return types.InternalErrorf("failed to decode endpoint IPv4 address (%s) after json unmarshal: %v", v.(string), err)
}
}
ep.id = epMap["id"].(string)
ep.Type = epMap["Type"].(string)
ep.nid = epMap["nid"].(string)
ep.profileID = epMap["profileID"].(string)
d, _ := json.Marshal(epMap["epOption"])
if err := json.Unmarshal(d, &ep.epOption); err != nil {
logrus.Warnf("Failed to decode endpoint container config %v", err)
}
d, _ = json.Marshal(epMap["epConnectivity"])
if err := json.Unmarshal(d, &ep.epConnectivity); err != nil {
logrus.Warnf("Failed to decode endpoint external connectivity configuration %v", err)
}
d, _ = json.Marshal(epMap["PortMapping"])
if err := json.Unmarshal(d, &ep.portMapping); err != nil {
logrus.Warnf("Failed to decode endpoint port mapping %v", err)
}
return nil
}
func (ep *hnsEndpoint) Key() []string {
return []string{windowsEndpointPrefix + ep.Type, ep.id}
}
func (ep *hnsEndpoint) KeyPrefix() []string {
return []string{windowsEndpointPrefix + ep.Type}
}
func (ep *hnsEndpoint) Value() []byte {
b, err := json.Marshal(ep)
if err != nil {
return nil
}
return b
}
func (ep *hnsEndpoint) SetValue(value []byte) error {
return json.Unmarshal(value, ep)
}
func (ep *hnsEndpoint) Index() uint64 {
return ep.dbIndex
}
func (ep *hnsEndpoint) SetIndex(index uint64) {
ep.dbIndex = index
ep.dbExists = true
}
func (ep *hnsEndpoint) Exists() bool {
return ep.dbExists
}
func (ep *hnsEndpoint) Skip() bool {
return false
}
func (ep *hnsEndpoint) New() datastore.KVObject {
return &hnsEndpoint{Type: ep.Type}
}
func (ep *hnsEndpoint) CopyTo(o datastore.KVObject) error {
dstEp := o.(*hnsEndpoint)
*dstEp = *ep
return nil
}
func (ep *hnsEndpoint) DataScope() string {
return datastore.LocalScope
}

View file

@ -1152,6 +1152,9 @@ func (c *controller) cleanupLocalEndpoints() {
} }
for _, n := range nl { for _, n := range nl {
if n.ConfigOnly() {
continue
}
epl, err := n.getEndpointsFromStore() epl, err := n.getEndpointsFromStore()
if err != nil { if err != nil {
logrus.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err) logrus.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err)

29
vendor/github.com/docker/libnetwork/firewall_linux.go generated vendored Normal file
View file

@ -0,0 +1,29 @@
package libnetwork
import (
"github.com/Sirupsen/logrus"
"github.com/docker/libnetwork/iptables"
)
const userChain = "DOCKER-USER"
// This chain allow users to configure firewall policies in a way that persists
// docker operations/restarts. Docker will not delete or modify any pre-existing
// rules from the DOCKER-USER filter chain.
func arrangeUserFilterRule() {
_, err := iptables.NewChain(userChain, iptables.Filter, false)
if err != nil {
logrus.Warnf("Failed to create %s chain: %v", userChain, err)
return
}
if err = iptables.AddReturnRule(userChain); err != nil {
logrus.Warnf("Failed to add the RETURN rule for %s: %v", userChain, err)
return
}
err = iptables.EnsureJumpRule("FORWARD", userChain)
if err != nil {
logrus.Warnf("Failed to ensure the jump rule for %s: %v", userChain, err)
}
}

View file

@ -0,0 +1,6 @@
// +build !linux
package libnetwork
func arrangeUserFilterRule() {
}

View file

@ -498,3 +498,44 @@ func parseVersionNumbers(input string) (major, minor, micro int) {
func supportsCOption(mj, mn, mc int) bool { func supportsCOption(mj, mn, mc int) bool {
return mj > 1 || (mj == 1 && (mn > 4 || (mn == 4 && mc >= 11))) return mj > 1 || (mj == 1 && (mn > 4 || (mn == 4 && mc >= 11)))
} }
// AddReturnRule adds a return rule for the chain in the filter table
func AddReturnRule(chain string) error {
var (
table = Filter
args = []string{"-j", "RETURN"}
)
if Exists(table, chain, args...) {
return nil
}
err := RawCombinedOutput(append([]string{"-A", chain}, args...)...)
if err != nil {
return fmt.Errorf("unable to add return rule in %s chain: %s", chain, err.Error())
}
return nil
}
// EnsureJumpRule ensures the jump rule is on top
func EnsureJumpRule(fromChain, toChain string) error {
var (
table = Filter
args = []string{"-j", toChain}
)
if Exists(table, fromChain, args...) {
err := RawCombinedOutput(append([]string{"-D", fromChain}, args...)...)
if err != nil {
return fmt.Errorf("unable to remove jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
}
}
err := RawCombinedOutput(append([]string{"-I", fromChain}, args...)...)
if err != nil {
return fmt.Errorf("unable to insert jump to %s rule in %s chain: %s", toChain, fromChain, err.Error())
}
return nil
}

View file

@ -67,6 +67,8 @@ type NetworkInfo interface {
Internal() bool Internal() bool
Attachable() bool Attachable() bool
Ingress() bool Ingress() bool
ConfigFrom() string
ConfigOnly() bool
Labels() map[string]string Labels() map[string]string
Dynamic() bool Dynamic() bool
Created() time.Time Created() time.Time
@ -193,7 +195,7 @@ type network struct {
networkType string networkType string
id string id string
created time.Time created time.Time
scope string scope string // network data scope
labels map[string]string labels map[string]string
ipamType string ipamType string
ipamOptions map[string]string ipamOptions map[string]string
@ -219,6 +221,8 @@ type network struct {
ingress bool ingress bool
driverTables []networkDBTable driverTables []networkDBTable
dynamic bool dynamic bool
configOnly bool
configFrom string
sync.Mutex sync.Mutex
} }
@ -348,6 +352,95 @@ func (i *IpamInfo) CopyTo(dstI *IpamInfo) error {
return nil return nil
} }
func (n *network) validateConfiguration() error {
if n.configOnly {
// Only supports network specific configurations.
// Network operator configurations are not supported.
if n.ingress || n.internal || n.attachable {
return types.ForbiddenErrorf("configuration network can only contain network " +
"specific fields. Network operator fields like " +
"[ ingress | internal | attachable ] are not supported.")
}
}
if n.configFrom != "" {
if n.configOnly {
return types.ForbiddenErrorf("a configuration network cannot depend on another configuration network")
}
if n.ipamType != "" &&
n.ipamType != defaultIpamForNetworkType(n.networkType) ||
n.enableIPv6 ||
len(n.labels) > 0 || len(n.ipamOptions) > 0 ||
len(n.ipamV4Config) > 0 || len(n.ipamV6Config) > 0 {
return types.ForbiddenErrorf("user specified configurations are not supported if the network depends on a configuration network")
}
if len(n.generic) > 0 {
if data, ok := n.generic[netlabel.GenericData]; ok {
var (
driverOptions map[string]string
opts interface{}
)
switch data.(type) {
case map[string]interface{}:
opts = data.(map[string]interface{})
case map[string]string:
opts = data.(map[string]string)
}
ba, err := json.Marshal(opts)
if err != nil {
return fmt.Errorf("failed to validate network configuration: %v", err)
}
if err := json.Unmarshal(ba, &driverOptions); err != nil {
return fmt.Errorf("failed to validate network configuration: %v", err)
}
if len(driverOptions) > 0 {
return types.ForbiddenErrorf("network driver options are not supported if the network depends on a configuration network")
}
}
}
}
return nil
}
// Applies network specific configurations
func (n *network) applyConfigurationTo(to *network) error {
to.enableIPv6 = n.enableIPv6
if len(n.labels) > 0 {
to.labels = make(map[string]string, len(n.labels))
for k, v := range n.labels {
if _, ok := to.labels[k]; !ok {
to.labels[k] = v
}
}
}
if len(n.ipamOptions) > 0 {
to.ipamOptions = make(map[string]string, len(n.ipamOptions))
for k, v := range n.ipamOptions {
if _, ok := to.ipamOptions[k]; !ok {
to.ipamOptions[k] = v
}
}
}
if len(n.ipamV4Config) > 0 {
to.ipamV4Config = make([]*IpamConf, 0, len(n.ipamV4Config))
for _, v4conf := range n.ipamV4Config {
to.ipamV4Config = append(to.ipamV4Config, v4conf)
}
}
if len(n.ipamV6Config) > 0 {
to.ipamV6Config = make([]*IpamConf, 0, len(n.ipamV6Config))
for _, v6conf := range n.ipamV6Config {
to.ipamV6Config = append(to.ipamV6Config, v6conf)
}
}
if len(n.generic) > 0 {
to.generic = options.Generic{}
for k, v := range n.generic {
to.generic[k] = v
}
}
return nil
}
func (n *network) CopyTo(o datastore.KVObject) error { func (n *network) CopyTo(o datastore.KVObject) error {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
@ -370,6 +463,8 @@ func (n *network) CopyTo(o datastore.KVObject) error {
dstN.attachable = n.attachable dstN.attachable = n.attachable
dstN.inDelete = n.inDelete dstN.inDelete = n.inDelete
dstN.ingress = n.ingress dstN.ingress = n.ingress
dstN.configOnly = n.configOnly
dstN.configFrom = n.configFrom
// copy labels // copy labels
if dstN.labels == nil { if dstN.labels == nil {
@ -419,7 +514,12 @@ func (n *network) CopyTo(o datastore.KVObject) error {
} }
func (n *network) DataScope() string { func (n *network) DataScope() string {
return n.Scope() s := n.Scope()
// All swarm scope networks have local datascope
if s == datastore.SwarmScope {
s = datastore.LocalScope
}
return s
} }
func (n *network) getEpCnt() *endpointCnt { func (n *network) getEpCnt() *endpointCnt {
@ -479,6 +579,8 @@ func (n *network) MarshalJSON() ([]byte, error) {
netMap["attachable"] = n.attachable netMap["attachable"] = n.attachable
netMap["inDelete"] = n.inDelete netMap["inDelete"] = n.inDelete
netMap["ingress"] = n.ingress netMap["ingress"] = n.ingress
netMap["configOnly"] = n.configOnly
netMap["configFrom"] = n.configFrom
return json.Marshal(netMap) return json.Marshal(netMap)
} }
@ -583,6 +685,12 @@ func (n *network) UnmarshalJSON(b []byte) (err error) {
if v, ok := netMap["ingress"]; ok { if v, ok := netMap["ingress"]; ok {
n.ingress = v.(bool) n.ingress = v.(bool)
} }
if v, ok := netMap["configOnly"]; ok {
n.configOnly = v.(bool)
}
if v, ok := netMap["configFrom"]; ok {
n.configFrom = v.(string)
}
// Reconcile old networks with the recently added `--ipv6` flag // Reconcile old networks with the recently added `--ipv6` flag
if !n.enableIPv6 { if !n.enableIPv6 {
n.enableIPv6 = len(n.ipamV6Info) > 0 n.enableIPv6 = len(n.ipamV6Info) > 0
@ -659,6 +767,14 @@ func NetworkOptionAttachable(attachable bool) NetworkOption {
} }
} }
// NetworkOptionScope returns an option setter to overwrite the network's scope.
// By default the network's scope is set to the network driver's datascope.
func NetworkOptionScope(scope string) NetworkOption {
return func(n *network) {
n.scope = scope
}
}
// NetworkOptionIpam function returns an option setter for the ipam configuration for this network // NetworkOptionIpam function returns an option setter for the ipam configuration for this network
func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption { func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption {
return func(n *network) { return func(n *network) {
@ -713,6 +829,23 @@ func NetworkOptionDeferIPv6Alloc(enable bool) NetworkOption {
} }
} }
// NetworkOptionConfigOnly tells controller this network is
// a configuration only network. It serves as a configuration
// for other networks.
func NetworkOptionConfigOnly() NetworkOption {
return func(n *network) {
n.configOnly = true
}
}
// NetworkOptionConfigFrom tells controller to pick the
// network configuration from a configuration only network
func NetworkOptionConfigFrom(name string) NetworkOption {
return func(n *network) {
n.configFrom = name
}
}
func (n *network) processOptions(options ...NetworkOption) { func (n *network) processOptions(options ...NetworkOption) {
for _, opt := range options { for _, opt := range options {
if opt != nil { if opt != nil {
@ -757,6 +890,14 @@ func (n *network) driverScope() string {
return cap.DataScope return cap.DataScope
} }
func (n *network) driverIsMultihost() bool {
_, cap, err := n.resolveDriver(n.networkType, true)
if err != nil {
return false
}
return cap.ConnectivityScope == datastore.GlobalScope
}
func (n *network) driver(load bool) (driverapi.Driver, error) { func (n *network) driver(load bool) (driverapi.Driver, error) {
d, cap, err := n.resolveDriver(n.networkType, load) d, cap, err := n.resolveDriver(n.networkType, load)
if err != nil { if err != nil {
@ -767,14 +908,14 @@ func (n *network) driver(load bool) (driverapi.Driver, error) {
isAgent := c.isAgent() isAgent := c.isAgent()
n.Lock() n.Lock()
// If load is not required, driver, cap and err may all be nil // If load is not required, driver, cap and err may all be nil
if cap != nil { if n.scope == "" && cap != nil {
n.scope = cap.DataScope n.scope = cap.DataScope
} }
if isAgent || n.dynamic { if isAgent && n.dynamic {
// If we are running in agent mode then all networks // If we are running in agent mode and the network
// in libnetwork are local scope regardless of the // is dynamic, then the networks are swarm scoped
// backing driver. // regardless of the backing driver.
n.scope = datastore.LocalScope n.scope = datastore.SwarmScope
} }
n.Unlock() n.Unlock()
return d, nil return d, nil
@ -797,6 +938,9 @@ func (n *network) delete(force bool) error {
} }
if !force && n.getEpCnt().EndpointCnt() != 0 { if !force && n.getEpCnt().EndpointCnt() != 0 {
if n.configOnly {
return types.ForbiddenErrorf("configuration network %q is in use", n.Name())
}
return &ActiveEndpointsError{name: n.name, id: n.id} return &ActiveEndpointsError{name: n.name, id: n.id}
} }
@ -806,6 +950,21 @@ func (n *network) delete(force bool) error {
return fmt.Errorf("error marking network %s (%s) for deletion: %v", n.Name(), n.ID(), err) return fmt.Errorf("error marking network %s (%s) for deletion: %v", n.Name(), n.ID(), err)
} }
if n.ConfigFrom() != "" {
if t, err := c.getConfigNetwork(n.ConfigFrom()); err == nil {
if err := t.getEpCnt().DecEndpointCnt(); err != nil {
logrus.Warnf("Failed to update reference count for configuration network %q on removal of network %q: %v",
t.Name(), n.Name(), err)
}
} else {
logrus.Warnf("Could not find configuration network %q during removal of network %q", n.configOnly, n.Name())
}
}
if n.configOnly {
goto removeFromStore
}
if err = n.deleteNetwork(); err != nil { if err = n.deleteNetwork(); err != nil {
if !force { if !force {
return err return err
@ -831,6 +990,7 @@ func (n *network) delete(force bool) error {
c.cleanupServiceBindings(n.ID()) c.cleanupServiceBindings(n.ID())
removeFromStore:
// deleteFromStore performs an atomic delete operation and the // deleteFromStore performs an atomic delete operation and the
// network.epCnt will help prevent any possible // network.epCnt will help prevent any possible
// race between endpoint join and network delete // race between endpoint join and network delete
@ -892,6 +1052,10 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi
return nil, ErrInvalidName(name) return nil, ErrInvalidName(name)
} }
if n.ConfigOnly() {
return nil, types.ForbiddenErrorf("cannot create endpoint on configuration-only network")
}
if _, err = n.EndpointByName(name); err == nil { if _, err = n.EndpointByName(name); err == nil {
return nil, types.ForbiddenErrorf("endpoint with name %s already exists in network %s", name, n.Name()) return nil, types.ForbiddenErrorf("endpoint with name %s already exists in network %s", name, n.Name())
} }
@ -1611,6 +1775,20 @@ func (n *network) IPv6Enabled() bool {
return n.enableIPv6 return n.enableIPv6
} }
func (n *network) ConfigFrom() string {
n.Lock()
defer n.Unlock()
return n.configFrom
}
func (n *network) ConfigOnly() bool {
n.Lock()
defer n.Unlock()
return n.configOnly
}
func (n *network) Labels() map[string]string { func (n *network) Labels() map[string]string {
n.Lock() n.Lock()
defer n.Unlock() defer n.Unlock()
@ -1778,3 +1956,24 @@ func (n *network) ExecFunc(f func()) error {
func (n *network) NdotsSet() bool { func (n *network) NdotsSet() bool {
return false return false
} }
// config-only network is looked up by name
func (c *controller) getConfigNetwork(name string) (*network, error) {
var n Network
s := func(current Network) bool {
if current.Info().ConfigOnly() && current.Name() == name {
n = current
return true
}
return false
}
c.WalkNetworks(s)
if n == nil {
return nil, types.NotFoundErrorf("configuration network %q not found", name)
}
return n.(*network), nil
}

View file

@ -480,26 +480,31 @@ func (nDB *NetworkDB) bulkSyncTables() {
func (nDB *NetworkDB) bulkSync(nodes []string, all bool) ([]string, error) { func (nDB *NetworkDB) bulkSync(nodes []string, all bool) ([]string, error) {
if !all { if !all {
// If not all, then just pick one. // Get 2 random nodes. 2nd node will be tried if the bulk sync to
nodes = nDB.mRandomNodes(1, nodes) // 1st node fails.
nodes = nDB.mRandomNodes(2, nodes)
} }
if len(nodes) == 0 { if len(nodes) == 0 {
return nil, nil return nil, nil
} }
logrus.Debugf("%s: Initiating bulk sync with nodes %v", nDB.config.NodeName, nodes)
var err error var err error
var networks []string var networks []string
for _, node := range nodes { for _, node := range nodes {
if node == nDB.config.NodeName { if node == nDB.config.NodeName {
continue continue
} }
logrus.Debugf("%s: Initiating bulk sync with node %v", nDB.config.NodeName, node)
networks = nDB.findCommonNetworks(node) networks = nDB.findCommonNetworks(node)
err = nDB.bulkSyncNode(networks, node, true) err = nDB.bulkSyncNode(networks, node, true)
// if its periodic bulksync stop after the first successful sync
if !all && err == nil {
break
}
if err != nil { if err != nil {
err = fmt.Errorf("bulk sync failed on node %s: %v", node, err) err = fmt.Errorf("bulk sync to node %s failed: %v", node, err)
logrus.Warn(err.Error())
} }
} }

View file

@ -98,7 +98,9 @@ func (c *controller) getNetworkFromStore(nid string) (*network, error) {
} }
n.epCnt = ec n.epCnt = ec
n.scope = store.Scope() if n.scope == "" {
n.scope = store.Scope()
}
return n, nil return n, nil
} }
@ -132,7 +134,9 @@ func (c *controller) getNetworksForScope(scope string) ([]*network, error) {
} }
n.epCnt = ec n.epCnt = ec
n.scope = scope if n.scope == "" {
n.scope = scope
}
nl = append(nl, n) nl = append(nl, n)
} }
@ -171,7 +175,9 @@ func (c *controller) getNetworksFromStore() ([]*network, error) {
ec.n = n ec.n = n
n.epCnt = ec n.epCnt = ec
} }
n.scope = store.Scope() if n.scope == "" {
n.scope = store.Scope()
}
n.Unlock() n.Unlock()
nl = append(nl, n) nl = append(nl, n)
} }
@ -350,17 +356,18 @@ func (c *controller) networkWatchLoop(nw *netWatch, ep *endpoint, ecCh <-chan da
} }
func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoint) { func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoint) {
if !c.isDistributedControl() && ep.getNetwork().driverScope() == datastore.GlobalScope { n := ep.getNetwork()
if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() {
return return
} }
c.Lock() c.Lock()
nw, ok := nmap[ep.getNetwork().ID()] nw, ok := nmap[n.ID()]
c.Unlock() c.Unlock()
if ok { if ok {
// Update the svc db for the local endpoint join right away // Update the svc db for the local endpoint join right away
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), true) n.updateSvcRecord(ep, c.getLocalEps(nw), true)
c.Lock() c.Lock()
nw.localEps[ep.ID()] = ep nw.localEps[ep.ID()] = ep
@ -381,15 +388,15 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi
// Update the svc db for the local endpoint join right away // Update the svc db for the local endpoint join right away
// Do this before adding this ep to localEps so that we don't // Do this before adding this ep to localEps so that we don't
// try to update this ep's container's svc records // try to update this ep's container's svc records
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), true) n.updateSvcRecord(ep, c.getLocalEps(nw), true)
c.Lock() c.Lock()
nw.localEps[ep.ID()] = ep nw.localEps[ep.ID()] = ep
nmap[ep.getNetwork().ID()] = nw nmap[n.ID()] = nw
nw.stopCh = make(chan struct{}) nw.stopCh = make(chan struct{})
c.Unlock() c.Unlock()
store := c.getStore(ep.getNetwork().DataScope()) store := c.getStore(n.DataScope())
if store == nil { if store == nil {
return return
} }
@ -398,7 +405,7 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi
return return
} }
ch, err := store.Watch(ep.getNetwork().getEpCnt(), nw.stopCh) ch, err := store.Watch(n.getEpCnt(), nw.stopCh)
if err != nil { if err != nil {
logrus.Warnf("Error creating watch for network: %v", err) logrus.Warnf("Error creating watch for network: %v", err)
return return
@ -408,12 +415,13 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi
} }
func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoint) { func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoint) {
if !c.isDistributedControl() && ep.getNetwork().driverScope() == datastore.GlobalScope { n := ep.getNetwork()
if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() {
return return
} }
c.Lock() c.Lock()
nw, ok := nmap[ep.getNetwork().ID()] nw, ok := nmap[n.ID()]
if ok { if ok {
delete(nw.localEps, ep.ID()) delete(nw.localEps, ep.ID())
@ -422,7 +430,7 @@ func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoi
// Update the svc db about local endpoint leave right away // Update the svc db about local endpoint leave right away
// Do this after we remove this ep from localEps so that we // Do this after we remove this ep from localEps so that we
// don't try to remove this svc record from this ep's container. // don't try to remove this svc record from this ep's container.
ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), false) n.updateSvcRecord(ep, c.getLocalEps(nw), false)
c.Lock() c.Lock()
if len(nw.localEps) == 0 { if len(nw.localEps) == 0 {
@ -430,9 +438,9 @@ func (c *controller) processEndpointDelete(nmap map[string]*netWatch, ep *endpoi
// This is the last container going away for the network. Destroy // This is the last container going away for the network. Destroy
// this network's svc db entry // this network's svc db entry
delete(c.svcRecords, ep.getNetwork().ID()) delete(c.svcRecords, n.ID())
delete(nmap, ep.getNetwork().ID()) delete(nmap, n.ID())
} }
} }
c.Unlock() c.Unlock()
@ -478,7 +486,7 @@ func (c *controller) networkCleanup() {
} }
var populateSpecial NetworkWalker = func(nw Network) bool { var populateSpecial NetworkWalker = func(nw Network) bool {
if n := nw.(*network); n.hasSpecialDriver() { if n := nw.(*network); n.hasSpecialDriver() && !n.ConfigOnly() {
if err := n.getController().addNetwork(n); err != nil { if err := n.getController().addNetwork(n); err != nil {
logrus.Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type()) logrus.Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type())
} }