Esempio n. 1
0
 def get_ns_inst_error_count(self, func, request, project_id, category,
                             kind, ns_name_list, inst_name, cluster_id_list,
                             ns_name_inst):
     """获取实例错误数据"""
     error_data = {}
     all_data = {}
     category = REVERSE_CATEGORY_MAP[category]
     for cluster_id in set(cluster_id_list):
         # 要展示client创建的应用,因此不能指定名称和命名空间
         flag, resp = func(
             request,
             project_id,
             cluster_id,
             instance_name=inst_name,
             category=category,
             project_kind=kind,
             namespace=",".join(set(ns_name_list)),
             field=[
                 "data.status",
                 "resourceName",
                 "namespace",
                 "data.spec.parallelism",
                 "data.spec.paused",
                 'data.spec.replicas',
             ],
         )
         if not flag:
             logger.error("请求storage接口出现异常, 详情: %s" % resp)
             continue
         resp_data = resp.get("data") or []
         diff_inst = set(ns_name_inst) - set(
             [(cluster_id, info["namespace"], info["resourceName"])
              for info in resp_data])
         for info in resp_data:
             ns_name = info["namespace"]
             spec = (info.get("data") or {}).get("spec") or {}
             # 针对不同的模板获取不同的值
             replicas, available = utils.get_k8s_desired_ready_instance_count(
                 info, category)
             pause_status = spec.get("paused")
             if (cluster_id, ns_name) in all_data:
                 all_data[(cluster_id, ns_name)] += 1
             else:
                 all_data[(cluster_id, ns_name)] = 1
             if (available != replicas
                     or available <= 0) and (not pause_status):
                 if (cluster_id, ns_name) in error_data:
                     error_data[(cluster_id, ns_name)] += 1
                 else:
                     error_data[(cluster_id, ns_name)] = 1
         for info in diff_inst:
             if (cluster_id, info[0]) in all_data:
                 all_data[(cluster_id, info[0])] += 1
             if info[0] in error_data:
                 error_data[(cluster_id, info[0])] += 1
             else:
                 error_data[(cluster_id, info[0])] = 1
     return error_data, all_data
Esempio n. 2
0
    def get_k8s_category_info(self, request, project_id, resource_name, inst_name, cluster_id, ns_name):
        """获取分类的上报信息
        {'BCS-K8S-15007': {'K8sDeployment': {'inst_list': ['bellke-test-deploy-1'], 'ns_list': ['abc1']}}}
        """
        ret_data = {}
        client = K8SClient(
            request.user.token.access_token,
            project_id, cluster_id, None
        )
        curr_func = FUNC_MAP[resource_name] % 'get'
        resp = retry_requests(
            getattr(client, curr_func),
            params={
                "name": inst_name,
                "namespace": ns_name,
                "field": ','.join(app_constants.RESOURCE_STATUS_FIELD_LIST)
            }
        )
        if resp.get('code') != ErrorCode.NoError:
            raise error_codes.APIError.f(resp.get('message'))
        data = resp.get('data') or []

        # 添加HPA绑定信息
        data = get_deployment_hpa(request, project_id, cluster_id, ns_name, data)

        for info in data:
            spec = getitems(info, ['data', 'spec'], default={})
            # 针对不同的模板获取不同的值
            replicas, available = utils.get_k8s_desired_ready_instance_count(info, resource_name)
            curr_key = (info['namespace'], info['resourceName'])
            labels = getitems(info, ['data', 'metadata', 'labels'], default={})
            source_type = labels.get('io.tencent.paas.source_type') or 'other'
            annotations = getitems(info, ['data', 'metadata', 'annotations'], default={})
            ret_data[curr_key] = {
                'backend_status': 'BackendNormal',
                'backend_status_message': _('请求失败,已通知管理员!'),
                'category': resource_name,
                'pod_count': f'{available}/{replicas}',
                'build_instance': available,
                'instance': replicas,
                'status': utils.get_k8s_resource_status(resource_name, info, replicas, available),
                'name': info['resourceName'],
                'namespace': info['namespace'],
                'create_at': info['createTime'],
                'update_at': info['updateTime'],
                'source_type': SOURCE_TYPE_MAP.get(source_type),
                'version': get_instance_version_name(annotations, labels),  # 标识应用的线上版本
                'hpa': info['hpa']  # 是否绑定了HPA
            }
            if spec.get('paused'):
                ret_data[curr_key]['status'] = 'Paused'
        return ret_data
Esempio n. 3
0
 def get_k8s_category_info(self, request, project_id, cluster_ns_inst,
                           category):
     """获取分类的上报信息
     添加类型,只是为了mesos和k8s进行适配
     {'BCS-K8S-15007': {'K8sDeployment': {'inst_list': ['bellke-test-deploy-1'],
     'ns_list': ['abc1'], 'inst_ns_map': {'test1': 'deployment-232132132'}}}}
     """
     resource_name = CATEGORY_MAP[category]
     ret_data = {}
     for cluster_id, info in cluster_ns_inst.items():
         client = K8SClient(request.user.token.access_token, project_id,
                            cluster_id, None)
         if not info.get(resource_name):
             continue
         curr_func = FUNC_MAP[category] % 'get'
         resp = getattr(client, curr_func)({
             'name':
             ','.join(info[resource_name]['inst_list']),
             'namespace':
             ','.join(info[resource_name]['ns_list']),
             'field':
             ','.join(app_constants.RESOURCE_STATUS_FIELD_LIST)
         })
         if resp.get('code') != ErrorCode.NoError:
             raise error_codes.APIError(resp.get('message'))
         data = resp.get('data') or []
         inst_ns_map = info[resource_name]['inst_ns_map']
         exist_item = []
         for info_item in data:
             exist_item.append(info_item['resourceName'])
             replicas, available = utils.get_k8s_desired_ready_instance_count(
                 info_item, category)
             if available != replicas or available == 0:
                 curr_key = f'{resource_name}:{info_item["resourceName"]}'
                 if curr_key in ret_data:
                     ret_data[curr_key] += 1
                 else:
                     ret_data[curr_key] = 1
         for key, val in inst_ns_map.items():
             if val in exist_item:
                 continue
             curr_key = f'{resource_name}:{val}'
             if curr_key in ret_data:
                 ret_data[curr_key] += 1
             else:
                 ret_data[curr_key] = 1
     return ret_data
Esempio n. 4
0
 def get_k8s_category_info(self, request, project_id, cluster_ns_inst,
                           category):
     """获取分类的上报信息
     {'BCS-K8S-15007': {'K8sDeployment': {'inst_list': ['bellke-test-deploy-1'], 'ns_list': ['abc1']}}}
     """
     ret_data = {}
     for cluster_id, app_info in cluster_ns_inst.items():
         client = K8SClient(request.user.token.access_token, project_id,
                            cluster_id, None)
         curr_func = FUNC_MAP[category] % 'get'
         resp = getattr(client, curr_func)({
             'field':
             ','.join(app_constants.RESOURCE_STATUS_FIELD_LIST)
         })
         if resp.get('code') != ErrorCode.NoError:
             raise error_codes.APIError.f(resp.get('message'))
         data = resp.get('data') or []
         # TODO: 关于状态匹配可以再根据实际情况进行调整
         for info in data:
             curr_app_id = f'{info["namespace"]}:{info["resourceName"]}'
             if curr_app_id not in app_info:
                 continue
             spec = (info.get('data') or {}).get('spec') or {}
             # 针对不同的模板获取不同的值
             replicas, available = utils.get_k8s_desired_ready_instance_count(
                 info, category)
             curr_key = (cluster_id, info['namespace'],
                         info['resourceName'])
             ret_data[curr_key] = {
                 'pod_count':
                 f'{available}/{replicas}',
                 'build_instance':
                 available,
                 'instance':
                 replicas,
                 'status':
                 utils.get_k8s_resource_status(category, info, replicas,
                                               available),
             }
             if spec.get('paused'):
                 ret_data[curr_key]['status'] = 'Paused'
     return ret_data
Esempio n. 5
0
 def get_k8s_category_info(self, request, project_id, resource_name,
                           inst_name, cluster_id, ns_name):
     """获取分类的上报信息
     {'BCS-K8S-15007': {'K8sDeployment': {'inst_list': ['bellke-test-deploy-1'], 'ns_list': ['abc1']}}}
     """
     ret_data = {}
     client = K8SClient(request.user.token.access_token, project_id,
                        cluster_id, None)
     field_list = [
         'data.status', 'resourceName', 'namespace',
         'data.spec.parallelism', 'data.spec.paused', 'createTime',
         'updateTime', 'data.metadata.labels', 'data.spec.replicas'
     ]
     curr_func = FUNC_MAP[resource_name] % 'get'
     resp = getattr(client, curr_func)({
         'name': inst_name,
         'namespace': ns_name,
         'field': ','.join(field_list)
     })
     if resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError.f(resp.get('message'))
     data = resp.get('data') or []
     for info in data:
         spec = getitems(info, ['data', 'spec'], default={})
         # 针对不同的模板获取不同的值
         replicas, available = utils.get_k8s_desired_ready_instance_count(
             info, resource_name)
         curr_key = (info['namespace'], info['resourceName'])
         labels = getitems(info, ['data', 'metadata', 'labels'], default={})
         source_type = labels.get('io.tencent.paas.source_type') or 'other'
         ret_data[curr_key] = {
             'backend_status':
             'BackendNormal',
             'backend_status_message':
             '请求失败,已通知管理员!',
             'category':
             resource_name,
             'pod_count':
             f'{available}/{replicas}',
             'build_instance':
             available,
             'instance':
             replicas,
             'status':
             'Unready' if
             (available != replicas or available <= 0) else 'Running',
             'name':
             info['resourceName'],
             'namespace':
             info['namespace'],
             'create_at':
             info['createTime'],
             'update_at':
             info['updateTime'],
             'source_type':
             SOURCE_TYPE_MAP.get(source_type),
             'version':
             labels.get('io.tencent.paas.version')  # 标识应用的线上版本
         }
         if spec.get('paused'):
             ret_data[curr_key]['status'] = 'Paused'
     return ret_data