Beispiel #1
0
def get_metric_instances(access_token: str, project_id: str, metric_name: str, env: str,
                         cluster_id_list: list, ns_dict: dict, instance_info) -> list:
    metric_instance_list = []

    stag = settings.BCS_API_ENV[env]
    client = MesosClient(access_token=access_token, project_id=project_id, cluster_id=None, env=stag)
    try:
        metric_data = client.get_metrics(metric_name, cluster_id_list).get('data') or []
    except Exception as e:
        logger.exception("get_metrics error: %s", e)
        metric_data = []

    for _m in metric_data:
        namespace = _m.get('namespace')
        selector = _m.get('selector')

        s_key = f'{LABLE_METRIC_SELECTOR_LABEL}.{metric_name}'
        if s_key not in selector:
            continue

        selector_str = f'"{s_key}": "{metric_name}"'
        # 查询跟metric相关的应用
        ns_id = ns_dict.get(namespace)
        ins_list = instance_info.filter(
            namespace=ns_id, config__contains=selector_str).values_list('name', 'category')
        for _ins in ins_list:

            metric_instance_list.append({
                'ns_id': ns_id,
                'namespace': namespace,
                'name': _ins[0],
                'category': CATE_SHOW_NAME.get(_ins[1], _ins[1])
            })

    return metric_instance_list
Beispiel #2
0
def get_real_category(project_kind, category):
    if category not in CATE_SHOW_NAME.values():
        return category
    project_kind_name = ClusterType.get(project_kind)
    if project_kind_name == 'Kubernetes':
        category = 'K8s%s' % category
    elif project_kind_name == 'Mesos':
        category = category.lower()
    return category
Beispiel #3
0
def get_all_template_info_by_project(project_id):
    # 获取项目下所有的模板信息
    # 暂时不支持YAML模板查询
    temps = Template.objects.filter(
        project_id=project_id,
        edit_mode=TemplateEditMode.PageForm.value).values('id', 'name')
    tem_dict = {tem['id']: tem['name'] for tem in temps}
    tem_ids = tem_dict.keys()

    # 获取所有的可见版本
    show_vers = ShowVersion.objects.filter(template_id__in=tem_ids).values(
        'id', 'real_version_id', 'name', 'template_id')

    resource_list = []

    for _show in show_vers:
        show_version_id = _show['id']
        real_version_id = _show['real_version_id']
        template_id = _show['template_id']
        template_name = tem_dict.get(template_id)

        # 每个版本的具体信息
        versioned_entity = VersionedEntity.objects.get(template_id=template_id,
                                                       id=real_version_id)
        entity = versioned_entity.get_entity()
        for category in entity:
            ids = entity.get(category)
            id_list = ids.split(',') if ids else []
            res_list = MODULE_DICT.get(category).objects.filter(
                id__in=id_list).values('id', 'name', 'config')
            for resource in res_list:
                config = resource['config']
                resource_list.append({
                    'template_id':
                    template_id,
                    'template_name':
                    template_name,
                    'show_version_id':
                    show_version_id,
                    'show_version_name':
                    _show['name'],
                    'category':
                    category,
                    'category_name':
                    CATE_SHOW_NAME.get(category, category),
                    'resource_id':
                    resource['id'],
                    'resource_name':
                    resource['name'],
                    'config':
                    config
                })
    return resource_list
Beispiel #4
0
 def get_resource_by_show_name(self, request, project_id, template_id):
     validate_template_id(project_id, template_id)
     show_version_name = request.GET.get('show_version_name')
     data = get_res_by_show_name(template_id, show_version_name)
     # 传给前端的资源类型统一
     new_data = {CATE_SHOW_NAME.get(x, x): data[x] for x in data}
     return Response({
         "code": 0,
         "message": "OK",
         "data": {
             "data": new_data
         }
     })
Beispiel #5
0
def to_bcs_res_name(project_kind, origin_name):
    if origin_name not in CATE_SHOW_NAME.values():
        return origin_name
    if project_kind == ProjectKind.K8S.value:
        return f'K8s{origin_name}'
    return origin_name.lower()