Skip to content

Commit 00f72cb

Browse files
committed
report external network not found when swarm is disabled
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent fd7847f commit 00f72cb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkg/compose/compose.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"strconv"
2626
"strings"
27+
"sync"
2728

2829
"github.com/compose-spec/compose-go/types"
2930
"github.com/distribution/distribution/v3/reference"
@@ -33,6 +34,7 @@ import (
3334
"github.com/docker/cli/cli/streams"
3435
moby "github.com/docker/docker/api/types"
3536
"github.com/docker/docker/api/types/filters"
37+
"github.com/docker/docker/api/types/swarm"
3638
"github.com/docker/docker/client"
3739
"github.com/opencontainers/go-digest"
3840
"github.com/pkg/errors"
@@ -262,3 +264,23 @@ func (s *composeService) actualNetworks(ctx context.Context, projectName string)
262264
}
263265
return actual, nil
264266
}
267+
268+
var swarmEnabled = struct {
269+
once sync.Once
270+
val bool
271+
err error
272+
}{}
273+
274+
func (s *composeService) isSWarmEnabled(ctx context.Context) (bool, error) {
275+
swarmEnabled.once.Do(func() {
276+
info, err := s.apiClient().Info(ctx)
277+
if err != nil {
278+
swarmEnabled.err = err
279+
}
280+
if info.Swarm.LocalNodeState == swarm.LocalNodeStateInactive {
281+
swarmEnabled.val = info.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
282+
}
283+
})
284+
return swarmEnabled.val, swarmEnabled.err
285+
286+
}

pkg/compose/create.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,13 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi
11031103
// Here we assume `driver` is relevant for a network we don't manage
11041104
// which is a non-sense, but this is our legacy ¯\(ツ)/¯
11051105
// networkAttach will later fail anyway if network actually doesn't exists
1106-
return nil
1106+
enabled, err := s.isSWarmEnabled(ctx)
1107+
if err != nil {
1108+
return err
1109+
}
1110+
if enabled {
1111+
return nil
1112+
}
11071113
}
11081114
return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
11091115
}

0 commit comments

Comments
 (0)