Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope filter in GET /networks/(id or name) #33630

Merged
merged 1 commit into from
Jun 13, 2017

Conversation

yongtang
Copy link
Member

- What I did
This fix tries to add a scope in the query of GET /networks/(id or name) (NetworkInspect) so that in case of duplicate network names, it is possible to locate the network ID based on the network scope (local, 'swarm', or global).

Multiple networks might exist in different scopes, which is a legitimate case. For example, a network name foo might exists locally and in swarm network.

However, before this PR it was not possible to query a network name foo in a specific scope like swarm.

- How I did it

This fix fixes the issue by allowing a scope query in GET /networks/(id or name).

- How to verify it

Additional test cases have been added to unit tests and integration tests.

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)
cat-1796843_1280

This fix is related to docker/cli#167, #30897, #33561, #30242

This fix fixes docker/cli#167

Signed-off-by: Yong Tang yong.tang.github@outlook.com

@yongtang
Copy link
Member Author

/cc @dnephin

@@ -112,15 +113,15 @@ func (n *networkRouter) getNetwork(ctx context.Context, w http.ResponseWriter, r

nw := n.backend.GetNetworks()
for _, network := range nw {
if network.ID() == term {
if network.ID() == term && (len(scope) == 0 || network.Info().Scope() == scope) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A isMatchingScope(network, scope) would be great here so it can be used in all 6 places.

not: I think comparing string to "" makes the intent more obvious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dnephin. The PR has been updated.

NetworkInspect(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, error)
NetworkInspectWithRaw(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, []byte, error)
NetworkInspect(ctx context.Context, networkID string, scope string, verbose bool) (types.NetworkResource, error)
NetworkInspectWithRaw(ctx context.Context, networkID string, scope string, verbose bool) (types.NetworkResource, []byte, error)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that there is two flags,

type NetworkInspectOptions struct {
    Scope string
    Verbose bool
}

would be great, instead of adding more args.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -51,6 +51,13 @@ func TestNetworkInspect(t *testing.T) {
content []byte
err error
)
if strings.HasPrefix(req.URL.RawQuery, "scope=global") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the check below should probably use Contains now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

d := s.AddDaemon(c, true, true)

out, err := d.Cmd("network", "create", "-d", "overlay", "foo")
c.Assert(err, checker.IsNil, check.Commentf(out))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. I think we probably want to create using an API call as well. Maybe it's fine for now.

Using the docker client should make it just as easy to create the network.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Now API call is used to create the network.

@@ -93,4 +100,9 @@ func TestNetworkInspect(t *testing.T) {
if !ok {
t.Fatalf("expected service `web` missing in the verbose output")
}

_, err = client.NetworkInspect(context.Background(), "network_id", "global", false)
if !IsErrNetworkNotFound(err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.EqualError(t, err, ...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

This fix tries to add a `scope` in the query of `/networks/<id>`
(`NetworkInspect`) so that in case of duplicate network names,
it is possible to locate the network ID based on the network
scope (`local`, 'swarm', or `global`).

Multiple networks might exist in different scopes, which is a legitimate case.
For example, a network name `foo` might exists locally and in swarm network.

However, before this PR it was not possible to query a network name `foo`
in a specific scope like swarm.

This fix fixes the issue by allowing a `scope` query in `/networks/<id>`.

Additional test cases have been added to unit tests and integration tests.

This fix is related to docker/cli#167, moby#30897, moby#33561, moby#30242

This fix fixes docker/cli#167

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
@yongtang yongtang force-pushed the 167-cli-network-inspect-scope branch from 8255bca to 158b2a1 Compare June 12, 2017 16:59
@yongtang
Copy link
Member Author

Thanks @dnephin for the review. The PR has been updated. Please take a look.

Copy link
Member

@dnephin dnephin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

var n types.NetworkCreateResponse
networkCreateRequest.NetworkCreate.Driver = "overlay"

status, out, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use the docker client/ here, which you can get from `integration-cli/request.NewClient(), but this is ok as well I guess.

@aaronlehmann
Copy link
Contributor

LGTM

@aaronlehmann aaronlehmann merged commit 4310f7d into moby:master Jun 13, 2017
@GordonTheTurtle GordonTheTurtle added this to the 17.06.0 milestone Jun 13, 2017
@yongtang yongtang deleted the 167-cli-network-inspect-scope branch June 13, 2017 01:42
andrewhsu pushed a commit to docker-archive/docker-ce that referenced this pull request Jun 24, 2017
This fix updates docker/docker to 4310f7d

This fix is related to moby/moby#33630 and docker/cli#167

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Upstream-commit: 8c2f81892beb7634a97efeec053f7052c36b4039
Component: cli
andrewhsu pushed a commit to docker-archive/docker-ce that referenced this pull request Jun 24, 2017
This fix use `scope=swarm` for service related network inspect.
The purpose is that, in case multiple networks with the same
name exist in different scopes, it is still possible to obtain
the network for services.

This fix is related to moby/moby#33630 and docker/cli#167

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
Upstream-commit: 657457ee2cc1b4ced804674ad1b3bfe19b849f31
Component: cli
@thaJeztah thaJeztah removed this from the 17.06.0 milestone Jul 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Service creation with duplicate network name on different scopes (one in local and one in swarm)
5 participants