diff options
author | Joffrey F <joffrey@docker.com> | 2016-08-23 15:12:48 -0700 |
---|---|---|
committer | Joffrey F <joffrey@docker.com> | 2016-08-23 15:12:48 -0700 |
commit | 3709c709f3d0d6083d2ff189c9780b1b30c192fa (patch) | |
tree | 0f66272fd97e0c3c9ce6cb7f0646f7c2f0cc918d | |
parent | abd461150a7b66314ab88c173475680fee2ec7eb (diff) | |
parent | 0e4314a872a1dbf0dd81408e6628bcac9de3e14a (diff) | |
download | docker-py-3709c709f3d0d6083d2ff189c9780b1b30c192fa.tar.gz |
Merge branch 'joshpurvis-jp-cpushares'
-rw-r--r-- | docker/utils/utils.py | 27 | ||||
-rw-r--r-- | docs/api.md | 1 | ||||
-rw-r--r-- | docs/hostconfig.md | 2 | ||||
-rw-r--r-- | tests/integration/container_test.py | 31 | ||||
-rw-r--r-- | tests/unit/container_test.py | 54 |
5 files changed, 111 insertions, 4 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py index b8e947c..0487cde 100644 --- a/docker/utils/utils.py +++ b/docker/utils/utils.py @@ -620,7 +620,8 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None, device_write_bps=None, device_read_iops=None, device_write_iops=None, oom_kill_disable=False, shm_size=None, sysctls=None, version=None, tmpfs=None, - oom_score_adj=None, dns_opt=None): + oom_score_adj=None, dns_opt=None, cpu_shares=None, + cpuset_cpus=None): host_config = {} @@ -809,6 +810,21 @@ def create_host_config(binds=None, port_bindings=None, lxc_conf=None, host_config['CpuPeriod'] = cpu_period + if cpu_shares: + if version_lt(version, '1.18'): + raise host_config_version_error('cpu_shares', '1.18') + + if not isinstance(cpu_shares, int): + raise host_config_type_error('cpu_shares', cpu_shares, 'int') + + host_config['CpuShares'] = cpu_shares + + if cpuset_cpus: + if version_lt(version, '1.18'): + raise host_config_version_error('cpuset_cpus', '1.18') + + host_config['CpuSetCpus'] = cpuset_cpus + if blkio_weight: if not isinstance(blkio_weight, int): raise host_config_type_error('blkio_weight', blkio_weight, 'int') @@ -981,6 +997,14 @@ def create_container_config( 'labels were only introduced in API version 1.18' ) + if cpuset is not None or cpu_shares is not None: + if version_gte(version, '1.18'): + warnings.warn( + 'The cpuset_cpus and cpu_shares options have been moved to ' + 'host_config in API version 1.18, and will be removed', + DeprecationWarning + ) + if stop_signal is not None and compare_version('1.21', version) < 0: raise errors.InvalidVersion( 'stop_signal was only introduced in API version 1.21' @@ -1010,6 +1034,7 @@ def create_container_config( if mem_limit is not None: mem_limit = parse_bytes(mem_limit) + if memswap_limit is not None: memswap_limit = parse_bytes(memswap_limit) diff --git a/docs/api.md b/docs/api.md index 0c2b15e..895d7d4 100644 --- a/docs/api.md +++ b/docs/api.md @@ -246,7 +246,6 @@ from. Optionally a single string joining container id's with commas * network_disabled (bool): Disable networking * name (str): A name for the container * entrypoint (str or list): An entrypoint -* cpu_shares (int): CPU shares (relative weight) * working_dir (str): Path to the working directory * domainname (str or list): Set custom DNS search domains * memswap_limit (int): diff --git a/docs/hostconfig.md b/docs/hostconfig.md index 01c4625..229a28c 100644 --- a/docs/hostconfig.md +++ b/docs/hostconfig.md @@ -109,6 +109,8 @@ for example: * cpu_group (int): The length of a CPU period in microseconds. * cpu_period (int): Microseconds of CPU time that the container can get in a CPU period. +* cpu_shares (int): CPU shares (relative weight) +* cpuset_cpus (str): CPUs in which to allow execution (0-3, 0,1) * blkio_weight: Block IO weight (relative weight), accepts a weight value between 10 and 1000. * blkio_weight_device: Block IO weight (relative device weight) in the form of: `[{"Path": "device_path", "Weight": weight}]` diff --git a/tests/integration/container_test.py b/tests/integration/container_test.py index 334c81d..27d3046 100644 --- a/tests/integration/container_test.py +++ b/tests/integration/container_test.py @@ -1096,11 +1096,38 @@ class ContainerUpdateTest(helpers.BaseTestCase): container = self.client.create_container( BUSYBOX, 'top', host_config=self.client.create_host_config( mem_limit=old_mem_limit - ), cpu_shares=102 + ) ) self.tmp_containers.append(container) self.client.start(container) self.client.update_container(container, mem_limit=new_mem_limit) inspect_data = self.client.inspect_container(container) self.assertEqual(inspect_data['HostConfig']['Memory'], new_mem_limit) - self.assertEqual(inspect_data['HostConfig']['CpuShares'], 102) + + +class ContainerCPUTest(helpers.BaseTestCase): + @requires_api_version('1.18') + def test_container_cpu_shares(self): + cpu_shares = 512 + container = self.client.create_container( + BUSYBOX, 'ls', host_config=self.client.create_host_config( + cpu_shares=cpu_shares + ) + ) + self.tmp_containers.append(container) + self.client.start(container) + inspect_data = self.client.inspect_container(container) + self.assertEqual(inspect_data['HostConfig']['CpuShares'], 512) + + @requires_api_version('1.18') + def test_container_cpuset(self): + cpuset_cpus = "0,1" + container = self.client.create_container( + BUSYBOX, 'ls', host_config=self.client.create_host_config( + cpuset_cpus=cpuset_cpus + ) + ) + self.tmp_containers.append(container) + self.client.start(container) + inspect_data = self.client.inspect_container(container) + self.assertEqual(inspect_data['HostConfig']['CpusetCpus'], cpuset_cpus) diff --git a/tests/unit/container_test.py b/tests/unit/container_test.py index 4c94c84..c480462 100644 --- a/tests/unit/container_test.py +++ b/tests/unit/container_test.py @@ -286,6 +286,33 @@ class CreateContainerTest(DockerClientTest): self.assertEqual(args[1]['headers'], {'Content-Type': 'application/json'}) + @requires_api_version('1.18') + def test_create_container_with_host_config_cpu_shares(self): + self.client.create_container( + 'busybox', 'ls', host_config=self.client.create_host_config( + cpu_shares=512 + ) + ) + + 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": { + "CpuShares": 512, + "NetworkMode": "default" + }}''')) + self.assertEqual(args[1]['headers'], + {'Content-Type': 'application/json'}) + def test_create_container_with_cpuset(self): self.client.create_container('busybox', 'ls', cpuset='0,1') @@ -306,6 +333,33 @@ class CreateContainerTest(DockerClientTest): self.assertEqual(args[1]['headers'], {'Content-Type': 'application/json'}) + @requires_api_version('1.18') + def test_create_container_with_host_config_cpuset(self): + self.client.create_container( + 'busybox', 'ls', host_config=self.client.create_host_config( + cpuset_cpus='0,1' + ) + ) + + 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": { + "CpuSetCpus": "0,1", + "NetworkMode": "default" + }}''')) + self.assertEqual(args[1]['headers'], + {'Content-Type': 'application/json'}) + def test_create_container_with_cgroup_parent(self): self.client.create_container( 'busybox', 'ls', host_config=self.client.create_host_config( |