Skip to content

Commit 6e3514c

Browse files
authored
Merge pull request #3172 from DataDog/haissam/fix-image_tag_extractor
[docker] fix image tag extraction
2 parents efd90f0 + 50391a4 commit 6e3514c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

tests/checks/integration/test_docker_daemon.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,10 +614,16 @@ def test_image_tags_extraction(self):
614614
({'RepoTags': ['localhost/redis:latest']}, [['localhost/redis'], ['latest']]),
615615
({'RepoTags': ['localhost:5000/redis:latest']}, [['localhost:5000/redis'], ['latest']]),
616616
({'RepoTags': ['localhost:5000/redis:latest', 'localhost:5000/redis:v1.1']}, [['localhost:5000/redis'], ['latest', 'v1.1']]),
617+
({'RepoTags': [], 'RepoDigests': [u'datadog/docker-dd-agent@sha256:47a59c2ea4f6d9555884aacc608b303f18bde113b1a3a6743844bfc364d73b44']},
618+
[['datadog/docker-dd-agent'], None]),
617619
]
618620
for entity in entities:
619621
self.assertEqual(sorted(DockerUtil.image_tag_extractor(entity[0], 0)), sorted(entity[1][0]))
620-
self.assertEqual(sorted(DockerUtil.image_tag_extractor(entity[0], 1)), sorted(entity[1][1]))
622+
tags = DockerUtil.image_tag_extractor(entity[0], 1)
623+
if isinstance(entity[1][1], list):
624+
self.assertEqual(sorted(tags), sorted(entity[1][1]))
625+
else:
626+
self.assertEqual(tags, entity[1][1])
621627

622628
def test_container_name_extraction(self):
623629
containers = [

utils/dockerutil.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def image_tag_extractor(cls, entity, key):
382382
# the split will be like [repo_url, repo_port/image_name, image_tag]. Let's avoid that
383383
split = [':'.join(split[:-1]), split[-1]]
384384
return [split[key]]
385-
if "RepoTags" in entity:
385+
if entity.get('RepoTags'):
386386
splits = [el.split(":") for el in entity["RepoTags"]]
387387
tags = set()
388388
for split in splits:
@@ -392,6 +392,14 @@ def image_tag_extractor(cls, entity, key):
392392
tags.add(split[key])
393393
if len(tags) > 0:
394394
return list(tags)
395+
elif entity.get('RepoDigests'):
396+
# the human-readable tag is not mentioned in RepoDigests, only the image name
397+
if key != 0:
398+
return None
399+
split = entity['RepoDigests'][0].split('@')
400+
if len(split) > 1:
401+
return [split[key]]
402+
395403
return None
396404

397405
@classmethod

0 commit comments

Comments
 (0)