-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Add scope
filter in GET /networks/(id or name)
#33630
Conversation
/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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
client/interface.go
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
client/network_inspect_test.go
Outdated
@@ -51,6 +51,13 @@ func TestNetworkInspect(t *testing.T) { | |||
content []byte | |||
err error | |||
) | |||
if strings.HasPrefix(req.URL.RawQuery, "scope=global") { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
client/network_inspect_test.go
Outdated
@@ -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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert.EqualError(t, err, ...)
There was a problem hiding this comment.
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>
8255bca
to
158b2a1
Compare
Thanks @dnephin for the review. The PR has been updated. Please take a look. |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
LGTM |
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
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
- What I did
This fix tries to add a
scope
in the query ofGET /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', orglobal
).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 inGET /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)
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