diff --git a/newsfragments/custom-pod-name-argument.change b/newsfragments/custom-pod-name-argument.change new file mode 100644 index 0000000..3fa559e --- /dev/null +++ b/newsfragments/custom-pod-name-argument.change @@ -0,0 +1 @@ +- Change behaviour of `--in-pod` to handle custom pod names instead of only disabling pod feature diff --git a/podman_compose.py b/podman_compose.py index 6c2454b..c2c88de 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -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_\n" + " 'false' - do not use a pod\n" + " '' - create/use a custom pod with the given name" + ), metavar="in_pod", type=str, default=None, diff --git a/tests/integration/in_pod/custom_x-podman_custom_name/docker-compose.yml b/tests/integration/in_pod/custom_x-podman_custom_name/docker-compose.yml new file mode 100644 index 0000000..f3550a6 --- /dev/null +++ b/tests/integration/in_pod/custom_x-podman_custom_name/docker-compose.yml @@ -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 diff --git a/tests/integration/in_pod/test_podman_compose_in_pod.py b/tests/integration/in_pod/test_podman_compose_in_pod.py index 736ad29..0bfe78b 100644 --- a/tests/integration/in_pod/test_podman_compose_in_pod.py +++ b/tests/integration/in_pod/test_podman_compose_in_pod.py @@ -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 diff --git a/tests/unit/test_can_merge_build.py b/tests/unit/test_can_merge_build.py index 20e0863..4e7b22c 100644 --- a/tests/unit/test_can_merge_build.py +++ b/tests/unit/test_can_merge_build.py @@ -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 diff --git a/tests/unit/test_normalize_final_build.py b/tests/unit/test_normalize_final_build.py index 40e81aa..27a827b 100644 --- a/tests/unit/test_normalize_final_build.py +++ b/tests/unit/test_normalize_final_build.py @@ -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