|
@@ -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
|
|
|
+}
|