Skip to content

Commit ac7ec5c

Browse files
committed
Support network level mac_address attribute
Signed-off-by: Songmin Li <[email protected]>
1 parent 75d7be2 commit ac7ec5c

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

docs/Extensions.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Podman-compose in addition supports the specification of MAC addresses on a per-
3838
is done by adding a `x-podman.mac_address` key to the network configuration in the container. The
3939
value of the `x-podman.mac_address` key is the MAC address to be used for the network interface.
4040

41+
Note that the [compose spec](https://github.com/compose-spec/compose-spec/blob/main/05-services.md#mac_address)
42+
now supports `mac_address` on the network level, so we recommend using
43+
the standard `mac_address` key for setting the MAC address. The
44+
`x-podman.mac_address` is still supported for backwards compatibility.
45+
46+
4147
Specifying a MAC address for the container and for individual networks at the same time is not
4248
supported.
4349

@@ -69,7 +75,7 @@ services:
6975
x-podman.mac_address: "02:aa:aa:aa:aa:aa"
7076
net1:
7177
ipv4_address: "192.168.1.10"
72-
x-podman.mac_address: "02:bb:bb:bb:bb:bb"
78+
mac_address: "02:bb:bb:bb:bb:bb" # mac_address is supported
7379
```
7480

7581
## Podman-specific network modes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support network level mac_address attribute.

podman_compose.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def get_net_args_from_networks(compose, cnt):
966966
# specified on the network level as well
967967
if mac_address is not None:
968968
for net_config in multiple_nets.values():
969-
network_mac = net_config.get("x-podman.mac_address")
969+
network_mac = net_config.get("mac_address", net_config.get("x-podman.mac_address"))
970970
if network_mac is not None:
971971
raise RuntimeError(
972972
f"conflicting mac addresses {mac_address} and {network_mac}:"
@@ -983,8 +983,10 @@ def get_net_args_from_networks(compose, cnt):
983983

984984
ipv4 = net_config_.get("ipv4_address")
985985
ipv6 = net_config_.get("ipv6_address")
986-
# custom extension; not supported by docker-compose v3
987-
mac = net_config_.get("x-podman.mac_address")
986+
# Note: mac_address is supported by compose spec now, and x-podman.mac_address
987+
# is only for backward compatibility
988+
# https://github.com/compose-spec/compose-spec/blob/main/05-services.md#mac_address
989+
mac = net_config_.get("mac_address", net_config_.get("x-podman.mac_address"))
988990
aliases_on_net = norm_as_list(net_config_.get("aliases", []))
989991

990992
# if a mac_address was specified on the container level, apply it to the first network

tests/integration/nets_test_ip/docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
x-podman.mac_address: "02:01:01:00:01:01"
2424
internal-network:
2525
ipv4_address: "172.19.2.10"
26-
x-podman.mac_address: "02:01:01:00:02:01"
26+
mac_address: "02:01:01:00:02:01"
2727
volumes:
2828
- ./test1.txt:/var/www/html/index.txt:ro,z
2929
web2:

tests/unit/test_get_net_args.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,15 @@ def test_one_mac_two_nets(self):
149149
args = get_net_args(compose, container)
150150
self.assertListEqual(expected_args, args)
151151

152-
def test_mac_on_network(self):
152+
@parameterized.expand([
153+
"mac_address",
154+
"x-podman.mac_address",
155+
])
156+
def test_mac_on_network(self, mac_attr):
153157
mac = "00:11:22:33:44:55"
154158
compose = get_networked_compose()
155159
container = get_minimal_container()
156-
container["networks"] = {"net0": {"x-podman.mac_address": mac}}
160+
container["networks"] = {"net0": {mac_attr: mac}}
157161

158162
expected_args = [
159163
f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}",

0 commit comments

Comments
 (0)