Skip to content

Commit

Permalink
Allow providing custom in_pod argument as a global compose file variable
Browse files Browse the repository at this point in the history
Default command line argument `in_pod` was set to True, but this breaks
the compose file for users who want to use `--userns` argument. This
commit sets default `in_pod` value to None, and later resolves whether
to create a pod by checking compose file, as new argument in compose
file x-podman is now available. Now it is convenient for users to pass
custom `in_pod` value (True or False) as a compose file argument when
command line value of `in_pod` is not provided.

Signed-off-by: Monika Kairaityte <[email protected]>
  • Loading branch information
mokibit committed Jun 20, 2024
1 parent 14f39e5 commit ba2beb2
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 3 deletions.
20 changes: 20 additions & 0 deletions docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,23 @@ In addition, podman-compose supports the following podman-specific values for `n

The options to the network modes are passed to the `--network` option of the `podman create` command
as-is.


## Custom pods management

Podman-compose can have containers in pods. This can be controlled by extension key x-podman in_pod.
It allows providing custom value for --in-pod and is especially relevant when --userns has to be set.

For example, the following docker-compose.yml allows using userns_mode by overriding the default
value of --in-pod (unless it was specifically provided by "--in-pod=True" in command line interface).
```yml
version: "3"
services:
cont:
image: nopush/podman-compose-test
userns_mode: keep-id:uid=1000
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
x-podman:
in_pod: false
```
30 changes: 27 additions & 3 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,18 @@ async def run(self):
if isinstance(retcode, int):
sys.exit(retcode)

def resolve_in_pod(self, compose):
if self.global_args.in_pod_bool is None:
extension_dict = compose.get("x-podman", None)
if extension_dict is not None:
in_pod_value = extension_dict.get("in_pod", None)
if in_pod_value is not None:
self.global_args.in_pod_bool = in_pod_value
else:
self.global_args.in_pod_bool = True
# otherwise use `in_pod` value provided by command line
return self.global_args.in_pod_bool

def _parse_compose_file(self):
args = self.global_args
# cmd = args.command
Expand Down Expand Up @@ -1968,6 +1980,8 @@ def _parse_compose_file(self):
given_containers = list(container_by_name.values())
given_containers.sort(key=lambda c: len(c.get("_deps", None) or []))
# log("sorted:", [c["name"] for c in given_containers])

args.in_pod_bool = self.resolve_in_pod(compose)
pods, containers = transform(args, project_name, given_containers)
self.pods = pods
self.containers = containers
Expand Down Expand Up @@ -2005,12 +2019,22 @@ def _parse_args(self):
for cmd_parser in cmd._parse_args: # pylint: disable=protected-access
cmd_parser(subparser)
self.global_args = parser.parse_args()
if self.global_args.in_pod.lower() not in ('', 'true', '1', 'false', '0'):
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'
)
self.global_args.in_pod_bool = self.global_args.in_pod.lower() in ('', 'true', '1')

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"
Expand All @@ -2029,7 +2053,7 @@ def _init_global_parser(parser):
help="pod creation",
metavar="in_pod",
type=str,
default="true",
default=None,
)
parser.add_argument(
"--pod-args",
Expand Down
9 changes: 9 additions & 0 deletions tests/in_pod/custom_x-podman_false/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3"
services:
cont:
image: nopush/podman-compose-test
userns_mode: keep-id:uid=1000
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]

x-podman:
in_pod: false
6 changes: 6 additions & 0 deletions tests/in_pod/custom_x-podman_not_exists/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3"
services:
cont:
image: nopush/podman-compose-test
userns_mode: keep-id:uid=1000
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]
9 changes: 9 additions & 0 deletions tests/in_pod/custom_x-podman_true/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3"
services:
cont:
image: nopush/podman-compose-test
userns_mode: keep-id:uid=1000
command: ["dumb-init", "/bin/busybox", "httpd", "-f", "-p", "8080"]

x-podman:
in_pod: true
Loading

0 comments on commit ba2beb2

Please sign in to comment.