Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 9b2a406

Browse files
authored
Merge pull request #161 from alexmavr/backport-cli#362-17.06
[17.06] backport Skip inspect of built-in networks on stack deploy
2 parents 344dbf9 + 07c249a commit 9b2a406

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

components/cli/cli/command/stack/deploy_composefile.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,18 @@ func validateExternalNetworks(
174174
externalNetworks []string,
175175
) error {
176176
for _, networkName := range externalNetworks {
177+
if !container.NetworkMode(networkName).IsUserDefined() {
178+
// Networks that are not user defined always exist on all nodes as
179+
// local-scoped networks, so there's no need to inspect them.
180+
continue
181+
}
177182
network, err := client.NetworkInspect(ctx, networkName, false)
178183
switch {
179184
case dockerclient.IsErrNotFound(err):
180185
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)
181186
case err != nil:
182187
return err
183-
case container.NetworkMode(networkName).IsUserDefined() && network.Scope != "swarm":
188+
case network.Scope != "swarm":
184189
return errors.Errorf("network %q is declared as external, but it is not in the right scope: %q instead of \"swarm\"", networkName, network.Scope)
185190
}
186191
}

components/cli/cli/command/stack/deploy_composefile_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func (n notFound) NotFound() bool {
4242

4343
func TestValidateExternalNetworks(t *testing.T) {
4444
var testcases = []struct {
45+
inspected bool
46+
noInspect bool
4547
inspectResponse types.NetworkResource
4648
inspectError error
4749
expectedMsg string
@@ -56,7 +58,8 @@ func TestValidateExternalNetworks(t *testing.T) {
5658
expectedMsg: "Unexpected",
5759
},
5860
{
59-
network: "host",
61+
noInspect: true,
62+
network: "host",
6063
},
6164
{
6265
network: "user",
@@ -71,11 +74,15 @@ func TestValidateExternalNetworks(t *testing.T) {
7174
for _, testcase := range testcases {
7275
fakeClient := &network.FakeClient{
7376
NetworkInspectFunc: func(_ context.Context, _ string, _ bool) (types.NetworkResource, error) {
77+
testcase.inspected = true
7478
return testcase.inspectResponse, testcase.inspectError
7579
},
7680
}
7781
networks := []string{testcase.network}
7882
err := validateExternalNetworks(context.Background(), fakeClient, networks)
83+
if testcase.noInspect && testcase.inspected {
84+
assert.Fail(t, "expected no network inspect operation but one occurent")
85+
}
7986
if testcase.expectedMsg == "" {
8087
assert.NoError(t, err)
8188
} else {

0 commit comments

Comments
 (0)