This fix tries to address the issue raised in 30242 where the `Scope`
field always changed to `swarm` in the ouput of `docker network ls/inspect`
when duplicate networks name exist.
The reason for the issue was that `buildNetworkResource()` use network name
(which may not be unique) to check for the scope.
This fix fixes the issue by always use network ID in `buildNetworkResource()`.
A test has been added. The test fails before the fix and passes after the fix.
This fix fixes 30242.
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| ... | ... |
@@ -178,7 +178,7 @@ func (n *networkRouter) buildNetworkResource(nw libnetwork.Network) *types.Netwo |
| 178 | 178 |
r.Created = info.Created() |
| 179 | 179 |
r.Scope = info.Scope() |
| 180 | 180 |
if n.cluster.IsManager() {
|
| 181 |
- if _, err := n.cluster.GetNetwork(nw.Name()); err == nil {
|
|
| 181 |
+ if _, err := n.cluster.GetNetwork(nw.ID()); err == nil {
|
|
| 182 | 182 |
r.Scope = "swarm" |
| 183 | 183 |
} |
| 184 | 184 |
} else if info.Dynamic() {
|
| ... | ... |
@@ -3,6 +3,7 @@ |
| 3 | 3 |
package main |
| 4 | 4 |
|
| 5 | 5 |
import ( |
| 6 |
+ "encoding/json" |
|
| 6 | 7 |
"fmt" |
| 7 | 8 |
"net" |
| 8 | 9 |
"net/http" |
| ... | ... |
@@ -14,6 +15,7 @@ import ( |
| 14 | 14 |
"syscall" |
| 15 | 15 |
"time" |
| 16 | 16 |
|
| 17 |
+ "github.com/docker/docker/api/types" |
|
| 17 | 18 |
"github.com/docker/docker/api/types/swarm" |
| 18 | 19 |
"github.com/docker/docker/integration-cli/checker" |
| 19 | 20 |
"github.com/docker/docker/integration-cli/daemon" |
| ... | ... |
@@ -1329,3 +1331,56 @@ func (s *DockerSwarmSuite) TestAPISwarmErrorHandling(c *check.C) {
|
| 1329 | 1329 |
c.Assert(err, checker.NotNil) |
| 1330 | 1330 |
c.Assert(err.Error(), checker.Contains, "address already in use") |
| 1331 | 1331 |
} |
| 1332 |
+ |
|
| 1333 |
+// Test case for 30242, where duplicate networks, with different drivers `bridge` and `overlay`, |
|
| 1334 |
+// caused both scopes to be `swarm` for `docker network inspect` and `docker network ls`. |
|
| 1335 |
+// This test makes sure the fixes correctly output scopes instead. |
|
| 1336 |
+func (s *DockerSwarmSuite) TestAPIDuplicateNetworks(c *check.C) {
|
|
| 1337 |
+ d := s.AddDaemon(c, true, true) |
|
| 1338 |
+ |
|
| 1339 |
+ name := "foo" |
|
| 1340 |
+ networkCreateRequest := types.NetworkCreateRequest{
|
|
| 1341 |
+ Name: name, |
|
| 1342 |
+ NetworkCreate: types.NetworkCreate{
|
|
| 1343 |
+ CheckDuplicate: false, |
|
| 1344 |
+ }, |
|
| 1345 |
+ } |
|
| 1346 |
+ |
|
| 1347 |
+ var n1 types.NetworkCreateResponse |
|
| 1348 |
+ networkCreateRequest.NetworkCreate.Driver = "bridge" |
|
| 1349 |
+ |
|
| 1350 |
+ status, out, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
|
|
| 1351 |
+ c.Assert(err, checker.IsNil, check.Commentf(string(out))) |
|
| 1352 |
+ c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out))) |
|
| 1353 |
+ |
|
| 1354 |
+ c.Assert(json.Unmarshal(out, &n1), checker.IsNil) |
|
| 1355 |
+ |
|
| 1356 |
+ var n2 types.NetworkCreateResponse |
|
| 1357 |
+ networkCreateRequest.NetworkCreate.Driver = "overlay" |
|
| 1358 |
+ |
|
| 1359 |
+ status, out, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
|
|
| 1360 |
+ c.Assert(err, checker.IsNil, check.Commentf(string(out))) |
|
| 1361 |
+ c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out))) |
|
| 1362 |
+ |
|
| 1363 |
+ c.Assert(json.Unmarshal(out, &n2), checker.IsNil) |
|
| 1364 |
+ |
|
| 1365 |
+ var r1 types.NetworkResource |
|
| 1366 |
+ |
|
| 1367 |
+ status, out, err = d.SockRequest("GET", "/networks/"+n1.ID, nil)
|
|
| 1368 |
+ c.Assert(err, checker.IsNil, check.Commentf(string(out))) |
|
| 1369 |
+ c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out))) |
|
| 1370 |
+ |
|
| 1371 |
+ c.Assert(json.Unmarshal(out, &r1), checker.IsNil) |
|
| 1372 |
+ |
|
| 1373 |
+ c.Assert(r1.Scope, checker.Equals, "local") |
|
| 1374 |
+ |
|
| 1375 |
+ var r2 types.NetworkResource |
|
| 1376 |
+ |
|
| 1377 |
+ status, out, err = d.SockRequest("GET", "/networks/"+n2.ID, nil)
|
|
| 1378 |
+ c.Assert(err, checker.IsNil, check.Commentf(string(out))) |
|
| 1379 |
+ c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out))) |
|
| 1380 |
+ |
|
| 1381 |
+ c.Assert(json.Unmarshal(out, &r2), checker.IsNil) |
|
| 1382 |
+ |
|
| 1383 |
+ c.Assert(r2.Scope, checker.Equals, "swarm") |
|
| 1384 |
+} |