Skip to content

Commit 14eec99

Browse files
authored
Merge pull request #1907 from docker/c5622_device_cgroup_rules
Add support for device_cgroup_rules parameter in host config
2 parents 9e75609 + 48e45af commit 14eec99

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

docker/api/container.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ def create_host_config(self, *args, **kwargs):
438438
``0,1``).
439439
cpuset_mems (str): Memory nodes (MEMs) in which to allow execution
440440
(``0-3``, ``0,1``). Only effective on NUMA systems.
441+
device_cgroup_rules (:py:class:`list`): A list of cgroup rules to
442+
apply to the container.
441443
device_read_bps: Limit read rate (bytes per second) from a device
442444
in the form of: `[{"Path": "device_path", "Rate": rate}]`
443445
device_read_iops: Limit read rate (IO per second) from a device.

docker/models/containers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ def run(self, image, command=None, stdout=True, stderr=False,
515515
(``0-3``, ``0,1``). Only effective on NUMA systems.
516516
detach (bool): Run container in the background and return a
517517
:py:class:`Container` object.
518+
device_cgroup_rules (:py:class:`list`): A list of cgroup rules to
519+
apply to the container.
518520
device_read_bps: Limit read rate (bytes per second) from a device
519521
in the form of: `[{"Path": "device_path", "Rate": rate}]`
520522
device_read_iops: Limit read rate (IO per second) from a device.
@@ -912,6 +914,7 @@ def prune(self, filters=None):
912914
'cpuset_mems',
913915
'cpu_rt_period',
914916
'cpu_rt_runtime',
917+
'device_cgroup_rules',
915918
'device_read_bps',
916919
'device_read_iops',
917920
'device_write_bps',

docker/types/containers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def __init__(self, version, binds=None, port_bindings=None,
120120
init=None, init_path=None, volume_driver=None,
121121
cpu_count=None, cpu_percent=None, nano_cpus=None,
122122
cpuset_mems=None, runtime=None, mounts=None,
123-
cpu_rt_period=None, cpu_rt_runtime=None):
123+
cpu_rt_period=None, cpu_rt_runtime=None,
124+
device_cgroup_rules=None):
124125

125126
if mem_limit is not None:
126127
self['Memory'] = parse_bytes(mem_limit)
@@ -466,6 +467,15 @@ def __init__(self, version, binds=None, port_bindings=None,
466467
raise host_config_version_error('mounts', '1.30')
467468
self['Mounts'] = mounts
468469

470+
if device_cgroup_rules is not None:
471+
if version_lt(version, '1.28'):
472+
raise host_config_version_error('device_cgroup_rules', '1.28')
473+
if not isinstance(device_cgroup_rules, list):
474+
raise host_config_type_error(
475+
'device_cgroup_rules', device_cgroup_rules, 'list'
476+
)
477+
self['DeviceCgroupRules'] = device_cgroup_rules
478+
469479

470480
def host_config_type_error(param, param_value, expected):
471481
error_msg = 'Invalid type for {0} param: expected {1} but found {2}'

tests/integration/api_container_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,21 @@ def test_create_with_cpu_rt_options(self):
474474
assert config['HostConfig']['CpuRealtimeRuntime'] == 500
475475
assert config['HostConfig']['CpuRealtimePeriod'] == 1000
476476

477+
@requires_api_version('1.28')
478+
def test_create_with_device_cgroup_rules(self):
479+
rule = 'c 7:128 rwm'
480+
ctnr = self.client.create_container(
481+
BUSYBOX, 'cat /sys/fs/cgroup/devices/devices.list',
482+
host_config=self.client.create_host_config(
483+
device_cgroup_rules=[rule]
484+
)
485+
)
486+
self.tmp_containers.append(ctnr)
487+
config = self.client.inspect_container(ctnr)
488+
assert config['HostConfig']['DeviceCgroupRules'] == [rule]
489+
self.client.start(ctnr)
490+
assert rule in self.client.logs(ctnr).decode('utf-8')
491+
477492

478493
class VolumeBindTest(BaseAPIIntegrationTest):
479494
def setUp(self):

0 commit comments

Comments
 (0)