mirror of https://github.com/docker/cli.git
Skip inspects of built-in networks on stack deploy
Signed-off-by: Alex Mavrogiannis <alex.mavrogiannis@docker.com>
This commit is contained in:
parent
7f684c7512
commit
7f53c99dfe
|
@ -171,13 +171,18 @@ func validateExternalNetworks(
|
||||||
externalNetworks []string,
|
externalNetworks []string,
|
||||||
) error {
|
) error {
|
||||||
for _, networkName := range externalNetworks {
|
for _, networkName := range externalNetworks {
|
||||||
|
if !container.NetworkMode(networkName).IsUserDefined() {
|
||||||
|
// Networks that are not user defined always exist on all nodes as
|
||||||
|
// local-scoped networks, so there's no need to inspect them.
|
||||||
|
continue
|
||||||
|
}
|
||||||
network, err := client.NetworkInspect(ctx, networkName, types.NetworkInspectOptions{})
|
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)
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
case container.NetworkMode(networkName).IsUserDefined() && network.Scope != "swarm":
|
case network.Scope != "swarm":
|
||||||
return errors.Errorf("network %q is declared as external, but it is not in the right scope: %q instead of \"swarm\"", networkName, network.Scope)
|
return errors.Errorf("network %q is declared as external, but it is not in the right scope: %q instead of \"swarm\"", networkName, network.Scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,8 @@ func (n notFound) NotFound() bool {
|
||||||
|
|
||||||
func TestValidateExternalNetworks(t *testing.T) {
|
func TestValidateExternalNetworks(t *testing.T) {
|
||||||
var testcases = []struct {
|
var testcases = []struct {
|
||||||
|
inspected bool
|
||||||
|
noInspect bool
|
||||||
inspectResponse types.NetworkResource
|
inspectResponse types.NetworkResource
|
||||||
inspectError error
|
inspectError error
|
||||||
expectedMsg string
|
expectedMsg string
|
||||||
|
@ -56,7 +58,8 @@ func TestValidateExternalNetworks(t *testing.T) {
|
||||||
expectedMsg: "Unexpected",
|
expectedMsg: "Unexpected",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
network: "host",
|
noInspect: true,
|
||||||
|
network: "host",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
network: "user",
|
network: "user",
|
||||||
|
@ -71,11 +74,15 @@ func TestValidateExternalNetworks(t *testing.T) {
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
fakeClient := &network.FakeClient{
|
fakeClient := &network.FakeClient{
|
||||||
NetworkInspectFunc: func(_ context.Context, _ string, _ types.NetworkInspectOptions) (types.NetworkResource, error) {
|
NetworkInspectFunc: func(_ context.Context, _ string, _ types.NetworkInspectOptions) (types.NetworkResource, error) {
|
||||||
|
testcase.inspected = true
|
||||||
return testcase.inspectResponse, testcase.inspectError
|
return testcase.inspectResponse, testcase.inspectError
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
networks := []string{testcase.network}
|
networks := []string{testcase.network}
|
||||||
err := validateExternalNetworks(context.Background(), fakeClient, networks)
|
err := validateExternalNetworks(context.Background(), fakeClient, networks)
|
||||||
|
if testcase.noInspect && testcase.inspected {
|
||||||
|
assert.Fail(t, "expected no network inspect operation but one occurent")
|
||||||
|
}
|
||||||
if testcase.expectedMsg == "" {
|
if testcase.expectedMsg == "" {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue