def delete(self): # 添加权限限制 self.check_cluster_perm() # 针对导入/纳管的集群,需要删除节点记录 cluster = cluster_utils.get_cluster(self.access_token, self.project_id, self.cluster_id) if cluster["state"] == ClusterState.Existing.value: self.delete_cluster_nodes(self.access_token, self.project_id, self.cluster_id) # 更新集群状态为删除中 data = self.update_cluster_status(status=CommonStatus.Removing) # 删除平台数据库记录的已经实例化,但是没有删除的实例信息 ns_id_list = self.get_cluster_ns_list() self.clean_instance(ns_id_list) self.delete_namespaces() self.delete_helm_release() # 下发删除任务 with client.ContextActivityLogClient( project_id=self.project_id, user=self.username, resource_type=ACTIVITY_RESOURCE_TYPE, resource=data['name'], resource_id=self.cluster_id, ).log_delete(): snapshot = self.get_cluster_snapshot() self.config = json.loads(snapshot.get('configure', '{}')) self.control_ip = self.config.pop('control_ip', []) self.websvr = self.config.pop('websvr', []) task = self.delete_cluster_via_bcs() if task and not task.is_finished and task.is_polling: task.polling_task() return Response({})
def get(self, request, project_id, cluster_id): """获取session信息 """ cluster_data = cluster.get_cluster(request.user.token.access_token, project_id, cluster_id) self.cluster_name = cluster_data.get("name", "")[:32] # 检查白名单, 不在名单中再通过权限中心校验 if not utils.allowed_login_web_console(request.user.username): perm = bcs_perm.Cluster(request, project_id, cluster_id) try: perm.can_use(raise_exception=True) except Exception as error: utils.activity_log(project_id, cluster_id, self.cluster_name, request.user.username, False, _("集群不正确或没有集群使用权限")) raise error # 获取web-console context信息 if request.project.kind == ProjectKind.MESOS.value: # 添加白名单控制 from .views_bk import ensure_mesos_wlist ensure_mesos_wlist(project_id, cluster_id, request.user.username) context = self.get_mesos_context(request, project_id, cluster_id) else: context = self.get_k8s_context(request, project_id, cluster_id) context["username"] = request.user.username context.setdefault("namespace", constants.NAMESPACE) logger.info(context) session = session_mgr.create(project_id, cluster_id) session_id = session.set(context) # 替换http为ws地址 bcs_api_url = urlparse(settings.DEVOPS_BCS_API_URL) if bcs_api_url.scheme == "https": scheme = "wss" else: scheme = "ws" bcs_api_url = bcs_api_url._replace(scheme=scheme) # 连接ws的来源方式, 有容器直连(direct)和多tab管理(mgr) source = request.query_params.get("source", "direct") ws_url = f"{bcs_api_url.geturl()}/web_console/projects/{project_id}/clusters/{cluster_id}/ws/?session_id={session_id}&source={source}" # noqa data = {"session_id": session_id, "ws_url": ws_url} utils.activity_log(project_id, cluster_id, self.cluster_name, request.user.username, True) return BKAPIResponse(data, message=_("获取session成功"))
def can_delete_node(self, access_token, project_id, cluster_id): cluster = get_cluster(access_token, project_id, cluster_id) # 针对导入/纳管的集群,不允许通过平台删除节点 if cluster["state"] == ClusterState.Existing.value: raise ValidationError(_("导入的集群不允许通过平台删除节点"))