Example #1
0
    def sync_namespaces(self, request, project_id_or_code, cluster_id):
        """同步集群命名空间到 BCSCC"""
        request.audit_ctx.update_fields(
            resource_type=ResourceType.Cluster, resource=cluster_id, description=f'同步集群 {cluster_id} 命名空间'
        )

        project_id = request.project.project_id
        # 统一: 通过cc获取的数据,添加cc限制,区别于直接通过线上直接获取的命名空间
        cc_namespaces = ns_utils.get_namespaces_by_cluster_id(request.user.token.access_token, project_id, cluster_id)
        # 转换格式,方便其他系统使用
        cc_namespace_name_id = {info["name"]: info["id"] for info in cc_namespaces}
        # 获取线上的命名空间
        access_token = request.user.token.access_token
        namespaces = ns_utils.get_k8s_namespaces(access_token, project_id, cluster_id)
        # NOTE: 忽略k8s系统和平台自身的namespace
        namespace_name_list = [
            info["resourceName"] for info in namespaces if info["resourceName"] not in K8S_PLAT_NAMESPACE
        ]
        if not (cc_namespaces and namespaces):
            return Response()
        # 根据namespace和realtime namespace进行删除或创建
        # 删除命名空间
        delete_ns_name_list = set(cc_namespace_name_id.keys()) - set(namespace_name_list)
        delete_ns_id_list = [cc_namespace_name_id[name] for name in delete_ns_name_list]
        self._delete_cc_ns(request, project_id, cluster_id, delete_ns_id_list)

        # 向V0权限中心注册命名空间数据
        add_ns_name_list = set(namespace_name_list) - set(cc_namespace_name_id.keys())
        self._add_cc_ns(request, project_id, cluster_id, add_ns_name_list)
        return Response()
Example #2
0
def get_namespace_id(access_token, project_id, cluster_id, namespace):
    namespaces = get_namespaces_by_cluster_id(access_token, project_id,
                                              cluster_id)
    for ns in namespaces:
        if ns["name"] == namespace:
            return ns["id"]
    raise serializers.ValidationError(
        _("项目(id:{})下不存在命名空间({}/{})").format(project_id, cluster_id,
                                             namespace))
Example #3
0
    def get_cc_namespace_info(self, name: str) -> Dict:
        """
        获取 CC 中命名空间信息

        :param name: 命名空间名称
        :return: Namespace 信息
        """
        cc_namespaces = get_namespaces_by_cluster_id(
            self.ctx_cluster.context.auth.access_token,
            self.ctx_cluster.project_id, self.ctx_cluster.id)
        for ns in cc_namespaces:
            if ns['name'] == name:
                return self._extract_namespace_info(ns)
Example #4
0
    def get_or_create_cc_namespace(self, name: str, username: str) -> Dict:
        """
        尝试在 PaaSCC 中查询指定命名空间,若不存在则创建

        :param name: 命名空间名称
        :param username: 操作者
        :return: Namespace 信息
        """
        # 假定cc中有,集群中也存在
        cc_namespaces = get_namespaces_by_cluster_id(
            self.ctx_cluster.context.auth.access_token,
            self.ctx_cluster.project_id, self.ctx_cluster.id)
        for ns in cc_namespaces:
            if ns["name"] == name:
                return self._extract_namespace_info(ns)

        return self._create_namespace(username, name)
Example #5
0
    def sync_namespaces(self, request, project_id_or_code, cluster_id):
        """同步集群下命名空间
        NOTE: 先仅处理k8s类型
        """
        project_id = request.project.project_id
        # 统一: 通过cc获取的数据,添加cc限制,区别于直接通过线上直接获取的命名空间
        cc_namespaces = ns_utils.get_namespaces_by_cluster_id(
            request.user.token.access_token, project_id, cluster_id)
        # 转换格式,方便其他系统使用
        cc_namespace_name_id = {
            info["name"]: info["id"]
            for info in cc_namespaces
        }
        # 获取线上的命名空间
        access_token = request.user.token.access_token
        namespaces = ns_utils.get_k8s_namespaces(access_token, project_id,
                                                 cluster_id)
        # NOTE: 忽略k8s系统和平台自身的namespace
        namespace_name_list = [
            info["resourceName"] for info in namespaces
            if info["resourceName"] not in K8S_SYS_NAMESPACE
            and info["resourceName"] not in K8S_PLAT_NAMESPACE
        ]
        if not (cc_namespaces and namespaces):
            return Response()
        # 根据namespace和realtime namespace进行删除或创建
        # 删除命名空间
        delete_ns_name_list = set(
            cc_namespace_name_id.keys()) - set(namespace_name_list)
        delete_ns_id_list = [
            cc_namespace_name_id[name] for name in delete_ns_name_list
        ]
        self.delete_cc_ns(request, project_id, cluster_id, delete_ns_id_list)

        # 添加命名空间
        add_ns_name_list = set(namespace_name_list) - set(
            cc_namespace_name_id.keys())
        self.add_cc_ns(request, project_id, cluster_id, add_ns_name_list)

        return Response()
Example #6
0
 def list_by_cluster_id(self, request, project_id_or_code, cluster_id):
     namespaces = ns_utils.get_namespaces_by_cluster_id(
         request.user.token.access_token, request.project.project_id,
         cluster_id)
     return Response(namespaces)
Example #7
0
 def list_by_cluster_id(self, request, project_id, cluster_id):
     namespaces = get_namespaces_by_cluster_id(request.user.token.access_token, project_id, cluster_id)
     return Response(namespaces)