Exemple #1
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
Exemple #2
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
Exemple #3
0
def test_k8s_job_status(replicas, available, completions, expect):
    resource = FancyDict(data=FancyDict(spec=FancyDict(completions=completions)))
    status = get_k8s_resource_status("job", resource, replicas, available)
    assert status == expect
Exemple #4
0
def test_k8s_resource_status(replicas, available, expect):
    resource = FancyDict()
    status = get_k8s_resource_status("deployment", resource, replicas, available)
    assert status == expect