Merge pull request #9520 from milas/bugfix-network-ambiguity

fix: prevent network name ambiguity
This commit is contained in:
Guillaume Lours 2022-06-02 12:44:12 +02:00 committed by GitHub
commit d5528f3a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 74 additions and 67 deletions

View File

@ -31,6 +31,7 @@ import (
moby "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/blkiodev"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice"
@ -1007,9 +1008,17 @@ func getAliases(s types.ServiceConfig, c *types.ServiceNetworkConfig) []string {
}
func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfig) error {
_, err := s.apiClient().NetworkInspect(ctx, n.Name, moby.NetworkInspectOptions{})
// NetworkInspect will match on ID prefix, so NetworkList with a name
// filter is used to look for an exact match to prevent e.g. a network
// named `db` from getting erroneously matched to a network with an ID
// like `db9086999caf`
networks, err := s.apiClient().NetworkList(ctx, moby.NetworkListOptions{
Filters: filters.NewArgs(filters.Arg("name", n.Name)),
})
if err != nil {
if errdefs.IsNotFound(err) {
return err
}
if len(networks) == 0 {
if n.External.External {
if n.Driver == "overlay" {
// Swarm nodes do not register overlay networks that were
@ -1075,8 +1084,6 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi
w.Event(progress.CreatedEvent(networkEventName))
return nil
}
return err
}
return nil
}