From e37172c6131b639ecab423cc106ffc91c12cdf01 Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Fri, 2 Feb 2024 10:16:43 +0100 Subject: [PATCH] api/t/network: ValidateIPAM: ignore v6 subnet when IPv6 is disabled Commit 4f47013feb introduced a new validation step to make sure no IPv6 subnet is configured on a network which has EnableIPv6=false. Commit 5d5eeac310 then removed that validation step and automatically enabled IPv6 for networks with a v6 subnet. But this specific commit was reverted in c59e93a67b and now the error introduced by 4f47013feb is re-introduced. But it turns out some users expect a network created with an IPv6 subnet and EnableIPv6=false to actually have no IPv6 connectivity. This restores that behavior. Signed-off-by: Albin Kerouanton --- api/types/network/ipam.go | 8 ++++---- api/types/network/ipam_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) 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",