report external network not found when swarm is disabled

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-05-14 14:32:23 +02:00 committed by Nicolas De loof
parent fd7847f2ac
commit 00f72cb553
2 changed files with 29 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"github.com/compose-spec/compose-go/types"
"github.com/distribution/distribution/v3/reference"
@ -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"
@ -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
}

View File

@ -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)
}