From 33848bc11c66c0e2142bc9a38234cfc9e29acbc7 Mon Sep 17 00:00:00 2001 From: Jaime Fullaondo Date: Mon, 17 Jul 2017 17:20:54 +0200 Subject: [PATCH] [auto-discovery] tags could be a dict of KV pairs (#3442) * [auto-discovery] tpl_tags can also be dicts apparently - parse them. * [test][auto-discovery] add test to templates. --- tests/core/test_service_discovery.py | 8 ++++++++ utils/service_discovery/sd_docker_backend.py | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/core/test_service_discovery.py b/tests/core/test_service_discovery.py index cc4c1b9bef..401c884008 100644 --- a/tests/core/test_service_discovery.py +++ b/tests/core/test_service_discovery.py @@ -434,6 +434,13 @@ def test_fill_tpl(self, *args): ['host', 'port_1'], ['foo', 'bar:baz']), ({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']}, {'host': '127.0.0.1', 'port_1': '42'}) + ), + ( + ({'NetworkSettings': {'IPAddress': '127.0.0.1', 'Ports': {'42/tcp': None, '22/tcp': None}}}, + {'host': '%%host%%', 'port': '%%port_1%%', 'tags': {'env': 'test'}}, + ['host', 'port_1'], ['foo', 'bar:baz']), + ({'host': '%%host%%', 'port': '%%port_1%%', 'tags': ['env:test', 'foo', 'bar:baz']}, + {'host': '127.0.0.1', 'port_1': '42'}) ) ] @@ -529,6 +536,7 @@ def test_fill_tpl(self, *args): for key in instance_tpl.keys(): if isinstance(instance_tpl[key], list): self.assertEquals(len(instance_tpl[key]), len(co[1][0].get(key))) + for elem in instance_tpl[key]: self.assertTrue(elem in co[1][0].get(key)) else: diff --git a/utils/service_discovery/sd_docker_backend.py b/utils/service_discovery/sd_docker_backend.py index e1a0460299..7093678c5c 100644 --- a/utils/service_discovery/sd_docker_backend.py +++ b/utils/service_discovery/sd_docker_backend.py @@ -491,7 +491,11 @@ def _fill_tpl(self, state, c_id, instance_tpl, variables, tags=None): # add default tags to the instance if tags: tpl_tags = instance_tpl.get('tags', []) - tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags] + if isinstance(tpl_tags, dict): + for key, val in tpl_tags.iteritems(): + tags.append("{}:{}".format(key, val)) + else: + tags += tpl_tags if isinstance(tpl_tags, list) else [tpl_tags] instance_tpl['tags'] = list(set(tags)) for var in variables: