Skip to content

Commit

Permalink
Set mac_address in bridge mode.
Browse files Browse the repository at this point in the history
The top-level mac_address should be part of networks, and docker >= 25.0 will ignore this attribute
in favor of the network-scoped mac_address.

We set the mac_address in the bridge mode here for backward compatibility.

Signed-off-by: Songmin Li <[email protected]>
  • Loading branch information
lisongmin committed Dec 19, 2024
1 parent 4941bac commit 2890304
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,12 @@ def get_net_args_from_network_mode(compose, cnt):
if cnt.get("_aliases"):
aliases_on_container.extend(cnt.get("_aliases"))
net_options = [f"alias={alias}" for alias in aliases_on_container]
mac_address = cnt.get("mac_address")
if mac_address:
net_options.append(f"mac={mac_address}")

net = f"{net}," if ":" in net else f"{net}:"
net_args.append(f"--network={net}{'.'.join(net_options)}")
net_args.append(f"--network={net}{','.join(net_options)}")
else:
log.fatal("unknown network_mode [%s]", net)
sys.exit(1)
Expand Down Expand Up @@ -944,7 +948,9 @@ def get_net_args_from_networks(compose, cnt):
# It seems weird, but we should keep this behavior to avoid
# breaking changes.
net_options = [f"alias={alias}" for alias in aliases_on_container]
net_args.append(f"--network=bridge:{'.'.join(net_options)}")
if mac_address:
net_options.append(f"mac={mac_address}")
net_args.append(f"--network=bridge:{','.join(net_options)}")
return net_args

multiple_nets = {compose.default_net: {}}
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/test_get_net_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ def test_default_net_is_None(self):
compose = get_networked_compose()
container = get_minimal_container()

mac_address = "11:22:33:44:55:66"
container["mac_address"] = mac_address

compose.default_net = None

expected_args = [
f"--network=bridge:alias={SERVICE_NAME}",
f"--network=bridge:alias={SERVICE_NAME},mac={mac_address}",
]
args = get_net_args(compose, container)
self.assertListEqual(expected_args, args)
Expand Down Expand Up @@ -275,8 +278,11 @@ def test_mixed_config(self):
self.assertListEqual(expected_args, args)

@parameterized.expand([
("bridge", [f"--network=bridge:alias={SERVICE_NAME}"]),
("bridge:ip=10.88.0.3", [f"--network=bridge:ip=10.88.0.3,alias={SERVICE_NAME}"]),
("bridge", [f"--network=bridge:alias={SERVICE_NAME},mac=11:22:33:44:55:66"]),
(
"bridge:ip=10.88.0.3",
[f"--network=bridge:ip=10.88.0.3,alias={SERVICE_NAME},mac=11:22:33:44:55:66"],
),
("host", ["--network=host"]),
("none", ["--network=none"]),
("slirp4netns", ["--network=slirp4netns"]),
Expand All @@ -292,6 +298,10 @@ def test_network_modes(self, network_mode, expected_args):
container = get_minimal_container()
container["network_mode"] = network_mode

mac_address = "11:22:33:44:55:66"
container["network_mode"] = network_mode
container["mac_address"] = mac_address

args = get_net_args(compose, container)
self.assertListEqual(expected_args, args)

Expand Down

0 comments on commit 2890304

Please sign in to comment.