From 8f2dbd9b12914f3049c31723553efeb5ecd276d3 Mon Sep 17 00:00:00 2001 From: aiordache Date: Tue, 1 Dec 2020 12:40:05 +0100 Subject: [PATCH] Add devices to config hash to trigger container recreate on change * add unit test * update path to compose spec schema in Makefile Signed-off-by: aiordache --- Makefile | 2 +- compose/config/config.py | 1 + compose/service.py | 8 ++++++-- tests/unit/service_test.py | 23 +++++++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f673eace0a..0a7a5c366b 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ ifeq ($(UNAME_S),Darwin) BUILD_SCRIPT = osx endif -COMPOSE_SPEC_SCHEMA_PATH = "compose/config/config_schema_compose_spec.json" +COMPOSE_SPEC_SCHEMA_PATH = "compose/config/compose_spec.json" COMPOSE_SPEC_RAW_URL = "https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json" all: cli diff --git a/compose/config/config.py b/compose/config/config.py index 1b067e7887..2c50827362 100644 --- a/compose/config/config.py +++ b/compose/config/config.py @@ -1166,6 +1166,7 @@ def merge_reservations(base, override): md.merge_scalar('cpus') md.merge_scalar('memory') md.merge_sequence('generic_resources', types.GenericResource.parse) + md.merge_field('devices', merge_unique_objects_lists, default=[]) return dict(md) diff --git a/compose/service.py b/compose/service.py index e00a537cfc..cb0baefc60 100644 --- a/compose/service.py +++ b/compose/service.py @@ -709,7 +709,7 @@ def image_id(): except NoSuchImageError: return None - return { + c = { 'options': self.options, 'image_id': image_id(), 'links': self.get_link_names(), @@ -719,9 +719,13 @@ def image_id(): 'volumes_from': [ (v.source.name, v.mode) for v in self.volumes_from if isinstance(v.source, Service) - ], + ] } + if self.device_requests: + c['devices'] = self.device_requests + return c + def get_dependency_names(self): net_name = self.network_mode.service_name pid_namespace = self.pid_mode.service_name diff --git a/tests/unit/service_test.py b/tests/unit/service_test.py index 72cb1d7f9e..4d49d2f253 100644 --- a/tests/unit/service_test.py +++ b/tests/unit/service_test.py @@ -732,6 +732,29 @@ def test_config_dict_with_network_mode_from_container(self): } assert config_dict == expected + def test_config_dict_with_device_requests(self): + self.mock_client.inspect_image.return_value = {'Id': 'abcd'} + service = Service( + 'foo', + image='example.com/foo', + client=self.mock_client, + network_mode=ServiceNetworkMode(Service('other')), + networks={'default': None}, + device_requests=[{'driver': 'nvidia', 'device_ids': ['0'], 'capabilities': ['gpu']}]) + + config_dict = service.config_dict() + expected = { + 'image_id': 'abcd', + 'options': {'image': 'example.com/foo'}, + 'links': [], + 'net': 'other', + 'secrets': [], + 'networks': {'default': None}, + 'volumes_from': [], + 'devices': [{'driver': 'nvidia', 'device_ids': ['0'], 'capabilities': ['gpu']}], + } + assert config_dict == expected + def test_config_hash_matches_label(self): self.mock_client.inspect_image.return_value = {'Id': 'abcd'} service = Service(