From 4f2ced888b1776cdbabced86de6a566d041216d0 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Tue, 17 Jan 2023 18:36:58 +0000 Subject: [PATCH 1/5] Add support for the --cgroupns arg --- python_on_whales/components/container/cli_wrapper.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python_on_whales/components/container/cli_wrapper.py b/python_on_whales/components/container/cli_wrapper.py index 1753ca5c..7d8f7adc 100644 --- a/python_on_whales/components/container/cli_wrapper.py +++ b/python_on_whales/components/container/cli_wrapper.py @@ -492,6 +492,7 @@ def create( cap_add: List[str] = [], cap_drop: List[str] = [], cgroup_parent: Optional[str] = None, + cgroupns: Optional[str] = None, cidfile: Optional[ValidPath] = None, cpu_period: Optional[int] = None, cpu_quota: Optional[int] = None, @@ -617,6 +618,7 @@ def create( full_cmd.add_args_list("--cap-drop", cap_drop) full_cmd.add_simple_arg("--cgroup-parent", cgroup_parent) + full_cmd.add_simple_arg("--cgroupns", cgroupns) full_cmd.add_simple_arg("--cidfile", cidfile) full_cmd.add_simple_arg("--cpu-period", cpu_period) @@ -1180,6 +1182,7 @@ def run( cap_add: List[str] = [], cap_drop: List[str] = [], cgroup_parent: Optional[str] = None, + cgroupns: Optional[str] = None, cidfile: Optional[ValidPath] = None, cpu_period: Optional[int] = None, cpu_quota: Optional[int] = None, @@ -1449,6 +1452,7 @@ def run( full_cmd.add_args_list("--cap-drop", cap_drop) full_cmd.add_simple_arg("--cgroup-parent", cgroup_parent) + full_cmd.add_simple_arg("--cgroupns", cgroupns) full_cmd.add_simple_arg("--cidfile", cidfile) full_cmd.add_simple_arg("--cpu-period", cpu_period) From 448c99da3554b6ad79fc082e1b7937ebfe66dde8 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Wed, 18 Jan 2023 00:03:02 +0000 Subject: [PATCH 2/5] Add arg in docstring --- python_on_whales/components/container/cli_wrapper.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python_on_whales/components/container/cli_wrapper.py b/python_on_whales/components/container/cli_wrapper.py index 7d8f7adc..48c52721 100644 --- a/python_on_whales/components/container/cli_wrapper.py +++ b/python_on_whales/components/container/cli_wrapper.py @@ -1349,6 +1349,7 @@ def run( `add_hosts=[("my_host_1", "192.168.30.31"), ("host2", "192.168.80.81")]` blkio_weight: Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) + cgroupns: Cgroup namespace mode to use, one of 'host' or 'private'. cpu_period: Limit CPU CFS (Completely Fair Scheduler) period cpu_quota: Limit CPU CFS (Completely Fair Scheduler) quota cpu_rt_period: Limit CPU real-time period in microseconds From d86d2d31a82d206668e92f7bbac593e2963ab8cb Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Wed, 18 Jan 2023 10:09:07 +0000 Subject: [PATCH 3/5] Add cgroupns_mode to ContainerHostConfig and add testcase --- python_on_whales/components/container/models.py | 1 + tests/python_on_whales/components/test_container.py | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/python_on_whales/components/container/models.py b/python_on_whales/components/container/models.py index 8fb7de7c..7dd94cd8 100644 --- a/python_on_whales/components/container/models.py +++ b/python_on_whales/components/container/models.py @@ -172,6 +172,7 @@ class ContainerHostConfig(DockerCamelModel): capabilities: List[str] cap_add: List[str] cap_drop: List[str] + cgroupns_mode: Optional[str] dns: List[str] dns_options: List[str] dns_search: List[str] diff --git a/tests/python_on_whales/components/test_container.py b/tests/python_on_whales/components/test_container.py index 69dc48fc..4db11a1c 100644 --- a/tests/python_on_whales/components/test_container.py +++ b/tests/python_on_whales/components/test_container.py @@ -142,6 +142,13 @@ def test_container_create_with_random_ports(): assert container.network_settings.ports["90/tcp"][0]["HostPort"] is not None +def test_container_create_with_cgroupns(): + with docker.container.run( + "ubuntu", ["sleep", "infinity"], cgroupns="host" + ) as container: + assert container.host_config.cgroupns_mode == "host" + + def test_fails_correctly_create_start(): python_code = """ import sys From da321dff8c8b958a4a72d115e41ec2191051a9b2 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Wed, 18 Jan 2023 11:16:28 +0000 Subject: [PATCH 4/5] Remove unnecessary 'Optional' --- python_on_whales/components/container/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_on_whales/components/container/models.py b/python_on_whales/components/container/models.py index 7dd94cd8..8dd1fdef 100644 --- a/python_on_whales/components/container/models.py +++ b/python_on_whales/components/container/models.py @@ -172,7 +172,7 @@ class ContainerHostConfig(DockerCamelModel): capabilities: List[str] cap_add: List[str] cap_drop: List[str] - cgroupns_mode: Optional[str] + cgroupns_mode: str dns: List[str] dns_options: List[str] dns_search: List[str] From 09c06fd574fde31a6b033f73d7a970147da80852 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Sat, 28 Jan 2023 14:08:40 +0000 Subject: [PATCH 5/5] Detach from test container and stop after timeout of 1s --- tests/python_on_whales/components/test_container.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python_on_whales/components/test_container.py b/tests/python_on_whales/components/test_container.py index 4db11a1c..08755279 100644 --- a/tests/python_on_whales/components/test_container.py +++ b/tests/python_on_whales/components/test_container.py @@ -144,7 +144,7 @@ def test_container_create_with_random_ports(): def test_container_create_with_cgroupns(): with docker.container.run( - "ubuntu", ["sleep", "infinity"], cgroupns="host" + "ubuntu", ["sleep", "infinity"], cgroupns="host", detach=True, stop_timeout=1 ) as container: assert container.host_config.cgroupns_mode == "host"