Skip to content
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

report external network not found when swarm is disabled #10565

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"strconv"
"strings"
"sync"

"github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference"
Expand All @@ -33,6 +34,7 @@ import (
"github.com/docker/cli/cli/streams"
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
Expand Down Expand Up @@ -262,3 +264,23 @@ func (s *composeService) actualNetworks(ctx context.Context, projectName string)
}
return actual, nil
}

var swarmEnabled = struct {
once sync.Once
val bool
err error
}{}

func (s *composeService) isSWarmEnabled(ctx context.Context) (bool, error) {
swarmEnabled.once.Do(func() {
info, err := s.apiClient().Info(ctx)
if err != nil {
swarmEnabled.err = err
}
if info.Swarm.LocalNodeState == swarm.LocalNodeStateInactive {
swarmEnabled.val = info.Swarm.LocalNodeState == swarm.LocalNodeStateInactive
}
})
return swarmEnabled.val, swarmEnabled.err

}
8 changes: 7 additions & 1 deletion pkg/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,13 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi
// Here we assume `driver` is relevant for a network we don't manage
// which is a non-sense, but this is our legacy ¯\(ツ)/¯
// networkAttach will later fail anyway if network actually doesn't exists
return nil
enabled, err := s.isSWarmEnabled(ctx)
if err != nil {
return err
}
if enabled {
return nil
}
}
return fmt.Errorf("network %s declared as external, but could not be found", n.Name)
}
Expand Down