Skip to content

Commit 8bb4310

Browse files
authored
Merge pull request #1182 from zeyugao/pids_limit
Implement pids_limit
2 parents 150ab02 + 98f166d commit 8bb4310

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add support for `pids_limit` and `deploy.resources.limits.pids`

podman_compose.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,22 @@ def container_to_cpu_res_args(cnt, podman_args):
771771
str(mem_res).lower(),
772772
))
773773

774+
# Handle pids limit from both container level and deploy section
775+
pids_limit = cnt.get("pids_limit")
776+
deploy_pids = limits.get("pids")
777+
778+
# Ensure consistency between pids_limit and deploy.resources.limits.pids
779+
if pids_limit is not None and deploy_pids is not None:
780+
if str(pids_limit) != str(deploy_pids):
781+
raise ValueError(
782+
f"Inconsistent PIDs limit: pids_limit ({pids_limit}) and "
783+
f"deploy.resources.limits.pids ({deploy_pids}) must be the same"
784+
)
785+
786+
final_pids_limit = pids_limit if pids_limit is not None else deploy_pids
787+
if final_pids_limit is not None:
788+
podman_args.extend(["--pids-limit", str(final_pids_limit)])
789+
774790

775791
def port_dict_to_str(port_desc):
776792
# NOTE: `mode: host|ingress` is ignored

tests/unit/test_container_to_args.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,67 @@ async def test_cpuset(self):
630630
"busybox",
631631
],
632632
)
633+
634+
async def test_pids_limit_container_level(self):
635+
c = create_compose_mock()
636+
cnt = get_minimal_container()
637+
cnt["pids_limit"] = 100
638+
639+
args = await container_to_args(c, cnt)
640+
self.assertEqual(
641+
args,
642+
[
643+
"--name=project_name_service_name1",
644+
"-d",
645+
"--network=bridge:alias=service_name",
646+
"--pids-limit",
647+
"100",
648+
"busybox",
649+
],
650+
)
651+
652+
async def test_pids_limit_deploy_section(self):
653+
c = create_compose_mock()
654+
cnt = get_minimal_container()
655+
cnt["deploy"] = {"resources": {"limits": {"pids": 100}}}
656+
657+
args = await container_to_args(c, cnt)
658+
self.assertEqual(
659+
args,
660+
[
661+
"--name=project_name_service_name1",
662+
"-d",
663+
"--network=bridge:alias=service_name",
664+
"--pids-limit",
665+
"100",
666+
"busybox",
667+
],
668+
)
669+
670+
async def test_pids_limit_both_same(self):
671+
c = create_compose_mock()
672+
cnt = get_minimal_container()
673+
cnt["pids_limit"] = 100
674+
cnt["deploy"] = {"resources": {"limits": {"pids": 100}}}
675+
676+
args = await container_to_args(c, cnt)
677+
self.assertEqual(
678+
args,
679+
[
680+
"--name=project_name_service_name1",
681+
"-d",
682+
"--network=bridge:alias=service_name",
683+
"--pids-limit",
684+
"100",
685+
"busybox",
686+
],
687+
)
688+
689+
async def test_pids_limit_both_different(self):
690+
c = create_compose_mock()
691+
cnt = get_minimal_container()
692+
cnt["pids_limit"] = 100
693+
cnt["deploy"] = {"resources": {"limits": {"pids": 200}}}
694+
695+
with self.assertRaises(ValueError):
696+
await container_to_args(c, cnt)

0 commit comments

Comments
 (0)