本文整理汇总了Python中utils.kubernetes.KubeUtil.extract_event_tags方法的典型用法代码示例。如果您正苦于以下问题:Python KubeUtil.extract_event_tags方法的具体用法?Python KubeUtil.extract_event_tags怎么用?Python KubeUtil.extract_event_tags使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.kubernetes.KubeUtil
的用法示例。
在下文中一共展示了KubeUtil.extract_event_tags方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Kubernetes
# 需要导入模块: from utils.kubernetes import KubeUtil [as 别名]
# 或者: from utils.kubernetes.KubeUtil import extract_event_tags [as 别名]
#.........这里部分代码省略.........
self._update_pods_metrics(instance, pods_list)
self._update_node(instance)
def _update_node(self, instance):
machine_info = self.kubeutil.retrieve_machine_info()
num_cores = machine_info.get('num_cores', 0)
memory_capacity = machine_info.get('memory_capacity', 0)
tags = instance.get('tags', [])
self.publish_gauge(self, NAMESPACE + '.cpu.capacity', float(num_cores), tags)
self.publish_gauge(self, NAMESPACE + '.memory.capacity', float(memory_capacity), tags)
# TODO(markine): Report 'allocatable' which is capacity minus capacity
# reserved for system/Kubernetes.
def _update_pods_metrics(self, instance, pods):
"""
Reports the number of running pods, tagged by service and creator
We go though all the pods, extract tags then count them by tag list, sorted and
serialized in a pipe-separated string (it is an illegar character for tags)
"""
tags_map = defaultdict(int)
for pod in pods['items']:
pod_meta = pod.get('metadata', {})
pod_tags = self.kubeutil.get_pod_creator_tags(pod_meta, legacy_rep_controller_tag=True)
services = self.kubeutil.match_services_for_pod(pod_meta)
if isinstance(services, list):
for service in services:
pod_tags.append('kube_service:%s' % service)
if 'namespace' in pod_meta:
pod_tags.append('kube_namespace:%s' % pod_meta['namespace'])
tags_map[frozenset(pod_tags)] += 1
commmon_tags = instance.get('tags', [])
for pod_tags, pod_count in tags_map.iteritems():
tags = list(pod_tags)
tags.extend(commmon_tags)
self.publish_gauge(self, NAMESPACE + '.pods.running', pod_count, tags)
def _update_kube_events(self, instance, pods_list, event_items):
"""
Process kube events and send ddog events
The namespace filtering is done here instead of KubeEventRetriever
to avoid interfering with service discovery
"""
node_ip, node_name = self.kubeutil.get_node_info()
self.log.debug('Processing events on {} [{}]'.format(node_name, node_ip))
k8s_namespaces = instance.get('namespaces', DEFAULT_NAMESPACES)
if not isinstance(k8s_namespaces, list):
self.log.warning('Configuration key "namespaces" is not a list: fallback to the default value')
k8s_namespaces = DEFAULT_NAMESPACES
# handle old config value
if 'namespace' in instance and instance.get('namespace') not in (None, 'default'):
self.log.warning('''The 'namespace' parameter is deprecated and will stop being supported starting '''
'''from 5.13. Please use 'namespaces' and/or 'namespace_name_regexp' instead.''')
k8s_namespaces.append(instance.get('namespace'))
if self.k8s_namespace_regexp:
namespaces_endpoint = '{}/namespaces'.format(self.kubeutil.kubernetes_api_url)
self.log.debug('Kubernetes API endpoint to query namespaces: %s' % namespaces_endpoint)
namespaces = self.kubeutil.retrieve_json_auth(namespaces_endpoint)
for namespace in namespaces.get('items', []):
name = namespace.get('metadata', {}).get('name', None)
if name and self.k8s_namespace_regexp.match(name):
k8s_namespaces.append(name)
k8s_namespaces = set(k8s_namespaces)
for event in event_items:
event_ts = calendar.timegm(time.strptime(event.get('lastTimestamp'), '%Y-%m-%dT%H:%M:%SZ'))
involved_obj = event.get('involvedObject', {})
# filter events by white listed namespaces (empty namespace belong to the 'default' one)
if involved_obj.get('namespace', 'default') not in k8s_namespaces:
continue
tags = self.kubeutil.extract_event_tags(event)
tags.extend(instance.get('tags', []))
title = '{} {} on {}'.format(involved_obj.get('name'), event.get('reason'), node_name)
message = event.get('message')
source = event.get('source')
if source:
message += '\nSource: {} {}\n'.format(source.get('component', ''), source.get('host', ''))
msg_body = "%%%\n{}\n```\n{}\n```\n%%%".format(title, message)
dd_event = {
'timestamp': event_ts,
'host': node_ip,
'event_type': EVENT_TYPE,
'msg_title': title,
'msg_text': msg_body,
'source_type_name': EVENT_TYPE,
'event_object': 'kubernetes:{}'.format(involved_obj.get('name')),
'tags': tags,
}
self.event(dd_event)