class LBController: def __init__(self, ctx_cluster: CtxCluster): self.node_client = Node(ctx_cluster) def add_labels(self, ip_list: List[str]): """添加lb的标签""" node_label_list = self._construct_node_label_list(ip_list) self.node_client.set_labels_for_multi_nodes(node_label_list) def delete_labels(self, ip_list: List[str]): node_label_list = self._construct_node_label_list(ip_list, LBLabelOp.DELETE) self.node_client.set_labels_for_multi_nodes(node_label_list) def _construct_node_label_list(self, ip_list: List[str], op: str = LBLabelOp.ADD) -> List: """查询节点的标签""" node_list = self.node_client.list(is_format=False) # 获取节点标签 node_label_list = [] for node in node_list.items: if node.inner_ip not in ip_list: continue labels = self._construct_labels_by_op(node.labels, op) node_label_list.append({"node_name": node.name, "labels": labels}) return node_label_list def _construct_labels_by_op(self, labels: Dict, op: str = LBLabelOp.ADD) -> Dict: if op == LBLabelOp.ADD: labels.update(K8S_LB_LABEL) else: for key in K8S_LB_LABEL: labels[key] = None return labels
def set_labels(self, request, project_id, cluster_id): """设置节点标签""" params = self.params_validate(slz.NodeLabelListSLZ) node_client = Node(request.ctx_cluster) node_client.set_labels_for_multi_nodes(params["node_label_list"]) # 获取节点名称,用于审计 node_names = get_nodes_repr([n["node_name"] for n in params["node_label_list"]]) request.audit_ctx.update_fields(resource=node_names, extra=params, description=_("节点设置标签")) return Response()
def set_labels(self, request, project_id, cluster_id): """设置节点标签""" params = self.params_validate(slz.NodeLabelListSLZ) node_client = Node(request.ctx_cluster) node_client.set_labels_for_multi_nodes(params["node_label_list"]) return Response()