Example #1
0
    def get_taskgroup(self, request, project_id, lb_id):
        # 判断 lb 的状态
        validate_res, validate_msg = self.validate_lb(request,
                                                      project_id,
                                                      lb_id,
                                                      "get_taskgroup",
                                                      is_check_use=False)
        if not validate_res:
            return Response(validate_msg)

        # 获取kind
        flag, project_kind = self.get_project_kind(request, project_id)
        if not flag:
            return project_kind
        # 根据 lb 获取 Deployment的信息
        name = self.lb_data.get("name")
        cluster_id = self.lb_data.get('cluster_id')
        data_dict = self.lb_data.get('data_dict')
        # 查询 namespace
        if data_dict:
            data_dict = json.loads(data_dict)
        else:
            data_dict = {}
        namespace = get_namespace_name(request.user.token.access_token,
                                       project_id, data_dict)
        # 获取taskagroup或者group
        field = self.get_filed(project_kind)
        rc_names = self.get_rc_name_by_deployment_base(
            request,
            project_id,
            cluster_id,
            name,
            project_kind=project_kind,
            namespace=namespace)
        rc_names = list(set(rc_names)) or ["None"]
        flag, resp = self.get_pod_or_taskgroup(
            request,
            project_id,
            cluster_id,
            field=field,
            app_name=",".join(rc_names),
            ns_name=namespace,
        )
        if not flag:
            return resp
        if project_kind == 2:
            ret_data = self.get_task_group_info_base(resp, namespace=namespace)
        else:
            ret_data = []
        # 处理数据方便前台使用
        return APIResponse({"data": ret_data})
Example #2
0
    def delete_by_bcs(self, request, project_id, lb_id):
        # 判断 lb 的状态
        validate_res, validate_msg = self.validate_lb(request, project_id,
                                                      lb_id, "delete_by_bcs")
        if not validate_res:
            return Response(validate_msg)
        # 根据 lb 获取 Deployment的信息
        lb_name = self.lb_data.get("name")
        cluster_id = self.lb_data.get('cluster_id')

        # 查询 namespace
        data_dict = self.lb_data.get('data_dict')
        if data_dict:
            data_dict = json.loads(data_dict)
        else:
            data_dict = {}
        # 兼容已存在的数据
        namespace = get_namespace_name(request.user.token.access_token,
                                       project_id, data_dict)

        username = request.user.username
        access_token = request.user.token.access_token
        # enforce delete flag, 1: enforce delete 0: not enforce
        enforce = request.query_params.get('enforce', 0)
        with activity_client.ContextActivityLogClient(
                project_id=project_id,
                user=username,
                resource_type="lb",
                resource=lb_name,
                resource_id=lb_id,
                extra=json.dumps(self.lb_data)).log_stop() as ual_client:
            resust = delete_lb_by_bcs(access_token,
                                      project_id,
                                      cluster_id,
                                      namespace,
                                      lb_name,
                                      lb_id,
                                      enforce=enforce)
            ual_client.update_log(
                activity_status='succeed'
                if resust.get('result') else 'failed',
                description=_("停止LoadBalance:{}").format(
                    resust.get('message')),
            )
        return Response(resust)