Allow specifying custom pod name in --in-pod

Fixes #958: missing or incorrect use of --in-pod and pod-args
Fixes #693: --in-pod 'name' no function

Signed-off-by: norbiros <norbiros@protonmail.com>
Signed-off-by: Norbiros <norbiros@protonmail.com>
This commit is contained in:
norbiros 2025-05-14 21:02:05 +02:00 committed by Povilas Kanapickas
parent bfaf77a506
commit 3d47849d28
6 changed files with 60 additions and 30 deletions

View File

@ -0,0 +1 @@
- Change behaviour of `--in-pod` to handle custom pod names instead of only disabling pod feature

View File

@ -367,13 +367,18 @@ def default_network_name_for_project(compose, net, is_ext):
def transform(args, project_name, given_containers):
if not args.in_pod_bool:
pod_name = None
pods = []
else:
in_pod = str(args.in_pod).lower()
pod_name = None
pods = []
if in_pod in ('true', '1', 'none', ''):
pod_name = f"pod_{project_name}"
pod = {"name": pod_name}
pods = [pod]
elif in_pod not in ('false', '0'):
pod_name = args.in_pod
if pod_name:
pods = [{"name": pod_name}]
containers = []
for cnt in given_containers:
containers.append(dict(cnt, pod=pod_name))
@ -1049,13 +1054,13 @@ def get_net_args_from_networks(compose, cnt):
async def container_to_args(compose, cnt, detached=True, no_deps=False):
# TODO: double check -e , --add-host, -v, --read-only
dirname = compose.dirname
pod = cnt.get("pod", "")
name = cnt["name"]
podman_args = [f"--name={name}"]
if detached:
podman_args.append("-d")
pod = cnt.get("pod", "")
if pod:
podman_args.append(f"--pod={pod}")
deps = []
@ -1988,10 +1993,10 @@ class PodmanCompose:
sys.exit(retcode)
def resolve_in_pod(self):
if self.global_args.in_pod_bool is None:
self.global_args.in_pod_bool = self.x_podman.get("in_pod", True)
if self.global_args.in_pod in (None, ''):
self.global_args.in_pod = self.x_podman.get("in_pod", "1")
# otherwise use `in_pod` value provided by command line
return self.global_args.in_pod_bool
return self.global_args.in_pod
def resolve_pod_args(self):
# Priorities:
@ -2284,7 +2289,7 @@ class PodmanCompose:
self.x_podman = compose.get("x-podman", {})
args.in_pod_bool = self.resolve_in_pod()
args.in_pod = self.resolve_in_pod()
args.pod_arg_list = self.resolve_pod_args()
pods, containers = transform(args, project_name, given_containers)
self.pods = pods
@ -2323,22 +2328,6 @@ class PodmanCompose:
for cmd_parser in cmd._parse_args: # pylint: disable=protected-access
cmd_parser(subparser)
self.global_args = parser.parse_args(argv)
if self.global_args.in_pod is not None and self.global_args.in_pod.lower() not in (
'',
'true',
'1',
'false',
'0',
):
raise ValueError(
f'Invalid --in-pod value: \'{self.global_args.in_pod}\'. '
'It must be set to either of: empty value, true, 1, false, 0'
)
if self.global_args.in_pod == '' or self.global_args.in_pod is None:
self.global_args.in_pod_bool = None
else:
self.global_args.in_pod_bool = self.global_args.in_pod.lower() in ('true', '1')
if self.global_args.version:
self.global_args.command = "version"
@ -2354,7 +2343,12 @@ class PodmanCompose:
parser.add_argument("-v", "--version", help="show version", action="store_true")
parser.add_argument(
"--in-pod",
help="pod creation",
help=(
"Specify pod usage:\n"
" 'true' - create/use a pod named pod_<project name>\n"
" 'false' - do not use a pod\n"
" '<name>' - create/use a custom pod with the given name"
),
metavar="in_pod",
type=str,
default=None,

View File

@ -0,0 +1,8 @@
version: "3"
services:
cont:
image: nopush/podman-compose-test
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
x-podman:
in_pod: custom_test_pod_name

View File

@ -467,6 +467,33 @@ class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin):
# can not actually find this pod because it was not created
self.run_subprocess_assert_returncode(command_rm_pod, 1)
def test_x_podman_in_pod_custom_name(self):
"""
Test that podman-compose will create a pod with a custom name
"""
command_up = [
"python3",
os.path.join(base_path(), "podman_compose.py"),
"-f",
os.path.join(
base_path(),
"tests",
"integration",
"in_pod",
"custom_x-podman_custom_name",
"docker-compose.yml",
),
"up",
"--no-start",
]
try:
self.run_subprocess_assert_returncode(command_up)
finally:
command_rm_pod = ["podman", "pod", "rm", "custom_test_pod_name"]
self.run_subprocess_assert_returncode(command_rm_pod)
def test_x_podman_in_pod_not_exists_command_line_in_pod_empty_string(self):
"""
Test that podman-compose does not allow pod creating when --userns and --pod are set

View File

@ -149,7 +149,7 @@ def set_args(podman_compose: PodmanCompose, file_names: list[str]) -> None:
podman_compose.global_args.project_name = None
podman_compose.global_args.env_file = None
podman_compose.global_args.profile = []
podman_compose.global_args.in_pod_bool = True
podman_compose.global_args.in_pod = "1"
podman_compose.global_args.pod_args = None
podman_compose.global_args.no_normalize = True

View File

@ -249,7 +249,7 @@ def set_args(podman_compose: PodmanCompose, file_names: list[str], no_normalize:
podman_compose.global_args.project_name = None
podman_compose.global_args.env_file = None
podman_compose.global_args.profile = []
podman_compose.global_args.in_pod_bool = True
podman_compose.global_args.in_pod = "1"
podman_compose.global_args.pod_args = None
podman_compose.global_args.no_normalize = no_normalize