@@ -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