Esempio n. 1
0
 def get_mesos_app_deploy_with_post(
         self, request, project_id, cluster_id, instance_name=None,
         category="application", field=None, namespace=None):
     """查询mesos下application和deployment的状态
     """
     client = MesosClient(
         request.user.token.access_token,
         project_id, cluster_id, None
     )
     if category == "application":
         resp = retry_requests(
             client.get_application_with_post,
             data={
                 "name": instance_name,
                 "field": field or "data.metadata.name,data.metadata.namespace,data.status,data.message",
                 "namespace": namespace
             }
         )
     else:
         resp = retry_requests(
             client.get_deployment_with_post,
             data={
                 "name": instance_name,
                 "field": field or "data.metadata.name,data.metadata.namespace,data.status,data.message",
                 "namespace": namespace
             }
         )
     if resp.get("code") != ErrorCode.NoError:
         return False, APIResponse({
             "code": resp.get("code") or DEFAULT_ERROR_CODE,
             "message": resp.get("message")
         })
     return True, resp
Esempio n. 2
0
    def get_k8s_app_deploy_with_post(self,
                                     request,
                                     project_id,
                                     cluster_id,
                                     instance_name=None,
                                     category="application",
                                     namespace=None,
                                     field=None):
        """获取k8s下application和deployment状态"""
        client = K8SClient(request.user.token.access_token, project_id,
                           cluster_id, None)
        curr_func = getattr(client,
                            "%s_with_post" % (FUNC_MAP[category] % "get"))
        params = {
            "name": instance_name,
            "namespace": namespace,
            "field": ",".join(field)
        }
        resp = retry_requests(curr_func, params=params)
        if resp.get("code") != ErrorCode.NoError:
            return False, APIResponse({
                "code": resp.get("code") or DEFAULT_ERROR_CODE,
                "message": resp.get("message")
            })

        return True, resp
Esempio n. 3
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