Skip to content

Commit 5d5eeac

Browse files
committed
daemon: automatically set network EnableIPv6 if needed
PR 4f47013 added a validation step to `NetworkCreate` to ensure no IPv6 subnet could be set on a network if its `EnableIPv6` parameter is false. Before that, the daemon was accepting such request but was doing nothing with the IPv6 subnet. This validation step is now deleted, and we automatically set `EnableIPv6` if an IPv6 subnet was specified. Signed-off-by: Albin Kerouanton <[email protected]>
1 parent 6ce5aa1 commit 5d5eeac

3 files changed

Lines changed: 29 additions & 13 deletions

File tree

api/types/network/ipam.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,28 @@ const (
3030
ip6 ipFamily = "IPv6"
3131
)
3232

33-
func ValidateIPAM(ipam *IPAM, enableIPv6 bool) error {
33+
// HasIPv6Subnets checks whether there's any IPv6 subnets in the ipam parameter. It ignores any invalid Subnet and nil
34+
// ipam.
35+
func HasIPv6Subnets(ipam *IPAM) bool {
36+
if ipam == nil {
37+
return false
38+
}
39+
40+
for _, cfg := range ipam.Config {
41+
subnet, err := netip.ParsePrefix(cfg.Subnet)
42+
if err != nil {
43+
continue
44+
}
45+
46+
if subnet.Addr().Is6() {
47+
return true
48+
}
49+
}
50+
51+
return false
52+
}
53+
54+
func ValidateIPAM(ipam *IPAM) error {
3455
if ipam == nil {
3556
return nil
3657
}
@@ -51,10 +72,6 @@ func ValidateIPAM(ipam *IPAM, enableIPv6 bool) error {
5172
errs = append(errs, fmt.Errorf("invalid subnet %s: it should be %s", subnet, subnet.Masked()))
5273
}
5374

54-
if !enableIPv6 && subnetFamily == ip6 {
55-
errs = append(errs, fmt.Errorf("invalid subnet %s: IPv6 has not been enabled for this network", subnet))
56-
}
57-
5875
if ipRangeErrs := validateIPRange(cfg.IPRange, subnet, subnetFamily); len(ipRangeErrs) > 0 {
5976
errs = append(errs, ipRangeErrs...)
6077
}

api/types/network/ipam_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ func TestNetworkWithInvalidIPAM(t *testing.T) {
3030
"invalid auxiliary address DefaultGatewayIPv4: parent subnet is an IPv4 block",
3131
},
3232
},
33-
{
34-
name: "IPv6 subnet is discarded when IPv6 is disabled",
35-
ipam: IPAM{Config: []IPAMConfig{{Subnet: "2001:db8::/32"}}},
36-
ipv6: false,
37-
expectedErrors: []string{"invalid subnet 2001:db8::/32: IPv6 has not been enabled for this network"},
38-
},
3933
{
4034
name: "Invalid data - Subnet",
4135
ipam: IPAM{Config: []IPAMConfig{{Subnet: "foobar"}}},
@@ -128,7 +122,7 @@ func TestNetworkWithInvalidIPAM(t *testing.T) {
128122
t.Run(tc.name, func(t *testing.T) {
129123
t.Parallel()
130124

131-
errs := ValidateIPAM(&tc.ipam, tc.ipv6)
125+
errs := ValidateIPAM(&tc.ipam)
132126
if tc.expectedErrors == nil {
133127
assert.NilError(t, errs)
134128
return

daemon/network.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
303303
return nil, errdefs.Forbidden(errors.New(`This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.`))
304304
}
305305

306+
if network.HasIPv6Subnets(create.IPAM) {
307+
create.EnableIPv6 = true
308+
}
309+
306310
var warning string
307311
nw, err := daemon.GetNetworkByName(create.Name)
308312
if err != nil {
@@ -347,9 +351,10 @@ func (daemon *Daemon) createNetwork(cfg *config.Config, create types.NetworkCrea
347351
nwOptions = append(nwOptions, libnetwork.NetworkOptionConfigOnly())
348352
}
349353

350-
if err := network.ValidateIPAM(create.IPAM, create.EnableIPv6); err != nil {
354+
if err := network.ValidateIPAM(create.IPAM); err != nil {
351355
return nil, errdefs.InvalidParameter(err)
352356
}
357+
353358
if create.IPAM != nil {
354359
ipam := create.IPAM
355360
v4Conf, v6Conf, err := getIpamConfig(ipam.Config)

0 commit comments

Comments
 (0)