Skip to content

Commit b48fb79

Browse files
xvelloolivielpeau
authored andcommitted
rework AD image name extractor to support new swarm format
swarm can use org/image:tag@sha256:xxxxxxxx format, that throws off the parser. reworking it to handle all known formats and unit test it
1 parent 9f162fb commit b48fb79

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

tests/core/test_service_discovery.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ class TestServiceDiscovery(unittest.TestCase):
174174
])),
175175
}
176176

177+
image_formats = {
178+
# Don't crash on empty string or None
179+
'': '',
180+
None: '',
181+
# Shortest possibility
182+
'alpine': 'alpine',
183+
# Historical docker format
184+
'nginx:latest': 'nginx',
185+
# Org prefix to be removed
186+
'datadog/docker-dd-agent:latest-jmx': 'docker-dd-agent',
187+
# Sha-pinning used by many orchestrators
188+
'redis@sha256:5bef08742407efd622d243692b79ba0055383bbce12900324f75e56f589aedb0': 'redis',
189+
# Quirky pinning used by swarm
190+
'org/redis:latest@sha256:5bef08742407efd622d243692b79ba0055383bbce12900324f75e56f589aedb0': 'redis',
191+
# Custom registry, simple form
192+
'myregistry.local:5000/testing/test-image:version': 'test-image',
193+
# Custom registry, most insane form possible
194+
'myregistry.local:5000/testing/test-image:version@sha256:5bef08742407efd622d243692b79ba0055383bbce12900324f75e56f589aedb0': 'test-image',
195+
}
196+
177197
def setUp(self):
178198
self.etcd_agentConfig = {
179199
'service_discovery': True,
@@ -328,6 +348,16 @@ def test_get_config_templates(self, *args):
328348
self.assertEquals(sd_backend._get_config_templates(image), None)
329349
clear_singletons(agentConfig)
330350

351+
@mock.patch('config.get_auto_confd_path', return_value=os.path.join(
352+
os.path.dirname(__file__), 'fixtures/auto_conf/'))
353+
@mock.patch('utils.dockerutil.DockerUtil.client', return_value=None)
354+
#@mock.patch.object(AbstractConfigStore, 'get_check_tpls', side_effect=_get_check_tpls)
355+
def test_get_image_ident(self, *args):
356+
sd_backend = get_sd_backend(agentConfig=self.auto_conf_agentConfig)
357+
# normal cases
358+
for image, ident in self.image_formats.iteritems():
359+
self.assertEquals(ident, sd_backend.config_store._get_image_ident(image))
360+
331361
@mock.patch('config.get_auto_confd_path', return_value=os.path.join(
332362
os.path.dirname(__file__), 'fixtures/auto_conf/'))
333363
def test_render_template(self, mock_get_auto_confd_path):

utils/service_discovery/abstract_config_store.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,25 @@ def read_config_from_store(self, identifier):
366366

367367
return res
368368

369-
def _get_image_ident(self, ident):
369+
def _get_image_ident(self, image_name):
370370
"""Extract an identifier from the image"""
371+
# See image_formats in test_service_discovery.py for supported formats
372+
371373
# handle exceptionnal empty ident case (docker bug)
372-
if not ident:
374+
if not image_name:
373375
return ""
374-
# handle the 'redis@sha256:...' format
375-
if '@' in ident:
376-
return ident.split('@')[0].split('/')[-1]
377-
# if a custom image store is used there can be a port which adds a colon
378-
elif ident.count(':') > 1:
379-
return ident.split(':')[1].split('/')[-1]
380-
# otherwise we just strip the tag and keep the image name
381-
else:
382-
return ident.split(':')[0].split('/')[-1]
376+
ident = image_name
377+
# remove the @sha256: suffix if present
378+
if '@sha' in ident:
379+
ident = ident.split('@sha')[0]
380+
# remove image org / store prefix, we keep the last part after '/''
381+
if '/' in ident:
382+
ident = ident.split('/')[-1]
383+
# remove the image tag after :
384+
if ':' in ident:
385+
ident = ident.split(':')[0]
386+
387+
return ident
383388

384389
def crawl_config_template(self):
385390
"""Return whether or not configuration templates have changed since the previous crawl"""

0 commit comments

Comments
 (0)