Skip to content

Commit d86da7e

Browse files
committed
Change the annotation style
1 parent 5d4b38e commit d86da7e

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

tests/core/test_service_discovery.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _get_container_inspect(c_id):
4040
return None
4141

4242

43-
def _get_conf_tpls(image_name, kube_annotations=None, kube_pod_name=None):
43+
def _get_conf_tpls(image_name, kube_annotations=None, kube_pod_name=None, kube_container_name=None):
4444
"""Return a mocked configuration template from self.mock_templates."""
4545
return [(x, image_name + ':0', y) for x, y in
4646
copy.deepcopy(TestServiceDiscovery.mock_templates.get(image_name)[0])]
@@ -545,10 +545,11 @@ def test_get_check_tpls_kube(self, mock_client_read):
545545
config_store.get_check_tpls(
546546
'k8s-' + image, auto_conf=True,
547547
kube_pod_name=image,
548+
kube_container_name='foo',
548549
kube_annotations=dict(zip(
549-
['com.datadoghq.sd/check_names',
550-
'com.datadoghq.sd/init_configs',
551-
'com.datadoghq.sd/instances'],
550+
['sd.datadoghq.com/foo/check_names',
551+
'sd.datadoghq.com/foo/init_configs',
552+
'sd.datadoghq.com/foo/instances'],
552553
self.mock_tpls[image][0]))))
553554

554555
def test_get_config_id(self):

utils/service_discovery/abstract_config_store.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
INSTANCES = 'instances'
2828
KUBE_ANNOTATIONS = 'kube_annotations'
2929
KUBE_POD_NAME = 'kube_pod_name'
30-
KUBE_ANNOTATION_PREFIX = 'com.datadoghq.sd/'
30+
KUBE_CONTAINER_NAME = 'kube_container_name'
31+
KUBE_ANNOTATION_PREFIX = 'sd.datadoghq.com'
3132

3233

3334
class KeyNotFound(Exception):
@@ -99,11 +100,12 @@ def _populate_identifier_to_checks(self):
99100

100101
return identifier_to_checks
101102

102-
def _get_kube_config(self, identifier, kube_annotations):
103+
def _get_kube_config(self, identifier, kube_annotations, kube_container_name):
103104
try:
104-
check_names = json.loads(kube_annotations[KUBE_ANNOTATION_PREFIX + CHECK_NAMES])
105-
init_config_tpls = json.loads(kube_annotations[KUBE_ANNOTATION_PREFIX + INIT_CONFIGS])
106-
instance_tpls = json.loads(kube_annotations[KUBE_ANNOTATION_PREFIX + INSTANCES])
105+
prefix = '{}/{}/'.format(KUBE_ANNOTATION_PREFIX, kube_container_name)
106+
check_names = json.loads(kube_annotations[prefix + CHECK_NAMES])
107+
init_config_tpls = json.loads(kube_annotations[prefix + INIT_CONFIGS])
108+
instance_tpls = json.loads(kube_annotations[prefix + INSTANCES])
107109
return [check_names, init_config_tpls, instance_tpls]
108110
except KeyError:
109111
return None
@@ -132,9 +134,10 @@ def _get_auto_config(self, image_name):
132134

133135
def get_checks_to_refresh(self, identifier, **kwargs):
134136
to_check = set(self.identifier_to_checks[identifier])
135-
kube_annotations = kwargs.get('kube_annotations')
137+
kube_annotations = kwargs.get(KUBE_ANNOTATIONS)
138+
kube_container_name = kwargs.get(KUBE_CONTAINER_NAME)
136139
if kube_annotations:
137-
kube_config = self._get_kube_config(identifier, kube_annotations)
140+
kube_config = self._get_kube_config(identifier, kube_annotations, kube_container_name)
138141
if kube_config is not None:
139142
to_check.update(kube_config[0])
140143

@@ -149,8 +152,9 @@ def get_check_tpls(self, identifier, **kwargs):
149152
# annotations for configs before falling back to autoconf.
150153
kube_annotations = kwargs.get(KUBE_ANNOTATIONS)
151154
kube_pod_name = kwargs.get(KUBE_POD_NAME)
155+
kube_container_name = kwargs.get(KUBE_CONTAINER_NAME)
152156
if kube_annotations:
153-
kube_config = self._get_kube_config(identifier, kube_annotations)
157+
kube_config = self._get_kube_config(identifier, kube_annotations, kube_container_name)
154158
if kube_config is not None:
155159
check_names, init_config_tpls, instance_tpls = kube_config
156160
source = CONFIG_FROM_KUBE

utils/service_discovery/sd_docker_backend.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ def _get_checks_from_inspect(self, inspect):
6767
identifier = inspect.get('Config', {}).get('Labels', {}).get(DATADOG_ID) or \
6868
inspect.get('Config', {}).get('Image')
6969

70+
platform_kwargs = {}
7071
if Platform.is_k8s():
7172
kube_metadata = self._get_kube_config(inspect.get('Id'), 'metadata') or {}
72-
annotations = kube_metadata.get('annotations')
73-
else:
74-
annotations = {}
73+
platform_kwargs = {
74+
'kube_annotations': kube_metadata.get('annotations'),
75+
'kube_container_name': self._get_kube_container_name(inspect.get('Id')),
76+
}
7577

76-
return self.config_store.get_checks_to_refresh(identifier, kube_annotations=annotations)
78+
return self.config_store.get_checks_to_refresh(identifier, **platform_kwargs)
7779

7880
def _get_host_address(self, c_inspect, tpl_var):
7981
"""Extract the container IP from a docker inspect object, or the kubelet API."""
@@ -228,6 +230,14 @@ def _get_additional_tags(self, container_inspect, *args):
228230
tags.append('pod_name:%s' % pod_metadata.get('name'))
229231
return tags
230232

233+
def _get_kube_container_name(self, c_id):
234+
pods = self.kubeutil.retrieve_pods_list().get('items', [])
235+
for pod in pods:
236+
c_statuses = pod.get('status', {}).get('containerStatuses', [])
237+
for status in c_statuses:
238+
if c_id == status.get('containerID', '').split('//')[-1]:
239+
return status.get('name')
240+
231241
def _get_kube_config(self, c_id, key):
232242
"""Get a part of a pod config from the kubernetes API"""
233243
pods = self.kubeutil.retrieve_pods_list().get('items', [])
@@ -278,14 +288,15 @@ def get_config_id(self, image, labels):
278288
def _get_check_configs(self, c_id, identifier):
279289
"""Retrieve configuration templates and fill them with data pulled from docker and tags."""
280290
inspect = self.docker_client.inspect_container(c_id)
291+
platform_kwargs = {}
281292
if Platform.is_k8s():
282293
kube_metadata = self._get_kube_config(inspect.get('Id'), 'metadata') or {}
283-
annotations = kube_metadata.get('annotations')
284-
pod_name = kube_metadata.get('name')
285-
else:
286-
annotations = {}
287-
pod_name = None
288-
config_templates = self._get_config_templates(identifier, kube_annotations=annotations, kube_pod_name=pod_name)
294+
platform_kwargs = {
295+
'kube_pod_name': kube_metadata.get('name'),
296+
'kube_container_name': self._get_kube_container_name(inspect.get('Id')),
297+
'kube_annotations': kube_metadata.get('annotations'),
298+
}
299+
config_templates = self._get_config_templates(identifier, **platform_kwargs)
289300
if not config_templates:
290301
log.debug('No config template for container %s with identifier %s. '
291302
'It will be left unconfigured.' % (c_id[:12], identifier))
@@ -307,7 +318,7 @@ def _get_check_configs(self, c_id, identifier):
307318

308319
return check_configs
309320

310-
def _get_config_templates(self, identifier, kube_annotations=None, kube_pod_name=None):
321+
def _get_config_templates(self, identifier, **platform_kwargs):
311322
"""Extract config templates for an identifier from a K/V store and returns it as a dict object."""
312323
config_backend = self.agentConfig.get('sd_config_backend')
313324
templates = []
@@ -318,8 +329,7 @@ def _get_config_templates(self, identifier, kube_annotations=None, kube_pod_name
318329
auto_conf = False
319330

320331
# format [(source, ('ident', {init_tpl}, {instance_tpl}))]
321-
raw_tpls = self.config_store.get_check_tpls(
322-
identifier, auto_conf=auto_conf, kube_annotations=kube_annotations, kube_pod_name=kube_pod_name)
332+
raw_tpls = self.config_store.get_check_tpls(identifier, auto_conf=auto_conf, **platform_kwargs)
323333
for tpl in raw_tpls:
324334
# each template can come from either auto configuration or user-supplied templates
325335
try:

0 commit comments

Comments
 (0)