예제 #1
0
 def get_node_list(self):
     cluster_node_info = paas_cc.get_node_list(
         self.access_token, self.project_id, self.cluster_id, params={'limit': DEFAULT_NODE_LIMIT}
     )
     if cluster_node_info.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(cluster_node_info.get('message'))
     return cluster_node_info.get('data', {}).get('results', [])
예제 #2
0
 def get_node_list(self, request, project_id):
     """get node list"""
     resp = paas_cc.get_node_list(request.user.token.access_token,
                                  project_id, None)
     if resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError.f(resp.get('message'))
     return resp.get('data') or {}
예제 #3
0
 def create_node_label_via_bcs(self, request, project_id, cluster_id, node_id_labels={}):
     """调用BCS服务创建节点标签
     """
     client = K8SClient(
         request.user.token.access_token, project_id, cluster_id, None)
     node_list = paas_cc.get_node_list(
         request.user.token.access_token, project_id, cluster_id)
     if node_list.get("code") != 0:
         raise error_codes.APIError(_("查询节点失败,请联系管理员处理!"))
     results = node_list.get("data", {}).get("results", [])
     if not results:
         raise error_codes.APIError(_("当前集群下没有节点信息!"))
     # compose id: ip
     node_id_ip = {
         info["id"]: info["inner_ip"]
         for info in results
         if str(info["id"]) in node_id_labels.keys()
     }
     for node_id, ip in node_id_ip.items():
         k8s_resp = client.get_node_detail(ip)
         if k8s_resp.get("code") != 0:
             raise error_codes.APIError.f(k8s_resp.get("message"), replace=True)
         exist_metadata = (k8s_resp.get("data") or {}).get("metadata") or {}
         exist_labels = exist_metadata.get("labels") or {}
         if node_id_labels[str(node_id)] == "del":
             exist_labels.pop("nodetype", None)
         else:
             exist_labels.update(K8S_LB_LABEL)
         exist_labels["$patch"] = "replace"
         resp = client.create_node_labels(ip, exist_labels)
         if resp.get("code") != 0:
             raise error_codes.APIError(_("节点打标签异常,请联系管理员处理!"))
예제 #4
0
def get_node_metric(request, access_token, project_id, cluster_id,
                    cluster_type):
    node = paas_cc.get_node_list(access_token,
                                 project_id,
                                 cluster_id,
                                 params={"limit": 10000})
    if node.get('code') != 0:
        raise APIError(node.get('message'))
    # 过滤掉状态为removed的机器
    node_data = [
        info for info in node.get("data", {}).get("results") or []
        if info.get("status") not in [constants.NodeStatus.REMOVED.value]
    ]
    # 重新组装数据
    node = {
        "count": len(node_data),
        "results": node_data,
    }
    node_total = node['count']
    node_actived = 0
    node_disabled = 0

    if cluster_type != ClusterCOES.MESOS.value:
        # namespace 获取处理
        client = k8s.K8SClient(access_token,
                               project_id,
                               cluster_id=cluster_id,
                               env=None)
        namespace = client.get_namespace()
        if not namespace.get('result'):
            raise APIError(namespace.get('message'))

        # 节点状态处理 计算k8s有容器的节点
        if node_total > 0:
            node_ips = [i['inner_ip'] for i in node['results']]
            containers = k8s_containers(request, project_id, cluster_id,
                                        node_ips)
            for node in node_ips:
                if containers.get(node, 0) > 0:
                    node_actived += 1

    else:
        # 节点状态处理 计算mesos有容器的节点
        if node_total > 0:
            node_ips = [i['inner_ip'] for i in node['results']]
            containers = mesos_containers(request, project_id, cluster_id,
                                          node_ips)
            for node in node_ips:
                if containers.get(node, 0) > 0:
                    node_actived += 1

    node_disabled = node_total - node_actived
    data = {
        'total': node_total,
        'actived': node_actived,
        'disabled': node_disabled
    }
    return data
예제 #5
0
def get_cluster_nodes(access_token, project_id, cluster_id, raise_exception=True):
    """获取集群下的node信息"""
    resp = paas_cc.get_node_list(access_token, project_id, cluster_id)
    if resp.get("code") != ErrorCode.NoError:
        raise error_codes.APIError(_("获取集群node ip失败,{}").format(resp.get("message")))
    results = resp.get("data", {}).get("results") or []
    if not results and raise_exception:
        raise error_codes.APIError(_("获取集群node ip为空"))
    return results
예제 #6
0
    def get_node_info(self, access_token, project_id, cluster_id):
        node_list = paas_cc.get_node_list(access_token, project_id, cluster_id)
        if node_list.get("code") != 0:
            raise error_codes.APIError(_("查询节点失败,请联系管理员处理!"))
        results = node_list.get("data", {}).get("results", [])
        if not results:
            raise error_codes.APIError(_("当前集群下没有节点信息!"))

        return {info["id"]: info for info in results}
예제 #7
0
 def get_node_list(self, request, project_id, cluster_id):
     """get cluster node list"""
     resp = paas_cc.get_node_list(request.user.token.access_token,
                                  project_id, cluster_id)
     if resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(
             _("获取集群节点列表异常,{}").format(resp.get('message')))
     data = resp.get('data') or {}
     return data.get('results') or []
예제 #8
0
파일: base.py 프로젝트: ielgnaw/bk-bcs-saas
    def cluster_ready(self, cluster_id):
        """集群状态检查,至少有一个节点状态为Normal"""
        result = paas_cc.get_node_list(self.access_token, self.project_id, cluster_id)
        if result.get("code") != 0:
            raise ClusterNotReady("获取状态失败,请联系蓝鲸管理员解决")

        data = result["data"]["results"] or []
        normal_nodes = [i for i in data if i["status"] == NodeStatus.Normal]
        if len(normal_nodes) == 0:
            raise ClusterNotReady("没有可用节点,请添加或启用节点")
예제 #9
0
 def get_node_list(self, request, project_id, cluster_id):
     """get cluster node list
     """
     node_resp = paas_cc.get_node_list(
         request.user.token.access_token, project_id, cluster_id,
         params={'limit': cluster_constants.DEFAULT_NODE_LIMIT}
     )
     if node_resp.get('code') != ErrorCode.NoError:
         raise ValidationError(node_resp.get('message'))
     data = node_resp.get('data') or {}
     return [i['inner_ip'] for i in data.get('results') or []]
예제 #10
0
파일: node.py 프로젝트: ielgnaw/bk-bcs-saas
 def get_node_list(self, request, project_id, cluster_id):
     """get cluster node list"""
     node_resp = paas_cc.get_node_list(
         request.user.token.access_token,
         project_id,
         cluster_id,
         params={'limit': constants.DEFAULT_NODE_LIMIT},
     )
     if node_resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(node_resp.get('message'))
     return node_resp.get('data') or {}
예제 #11
0
 def get_cluster_node(self, request, project_id, cluster_id):
     """get cluster node list"""
     cluster_node_resp = paas_cc.get_node_list(
         request.user.token.access_token,
         project_id,
         cluster_id,
         params={"limit": cluster_constants.DEFAULT_NODE_LIMIT},
     )
     if cluster_node_resp.get("code") != ErrorCode.NoError:
         raise error_codes.APIError(cluster_node_resp.get("message"))
     data = cluster_node_resp.get("data") or {}
     return data.get("results") or []
예제 #12
0
    def cluster_ready(self, cluster_id):
        """集群状态检查,至少有一个节点状态为Normal
        """
        result = paas_cc.get_node_list(
            self.access_token, self.project_id, cluster_id)
        if result.get('code') != 0:
            raise ClusterNotReady("获取状态失败,请联系蓝鲸管理员解决")

        data = result['data']['results'] or []
        normal_nodes = [i for i in data
                        if i['status'] == NodeStatus.NORMAL.value]
        if len(normal_nodes) == 0:
            raise ClusterNotReady("没有可用节点,请添加或启用节点")
예제 #13
0
 def cluster_has_node(self, request, project_id):
     """cluster has node
     format: {cluster_id: True/False}
     """
     cluster_node_resp = paas_cc.get_node_list(
         request.user.token.access_token, project_id, None, params={"limit": cluster_constants.DEFAULT_NODE_LIMIT}
     )
     data = cluster_node_resp.get("data") or {}
     results = data.get("results") or []
     # compose the map for cluste and node
     return {
         info["cluster_id"]: True for info in results if info["status"] not in cluster_constants.FILTER_NODE_STATUS
     }
예제 #14
0
 def get_nodes(self, request, project_id):
     cluster_id = request.query_params.get('cluster_id')
     resp = paas_cc.get_node_list(request.user.token.access_token,
                                  project_id, cluster_id)
     if resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(
             f'get node error, {resp.get("message")}')
     data = resp.get('data') or {}
     results = data.get('results') or []
     return [[
         node['cluster_id'], node['inner_ip'],
         self.STATUS_MAP_NAME.get(node['status'])
     ] for node in results if node['status'] != NodeStatus.Removable]
예제 #15
0
    def create(self, request, project_id, *args, **kwargs):
        serializer = ClusterKubeConfigSLZ(data=request.data)
        serializer.is_valid(raise_exception=True)

        cluster_id = serializer.data["cluster_id"]

        check_cluster_perm(user=request.user, project_id=project_id, cluster_id=cluster_id, request=request)

        # 检查是否有 node, 没有node时,bcs-agent无法启动
        nodes_info = paas_cc.get_node_list(self.access_token, project_id, cluster_id)
        if not nodes_info["data"]["results"]:
            return Response(data={"code": 40032, "message": _("集群下没有Node节点,无法启用,请先添加")})
        data = helm_init(self.access_token, project_id, cluster_id, self.bcs_agent_namespace)

        return Response(data=data)
예제 #16
0
 def get_cluster_node_list(self, access_token, project_id, cluster_id):
     # TODO: 先兼容
     DEFAULT_NODE_LIMIT = 10000
     cluster_node_info = paas_cc.get_node_list(
         access_token,
         project_id,
         cluster_id,
         params={'limit': DEFAULT_NODE_LIMIT})
     if cluster_node_info.get('code') != ErrorCode.NoError:
         raise error_codes.APIError.f(cluster_node_info.get('message'))
     ip_info_list = cluster_node_info.get('data', {}).get('results', [])
     if not ip_info_list:
         raise error_codes.APIError.f("获取集群节点为空,请确认后重试")
     return [
         info['inner_ip'] for info in ip_info_list if info.get('status')
         not in [CommonStatus.Removed, CommonStatus.Removing]
     ]
예제 #17
0
 def get_nodes(self, request, project_id):
     req_data = request.data
     cluster_id = req_data.get("cluster_id")
     node_id_list = req_data.get("node_id_list")
     resp = paas_cc.get_node_list(request.user.token.access_token, project_id, cluster_id)
     if resp.get('code') != ErrorCode.NoError:
         raise error_codes.APIError(f'get node error, {resp.get("message")}')
     data = resp.get('data') or {}
     results = data.get('results') or []
     node_data = []
     for node in results:
         if node["status"] == NodeStatus.Removed:
             continue
         if node_id_list and node["id"] not in node_id_list:
             continue
         node_data.append([node["cluster_id"], node["inner_ip"], self.STATUS_MAP_NAME.get(node["status"])])
     return node_data
예제 #18
0
 def get_node_ip_map(self, request, project_id, cluster_id):
     node_list = paas_cc.get_node_list(self.request.user.token.access_token,
                                       project_id, cluster_id)
     node_list = node_list.get("data", {}).get("results") or []
     node_ip_map = {i["inner_ip"]: i["id"] for i in node_list}
     return node_ip_map
예제 #19
0
 def get_node_ip_list(self, request, project_id, cluster_id):
     node_list = paas_cc.get_node_list(self.request.user.token.access_token,
                                       project_id, cluster_id)
     node_list = node_list.get('data', {}).get('results') or []
     node_list = [i['inner_ip'] for i in node_list]
     return node_list