Skip to content

Commit ede24eb

Browse files
committed
feat: Add migration of IPv4-only to Dual Stack
1 parent 6100702 commit ede24eb

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

supervisor/docker/network.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,24 @@ def observer(self) -> IPv4Address:
7777
def _get_network(self) -> docker.models.networks.Network:
7878
"""Get supervisor network."""
7979
try:
80-
return self.docker.networks.get(DOCKER_NETWORK)
80+
if (network := self.docker.networks.get(DOCKER_NETWORK))["EnableIPv6"]:
81+
return network
82+
network.remove()
83+
_LOGGER.info("Migrating Supervisor network to IPv4/IPv6 Dual Stack")
8184
except docker.errors.NotFound:
8285
_LOGGER.info("Can't find Supervisor network, creating a new network")
8386

84-
ip6am_pool = docker.types.IPAMPool(DOCKER_IPV6_NETWORK_MASK)
85-
86-
ip4am_pool = docker.types.IPAMPool(
87-
subnet=str(DOCKER_IPV4_NETWORK_MASK),
88-
gateway=str(self.gateway),
89-
iprange=str(DOCKER_IPV4_NETWORK_RANGE),
87+
ipam_config = docker.types.IPAMConfig(
88+
pool_configs=[
89+
docker.types.IPAMPool(subnet=str(DOCKER_IPV6_NETWORK_MASK)),
90+
docker.types.IPAMPool(
91+
subnet=str(DOCKER_IPV4_NETWORK_MASK),
92+
gateway=str(self.gateway),
93+
iprange=str(DOCKER_IPV4_NETWORK_RANGE),
94+
),
95+
]
9096
)
9197

92-
ipam_config = docker.types.IPAMConfig(pool_configs=[ip6am_pool, ip4am_pool])
93-
9498
return self.docker.networks.create(
9599
DOCKER_NETWORK,
96100
driver="bridge",

tests/docker/test_network.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Test Internal network manager for Supervisor."""
2+
3+
from unittest.mock import patch
4+
5+
from docker.models.networks import NetworkCollection
6+
7+
from supervisor.docker.manager import DockerAPI
8+
9+
10+
class MockINetwork(dict):
11+
def __init__(self):
12+
pass
13+
14+
def __getattr__(self, attr: str):
15+
return False
16+
17+
18+
async def test_network_recreate_as_ipv6(docker: DockerAPI):
19+
"""Test remove existing network."""
20+
21+
with (
22+
patch("NetworkCollection.get", return_value=MockINetwork()),
23+
patch.object(NetworkCollection, "create") as mock_create,
24+
):
25+
docker.network._get_network()
26+
27+
mock_create.assert_called_with(enable_ipv6=True)

0 commit comments

Comments
 (0)