Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow providing custom in_pod argument as a global compose file variable #964

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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