Skip to content

Commit

Permalink
Merge pull request containers#964 from mokibit/set-custom-in_pod-in-c…
Browse files Browse the repository at this point in the history
…ompose-file

Allow providing custom in_pod argument as a global compose file variable
  • Loading branch information
p12tic committed Jun 22, 2024
2 parents 650a835 + 360b85b commit de05098
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 5 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
```
34 changes: 29 additions & 5 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 @@ -1792,8 +1804,8 @@ def _parse_compose_file(self):
os.environ.update({
key: value for key, value in dotenv_dict.items() if key.startswith("PODMAN_")
})
self.environ = dict(os.environ)
self.environ.update(dotenv_dict)
self.environ = dotenv_dict
self.environ.update(dict(os.environ))
# see: https://docs.docker.com/compose/reference/envvars/
# see: https://docs.docker.com/compose/env-file/
self.environ.update({
Expand Down Expand Up @@ -1971,6 +1983,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 @@ -2008,12 +2022,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 @@ -2032,7 +2056,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
6 changes: 6 additions & 0 deletions tests/env-file-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ podman-compose -f $(pwd)/project/container-compose.env-file-obj.yaml up
```
podman-compose -f $(pwd)/project/container-compose.env-file-obj-optional.yaml up
```

based on environment variable precedent this command should give podman-rocks-321

```
ZZVAR1=podman-rocks-321 podman-compose -f $(pwd)/project/container-compose.yaml --env-file $(pwd)/env-files/project-1.env up
```
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 de05098

Please sign in to comment.