From ec252350ae09d4028a960dcf50515e360b26cf72 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Thu, 2 Mar 2017 16:03:35 -0800 Subject: [PATCH] Add mem_reservation option to service config in 2.0 and 2.1 formats Signed-off-by: Joffrey F --- compose/config/config.py | 3 ++- compose/config/config_schema_v2.0.json | 3 ++- compose/config/config_schema_v2.1.json | 3 ++- compose/service.py | 4 +++- tests/integration/service_test.py | 5 +++++ tests/unit/service_test.py | 14 ++++++++++++++ 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/compose/config/config.py b/compose/config/config.py index 003b2e2f4..a6ea43cba 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -74,7 +74,8 @@ DOCKER_CONFIG_KEYS = [ 'labels', 'links', 'mac_address', - 'mem_limit', + 'mem_limit' + 'mem_reservation', 'memswap_limit', 'mem_swappiness', 'net', diff --git a/compose/config/config_schema_v2.0.json b/compose/config/config_schema_v2.0.json index 59c7b30c9..3871dbf26 100644 --- a/compose/config/config_schema_v2.0.json +++ b/compose/config/config_schema_v2.0.json @@ -138,8 +138,9 @@ "mac_address": {"type": "string"}, "mem_limit": {"type": ["number", "string"]}, - "memswap_limit": {"type": ["number", "string"]}, + "mem_reservation": {"type": ["string", "integer"]}, "mem_swappiness": {"type": "integer"}, + "memswap_limit": {"type": ["number", "string"]}, "network_mode": {"type": "string"}, "networks": { diff --git a/compose/config/config_schema_v2.1.json b/compose/config/config_schema_v2.1.json index d1ffff89a..05509ff1a 100644 --- a/compose/config/config_schema_v2.1.json +++ b/compose/config/config_schema_v2.1.json @@ -161,8 +161,9 @@ "mac_address": {"type": "string"}, "mem_limit": {"type": ["number", "string"]}, - "memswap_limit": {"type": ["number", "string"]}, + "mem_reservation": {"type": ["string", "integer"]}, "mem_swappiness": {"type": "integer"}, + "memswap_limit": {"type": ["number", "string"]}, "network_mode": {"type": "string"}, "networks": { diff --git a/compose/service.py b/compose/service.py index b42094e68..3266f4f6c 100644 --- a/compose/service.py +++ b/compose/service.py @@ -62,9 +62,10 @@ DOCKER_START_KEYS = [ 'log_driver', 'log_opt', 'mem_limit', + 'mem_reservation', 'memswap_limit', - 'oom_score_adj', 'mem_swappiness', + 'oom_score_adj', 'pid', 'privileged', 'restart', @@ -760,6 +761,7 @@ class Service(object): cap_add=options.get('cap_add'), cap_drop=options.get('cap_drop'), mem_limit=options.get('mem_limit'), + mem_reservation=options.get('mem_reservation'), memswap_limit=options.get('memswap_limit'), ulimits=build_ulimits(options.get('ulimits')), log_config=log_config, diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index 734da5dfa..082bff93c 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -872,6 +872,11 @@ class ServiceTest(DockerClientTestCase): container = create_and_start_container(service) self.assertEqual(container.get('HostConfig.MemorySwappiness'), 11) + def test_mem_reservation(self): + service = self.create_service('web', mem_reservation='20m') + container = create_and_start_container(service) + assert container.get('HostConfig.MemoryReservation') == 20 * 1024 * 1024 + def test_restart_always_value(self): service = self.create_service('web', restart={'Name': 'always'}) container = create_and_start_container(service) diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 0a66e4f3e..6d2962fb9 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -168,6 +168,20 @@ class ServiceTest(unittest.TestCase): 2000000000 ) + def test_mem_reservation(self): + self.mock_client.create_host_config.return_value = {} + + service = Service( + name='foo', + image='foo', + hostname='name', + client=self.mock_client, + mem_reservation='512m' + ) + service._get_container_create_options({'some': 'overrides'}, 1) + assert self.mock_client.create_host_config.called is True + assert self.mock_client.create_host_config.call_args[1]['mem_reservation'] == '512m' + def test_cgroup_parent(self): self.mock_client.create_host_config.return_value = {}