diff --git a/components/engine/vendor.conf b/components/engine/vendor.conf index a33afddce4..372618b63f 100644 --- a/components/engine/vendor.conf +++ b/components/engine/vendor.conf @@ -26,7 +26,7 @@ github.com/imdario/mergo 0.2.1 golang.org/x/sync de49d9dcd27d4f764488181bea099dfe6179bcf0 #get libnetwork packages -github.com/docker/libnetwork 6786135bf7de08ec26a72a6f7e4291d27d113a3f +github.com/docker/libnetwork b2bc1a68486ccf8ada503162d9f0df7d31bdd8fb github.com/docker/go-events 18b43f1bc85d9cdd42c05a6cd2d444c7a200a894 github.com/armon/go-radix e39d623f12e8e41c7b5529e9a9dd67a1e2261f80 github.com/armon/go-metrics eb0af217e5e9747e41dd5303755356b62d28e3ec diff --git a/components/engine/vendor/github.com/docker/libnetwork/agent.go b/components/engine/vendor/github.com/docker/libnetwork/agent.go index b4b7bdf693..d67d446fe8 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/agent.go +++ b/components/engine/vendor/github.com/docker/libnetwork/agent.go @@ -222,7 +222,7 @@ func (c *controller) agentSetup(clusterProvider cluster.Provider) error { return err } 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) } return false @@ -507,7 +507,7 @@ func (n *network) Services() map[string]ServiceInfo { } func (n *network) isClusterEligible() bool { - if n.driverScope() != datastore.GlobalScope { + if n.scope != datastore.SwarmScope || !n.driverIsMultihost() { return false } return n.getController().getAgent() != nil diff --git a/components/engine/vendor/github.com/docker/libnetwork/controller.go b/components/engine/vendor/github.com/docker/libnetwork/controller.go index 8b2a983d51..eb0d9e4b20 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/controller.go +++ b/components/engine/vendor/github.com/docker/libnetwork/controller.go @@ -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 } @@ -722,22 +722,46 @@ func (c *controller) NewNetwork(networkType, name string, id string, 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 { 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 { 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() { // 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.") } @@ -747,6 +771,26 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ... 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() if err != nil { 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 // end up with a datastore containing a network and not an epCnt, // 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 { return nil, err } + if network.configOnly { + return network, nil + } joinCluster(network) if !c.isDistributedControl() { @@ -796,11 +844,18 @@ func (c *controller) NewNetwork(networkType, name string, id string, options ... c.Unlock() } + c.Lock() + arrangeUserFilterRule() + c.Unlock() + return network, nil } var joinCluster NetworkWalker = func(nw Network) bool { n := nw.(*network) + if n.configOnly { + return false + } if err := n.joinCluster(); err != nil { 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 { + if n.configOnly { + continue + } if !doReplayPoolReserve(n) { continue } diff --git a/components/engine/vendor/github.com/docker/libnetwork/datastore/datastore.go b/components/engine/vendor/github.com/docker/libnetwork/datastore/datastore.go index 19bf0b026b..82feef1c84 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/datastore/datastore.go +++ b/components/engine/vendor/github.com/docker/libnetwork/datastore/datastore.go @@ -115,7 +115,10 @@ const ( // LocalScope indicates to store the KV object in local datastore such as boltdb LocalScope = "local" // 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" ) diff --git a/components/engine/vendor/github.com/docker/libnetwork/driverapi/driverapi.go b/components/engine/vendor/github.com/docker/libnetwork/driverapi/driverapi.go index 074438ef88..48a14ae57a 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/driverapi/driverapi.go +++ b/components/engine/vendor/github.com/docker/libnetwork/driverapi/driverapi.go @@ -161,7 +161,8 @@ type DriverCallback interface { // Capability represents the high level capabilities of the drivers which libnetwork can make use of type Capability struct { - DataScope string + DataScope string + ConnectivityScope string } // IPAMData represents the per-network ip related diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/bridge.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/bridge.go index e681b8f7c4..dd79f04d91 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/bridge.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/bridge.go @@ -153,7 +153,8 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { } c := driverapi.Capability{ - DataScope: datastore.LocalScope, + DataScope: datastore.LocalScope, + ConnectivityScope: datastore.LocalScope, } return dc.RegisterDriver(networkType, d, c) } diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/brmanager/brmanager.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/brmanager/brmanager.go new file mode 100644 index 0000000000..74bb95c001 --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/brmanager/brmanager.go @@ -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") +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/setup_ip_tables.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/setup_ip_tables.go index 839e16f8ff..769debcb80 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/setup_ip_tables.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/bridge/setup_ip_tables.go @@ -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) } - if err := addReturnRule(IsolationChain); err != nil { + if err := iptables.AddReturnRule(IsolationChain); err != nil { return nil, nil, nil, err } @@ -117,7 +117,7 @@ func (n *bridgeNetwork) setupIPTables(config *networkConfiguration, i *bridgeInt } d.Lock() - err = ensureJumpRule("FORWARD", IsolationChain) + err = iptables.EnsureJumpRule("FORWARD", IsolationChain) d.Unlock() if err != nil { return err @@ -280,46 +280,6 @@ func setINC(iface1, iface2 string, enable bool) error { 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() { for _, chainInfo := range []iptables.ChainInfo{ {Name: DockerChain, Table: iptables.Nat}, diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/host/host.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/host/host.go index 7b4a986e6c..a71d461380 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/host/host.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/host/host.go @@ -19,7 +19,8 @@ type driver struct { // Init registers a new instance of host driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.LocalScope, + DataScope: datastore.LocalScope, + ConnectivityScope: datastore.LocalScope, } return dc.RegisterDriver(networkType, &driver{}, c) } diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ipvlan.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ipvlan.go index 296804dc1a..c64ad555a3 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ipvlan.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ipvlan.go @@ -58,7 +58,8 @@ type network struct { // Init initializes and registers the libnetwork ipvlan driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.LocalScope, + DataScope: datastore.LocalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ networks: networkTable{}, diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ivmanager/ivmanager.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ivmanager/ivmanager.go new file mode 100644 index 0000000000..519f1e8795 --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/ipvlan/ivmanager/ivmanager.go @@ -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") +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/macvlan.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/macvlan.go index 49b9fbae00..872e6f3ec1 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/macvlan.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/macvlan.go @@ -60,7 +60,8 @@ type network struct { // Init initializes and registers the libnetwork macvlan driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.LocalScope, + DataScope: datastore.LocalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ networks: networkTable{}, diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/mvmanager/mvmanager.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/mvmanager/mvmanager.go new file mode 100644 index 0000000000..0f811ac36f --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/macvlan/mvmanager/mvmanager.go @@ -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") +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/overlay.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/overlay.go index 88e1010c43..8a932cf370 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/overlay.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/overlay.go @@ -56,7 +56,8 @@ type driver struct { // Init registers a new instance of overlay driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.GlobalScope, + DataScope: datastore.GlobalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ networks: networkTable{}, diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/ovmanager/ovmanager.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/ovmanager/ovmanager.go index dce8f98b8e..3c3c0bb257 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/ovmanager/ovmanager.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/overlay/ovmanager/ovmanager.go @@ -49,7 +49,8 @@ type network struct { func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { var err error c := driverapi.Capability{ - DataScope: datastore.GlobalScope, + DataScope: datastore.GlobalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/api/api.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/api/api.go index f9a341c540..d24f190162 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/api/api.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/api/api.go @@ -24,7 +24,8 @@ func (r *Response) GetError() string { // GetCapabilityResponse is the response of GetCapability request type GetCapabilityResponse struct { Response - Scope string + Scope string + ConnectivityScope string } // AllocateNetworkRequest requests allocation of new network by manager diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/driver.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/driver.go index 49a7fb4951..ffe0730c95 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/driver.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/remote/driver.go @@ -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) } + 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 } diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/bridge/bridge.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/bridge/bridge.go index 13dd5f14bc..2547016a4b 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/bridge/bridge.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/bridge/bridge.go @@ -159,7 +159,8 @@ func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { } c := driverapi.Capability{ - DataScope: datastore.LocalScope, + DataScope: datastore.LocalScope, + ConnectivityScope: datastore.LocalScope, } return dc.RegisterDriver(networkType, d, c) } diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/overlay/overlay.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/overlay/overlay.go index b001f58af7..0a5a1bfdce 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/overlay/overlay.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/solaris/overlay/overlay.go @@ -57,7 +57,8 @@ type driver struct { // Init registers a new instance of overlay driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.GlobalScope, + DataScope: datastore.GlobalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ networks: networkTable{}, diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/overlay/overlay_windows.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/overlay/overlay_windows.go index 2ae2cec77e..111e517dbd 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/overlay/overlay_windows.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/overlay/overlay_windows.go @@ -36,7 +36,8 @@ type driver struct { // Init registers a new instance of overlay driver func Init(dc driverapi.DriverCallback, config map[string]interface{}) error { c := driverapi.Capability{ - DataScope: datastore.GlobalScope, + DataScope: datastore.GlobalScope, + ConnectivityScope: datastore.GlobalScope, } d := &driver{ diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows.go index b6591a1d6d..c1c69f2991 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows.go +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows.go @@ -41,6 +41,8 @@ type networkConfiguration struct { DNSSuffix string SourceMac string NetworkAdapterName string + dbIndex uint64 + dbExists bool } // endpointConfiguration represents the user specified configuration for the sandbox endpoint @@ -59,17 +61,22 @@ type endpointConnectivity struct { type hnsEndpoint struct { id string + nid string profileID string + Type string macAddress net.HardwareAddr epOption *endpointOption // User specified parameters epConnectivity *endpointConnectivity // User specified parameters portMapping []types.PortBinding // Operation port bindings addr *net.IPNet gateway net.IP + dbIndex uint64 + dbExists bool } type hnsNetwork struct { id string + created bool config *networkConfiguration endpoints map[string]*hnsEndpoint // key: endpoint id driver *driver // The network's driver @@ -79,9 +86,14 @@ type hnsNetwork struct { type driver struct { name string networks map[string]*hnsNetwork + store datastore.DataStore 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 func IsBuiltinLocalDriver(networkType string) bool { 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 dc.RegisterDriver(networkType, newDriver(networkType), driverapi.Capability{ - DataScope: datastore.LocalScope, + d := newDriver(networkType) + + 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) { - config := &networkConfiguration{} + config := &networkConfiguration{Type: d.name} for label, value := range genericOptions { switch label { @@ -187,6 +207,21 @@ func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (s 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 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 { @@ -209,16 +244,11 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d return err } - network := &hnsNetwork{ - id: config.ID, - endpoints: make(map[string]*hnsEndpoint), - config: config, - driver: d, - } + err = d.createNetwork(config) - d.Lock() - d.networks[config.ID] = network - d.Unlock() + if err != nil { + return err + } // A non blank hnsid indicates that the 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 } - return nil + n, err := d.getNetwork(id) + n.created = true + return d.storeUpdate(config) } func (d *driver) DeleteNetwork(nid string) error { @@ -306,21 +338,25 @@ func (d *driver) DeleteNetwork(nid string) error { config := n.config n.Unlock() - // Cannot remove network if endpoints are still present - if len(n.endpoints) != 0 { - return fmt.Errorf("network %s has active endpoint", n.id) - } - - _, err = hcsshim.HNSNetworkRequest("DELETE", config.HnsID, "") - if err != nil { - return types.ForbiddenErrorf(err.Error()) + if n.created { + _, err = hcsshim.HNSNetworkRequest("DELETE", config.HnsID, "") + if err != nil && err.Error() != errNotFound { + return types.ForbiddenErrorf(err.Error()) + } } d.Lock() delete(d.networks, nid) 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) { @@ -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 endpoint := &hnsEndpoint{ id: eid, + nid: n.id, + Type: d.name, addr: &net.IPNet{IP: hnsresponse.IPAddress, Mask: hnsresponse.IPAddress.DefaultMask()}, macAddress: mac, } @@ -573,6 +611,10 @@ func (d *driver) CreateEndpoint(nid, eid string, ifInfo driverapi.InterfaceInfo, 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 } @@ -592,10 +634,13 @@ func (d *driver) DeleteEndpoint(nid, eid string) error { n.Unlock() _, err = hcsshim.HNSEndpointRequest("DELETE", ep.profileID, "") - if err != nil { + if err != nil && err.Error() != errNotFound { 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 } diff --git a/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows_store.go b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows_store.go new file mode 100644 index 0000000000..3c5cb04e21 --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/drivers/windows/windows_store.go @@ -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 +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/endpoint.go b/components/engine/vendor/github.com/docker/libnetwork/endpoint.go index 8e70c38d76..e607eddaf2 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/endpoint.go +++ b/components/engine/vendor/github.com/docker/libnetwork/endpoint.go @@ -1152,6 +1152,9 @@ func (c *controller) cleanupLocalEndpoints() { } for _, n := range nl { + if n.ConfigOnly() { + continue + } epl, err := n.getEndpointsFromStore() if err != nil { logrus.Warnf("Could not get list of endpoints in network %s during endpoint cleanup: %v", n.name, err) diff --git a/components/engine/vendor/github.com/docker/libnetwork/firewall_linux.go b/components/engine/vendor/github.com/docker/libnetwork/firewall_linux.go new file mode 100644 index 0000000000..8c27c1dd25 --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/firewall_linux.go @@ -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) + } +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/firewall_others.go b/components/engine/vendor/github.com/docker/libnetwork/firewall_others.go new file mode 100644 index 0000000000..c41b3e049f --- /dev/null +++ b/components/engine/vendor/github.com/docker/libnetwork/firewall_others.go @@ -0,0 +1,6 @@ +// +build !linux + +package libnetwork + +func arrangeUserFilterRule() { +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/iptables/iptables.go b/components/engine/vendor/github.com/docker/libnetwork/iptables/iptables.go index 818bcb5598..20edb9b5d6 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/iptables/iptables.go +++ b/components/engine/vendor/github.com/docker/libnetwork/iptables/iptables.go @@ -498,3 +498,44 @@ func parseVersionNumbers(input string) (major, minor, micro int) { func supportsCOption(mj, mn, mc int) bool { 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 +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/network.go b/components/engine/vendor/github.com/docker/libnetwork/network.go index 8077770018..5fcca95fc4 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/network.go +++ b/components/engine/vendor/github.com/docker/libnetwork/network.go @@ -67,6 +67,8 @@ type NetworkInfo interface { Internal() bool Attachable() bool Ingress() bool + ConfigFrom() string + ConfigOnly() bool Labels() map[string]string Dynamic() bool Created() time.Time @@ -193,7 +195,7 @@ type network struct { networkType string id string created time.Time - scope string + scope string // network data scope labels map[string]string ipamType string ipamOptions map[string]string @@ -219,6 +221,8 @@ type network struct { ingress bool driverTables []networkDBTable dynamic bool + configOnly bool + configFrom string sync.Mutex } @@ -348,6 +352,95 @@ func (i *IpamInfo) CopyTo(dstI *IpamInfo) error { 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 { n.Lock() defer n.Unlock() @@ -370,6 +463,8 @@ func (n *network) CopyTo(o datastore.KVObject) error { dstN.attachable = n.attachable dstN.inDelete = n.inDelete dstN.ingress = n.ingress + dstN.configOnly = n.configOnly + dstN.configFrom = n.configFrom // copy labels if dstN.labels == nil { @@ -419,7 +514,12 @@ func (n *network) CopyTo(o datastore.KVObject) error { } 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 { @@ -479,6 +579,8 @@ func (n *network) MarshalJSON() ([]byte, error) { netMap["attachable"] = n.attachable netMap["inDelete"] = n.inDelete netMap["ingress"] = n.ingress + netMap["configOnly"] = n.configOnly + netMap["configFrom"] = n.configFrom return json.Marshal(netMap) } @@ -583,6 +685,12 @@ func (n *network) UnmarshalJSON(b []byte) (err error) { if v, ok := netMap["ingress"]; ok { 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 if !n.enableIPv6 { 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 func NetworkOptionIpam(ipamDriver string, addrSpace string, ipV4 []*IpamConf, ipV6 []*IpamConf, opts map[string]string) NetworkOption { 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) { for _, opt := range options { if opt != nil { @@ -757,6 +890,14 @@ func (n *network) driverScope() string { 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) { d, cap, err := n.resolveDriver(n.networkType, load) if err != nil { @@ -767,14 +908,14 @@ func (n *network) driver(load bool) (driverapi.Driver, error) { isAgent := c.isAgent() n.Lock() // 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 } - if isAgent || n.dynamic { - // If we are running in agent mode then all networks - // in libnetwork are local scope regardless of the - // backing driver. - n.scope = datastore.LocalScope + if isAgent && n.dynamic { + // If we are running in agent mode and the network + // is dynamic, then the networks are swarm scoped + // regardless of the backing driver. + n.scope = datastore.SwarmScope } n.Unlock() return d, nil @@ -797,6 +938,9 @@ func (n *network) delete(force bool) error { } 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} } @@ -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) } + 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 !force { return err @@ -831,6 +990,7 @@ func (n *network) delete(force bool) error { c.cleanupServiceBindings(n.ID()) +removeFromStore: // deleteFromStore performs an atomic delete operation and the // network.epCnt will help prevent any possible // race between endpoint join and network delete @@ -892,6 +1052,10 @@ func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoi 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 { 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 } +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 { n.Lock() defer n.Unlock() @@ -1778,3 +1956,24 @@ func (n *network) ExecFunc(f func()) error { func (n *network) NdotsSet() bool { 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 +} diff --git a/components/engine/vendor/github.com/docker/libnetwork/networkdb/cluster.go b/components/engine/vendor/github.com/docker/libnetwork/networkdb/cluster.go index 35977fcae9..d448c8caef 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/networkdb/cluster.go +++ b/components/engine/vendor/github.com/docker/libnetwork/networkdb/cluster.go @@ -480,26 +480,31 @@ func (nDB *NetworkDB) bulkSyncTables() { func (nDB *NetworkDB) bulkSync(nodes []string, all bool) ([]string, error) { if !all { - // If not all, then just pick one. - nodes = nDB.mRandomNodes(1, nodes) + // Get 2 random nodes. 2nd node will be tried if the bulk sync to + // 1st node fails. + nodes = nDB.mRandomNodes(2, nodes) } if len(nodes) == 0 { return nil, nil } - logrus.Debugf("%s: Initiating bulk sync with nodes %v", nDB.config.NodeName, nodes) var err error var networks []string for _, node := range nodes { if node == nDB.config.NodeName { continue } - + logrus.Debugf("%s: Initiating bulk sync with node %v", nDB.config.NodeName, node) networks = nDB.findCommonNetworks(node) err = nDB.bulkSyncNode(networks, node, true) + // if its periodic bulksync stop after the first successful sync + if !all && err == nil { + break + } 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()) } } diff --git a/components/engine/vendor/github.com/docker/libnetwork/store.go b/components/engine/vendor/github.com/docker/libnetwork/store.go index 58e1d852f1..398794bfef 100644 --- a/components/engine/vendor/github.com/docker/libnetwork/store.go +++ b/components/engine/vendor/github.com/docker/libnetwork/store.go @@ -98,7 +98,9 @@ func (c *controller) getNetworkFromStore(nid string) (*network, error) { } n.epCnt = ec - n.scope = store.Scope() + if n.scope == "" { + n.scope = store.Scope() + } return n, nil } @@ -132,7 +134,9 @@ func (c *controller) getNetworksForScope(scope string) ([]*network, error) { } n.epCnt = ec - n.scope = scope + if n.scope == "" { + n.scope = scope + } nl = append(nl, n) } @@ -171,7 +175,9 @@ func (c *controller) getNetworksFromStore() ([]*network, error) { ec.n = n n.epCnt = ec } - n.scope = store.Scope() + if n.scope == "" { + n.scope = store.Scope() + } n.Unlock() 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) { - if !c.isDistributedControl() && ep.getNetwork().driverScope() == datastore.GlobalScope { + n := ep.getNetwork() + if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() { return } c.Lock() - nw, ok := nmap[ep.getNetwork().ID()] + nw, ok := nmap[n.ID()] c.Unlock() if ok { // 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() 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 // Do this before adding this ep to localEps so that we don't // 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() nw.localEps[ep.ID()] = ep - nmap[ep.getNetwork().ID()] = nw + nmap[n.ID()] = nw nw.stopCh = make(chan struct{}) c.Unlock() - store := c.getStore(ep.getNetwork().DataScope()) + store := c.getStore(n.DataScope()) if store == nil { return } @@ -398,7 +405,7 @@ func (c *controller) processEndpointCreate(nmap map[string]*netWatch, ep *endpoi return } - ch, err := store.Watch(ep.getNetwork().getEpCnt(), nw.stopCh) + ch, err := store.Watch(n.getEpCnt(), nw.stopCh) if err != nil { logrus.Warnf("Error creating watch for network: %v", err) 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) { - if !c.isDistributedControl() && ep.getNetwork().driverScope() == datastore.GlobalScope { + n := ep.getNetwork() + if !c.isDistributedControl() && n.Scope() == datastore.SwarmScope && n.driverIsMultihost() { return } c.Lock() - nw, ok := nmap[ep.getNetwork().ID()] + nw, ok := nmap[n.ID()] if ok { 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 // 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. - ep.getNetwork().updateSvcRecord(ep, c.getLocalEps(nw), false) + n.updateSvcRecord(ep, c.getLocalEps(nw), false) c.Lock() 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 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() @@ -478,7 +486,7 @@ func (c *controller) networkCleanup() { } 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 { logrus.Warnf("Failed to populate network %q with driver %q", nw.Name(), nw.Type()) }