Skip to content

Commit

Permalink
api: allow creating a network of which name is the prefix of the ID o…
Browse files Browse the repository at this point in the history
…f a swarm network

Previously, it doesn't allow creating such a network:

e.g.

    $ docker network inspect -f '{{.Id}}' ingress
    84xh9knigj6zyt00u31e26nj3
    $ docker network create 84
    Error response from daemon: network with name 84 already exists

Fix #27866

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Nov 1, 2016
1 parent 8cced87 commit 1e5398f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 11 additions & 2 deletions api/server/router/network/network_routes.go
Expand Up @@ -3,6 +3,7 @@ package network
import (
"encoding/json"
"net/http"
"strings"

"golang.org/x/net/context"

Expand Down Expand Up @@ -79,8 +80,16 @@ func (n *networkRouter) postNetworkCreate(ctx context.Context, w http.ResponseWr
return err
}

if _, err := n.clusterProvider.GetNetwork(create.Name); err == nil {
return libnetwork.NetworkNameError(create.Name)
if nw, err := n.clusterProvider.GetNetwork(create.Name); err == nil {
// GetNetwork can return nw if:
// 1. nw.Name == create.Name
// 2. nw.ID == create.Name
// 3. nw.ID starts with create.Name
//
// we do not need to raise an error for the case 3.
if !strings.HasPrefix(nw.ID, create.Name) {
return libnetwork.NetworkNameError(create.Name)
}
}

nw, err := n.backend.CreateNetwork(create)
Expand Down
18 changes: 18 additions & 0 deletions integration-cli/docker_cli_swarm_test.go
Expand Up @@ -588,3 +588,21 @@ func (s *DockerSwarmSuite) TestSwarmServiceEnvFile(c *check.C) {
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Contains, "[VAR1=C VAR2]")
}

// Test case for #27866
func (s *DockerSwarmSuite) TestSwarmNetworkCreateIssue27866(c *check.C) {
d := s.AddDaemon(c, true, true)
out, err := d.Cmd("network", "inspect", "-f", "{{.Id}}", "ingress")
c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
ingressID := strings.TrimSpace(out)
c.Assert(ingressID, checker.Not(checker.Equals), "")

// create a network of which name is the prefix of the ID of an overlay network
// (ingressID in this case)
newNetName := ingressID[0:2]
out, err = d.Cmd("network", "create", "--driver", "overlay", newNetName)
// In #27866, it was failing because of "network with name %s already exists"
c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
out, err = d.Cmd("network", "rm", newNetName)
c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
}

0 comments on commit 1e5398f

Please sign in to comment.