diff --git a/api/types/network/ipam.go b/api/types/network/ipam.go index ace145d7e3..f319e1402b 100644 --- a/api/types/network/ipam.go +++ b/api/types/network/ipam.go @@ -49,12 +49,12 @@ func ValidateIPAM(ipam *IPAM, enableIPv6 bool) error { subnetFamily = ip6 } - if subnet != subnet.Masked() { - errs = append(errs, fmt.Errorf("invalid subnet %s: it should be %s", subnet, subnet.Masked())) + if !enableIPv6 && subnetFamily == ip6 { + continue } - if !enableIPv6 && subnetFamily == ip6 { - errs = append(errs, fmt.Errorf("invalid subnet %s: IPv6 has not been enabled for this network", subnet)) + if subnet != subnet.Masked() { + errs = append(errs, fmt.Errorf("invalid subnet %s: it should be %s", subnet, subnet.Masked())) } if ipRangeErrs := validateIPRange(cfg.IPRange, subnet, subnetFamily); len(ipRangeErrs) > 0 { diff --git a/api/types/network/ipam_test.go b/api/types/network/ipam_test.go index 5b5b48480c..446dbae8aa 100644 --- a/api/types/network/ipam_test.go +++ b/api/types/network/ipam_test.go @@ -31,10 +31,10 @@ func TestNetworkWithInvalidIPAM(t *testing.T) { }, }, { - name: "IPv6 subnet is discarded when IPv6 is disabled", - ipam: IPAM{Config: []IPAMConfig{{Subnet: "2001:db8::/32"}}}, - ipv6: false, - expectedErrors: []string{"invalid subnet 2001:db8::/32: IPv6 has not been enabled for this network"}, + // Regression test for https://github.com/moby/moby/issues/47202 + name: "IPv6 subnet is discarded with no error when IPv6 is disabled", + ipam: IPAM{Config: []IPAMConfig{{Subnet: "2001:db8::/32"}}}, + ipv6: false, }, { name: "Invalid data - Subnet",