diff --git a/libnetwork/README.md b/libnetwork/README.md index 3f10a0311c..536f8aa2b3 100644 --- a/libnetwork/README.md +++ b/libnetwork/README.md @@ -34,7 +34,7 @@ func main() { // Create a network for containers to join. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use. - network, err := controller.NewNetwork(networkType, "network1") + network, err := controller.NewNetwork(networkType, "network1", "") if err != nil { log.Fatalf("controller.NewNetwork: %s", err) } diff --git a/libnetwork/api/api.go b/libnetwork/api/api.go index d3f070d220..c558d9a09d 100644 --- a/libnetwork/api/api.go +++ b/libnetwork/api/api.go @@ -307,7 +307,7 @@ func procCreateNetwork(c libnetwork.NetworkController, vars map[string]string, b if len(create.DriverOpts) > 0 { options = append(options, libnetwork.NetworkOptionDriverOpts(create.DriverOpts)) } - nw, err := c.NewNetwork(create.NetworkType, create.Name, options...) + nw, err := c.NewNetwork(create.NetworkType, create.Name, "", options...) if err != nil { return nil, convertNetworkError(err) } diff --git a/libnetwork/api/api_test.go b/libnetwork/api/api_test.go index 5e563c3c5b..e78e7c108e 100644 --- a/libnetwork/api/api_test.go +++ b/libnetwork/api/api_test.go @@ -98,7 +98,7 @@ func createTestNetwork(t *testing.T, network string) (libnetwork.NetworkControll }, } netGeneric := libnetwork.NetworkOptionGeneric(netOption) - nw, err := c.NewNetwork(bridgeNetType, network, netGeneric) + nw, err := c.NewNetwork(bridgeNetType, network, "", netGeneric) if err != nil { t.Fatal(err) } @@ -539,7 +539,7 @@ func TestProcGetServices(t *testing.T) { "BridgeName": netName1, }, } - nw1, err := c.NewNetwork(bridgeNetType, netName1, libnetwork.NetworkOptionGeneric(netOption)) + nw1, err := c.NewNetwork(bridgeNetType, netName1, "", libnetwork.NetworkOptionGeneric(netOption)) if err != nil { t.Fatal(err) } @@ -550,7 +550,7 @@ func TestProcGetServices(t *testing.T) { "BridgeName": netName2, }, } - nw2, err := c.NewNetwork(bridgeNetType, netName2, libnetwork.NetworkOptionGeneric(netOption)) + nw2, err := c.NewNetwork(bridgeNetType, netName2, "", libnetwork.NetworkOptionGeneric(netOption)) if err != nil { t.Fatal(err) } @@ -1731,7 +1731,7 @@ func TestHttpHandlerUninit(t *testing.T) { t.Fatalf("Expected empty list. Got %v", list) } - n, err := c.NewNetwork(bridgeNetType, "didietro", nil) + n, err := c.NewNetwork(bridgeNetType, "didietro", "", nil) if err != nil { t.Fatal(err) } diff --git a/libnetwork/cmd/dnet/dnet.go b/libnetwork/cmd/dnet/dnet.go index d84e03291f..57408665d1 100644 --- a/libnetwork/cmd/dnet/dnet.go +++ b/libnetwork/cmd/dnet/dnet.go @@ -201,7 +201,7 @@ func createDefaultNetwork(c libnetwork.NetworkController) { } } - _, err := c.NewNetwork(d, nw, createOptions...) + _, err := c.NewNetwork(d, nw, "", createOptions...) if err != nil { logrus.Errorf("Error creating default network : %s : %v", nw, err) } diff --git a/libnetwork/cmd/readme_test/readme.go b/libnetwork/cmd/readme_test/readme.go index 31c258b541..600f99bcff 100644 --- a/libnetwork/cmd/readme_test/readme.go +++ b/libnetwork/cmd/readme_test/readme.go @@ -30,7 +30,7 @@ func main() { // Create a network for containers to join. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can use. - network, err := controller.NewNetwork(networkType, "network1") + network, err := controller.NewNetwork(networkType, "network1", "") if err != nil { log.Fatalf("controller.NewNetwork: %s", err) } diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 812814a19a..d863a3fe98 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -15,7 +15,7 @@ create network namespaces and allocate interfaces for containers to use. // Create a network for containers to join. // NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make use of - network, err := controller.NewNetwork(networkType, "network1") + network, err := controller.NewNetwork(networkType, "network1", "") if err != nil { return } @@ -76,7 +76,7 @@ type NetworkController interface { Config() config.Config // Create a new network. The options parameter carries network specific options. - NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) + NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) // Networks returns the list of Network(s) managed by this controller. Networks() []Network @@ -426,18 +426,22 @@ func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver, // NewNetwork creates a new network of the specified network type. The options // are network specific and modeled in a generic way. -func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) { +func (c *controller) NewNetwork(networkType, name string, id string, options ...NetworkOption) (Network, error) { if !config.IsValidName(name) { return nil, ErrInvalidName(name) } + if id == "" { + id = stringid.GenerateRandomID() + } + // Construct the network object network := &network{ name: name, networkType: networkType, generic: map[string]interface{}{netlabel.GenericData: make(map[string]string)}, ipamType: ipamapi.DefaultIPAM, - id: stringid.GenerateRandomID(), + id: id, ctrlr: c, persist: true, drvOnce: &sync.Once{}, diff --git a/libnetwork/default_gateway_linux.go b/libnetwork/default_gateway_linux.go index 9376922a21..c08b061898 100644 --- a/libnetwork/default_gateway_linux.go +++ b/libnetwork/default_gateway_linux.go @@ -14,7 +14,7 @@ func (c *controller) createGWNetwork() (Network, error) { bridge.EnableIPMasquerade: strconv.FormatBool(true), } - n, err := c.NewNetwork("bridge", libnGWNetwork, + n, err := c.NewNetwork("bridge", libnGWNetwork, "", NetworkOptionDriverOpts(netOption), NetworkOptionEnableIPv6(false), ) diff --git a/libnetwork/libnetwork_internal_test.go b/libnetwork/libnetwork_internal_test.go index deb5b42b3e..c7707c3da0 100644 --- a/libnetwork/libnetwork_internal_test.go +++ b/libnetwork/libnetwork_internal_test.go @@ -339,11 +339,11 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { // Test whether ipam state release is invoked on network create failure from net driver // by checking whether subsequent network creation requesting same gateway IP succeeds ipamOpt := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.254"}}, nil, nil) - if _, err := c.NewNetwork(badDriverName, "badnet1", ipamOpt); err == nil { + if _, err := c.NewNetwork(badDriverName, "badnet1", "", ipamOpt); err == nil { t.Fatalf("bad network driver should have failed network creation") } - gnw, err := c.NewNetwork("bridge", "goodnet1", ipamOpt) + gnw, err := c.NewNetwork("bridge", "goodnet1", "", ipamOpt) if err != nil { t.Fatal(err) } @@ -351,7 +351,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { // Now check whether ipam release works on endpoint creation failure bd.failNetworkCreation = false - bnw, err := c.NewNetwork(badDriverName, "badnet2", ipamOpt) + bnw, err := c.NewNetwork(badDriverName, "badnet2", "", ipamOpt) if err != nil { t.Fatal(err) } @@ -363,7 +363,7 @@ func TestIpamReleaseOnNetDriverFailures(t *testing.T) { // Now create good bridge network with different gateway ipamOpt2 := NetworkOptionIpam(ipamapi.DefaultIPAM, "", []*IpamConf{{PreferredPool: "10.34.0.0/16", Gateway: "10.34.255.253"}}, nil, nil) - gnw, err = c.NewNetwork("bridge", "goodnet2", ipamOpt2) + gnw, err = c.NewNetwork("bridge", "goodnet2", "", ipamOpt2) if err != nil { t.Fatal(err) } diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index d5b57a12f3..042ef9813e 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -85,7 +85,7 @@ func createController() error { } func createTestNetwork(networkType, networkName string, netOption options.Generic, ipamV4Configs, ipamV6Configs []*libnetwork.IpamConf) (libnetwork.Network, error) { - return controller.NewNetwork(networkType, networkName, + return controller.NewNetwork(networkType, networkName, "", libnetwork.NetworkOptionGeneric(netOption), libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4Configs, ipamV6Configs, nil)) } @@ -333,7 +333,7 @@ func TestBridgeIpv6FromMac(t *testing.T) { ipamV4ConfList := []*libnetwork.IpamConf{{PreferredPool: "192.168.100.0/24", Gateway: "192.168.100.1"}} ipamV6ConfList := []*libnetwork.IpamConf{{PreferredPool: "fe90::/64", Gateway: "fe90::22"}} - network, err := controller.NewNetwork(bridgeNetType, "testipv6mac", + network, err := controller.NewNetwork(bridgeNetType, "testipv6mac", "", libnetwork.NetworkOptionGeneric(netOption), libnetwork.NetworkOptionEnableIPv6(true), libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", ipamV4ConfList, ipamV6ConfList, nil), @@ -386,7 +386,7 @@ func TestUnknownDriver(t *testing.T) { } func TestNilRemoteDriver(t *testing.T) { - _, err := controller.NewNetwork("framerelay", "dummy", + _, err := controller.NewNetwork("framerelay", "dummy", "", libnetwork.NetworkOptionGeneric(getEmptyGenericOption())) if err == nil { t.Fatal("Expected to fail. But instead succeeded") @@ -1016,7 +1016,7 @@ func TestEndpointJoin(t *testing.T) { }, } ipamV6ConfList := []*libnetwork.IpamConf{{PreferredPool: "fe90::/64", Gateway: "fe90::22"}} - n1, err := controller.NewNetwork(bridgeNetType, "testnetwork1", + n1, err := controller.NewNetwork(bridgeNetType, "testnetwork1", "", libnetwork.NetworkOptionGeneric(netOption), libnetwork.NetworkOptionEnableIPv6(true), libnetwork.NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, ipamV6ConfList, nil), @@ -2046,7 +2046,7 @@ func TestInvalidRemoteDriver(t *testing.T) { } defer ctrlr.Stop() - _, err = ctrlr.NewNetwork("invalid-network-driver", "dummy", + _, err = ctrlr.NewNetwork("invalid-network-driver", "dummy", "", libnetwork.NetworkOptionGeneric(getEmptyGenericOption())) if err == nil { t.Fatal("Expected to fail. But instead succeeded") @@ -2095,7 +2095,7 @@ func TestValidRemoteDriver(t *testing.T) { t.Fatal(err) } - n, err := controller.NewNetwork("valid-network-driver", "dummy", + n, err := controller.NewNetwork("valid-network-driver", "dummy", "", libnetwork.NetworkOptionGeneric(getEmptyGenericOption())) if err != nil { // Only fail if we could not find the plugin driver @@ -2357,7 +2357,7 @@ func TestParallel3(t *testing.T) { } func TestNullIpam(t *testing.T) { - _, err := controller.NewNetwork(bridgeNetType, "testnetworkinternal", libnetwork.NetworkOptionIpam(ipamapi.NullIPAM, "", nil, nil, nil)) + _, err := controller.NewNetwork(bridgeNetType, "testnetworkinternal", "", libnetwork.NetworkOptionIpam(ipamapi.NullIPAM, "", nil, nil, nil)) if err == nil || err.Error() != "ipv4 pool is empty" { t.Fatal("bridge network should complain empty pool") } diff --git a/libnetwork/sandbox_test.go b/libnetwork/sandbox_test.go index 13b587e0d4..2c8bcf86a5 100644 --- a/libnetwork/sandbox_test.go +++ b/libnetwork/sandbox_test.go @@ -38,7 +38,7 @@ func getTestEnv(t *testing.T) (NetworkController, Network, Network) { "BridgeName": name1, }, } - n1, err := c.NewNetwork(netType, name1, NetworkOptionGeneric(netOption1)) + n1, err := c.NewNetwork(netType, name1, "", NetworkOptionGeneric(netOption1)) if err != nil { t.Fatal(err) } @@ -49,7 +49,7 @@ func getTestEnv(t *testing.T) (NetworkController, Network, Network) { "BridgeName": name2, }, } - n2, err := c.NewNetwork(netType, name2, NetworkOptionGeneric(netOption2)) + n2, err := c.NewNetwork(netType, name2, "", NetworkOptionGeneric(netOption2)) if err != nil { t.Fatal(err) } diff --git a/libnetwork/store_test.go b/libnetwork/store_test.go index b7360ae79a..7741004423 100644 --- a/libnetwork/store_test.go +++ b/libnetwork/store_test.go @@ -55,7 +55,7 @@ func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Con if err != nil { t.Fatalf("Error new controller: %v", err) } - nw, err := ctrl.NewNetwork("host", "host") + nw, err := ctrl.NewNetwork("host", "host", "") if err != nil { t.Fatalf("Error creating default \"host\" network: %v", err) } @@ -91,7 +91,7 @@ func TestNoPersist(t *testing.T) { if err != nil { t.Fatalf("Error new controller: %v", err) } - nw, err := ctrl.NewNetwork("host", "host", NetworkOptionPersist(false)) + nw, err := ctrl.NewNetwork("host", "host", "", NetworkOptionPersist(false)) if err != nil { t.Fatalf("Error creating default \"host\" network: %v", err) }