Add support for setting container ulimit

- supports `ulimit: host` or other single value
- As well as `ulimit: [nofile=5000, nproc=5000]` array
- As well as `ulimit: {nofile: 5000, nproc: 5000}` dict

Authored-by: Tyler Ramer <tramer@pivotal.io>
This commit is contained in:
Tyler Ramer 2019-09-11 11:50:00 -04:00 committed by Muayyad Alsadi
parent f505e499a2
commit 243bdb688d
4 changed files with 65 additions and 0 deletions

View File

@ -209,6 +209,17 @@ def norm_as_dict(src):
raise ValueError("dictionary or iterable is expected")
return dst
def norm_ulimit(inner_value):
if is_dict(inner_value):
if not inner_value.keys() & {"soft", "hard"}:
raise ValueError("expected at least one soft or hard limit")
soft = inner_value.get("soft", inner_value.get("hard"))
hard = inner_value.get("hard", inner_value.get("soft"))
return "{}:{}".format(soft, hard)
elif is_list(inner_value): return norm_ulimit(norm_as_dict(inner_value))
# if int or string return as is
return inner_value
# transformation helpers
@ -482,6 +493,17 @@ def container_to_args(compose, cnt, detached=True, podman_command='run'):
podman_args.append('-i')
if cnt.get('tty'):
podman_args.append('--tty')
ulimit = cnt.get('ulimit', [])
if ulimit is not None:
# ulimit can be a single value, i.e. ulimit: host
if is_str(ulimit):
podman_args.extend(['--ulimit', ulimit])
# or a dictionary or list:
else:
ulimit = norm_as_dict(ulimit)
ulimit = [ "{}={}".format(ulimit_key, norm_ulimit(inner_value)) for ulimit_key, inner_value in ulimit.items()]
for i in ulimit:
podman_args.extend(['--ulimit', i])
# currently podman shipped by fedora does not package this
# if cnt.get('init'):
# args.append('--init')

3
tests/ulimit/Dockerfile Normal file
View File

@ -0,0 +1,3 @@
FROM busybox
COPY ./ulimit.sh /bin/ulimit.sh

View File

@ -0,0 +1,30 @@
version: "3"
services:
ulimit1:
image: ulimit_test
command: ["ulimit.sh" ]
ulimit: nofile=1001
build:
context: ./
dockerfile: Dockerfile
ulimit2:
image: ulimit_test
command: ["ulimit.sh" ]
ulimit:
- nproc=1002:2002
- nofile=1002
build:
context: ./
dockerfile: Dockerfile
ulimit3:
image: ulimit_test
command: [ "ulimit.sh" ]
ulimit:
nofile: 1003
nproc:
soft: 1003
hard: 2003
build:
context: ./
dockerfile: Dockerfile

10
tests/ulimit/ulimit.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
echo "soft process limit"
ulimit -S -u
echo "hard process limit"
ulimit -H -u
echo "soft nofile limit"
ulimit -S -n
echo "hard nofile limit"
ulimit -H -n