Skip to content

Commit

Permalink
Merge pull request #1093 from lisongmin/add-network-level-mac_address…
Browse files Browse the repository at this point in the history
…-attr

Support network level mac_address attribute
  • Loading branch information
p12tic authored Dec 29, 2024
2 parents 75d7be2 + ac7ec5c commit 61fa24b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ Podman-compose in addition supports the specification of MAC addresses on a per-
is done by adding a `x-podman.mac_address` key to the network configuration in the container. The
value of the `x-podman.mac_address` key is the MAC address to be used for the network interface.

Note that the [compose spec](https://github.com/compose-spec/compose-spec/blob/main/05-services.md#mac_address)
now supports `mac_address` on the network level, so we recommend using
the standard `mac_address` key for setting the MAC address. The
`x-podman.mac_address` is still supported for backwards compatibility.


Specifying a MAC address for the container and for individual networks at the same time is not
supported.

Expand Down Expand Up @@ -69,7 +75,7 @@ services:
x-podman.mac_address: "02:aa:aa:aa:aa:aa"
net1:
ipv4_address: "192.168.1.10"
x-podman.mac_address: "02:bb:bb:bb:bb:bb"
mac_address: "02:bb:bb:bb:bb:bb" # mac_address is supported
```

## Podman-specific network modes
Expand Down
1 change: 1 addition & 0 deletions newsfragments/network_level_mac_address.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support network level mac_address attribute.
8 changes: 5 additions & 3 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ def get_net_args_from_networks(compose, cnt):
# specified on the network level as well
if mac_address is not None:
for net_config in multiple_nets.values():
network_mac = net_config.get("x-podman.mac_address")
network_mac = net_config.get("mac_address", net_config.get("x-podman.mac_address"))
if network_mac is not None:
raise RuntimeError(
f"conflicting mac addresses {mac_address} and {network_mac}:"
Expand All @@ -983,8 +983,10 @@ def get_net_args_from_networks(compose, cnt):

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

# if a mac_address was specified on the container level, apply it to the first network
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/nets_test_ip/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ services:
x-podman.mac_address: "02:01:01:00:01:01"
internal-network:
ipv4_address: "172.19.2.10"
x-podman.mac_address: "02:01:01:00:02:01"
mac_address: "02:01:01:00:02:01"
volumes:
- ./test1.txt:/var/www/html/index.txt:ro,z
web2:
Expand Down
8 changes: 6 additions & 2 deletions tests/unit/test_get_net_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ def test_one_mac_two_nets(self):
args = get_net_args(compose, container)
self.assertListEqual(expected_args, args)

def test_mac_on_network(self):
@parameterized.expand([
"mac_address",
"x-podman.mac_address",
])
def test_mac_on_network(self, mac_attr):
mac = "00:11:22:33:44:55"
compose = get_networked_compose()
container = get_minimal_container()
container["networks"] = {"net0": {"x-podman.mac_address": mac}}
container["networks"] = {"net0": {mac_attr: mac}}

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

0 comments on commit 61fa24b

Please sign in to comment.