Explorar el Código

Avoid adding local reserved networks (bridge, none, host) to the datastore

Signed-off-by: Madhu Venugopal <madhu@docker.com>
Madhu Venugopal hace 10 años
padre
commit
594361552e
Se han modificado 2 ficheros con 28 adiciones y 1 borrados
  1. 17 1
      libnetwork/controller.go
  2. 11 0
      libnetwork/network.go

+ 17 - 1
libnetwork/controller.go

@@ -47,6 +47,7 @@ package libnetwork
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	"sync"
 
@@ -60,6 +61,9 @@ import (
 	"github.com/docker/swarm/pkg/store"
 )
 
+// TODO: Move it to error.go once the error refactoring is done
+var ErrInvalidDatastore = errors.New("Datastore is not initialized")
+
 // NetworkController provides the interface for controller instance which manages
 // networks.
 type NetworkController interface {
@@ -198,6 +202,9 @@ func (c *controller) NewNetwork(networkType, name string, options ...NetworkOpti
 	}
 
 	network.processOptions(options...)
+	if err := c.addNetworkToStore(network); err != nil {
+		return nil, err
+	}
 	// Create the network
 	if err := d.CreateNetwork(network.id, network.generic); err != nil {
 		return nil, err
@@ -206,7 +213,6 @@ func (c *controller) NewNetwork(networkType, name string, options ...NetworkOpti
 	// Store the network handler in controller
 	c.Lock()
 	c.networks[network.id] = network
-	c.store.PutObjectAtomic(network)
 	c.Unlock()
 
 	return network, nil
@@ -226,6 +232,16 @@ func (c *controller) newNetworkFromStore(n *network) {
 	// TODO : Populate n.endpoints back from endpoint dbstore
 }
 
+func (c *controller) addNetworkToStore(n *network) error {
+	if IsReservedNetwork(n.Name()) {
+		return nil
+	}
+	if c.store == nil {
+		return ErrInvalidDatastore
+	}
+	return c.store.PutObjectAtomic(n)
+}
+
 func (c *controller) watchNewNetworks() {
 	c.Lock()
 	store = c.store

+ 11 - 0
libnetwork/network.go

@@ -2,6 +2,7 @@ package libnetwork
 
 import (
 	"encoding/json"
+	"strings"
 	"sync"
 
 	"github.com/docker/docker/pkg/stringid"
@@ -254,3 +255,13 @@ func (n *network) EndpointByID(id string) (Endpoint, error) {
 	}
 	return nil, ErrNoSuchEndpoint(id)
 }
+
+func IsReservedNetwork(name string) bool {
+	reserved := []string{"bridge", "none", "host"}
+	for _, r := range reserved {
+		if strings.EqualFold(r, name) {
+			return true
+		}
+	}
+	return false
+}