Use `scope=swarm` for service related network inspect.

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>
This commit is contained in:
Yong Tang 2017-06-13 02:53:53 +00:00
parent 8c2f81892b
commit 657457ee2c
9 changed files with 15 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/inspect" "github.com/docker/cli/cli/command/inspect"
"github.com/docker/docker/api/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -40,7 +41,7 @@ func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
ctx := context.Background() ctx := context.Background()
getNetFunc := func(name string) (interface{}, []byte, error) { getNetFunc := func(name string) (interface{}, []byte, error) {
return client.NetworkInspectWithRaw(ctx, name, opts.verbose) return client.NetworkInspectWithRaw(ctx, name, types.NetworkInspectOptions{Verbose: opts.verbose})
} }
return inspect.Inspect(dockerCli.Out(), opts.names, opts.format, getNetFunc) return inspect.Inspect(dockerCli.Out(), opts.names, opts.format, getNetFunc)

View File

@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/docker/api/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -33,7 +34,7 @@ func runRemove(dockerCli *command.DockerCli, networks []string) error {
status := 0 status := 0
for _, name := range networks { for _, name := range networks {
if nw, _, err := client.NetworkInspectWithRaw(ctx, name, false); err == nil && if nw, _, err := client.NetworkInspectWithRaw(ctx, name, types.NetworkInspectOptions{}); err == nil &&
nw.Ingress && nw.Ingress &&
!command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), ingressWarning) { !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), ingressWarning) {
continue continue

View File

@ -61,7 +61,7 @@ func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
} }
getNetwork := func(ref string) (interface{}, []byte, error) { getNetwork := func(ref string) (interface{}, []byte, error) {
network, _, err := client.NetworkInspectWithRaw(ctx, ref, false) network, _, err := client.NetworkInspectWithRaw(ctx, ref, types.NetworkInspectOptions{Scope: "swarm"})
if err == nil || !apiclient.IsErrNetworkNotFound(err) { if err == nil || !apiclient.IsErrNetworkNotFound(err) {
return network, nil, err return network, nil, err
} }

View File

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -349,7 +350,7 @@ func convertNetworks(ctx context.Context, apiClient client.NetworkAPIClient, net
var netAttach []swarm.NetworkAttachmentConfig var netAttach []swarm.NetworkAttachmentConfig
for _, net := range networks.Value() { for _, net := range networks.Value() {
networkIDOrName := net.Target networkIDOrName := net.Target
_, err := apiClient.NetworkInspect(ctx, networkIDOrName, false) _, err := apiClient.NetworkInspect(ctx, networkIDOrName, types.NetworkInspectOptions{Scope: "swarm"})
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1008,7 +1008,7 @@ func updateNetworks(ctx context.Context, apiClient client.NetworkAPIClient, flag
toRemove := buildToRemoveSet(flags, flagNetworkRemove) toRemove := buildToRemoveSet(flags, flagNetworkRemove)
idsToRemove := make(map[string]struct{}) idsToRemove := make(map[string]struct{})
for networkIDOrName := range toRemove { for networkIDOrName := range toRemove {
network, err := apiClient.NetworkInspect(ctx, networkIDOrName, false) network, err := apiClient.NetworkInspect(ctx, networkIDOrName, types.NetworkInspectOptions{Scope: "swarm"})
if err != nil { if err != nil {
return err return err
} }

View File

@ -174,7 +174,7 @@ func validateExternalNetworks(
externalNetworks []string, externalNetworks []string,
) error { ) error {
for _, networkName := range externalNetworks { for _, networkName := range externalNetworks {
network, err := client.NetworkInspect(ctx, networkName, false) network, err := client.NetworkInspect(ctx, networkName, types.NetworkInspectOptions{})
switch { switch {
case dockerclient.IsErrNotFound(err): case dockerclient.IsErrNotFound(err):
return errors.Errorf("network %q is declared as external, but could not be found. You need to create a swarm-scoped network before the stack is deployed", networkName) return errors.Errorf("network %q is declared as external, but could not be found. You need to create a swarm-scoped network before the stack is deployed", networkName)

View File

@ -70,7 +70,7 @@ func TestValidateExternalNetworks(t *testing.T) {
for _, testcase := range testcases { for _, testcase := range testcases {
fakeClient := &network.FakeClient{ fakeClient := &network.FakeClient{
NetworkInspectFunc: func(_ context.Context, _ string, _ bool) (types.NetworkResource, error) { NetworkInspectFunc: func(_ context.Context, _ string, _ types.NetworkInspectOptions) (types.NetworkResource, error) {
return testcase.inspectResponse, testcase.inspectError return testcase.inspectResponse, testcase.inspectError
}, },
} }

View File

@ -68,7 +68,7 @@ func inspectImages(ctx context.Context, dockerCli *command.DockerCli) inspect.Ge
func inspectNetwork(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { func inspectNetwork(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc {
return func(ref string) (interface{}, []byte, error) { return func(ref string) (interface{}, []byte, error) {
return dockerCli.Client().NetworkInspectWithRaw(ctx, ref, false) return dockerCli.Client().NetworkInspectWithRaw(ctx, ref, types.NetworkInspectOptions{})
} }
} }

View File

@ -9,7 +9,7 @@ import (
// FakeClient is a fake NetworkAPIClient // FakeClient is a fake NetworkAPIClient
type FakeClient struct { type FakeClient struct {
NetworkInspectFunc func(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, error) NetworkInspectFunc func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error)
} }
// NetworkConnect fakes connecting to a network // NetworkConnect fakes connecting to a network
@ -28,15 +28,15 @@ func (c *FakeClient) NetworkDisconnect(ctx context.Context, networkID, container
} }
// NetworkInspect fakes inspecting a network // NetworkInspect fakes inspecting a network
func (c *FakeClient) NetworkInspect(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, error) { func (c *FakeClient) NetworkInspect(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, error) {
if c.NetworkInspectFunc != nil { if c.NetworkInspectFunc != nil {
return c.NetworkInspectFunc(ctx, networkID, verbose) return c.NetworkInspectFunc(ctx, networkID, options)
} }
return types.NetworkResource{}, nil return types.NetworkResource{}, nil
} }
// NetworkInspectWithRaw fakes inspecting a network with a raw response // NetworkInspectWithRaw fakes inspecting a network with a raw response
func (c *FakeClient) NetworkInspectWithRaw(ctx context.Context, networkID string, verbose bool) (types.NetworkResource, []byte, error) { func (c *FakeClient) NetworkInspectWithRaw(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
return types.NetworkResource{}, nil, nil return types.NetworkResource{}, nil, nil
} }