def create_node_label_via_bcs(self, request, project_id, cluster_id, node_id_labels={}): """调用BCS服务创建节点标签 """ nodes = cluster_utils.get_cluster_nodes( request.user.token.access_token, project_id, cluster_id) # compose id: ip node_id_ip = { info["id"]: info["inner_ip"] for info in nodes if str(info["id"]) in node_id_labels.keys() } client = K8SClient(request.user.token.access_token, project_id, cluster_id, None) 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(_("节点打标签异常,请联系管理员处理!"))
def list_labels(self, request, project_id): # TODO: 现阶段仅针对mesos if request.project.kind == ProjectKind.K8S.value: raise error_codes.NotOpen() access_token = request.user.token.access_token cluster_id = request.query_params.get("cluster_id") # cluster_id 为None时,查询项目下的所有集群的节点 nodes = node_utils.get_cluster_nodes(access_token, project_id, cluster_id, raise_exception=False) # 排除状态为已删除的节点 nodes = [ node for node in nodes if node["status"] not in [NodeStatus.Removed] ] project_kind_name = ProjectKind.get_choice_label(request.project.kind) labels = getattr(self, f"_{project_kind_name.lower()}_node_labels")( access_token, project_id, nodes) cluster_id_map = self._cluster_id_map(access_token, project_id) self.compose_node_data(nodes, labels, cluster_id_map) # 添加权限 nodes_results = bcs_perm.Cluster.hook_perms(request, project_id, nodes) return response.Response({ 'count': len(nodes_results), 'results': nodes_results })
def _get_cluster_node_ip_list(self, access_token, project_id, cluster_id): data = cluster_utils.get_cluster_nodes(access_token, project_id, cluster_id) return [info["inner_ip"] for info in data]
def get_nodes_id_ip(self, access_token, project_id, cluster_id): nodes = cluster_utils.get_cluster_nodes(access_token, project_id, cluster_id) return {info["id"]: info for info in nodes}