From 594361552eeee304a654181bb42a698ce14b6c5c Mon Sep 17 00:00:00 2001 From: Madhu Venugopal Date: Wed, 13 May 2015 14:12:57 -0700 Subject: [PATCH] Avoid adding local reserved networks (bridge, none, host) to the datastore Signed-off-by: Madhu Venugopal --- libnetwork/controller.go | 18 +++++++++++++++++- libnetwork/network.go | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 247f805734..d6b93b6b8d 100644 --- a/libnetwork/controller.go +++ b/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 diff --git a/libnetwork/network.go b/libnetwork/network.go index 2c3ebe0035..725972133e 100644 --- a/libnetwork/network.go +++ b/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 +}