fix: allow explicitly overlapping network subnets #5297
Testing
Did you test it with manual invocation of nerdctl too?
Also, can we have an integration test?
Yes to both. 27ac399 adds an integration subtest in cmd/nerdctl/network/network_create_linux_test.go that creates a network whose explicit subnet overlaps an existing one and runs a container on it.
Manual run in a Lima VM, before and after:
nerdctl network create --subnet 10.4.0.0/25 ... with an overlapping network already present fails with subnet 10.4.0.0/25 overlaps with other one on this address space.10.8.0.2/25. The rootful case where the subnet overlaps the host's own 192.168.5.0/24 works too.The new integration test passes with the branch build and fails with stock nerdctl. That run was on 33f0d7b; 6c96741 since then only skips the used-subnet lookup when a subnet is given, so the explicit-subnet path it tests no longer reads that list at all.
<!-- r:ef724be3d57e.99b077af96c0 -->
The test should also verify the functionality of networking by running actual containers and let them communicate
So why do we need to support overlapping pools?
Done in 8b8c0a7. The subtest now starts a container on the overlapping 10.7.0.0/25 network, checks that its address is inside that subnet, and pings it by name from a second container on the same network.
I ran it in a Lima VM (Ubuntu, rootless containerd). With the branch build it passes: the container got 10.7.0.4 and the ping came back. With stock nerdctl v2.3.5 it fails at network create with subnet 10.7.0.0/25 overlaps with other one on this address space. The rest of TestNetworkCreate passes too.
<!-- r:8a537d86c552.18998ff369ba -->
The cases from #4281 are mostly overlaps with subnets that already exist on the host, not with another nerdctl pool:
10.4.0.0/24) shows up as an interface inside the inner one, so the inner default network can't be created.usedSubnets() merges host interface addresses with the subnets of existing CNI configs, and any active nerdctl bridge also shows up as a host interface, so one check covered both. The test uses a second nerdctl network only because that's the overlap CI can set up anywhere. The change only applies when --subnet is given explicitly. Automatic allocation still picks a free subnet.
If you'd rather keep rejecting overlaps between two nerdctl-managed networks and only allow overlaps with host interfaces, I can split the check that way.
<!-- r:8a537d86c552.f74bc41581cf -->
AkihiroSuda
mvanhorn
Motivation
Creating an explicitly configured network whose subnet overlaps an existing host or CNI subnet fails. This prevents valid setups such as macvlan networks and nested nerdctl environments.
parseSubnetrejected any subnet that intersected the set of already used subnets, returning an overlap error even when the caller explicitly requested that subnet.What this changes
Removed the subnet-intersection rejection from
parseSubnet, while retaining validation that the supplied address is the canonical network address. Added a regression test confirming that an explicitly overlapping subnet is accepted.Testing
The netutil packages passed their Go tests. The modified files passed formatting and vet checks. The tests in
pkg/netutil/netutil_test.gofail without the fix in the nerdctl change (checked by reverting the source changes).Fixes #4281