mirror of
https://github.com/containers/podman-compose.git
synced 2025-04-27 10:58:49 +02:00
Support network scoped service aliases
Signed-off-by: Songmin Li <lisongmin@protonmail.com>
This commit is contained in:
parent
4a232f5e32
commit
978a1381bc
1
newsfragments/network-scoped-aliases.feature
Normal file
1
newsfragments/network-scoped-aliases.feature
Normal file
@ -0,0 +1 @@
|
|||||||
|
Support network scoped service aliases.
|
@ -857,6 +857,7 @@ async def assert_cnt_nets(compose, cnt):
|
|||||||
net = cnt.get("network_mode")
|
net = cnt.get("network_mode")
|
||||||
if net and not net.startswith("bridge"):
|
if net and not net.startswith("bridge"):
|
||||||
return
|
return
|
||||||
|
|
||||||
cnt_nets = cnt.get("networks")
|
cnt_nets = cnt.get("networks")
|
||||||
if cnt_nets and isinstance(cnt_nets, dict):
|
if cnt_nets and isinstance(cnt_nets, dict):
|
||||||
cnt_nets = list(cnt_nets.keys())
|
cnt_nets = list(cnt_nets.keys())
|
||||||
@ -910,12 +911,13 @@ def get_net_args_from_network_mode(compose, cnt):
|
|||||||
aliases_on_container = [service_name]
|
aliases_on_container = [service_name]
|
||||||
if cnt.get("_aliases"):
|
if cnt.get("_aliases"):
|
||||||
aliases_on_container.extend(cnt.get("_aliases"))
|
aliases_on_container.extend(cnt.get("_aliases"))
|
||||||
net_args.append("--network=bridge")
|
net_options = [f"alias={alias}" for alias in aliases_on_container]
|
||||||
mac_address = cnt.get("mac_address")
|
mac_address = cnt.get("mac_address")
|
||||||
if mac_address:
|
if mac_address:
|
||||||
net_args.append(f"--mac-address={mac_address}")
|
net_options.append(f"mac={mac_address}")
|
||||||
for alias in aliases_on_container:
|
|
||||||
net_args.extend([f"--network-alias={alias}"])
|
net = f"{net}," if ":" in net else f"{net}:"
|
||||||
|
net_args.append(f"--network={net}{','.join(net_options)}")
|
||||||
else:
|
else:
|
||||||
log.fatal("unknown network_mode [%s]", net)
|
log.fatal("unknown network_mode [%s]", net)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -937,29 +939,18 @@ def get_net_args_from_networks(compose, cnt):
|
|||||||
service_name = cnt["service_name"]
|
service_name = cnt["service_name"]
|
||||||
|
|
||||||
aliases_on_container = [service_name]
|
aliases_on_container = [service_name]
|
||||||
# NOTE: from podman manpage:
|
|
||||||
# NOTE: A container will only have access to aliases on the first network
|
|
||||||
# that it joins. This is a limitation that will be removed in a later
|
|
||||||
# release.
|
|
||||||
aliases_on_container.extend(cnt.get("_aliases", []))
|
aliases_on_container.extend(cnt.get("_aliases", []))
|
||||||
aliases_on_net = []
|
|
||||||
|
|
||||||
# TODO: add support for per-interface aliases
|
|
||||||
# See https://docs.docker.com/compose/compose-file/compose-file-v3/#aliases
|
|
||||||
# Even though podman accepts network-specific aliases (e.g., --network=bridge:alias=foo,
|
|
||||||
# podman currently ignores this if a per-container network-alias is set; as pdoman-compose
|
|
||||||
# always sets a network-alias to the container name, is currently doesn't make sense to
|
|
||||||
# implement this.
|
|
||||||
multiple_nets = cnt.get("networks", {})
|
multiple_nets = cnt.get("networks", {})
|
||||||
if not multiple_nets:
|
if not multiple_nets:
|
||||||
if not compose.default_net:
|
if not compose.default_net:
|
||||||
# The bridge mode in podman is using the `podman` network.
|
# The bridge mode in podman is using the `podman` network.
|
||||||
# It seems weird, but we should keep this behavior to avoid
|
# It seems weird, but we should keep this behavior to avoid
|
||||||
# breaking changes.
|
# breaking changes.
|
||||||
net_args.append("--network=bridge")
|
net_options = [f"alias={alias}" for alias in aliases_on_container]
|
||||||
if mac_address:
|
if mac_address:
|
||||||
net_args.append(f"--mac-address={mac_address}")
|
net_options.append(f"mac={mac_address}")
|
||||||
net_args.extend([f"--network-alias={alias}" for alias in aliases_on_container])
|
net_args.append(f"--network=bridge:{','.join(net_options)}")
|
||||||
return net_args
|
return net_args
|
||||||
|
|
||||||
multiple_nets = {compose.default_net: {}}
|
multiple_nets = {compose.default_net: {}}
|
||||||
@ -984,7 +975,7 @@ def get_net_args_from_networks(compose, cnt):
|
|||||||
)
|
)
|
||||||
|
|
||||||
for net_, net_config_ in multiple_nets.items():
|
for net_, net_config_ in multiple_nets.items():
|
||||||
net_desc = compose.networks[net_] or {}
|
net_desc = compose.networks.get(net_) or {}
|
||||||
is_ext = net_desc.get("external")
|
is_ext = net_desc.get("external")
|
||||||
ext_desc = is_ext if isinstance(is_ext, str) else {}
|
ext_desc = is_ext if isinstance(is_ext, str) else {}
|
||||||
default_net_name = default_network_name_for_project(compose, net_, is_ext)
|
default_net_name = default_network_name_for_project(compose, net_, is_ext)
|
||||||
@ -992,9 +983,9 @@ def get_net_args_from_networks(compose, cnt):
|
|||||||
|
|
||||||
ipv4 = net_config_.get("ipv4_address")
|
ipv4 = net_config_.get("ipv4_address")
|
||||||
ipv6 = net_config_.get("ipv6_address")
|
ipv6 = net_config_.get("ipv6_address")
|
||||||
# custom extension; not supported by docker-compose v3
|
|
||||||
mac = net_config_.get("x-podman.mac_address")
|
mac = net_config_.get("x-podman.mac_address")
|
||||||
aliases_on_net = norm_as_list(net_config_.get("aliases", []))
|
aliases_on_net = net_config_.get("aliases")
|
||||||
|
|
||||||
# if a mac_address was specified on the container level, apply it to the first network
|
# if a mac_address was specified on the container level, apply it to the first network
|
||||||
# This works for Python > 3.6, because dict insert ordering is preserved, so we are
|
# This works for Python > 3.6, because dict insert ordering is preserved, so we are
|
||||||
@ -1004,30 +995,24 @@ def get_net_args_from_networks(compose, cnt):
|
|||||||
mac = mac_address
|
mac = mac_address
|
||||||
mac_address = None
|
mac_address = None
|
||||||
|
|
||||||
if len(multiple_nets) == 1:
|
net_options = []
|
||||||
net_args.append(f"--network={net_name}")
|
if ipv4:
|
||||||
if ipv4:
|
net_options.append(f"ip={ipv4}")
|
||||||
net_args.append(f"--ip={ipv4}")
|
if ipv6:
|
||||||
if ipv6:
|
net_options.append(f"ip6={ipv6}")
|
||||||
net_args.append(f"--ip6={ipv6}")
|
if mac:
|
||||||
if mac:
|
net_options.append(f"mac={mac}")
|
||||||
net_args.append(f"--mac-address={mac}")
|
|
||||||
|
# Container level service aliases
|
||||||
|
net_options.extend([f"alias={alias}" for alias in aliases_on_container])
|
||||||
|
# network level service aliases
|
||||||
|
if aliases_on_net:
|
||||||
|
net_options.extend([f"alias={alias}" for alias in aliases_on_net])
|
||||||
|
|
||||||
|
if net_options:
|
||||||
|
net_args.append(f"--network={net_name}:" + ",".join(net_options))
|
||||||
else:
|
else:
|
||||||
net_options = []
|
net_args.append(f"--network={net_name}")
|
||||||
if ipv4:
|
|
||||||
net_options.append(f"ip={ipv4}")
|
|
||||||
if ipv6:
|
|
||||||
net_options.append(f"ip={ipv6}")
|
|
||||||
if mac:
|
|
||||||
net_options.append(f"mac={mac}")
|
|
||||||
|
|
||||||
if net_options:
|
|
||||||
net_args.append(f"--network={net_name}:" + ",".join(net_options))
|
|
||||||
else:
|
|
||||||
net_args.append(f"--network={net_name}")
|
|
||||||
|
|
||||||
net_args.extend([f"--network-alias={alias}" for alias in aliases_on_container])
|
|
||||||
net_args.extend([f"--network-alias={alias}" for alias in aliases_on_net])
|
|
||||||
|
|
||||||
return net_args
|
return net_args
|
||||||
|
|
||||||
@ -3487,7 +3472,7 @@ def compose_logs_parse(parser):
|
|||||||
parser.add_argument("-t", "--timestamps", action="store_true", help="Show timestamps.")
|
parser.add_argument("-t", "--timestamps", action="store_true", help="Show timestamps.")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--tail",
|
"--tail",
|
||||||
help="Number of lines to show from the end of the logs for each " "container.",
|
help="Number of lines to show from the end of the logs for each container.",
|
||||||
type=str,
|
type=str,
|
||||||
default="all",
|
default="all",
|
||||||
)
|
)
|
||||||
|
@ -51,8 +51,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -69,8 +68,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--runtime",
|
"--runtime",
|
||||||
"runsc",
|
"runsc",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -92,8 +90,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--sysctl",
|
"--sysctl",
|
||||||
"net.core.somaxconn=1024",
|
"net.core.somaxconn=1024",
|
||||||
"--sysctl",
|
"--sysctl",
|
||||||
@ -117,8 +114,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--sysctl",
|
"--sysctl",
|
||||||
"net.core.somaxconn=1024",
|
"net.core.somaxconn=1024",
|
||||||
"--sysctl",
|
"--sysctl",
|
||||||
@ -149,8 +145,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--pid",
|
"--pid",
|
||||||
"host",
|
"host",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -170,8 +165,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--http-proxy=false",
|
"--http-proxy=false",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -197,8 +191,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
'--uidmap',
|
'--uidmap',
|
||||||
'1000:1000:1',
|
'1000:1000:1',
|
||||||
'--uidmap',
|
'--uidmap',
|
||||||
@ -219,8 +212,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
'--gidmap',
|
'--gidmap',
|
||||||
'1000:1000:1',
|
'1000:1000:1',
|
||||||
'--gidmap',
|
'--gidmap',
|
||||||
@ -242,8 +234,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--rootfs",
|
"--rootfs",
|
||||||
"/path/to/rootfs",
|
"/path/to/rootfs",
|
||||||
],
|
],
|
||||||
@ -261,8 +252,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--no-hosts",
|
"--no-hosts",
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
@ -287,8 +277,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"ZZVAR2=podman-rocks-124",
|
"ZZVAR2=podman-rocks-124",
|
||||||
"-e",
|
"-e",
|
||||||
"ZZVAR3=podman-rocks-125",
|
"ZZVAR3=podman-rocks-125",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -321,8 +310,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"ZZVAR2=podman-rocks-124",
|
"ZZVAR2=podman-rocks-124",
|
||||||
"-e",
|
"-e",
|
||||||
"ZZVAR3=podman-rocks-125",
|
"ZZVAR3=podman-rocks-125",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -351,8 +339,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"ZZVAR1=podman-rocks-223",
|
"ZZVAR1=podman-rocks-223",
|
||||||
"-e",
|
"-e",
|
||||||
"ZZVAR2=podman-rocks-224",
|
"ZZVAR2=podman-rocks-224",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -376,8 +363,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"ZZVAR2=podman-rocks-124",
|
"ZZVAR2=podman-rocks-124",
|
||||||
"-e",
|
"-e",
|
||||||
"ZZVAR3=podman-rocks-125",
|
"ZZVAR3=podman-rocks-125",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -403,8 +389,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -428,8 +413,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--device",
|
"--device",
|
||||||
"nvidia.com/gpu=all",
|
"nvidia.com/gpu=all",
|
||||||
"--security-opt=label=disable",
|
"--security-opt=label=disable",
|
||||||
@ -463,8 +447,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--device",
|
"--device",
|
||||||
"nvidia.com/gpu=0",
|
"nvidia.com/gpu=0",
|
||||||
"--device",
|
"--device",
|
||||||
@ -500,8 +483,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--device",
|
"--device",
|
||||||
"nvidia.com/gpu=all",
|
"nvidia.com/gpu=all",
|
||||||
"--security-opt=label=disable",
|
"--security-opt=label=disable",
|
||||||
@ -535,8 +517,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--device",
|
"--device",
|
||||||
"nvidia.com/gpu=1",
|
"nvidia.com/gpu=1",
|
||||||
"--device",
|
"--device",
|
||||||
@ -581,8 +562,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
*expected_additional_args,
|
*expected_additional_args,
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -606,8 +586,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
f"--network={expected_network_name}",
|
f"--network={expected_network_name}:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@ -629,8 +608,7 @@ class TestContainerToArgs(unittest.IsolatedAsyncioTestCase):
|
|||||||
"/dev/ttyS0",
|
"/dev/ttyS0",
|
||||||
"--device-cgroup-rule",
|
"--device-cgroup-rule",
|
||||||
"c 100:200 rwm",
|
"c 100:200 rwm",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"busybox",
|
"busybox",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -36,8 +36,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret,type=env,target=ENV_SECRET",
|
"my_secret,type=env,target=ENV_SECRET",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -68,8 +67,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret,type=env,target=ENV_SECRET",
|
"my_secret,type=env,target=ENV_SECRET",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -152,8 +150,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret_name",
|
"my_secret_name",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -191,8 +188,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret_name,uid=103,gid=103,mode=400",
|
"my_secret_name,uid=103,gid=103,mode=400",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -257,8 +253,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret_name,type=env,target=does_not_equal_secret_name",
|
"my_secret_name,type=env,target=does_not_equal_secret_name",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -289,8 +284,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--secret",
|
"--secret",
|
||||||
"my_secret_name,type=does_not_equal_env",
|
"my_secret_name,type=does_not_equal_env",
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -361,8 +355,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--volume",
|
"--volume",
|
||||||
expected_volume_ref,
|
expected_volume_ref,
|
||||||
"busybox",
|
"busybox",
|
||||||
@ -398,8 +391,7 @@ class TestContainerToArgsSecrets(unittest.IsolatedAsyncioTestCase):
|
|||||||
[
|
[
|
||||||
"--name=project_name_service_name1",
|
"--name=project_name_service_name1",
|
||||||
"-d",
|
"-d",
|
||||||
"--network=bridge",
|
"--network=bridge:alias=service_name",
|
||||||
"--network-alias=service_name",
|
|
||||||
"--volume",
|
"--volume",
|
||||||
repo_root()
|
repo_root()
|
||||||
+ "/test_dirname/my_secret:/run/secrets/unused_params_warning:ro,rprivate,rbind",
|
+ "/test_dirname/my_secret:/run/secrets/unused_params_warning:ro,rprivate,rbind",
|
||||||
|
@ -24,6 +24,9 @@ def get_networked_compose(num_networks=1):
|
|||||||
"enable_ipv6": True,
|
"enable_ipv6": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if num_networks == 1:
|
||||||
|
compose.default_net = "net0"
|
||||||
|
|
||||||
return compose
|
return compose
|
||||||
|
|
||||||
|
|
||||||
@ -41,8 +44,22 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container = get_minimal_container()
|
container = get_minimal_container()
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
"--network=bridge",
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
]
|
||||||
|
args = get_net_args(compose, container)
|
||||||
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
|
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},mac={mac_address}",
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -53,21 +70,11 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {}}
|
container["networks"] = {"net0": {}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
def test_network_mode_and_networks_unsupported(self):
|
|
||||||
compose = get_networked_compose()
|
|
||||||
container = get_minimal_container()
|
|
||||||
container["networks"] = {"net0": {}}
|
|
||||||
container["network_mode"] = "none"
|
|
||||||
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
get_net_args(compose, container)
|
|
||||||
|
|
||||||
def test_alias(self):
|
def test_alias(self):
|
||||||
compose = get_networked_compose()
|
compose = get_networked_compose()
|
||||||
container = get_minimal_container()
|
container = get_minimal_container()
|
||||||
@ -75,10 +82,18 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["_aliases"] = ["alias1", "alias2"]
|
container["_aliases"] = ["alias1", "alias2"]
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME},alias=alias1,alias=alias2",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
]
|
||||||
"--network-alias=alias1",
|
args = get_net_args(compose, container)
|
||||||
"--network-alias=alias2",
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
|
def test_aliases_on_network_scope(self):
|
||||||
|
compose = get_networked_compose()
|
||||||
|
container = get_minimal_container()
|
||||||
|
container["networks"] = {"net0": {"aliases": ["alias1"]}}
|
||||||
|
|
||||||
|
expected_args = [
|
||||||
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME},alias=alias1",
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -90,9 +105,7 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {"ipv4_address": ip}}
|
container["networks"] = {"net0": {"ipv4_address": ip}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:ip={ip},alias={SERVICE_NAME}",
|
||||||
f"--ip={ip}",
|
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertEqual(expected_args, args)
|
self.assertEqual(expected_args, args)
|
||||||
@ -104,9 +117,7 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {"ipv6_address": ipv6_address}}
|
container["networks"] = {"net0": {"ipv6_address": ipv6_address}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:ip6={ipv6_address},alias={SERVICE_NAME}",
|
||||||
f"--ip6={ipv6_address}",
|
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -119,9 +130,7 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["mac_address"] = mac
|
container["mac_address"] = mac
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}",
|
||||||
f"--mac-address={mac}",
|
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -134,9 +143,20 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["mac_address"] = mac
|
container["mac_address"] = mac
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0:mac={mac}",
|
f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1",
|
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
]
|
||||||
|
args = get_net_args(compose, container)
|
||||||
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
|
def test_mac_on_network(self):
|
||||||
|
mac = "00:11:22:33:44:55"
|
||||||
|
compose = get_networked_compose()
|
||||||
|
container = get_minimal_container()
|
||||||
|
container["networks"] = {"net0": {"x-podman.mac_address": mac}}
|
||||||
|
|
||||||
|
expected_args = [
|
||||||
|
f"--network={PROJECT_NAME}_net0:mac={mac},alias={SERVICE_NAME}",
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -147,9 +167,8 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {}, "net1": {}}
|
container["networks"] = {"net0": {}, "net1": {}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1",
|
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -160,9 +179,8 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = ["net0", "net1"]
|
container["networks"] = ["net0", "net1"]
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0",
|
f"--network={PROJECT_NAME}_net0:alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1",
|
f"--network={PROJECT_NAME}_net1:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -175,9 +193,8 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {"ipv4_address": ip0}, "net1": {"ipv4_address": ip1}}
|
container["networks"] = {"net0": {"ipv4_address": ip0}, "net1": {"ipv4_address": ip1}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0:ip={ip0}",
|
f"--network={PROJECT_NAME}_net0:ip={ip0},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1:ip={ip1}",
|
f"--network={PROJECT_NAME}_net1:ip={ip1},alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -190,9 +207,8 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["networks"] = {"net0": {"ipv6_address": ip0}, "net1": {"ipv6_address": ip1}}
|
container["networks"] = {"net0": {"ipv6_address": ip0}, "net1": {"ipv6_address": ip1}}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0:ip={ip0}",
|
f"--network={PROJECT_NAME}_net0:ip6={ip0},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1:ip={ip1}",
|
f"--network={PROJECT_NAME}_net1:ip6={ip1},alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -209,9 +225,8 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0:mac={mac0}",
|
f"--network={PROJECT_NAME}_net0:mac={mac0},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1:mac={mac1}",
|
f"--network={PROJECT_NAME}_net1:mac={mac1},alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
@ -233,7 +248,7 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["mac_address"] = mac_1
|
container["mac_address"] = mac_1
|
||||||
|
|
||||||
expected_exception = (
|
expected_exception = (
|
||||||
r"specifying mac_address on both container and network level " r"is not supported"
|
r"specifying mac_address on both container and network level is not supported"
|
||||||
)
|
)
|
||||||
self.assertRaisesRegex(RuntimeError, expected_exception, get_net_args, compose, container)
|
self.assertRaisesRegex(RuntimeError, expected_exception, get_net_args, compose, container)
|
||||||
|
|
||||||
@ -254,17 +269,20 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container["mac_address"] = mac
|
container["mac_address"] = mac
|
||||||
|
|
||||||
expected_args = [
|
expected_args = [
|
||||||
f"--network={PROJECT_NAME}_net0:ip={ip4_0},ip={ip6_0},mac={mac}",
|
f"--network={PROJECT_NAME}_net0:ip={ip4_0},ip6={ip6_0},mac={mac},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net1:ip={ip4_1}",
|
f"--network={PROJECT_NAME}_net1:ip={ip4_1},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net2:ip={ip6_2}",
|
f"--network={PROJECT_NAME}_net2:ip6={ip6_2},alias={SERVICE_NAME}",
|
||||||
f"--network={PROJECT_NAME}_net3",
|
f"--network={PROJECT_NAME}_net3:alias={SERVICE_NAME}",
|
||||||
f"--network-alias={SERVICE_NAME}",
|
|
||||||
]
|
]
|
||||||
args = get_net_args(compose, container)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
@parameterized.expand([
|
@parameterized.expand([
|
||||||
("bridge", ["--network=bridge", f"--network-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"]),
|
("host", ["--network=host"]),
|
||||||
("none", ["--network=none"]),
|
("none", ["--network=none"]),
|
||||||
("slirp4netns", ["--network=slirp4netns"]),
|
("slirp4netns", ["--network=slirp4netns"]),
|
||||||
@ -280,6 +298,10 @@ class TestGetNetArgs(unittest.TestCase):
|
|||||||
container = get_minimal_container()
|
container = get_minimal_container()
|
||||||
container["network_mode"] = network_mode
|
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)
|
args = get_net_args(compose, container)
|
||||||
self.assertListEqual(expected_args, args)
|
self.assertListEqual(expected_args, args)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user