当前位置: 首页>>代码示例>>Python>>正文


Python KubeUtil.match_services_for_pod方法代码示例

本文整理汇总了Python中utils.kubernetes.KubeUtil.match_services_for_pod方法的典型用法代码示例。如果您正苦于以下问题:Python KubeUtil.match_services_for_pod方法的具体用法?Python KubeUtil.match_services_for_pod怎么用?Python KubeUtil.match_services_for_pod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在utils.kubernetes.KubeUtil的用法示例。


在下文中一共展示了KubeUtil.match_services_for_pod方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SDDockerBackend

# 需要导入模块: from utils.kubernetes import KubeUtil [as 别名]
# 或者: from utils.kubernetes.KubeUtil import match_services_for_pod [as 别名]

#.........这里部分代码省略.........

    def get_tags(self, state, c_id):
        """Extract useful tags from docker or platform APIs. These are collected by default."""
        tags = []

        ctr = state.inspect_container(c_id)
        # TODO: extend with labels, container ID, etc.
        tags.append('docker_image:%s' % self.dockerutil.image_name_extractor(ctr))
        tags.append('image_name:%s' % self.dockerutil.image_tag_extractor(ctr, 0)[0])
        tags.append('image_tag:%s' % self.dockerutil.image_tag_extractor(ctr, 1)[0])

        if Platform.is_k8s():
            pod_metadata = state.get_kube_config(c_id, 'metadata')

            if pod_metadata is None:
                log.warning("Failed to fetch pod metadata for container %s."
                            " Kubernetes tags may be missing." % c_id[:12])
                return []

            # get pod labels
            kube_labels = pod_metadata.get('labels', {})
            for label, value in kube_labels.iteritems():
                tags.append('%s:%s' % (label, value))

            # get kubernetes namespace
            namespace = pod_metadata.get('namespace')
            tags.append('kube_namespace:%s' % namespace)

            # add creator tags
            creator_tags = self.kubeutil.get_pod_creator_tags(pod_metadata)
            tags.extend(creator_tags)

            # add services tags
            services = self.kubeutil.match_services_for_pod(pod_metadata)
            for s in services:
                if s is not None:
                    tags.append('kube_service:%s' % s)

        elif Platform.is_swarm():
            c_labels = state.inspect_container(c_id).get('Config', {}).get('Labels', {})
            swarm_svc = c_labels.get(SWARM_SVC_LABEL)
            if swarm_svc:
                tags.append('swarm_service:%s' % swarm_svc)

        elif Platform.is_rancher():
            c_inspect = state.inspect_container(c_id)
            service_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_SVC_NAME)
            stack_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_STACK_NAME)
            container_name = c_inspect.get('Config', {}).get('Labels', {}).get(RANCHER_CONTAINER_NAME)
            if service_name:
                tags.append('rancher_service:%s' % service_name)
            if stack_name:
                tags.append('rancher_stack:%s' % stack_name)
            if container_name:
                tags.append('rancher_container:%s' % container_name)

        elif Platform.is_nomad():
            nomad_tags = self.nomadutil.extract_container_tags(state.inspect_container(c_id))
            if nomad_tags:
                tags.extend(nomad_tags)

        elif Platform.is_ecs_instance():
            ecs_tags = self.ecsutil.extract_container_tags(state.inspect_container(c_id))
            tags.extend(ecs_tags)

        return tags
开发者ID:netsil,项目名称:dd-agent,代码行数:70,代码来源:sd_docker_backend.py

示例2: Kubernetes

# 需要导入模块: from utils.kubernetes import KubeUtil [as 别名]
# 或者: from utils.kubernetes.KubeUtil import match_services_for_pod [as 别名]

#.........这里部分代码省略.........
                        values = [parse_quantity(s) for s in QUANTITY_EXP.findall(value_str)]
                        if len(values) != 1:
                            self.log.warning("Error parsing requests value string: %s", value_str)
                            continue
                        self.publish_gauge(self, '{}.{}.requests'.format(NAMESPACE, request), values[0], _tags)
                except (KeyError, AttributeError) as e:
                    self.log.debug("Unable to retrieve container requests for %s: %s", c_name, e)

        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.''')
开发者ID:dblackdblack,项目名称:integrations-core,代码行数:70,代码来源:check.py


注:本文中的utils.kubernetes.KubeUtil.match_services_for_pod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。