check the exact network's name before creating or stopping it

NetworkList API doesn't return the extact name match, so we can retrieve more than one network with a request

Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
This commit is contained in:
Guillaume Lours 2022-07-11 12:15:56 +02:00
parent 5bc4016e70
commit 50aa9750ee
No known key found for this signature in database
2 changed files with 16 additions and 7 deletions

View File

@ -1041,7 +1041,14 @@ func (s *composeService) ensureNetwork(ctx context.Context, n types.NetworkConfi
if err != nil {
return err
}
if len(networks) == 0 {
networkNotFound := true
for _, net := range networks {
if net.Name == n.Name {
networkNotFound = false
break
}
}
if networkNotFound {
if n.External.External {
if n.Driver == "overlay" {
// Swarm nodes do not register overlay networks that were

View File

@ -163,14 +163,16 @@ func (s *composeService) removeNetwork(ctx context.Context, name string, w progr
var removed int
for _, net := range networks {
if err := s.apiClient().NetworkRemove(ctx, net.ID); err != nil {
if errdefs.IsNotFound(err) {
continue
if net.Name == name {
if err := s.apiClient().NetworkRemove(ctx, net.ID); err != nil {
if errdefs.IsNotFound(err) {
continue
}
w.Event(progress.ErrorEvent(eventName))
return errors.Wrapf(err, fmt.Sprintf("failed to remove network %s", name))
}
w.Event(progress.ErrorEvent(eventName))
return errors.Wrapf(err, fmt.Sprintf("failed to remove network %s", name))
removed++
}
removed++
}
if removed == 0 {