From 3f7d622143cdb6c793ab6beb6dda6a50d17504de Mon Sep 17 00:00:00 2001 From: Alexey Rokhin Date: Wed, 19 Apr 2017 15:06:48 +0300 Subject: [PATCH 1/2] Add cpu_count, cpu_percent, cpus parameters to container HostConfig. Signed-off-by: Alexey Rokhin --- docker/models/containers.py | 6 ++++++ docker/types/containers.py | 27 +++++++++++++++++++++++++- tests/unit/api_container_test.py | 30 +++++++++++++++++++++++++++++ tests/unit/dockertypes_test.py | 33 ++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/docker/models/containers.py b/docker/models/containers.py index fb10ba90b3..f623f6b818 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -456,10 +456,13 @@ def run(self, image, command=None, stdout=True, stderr=False, cap_add (list of str): Add kernel capabilities. For example, ``["SYS_ADMIN", "MKNOD"]``. cap_drop (list of str): Drop kernel capabilities. + cpu_count (int): CPU count (Windows only). + cpu_percent (int): CPU percent (Windows only). cpu_period (int): The length of a CPU period in microseconds. cpu_quota (int): Microseconds of CPU time that the container can get in a CPU period. cpu_shares (int): CPU shares (relative weight). + cpus (float): Number of CPUs. cpuset_cpus (str): CPUs in which to allow execution (``0-3``, ``0,1``). detach (bool): Run container in the background and return a @@ -801,9 +804,12 @@ def prune(self, filters=None): 'cap_add', 'cap_drop', 'cgroup_parent', + 'cpu_count', + 'cpu_percent', 'cpu_period', 'cpu_quota', 'cpu_shares', + 'cpus', 'cpuset_cpus', 'device_read_bps', 'device_read_iops', diff --git a/docker/types/containers.py b/docker/types/containers.py index 0af24cb845..8c5e5ef7b5 100644 --- a/docker/types/containers.py +++ b/docker/types/containers.py @@ -118,7 +118,8 @@ def __init__(self, version, binds=None, port_bindings=None, tmpfs=None, oom_score_adj=None, dns_opt=None, cpu_shares=None, cpuset_cpus=None, userns_mode=None, pids_limit=None, isolation=None, auto_remove=False, storage_opt=None, - init=None, init_path=None, volume_driver=None): + init=None, init_path=None, volume_driver=None, + cpu_count=None, cpu_percent=None, cpus=None): if mem_limit is not None: self['Memory'] = parse_bytes(mem_limit) @@ -433,6 +434,30 @@ def __init__(self, version, binds=None, port_bindings=None, raise host_config_version_error('volume_driver', '1.21') self['VolumeDriver'] = volume_driver + if cpu_count: + if not isinstance(cpu_count, int): + raise host_config_type_error('cpu_count', cpu_count, 'int') + if version_lt(version, '1.25'): + raise host_config_version_error('cpu_count', '1.25') + + self['CpuCount'] = cpu_count + + if cpu_percent: + if not isinstance(cpu_percent, int): + raise host_config_type_error('cpu_percent', cpu_percent, 'int') + if version_lt(version, '1.25'): + raise host_config_version_error('cpu_percent', '1.25') + + self['CpuPercent'] = cpu_percent + + if cpus: + if not isinstance(cpus, (float, int)): + raise host_config_type_error('cpus', cpus, 'float') + if version_lt(version, '1.25'): + raise host_config_version_error('cpus', '1.25') + + self['NanoCpus'] = int(1000000000 * cpus) + def host_config_type_error(param, param_value, expected): error_msg = 'Invalid type for {0} param: expected {1} but found {2}' diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py index ad79c5c6fe..30287110d4 100644 --- a/tests/unit/api_container_test.py +++ b/tests/unit/api_container_test.py @@ -1152,6 +1152,36 @@ def test_create_container_with_unicode_envvars(self): self.assertEqual(args[0][1], url_prefix + 'containers/create') self.assertEqual(json.loads(args[1]['data'])['Env'], expected) + @requires_api_version('1.25') + def test_create_container_with_host_config_cpus(self): + self.client.create_container( + 'busybox', 'ls', host_config=self.client.create_host_config( + cpu_count=1, + cpu_percent=20, + cpus=10 + ) + ) + + args = fake_request.call_args + self.assertEqual(args[0][1], + url_prefix + 'containers/create') + + self.assertEqual(json.loads(args[1]['data']), + json.loads(''' + {"Tty": false, "Image": "busybox", + "Cmd": ["ls"], "AttachStdin": false, + "AttachStderr": true, + "AttachStdout": true, "OpenStdin": false, + "StdinOnce": false, + "NetworkDisabled": false, + "HostConfig": { + "CpuCount": 1, + "CpuPercent": 20, + "NanoCpus": 10000000000, + "NetworkMode": "default" + }}''')) + self.assertEqual(args[1]['headers'], {'Content-Type': 'application/json'}) + class ContainerTest(BaseAPIClientTest): def test_list_containers(self): diff --git a/tests/unit/dockertypes_test.py b/tests/unit/dockertypes_test.py index cb1d90ca2d..c3ee2e263e 100644 --- a/tests/unit/dockertypes_test.py +++ b/tests/unit/dockertypes_test.py @@ -172,6 +172,39 @@ def test_create_host_config_with_volume_driver(self): config = create_host_config(version='1.21', volume_driver='local') assert config.get('VolumeDriver') == 'local' + + def test_create_host_config_invalid_cpu_count_types(self): + with pytest.raises(TypeError): + create_host_config(version='1.25', cpu_count='1') + + def test_create_host_config_with_cpu_count(self): + config = create_host_config(version='1.25', cpu_count=2) + self.assertEqual(config.get('CpuCount'), 2) + self.assertRaises( + InvalidVersion, lambda: create_host_config( + version='1.24', cpu_count=1)) + + def test_create_host_config_invalid_cpu_percent_types(self): + with pytest.raises(TypeError): + create_host_config(version='1.25', cpu_percent='1') + + def test_create_host_config_with_cpu_percent(self): + config = create_host_config(version='1.25', cpu_percent=15) + self.assertEqual(config.get('CpuPercent'), 15) + self.assertRaises( + InvalidVersion, lambda: create_host_config( + version='1.24', cpu_percent=10)) + + def test_create_host_config_invalid_cpus_types(self): + with pytest.raises(TypeError): + create_host_config(version='1.25', cpus='0') + + def test_create_host_config_with_cpus(self): + config = create_host_config(version='1.25', cpus=100) + self.assertEqual(config.get('NanoCpus'), 100000000000) + self.assertRaises( + InvalidVersion, lambda: create_host_config( + version='1.24', cpus=1)) class ContainerConfigTest(unittest.TestCase): From c2f83d558e74c4e4d2faca8bcecf35eb9f9f8d96 Mon Sep 17 00:00:00 2001 From: Alexey Rokhin Date: Fri, 28 Apr 2017 14:49:40 +0300 Subject: [PATCH 2/2] cpus renamed to nano_cpus. Type and scale of nano_cpus are changed. Comments for new parameters are changed. Signed-off-by: Alexey Rokhin --- docker/models/containers.py | 9 +++++---- docker/types/containers.py | 12 ++++++------ tests/unit/api_container_test.py | 8 +++++--- tests/unit/dockertypes_test.py | 14 +++++++------- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/docker/models/containers.py b/docker/models/containers.py index f623f6b818..c2c0c404bb 100644 --- a/docker/models/containers.py +++ b/docker/models/containers.py @@ -456,13 +456,13 @@ def run(self, image, command=None, stdout=True, stderr=False, cap_add (list of str): Add kernel capabilities. For example, ``["SYS_ADMIN", "MKNOD"]``. cap_drop (list of str): Drop kernel capabilities. - cpu_count (int): CPU count (Windows only). - cpu_percent (int): CPU percent (Windows only). + cpu_count (int): Number of usable CPUs (Windows only). + cpu_percent (int): Usable percentage of the available CPUs + (Windows only). cpu_period (int): The length of a CPU period in microseconds. cpu_quota (int): Microseconds of CPU time that the container can get in a CPU period. cpu_shares (int): CPU shares (relative weight). - cpus (float): Number of CPUs. cpuset_cpus (str): CPUs in which to allow execution (``0-3``, ``0,1``). detach (bool): Run container in the background and return a @@ -526,6 +526,7 @@ def run(self, image, command=None, stdout=True, stderr=False, networks (:py:class:`list`): A list of network names to connect this container to. name (str): The name for this container. + nano_cpus (int): CPU quota in units of 10-9 CPUs. network_disabled (bool): Disable networking. network_mode (str): One of: @@ -809,7 +810,6 @@ def prune(self, filters=None): 'cpu_period', 'cpu_quota', 'cpu_shares', - 'cpus', 'cpuset_cpus', 'device_read_bps', 'device_read_iops', @@ -833,6 +833,7 @@ def prune(self, filters=None): 'mem_reservation', 'mem_swappiness', 'memswap_limit', + 'nano_cpus', 'network_mode', 'oom_kill_disable', 'oom_score_adj', diff --git a/docker/types/containers.py b/docker/types/containers.py index 8c5e5ef7b5..9f1a04d104 100644 --- a/docker/types/containers.py +++ b/docker/types/containers.py @@ -119,7 +119,7 @@ def __init__(self, version, binds=None, port_bindings=None, cpuset_cpus=None, userns_mode=None, pids_limit=None, isolation=None, auto_remove=False, storage_opt=None, init=None, init_path=None, volume_driver=None, - cpu_count=None, cpu_percent=None, cpus=None): + cpu_count=None, cpu_percent=None, nano_cpus=None): if mem_limit is not None: self['Memory'] = parse_bytes(mem_limit) @@ -450,13 +450,13 @@ def __init__(self, version, binds=None, port_bindings=None, self['CpuPercent'] = cpu_percent - if cpus: - if not isinstance(cpus, (float, int)): - raise host_config_type_error('cpus', cpus, 'float') + if nano_cpus: + if not isinstance(nano_cpus, int): + raise host_config_type_error('nano_cpus', nano_cpus, 'int') if version_lt(version, '1.25'): - raise host_config_version_error('cpus', '1.25') + raise host_config_version_error('nano_cpus', '1.25') - self['NanoCpus'] = int(1000000000 * cpus) + self['NanoCpus'] = nano_cpus def host_config_type_error(param, param_value, expected): diff --git a/tests/unit/api_container_test.py b/tests/unit/api_container_test.py index 30287110d4..901934ee7a 100644 --- a/tests/unit/api_container_test.py +++ b/tests/unit/api_container_test.py @@ -1158,7 +1158,7 @@ def test_create_container_with_host_config_cpus(self): 'busybox', 'ls', host_config=self.client.create_host_config( cpu_count=1, cpu_percent=20, - cpus=10 + nano_cpus=1000 ) ) @@ -1177,10 +1177,12 @@ def test_create_container_with_host_config_cpus(self): "HostConfig": { "CpuCount": 1, "CpuPercent": 20, - "NanoCpus": 10000000000, + "NanoCpus": 1000, "NetworkMode": "default" }}''')) - self.assertEqual(args[1]['headers'], {'Content-Type': 'application/json'}) + self.assertEqual( + args[1]['headers'], {'Content-Type': 'application/json'} + ) class ContainerTest(BaseAPIClientTest): diff --git a/tests/unit/dockertypes_test.py b/tests/unit/dockertypes_test.py index c3ee2e263e..63383677d7 100644 --- a/tests/unit/dockertypes_test.py +++ b/tests/unit/dockertypes_test.py @@ -172,7 +172,7 @@ def test_create_host_config_with_volume_driver(self): config = create_host_config(version='1.21', volume_driver='local') assert config.get('VolumeDriver') == 'local' - + def test_create_host_config_invalid_cpu_count_types(self): with pytest.raises(TypeError): create_host_config(version='1.25', cpu_count='1') @@ -195,16 +195,16 @@ def test_create_host_config_with_cpu_percent(self): InvalidVersion, lambda: create_host_config( version='1.24', cpu_percent=10)) - def test_create_host_config_invalid_cpus_types(self): + def test_create_host_config_invalid_nano_cpus_types(self): with pytest.raises(TypeError): - create_host_config(version='1.25', cpus='0') + create_host_config(version='1.25', nano_cpus='0') - def test_create_host_config_with_cpus(self): - config = create_host_config(version='1.25', cpus=100) - self.assertEqual(config.get('NanoCpus'), 100000000000) + def test_create_host_config_with_nano_cpus(self): + config = create_host_config(version='1.25', nano_cpus=1000) + self.assertEqual(config.get('NanoCpus'), 1000) self.assertRaises( InvalidVersion, lambda: create_host_config( - version='1.24', cpus=1)) + version='1.24', nano_cpus=1)) class ContainerConfigTest(unittest.TestCase):