-
Notifications
You must be signed in to change notification settings - Fork 813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AD] Add docker labels as tags in AD #3564
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,16 +72,72 @@ def test_image_name_from_image_repodigests(self): | |
self.assertEqual('alpine', du.image_name_extractor(co)) | ||
|
||
def test_extract_container_tags(self): | ||
test_data = [ | ||
no_label_test_data = [ | ||
# Nominal case | ||
[{'Image': 'redis:3.2'}, ['docker_image:redis:3.2', 'image_name:redis', 'image_tag:3.2']], | ||
# No tag | ||
[{'Image': 'redis'}, ['docker_image:redis', 'image_name:redis']], | ||
# No image | ||
[{}, []], | ||
] | ||
for test in test_data: | ||
self.assertEqual(test[1], DockerUtil().extract_container_tags(test[0])) | ||
labeled_test_data = [ | ||
# No labels | ||
( | ||
# ctr inspect | ||
{ | ||
'Image': 'redis:3.2', | ||
'Config': { | ||
'Labels': {} | ||
} | ||
}, | ||
# labels as tags | ||
[], | ||
# expected result | ||
['docker_image:redis:3.2', 'image_name:redis', 'image_tag:3.2'] | ||
), | ||
# Un-monitored labels | ||
( | ||
{ | ||
'Image': 'redis:3.2', | ||
'Config': { | ||
'Labels': { | ||
'foo': 'bar' | ||
} | ||
} | ||
}, | ||
[], | ||
['docker_image:redis:3.2', 'image_name:redis', 'image_tag:3.2'] | ||
), | ||
# no labels, with labels_as_tags list | ||
( | ||
{ | ||
'Image': 'redis:3.2', | ||
'Config': { | ||
'Labels': {} | ||
} | ||
}, | ||
['foo'], | ||
['docker_image:redis:3.2', 'image_name:redis', 'image_tag:3.2'] | ||
), | ||
# labels and labels_as_tags list | ||
( | ||
{ | ||
'Image': 'redis:3.2', | ||
'Config': { | ||
'Labels': {'foo': 'bar', 'f00': 'b4r'} | ||
} | ||
}, | ||
['foo'], | ||
['docker_image:redis:3.2', 'image_name:redis', 'image_tag:3.2', 'foo:bar'] | ||
), | ||
|
||
] | ||
for test in no_label_test_data: | ||
self.assertEqual(test[1], DockerUtil().extract_container_tags(test[0], [])) | ||
|
||
for test in labeled_test_data: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
self.assertEqual(test[2], DockerUtil().extract_container_tags(test[0], test[1])) | ||
|
||
|
||
def test_docker_host_tags_ok(self): | ||
mock_version = mock.MagicMock(name='version', return_value={'Version': '1.13.1'}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -513,23 +513,27 @@ def find_cgroup_filename_pattern(cls, mountpoints, container_id): | |
|
||
raise MountException("Cannot find Docker cgroup directory. Be sure your system is supported.") | ||
|
||
def extract_container_tags(self, co): | ||
def extract_container_tags(self, co, labels_as_tags): | ||
""" | ||
Retrives docker_image, image_name and image_tag tags as a list for a | ||
container. If the container or image is invalid, will gracefully | ||
return an empty list | ||
return an empty list. | ||
Also extract container labels on demand. | ||
""" | ||
tags = [] | ||
docker_image = self.image_name_extractor(co) | ||
image_name_array = self.image_tag_extractor(co, 0) | ||
image_tag_array = self.image_tag_extractor(co, 1) | ||
label_tags = self.label_extractor(co, labels_as_tags) | ||
|
||
if docker_image: | ||
tags.append('docker_image:%s' % docker_image) | ||
if image_name_array and len(image_name_array) > 0: | ||
tags.append('image_name:%s' % image_name_array[0]) | ||
if image_tag_array and len(image_tag_array) > 0: | ||
tags.append('image_tag:%s' % image_tag_array[0]) | ||
if label_tags: | ||
tags += label_tags | ||
return tags | ||
|
||
def image_tag_extractor(self, entity, key): | ||
|
@@ -603,6 +607,15 @@ def image_name_resolver(self, image): | |
else: | ||
return image | ||
|
||
def label_extractor(self, ctr, lbl_to_tags): | ||
"""Returns a list of tags based on a container and a label name list""" | ||
tags = [] | ||
labels = ctr.get('Config', {}).get('Labels', {}) | ||
for lbl_name, lbl_val in labels.iteritems(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be extra-safe, let's add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! |
||
if lbl_name in lbl_to_tags: | ||
tags.append('{}:{}'.format(lbl_name, lbl_val)) | ||
return tags | ||
|
||
@classmethod | ||
def container_name_extractor(cls, co): | ||
names = co.get('Names', []) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a reference to docker_daemon's
collect_labels_as_tags
for docker/kube metrics