def create_flow(self, request, project_id, data, perm): access_token = request.user.token.access_token project_kind = request.project.kind project_code = request.project.english_name ns_name = data['name'] cluster_id = data['cluster_id'] if ClusterType.get(project_kind) == 'Kubernetes': # k8s 集群需要调用 bcs api 初始化数据 self.init_namespace_by_bcs(access_token, project_id, project_code, data) has_image_secret = None else: self.init_mesos_ns_by_bcs(access_token, project_id, project_code, cluster_id, ns_name) has_image_secret = True result = paas_cc.create_namespace( access_token, project_id, cluster_id, ns_name, None, # description 现在没有用到 request.user.username, data['env_type'], has_image_secret, ) if result.get('code') != 0: if ClusterType.get(project_kind) != 'Kubernetes': self.delete_secret_for_mesos(access_token, project_id, cluster_id, ns_name) if 'Duplicate entry' in result.get('message', ''): message = _("创建失败,namespace名称已经在其他项目存在") else: message = result.get('message', '') return response.Response({ 'code': result['code'], 'data': None, 'message': message }) else: # 注册资源到权限中心 perm.register(result['data']['id'], f'{ns_name}({cluster_id})') # 创建成功后需要保存变量信息 result_data = result.get('data') if data.get('ns_vars') and result_data: ns_id = result_data.get('id') res, not_exist_vars = NameSpaceVariable.batch_save( ns_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ f'{i["key"]}[id:{i["id"]}]' for i in not_exist_vars ] result['message'] = _("以下变量不存在:{}").format( ';'.join(not_exist_show_msg)) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( ns_id, project_id) return result
def get_real_category(project_kind, category): if category not in CATE_SHOW_NAME.values(): return category project_kind_name = ClusterType.get(project_kind) if project_kind_name == 'Kubernetes': category = 'K8s%s' % category elif project_kind_name == 'Mesos': category = category.lower() return category
def get(self, request, project_id): """获取项目下的所有Ingress """ project_kind = request.project.kind project_kind_name = ClusterType.get(project_kind) if project_kind_name != 'Kubernetes': raise error_codes.CheckFailed.f("K8S项目才有Ingress", replace=True) cluster_dicts = self.get_project_cluster_info(request, project_id) cluster_data = cluster_dicts.get('results', {}) or {} # 获取命名空间的id namespace_dict = app_utils.get_ns_id_map( request.user.token.access_token, project_id) s_cate = 'K8sIngress' is_decode = False params = {} access_token = request.user.token.access_token data = [] for cluster_info in cluster_data: cluster_id = cluster_info.get('cluster_id') cluster_env = cluster_info.get('environment') code, cluster_data = self.get_ingress_by_cluser_id( request, params, project_id, cluster_id) # 单个集群错误时,不抛出异常信息 if code != ErrorCode.NoError: continue self.handle_data(request, cluster_data, project_kind, s_cate, access_token, project_id, cluster_id, is_decode, cluster_env, cluster_info.get('name', ''), namespace_dict=namespace_dict) data += cluster_data # 按时间倒序排列 data.sort(key=lambda x: x.get('createTime', ''), reverse=True) return APIResponse({ "code": ErrorCode.NoError, "data": { "data": data, "length": len(data) }, "message": "ok" })
def create(self, request, project_id, is_validate_perm=True): """新建命名空间 k8s 流程:新建namespace配置文件并下发 -> 新建包含仓库账号信息的sercret配置文件并下发 -> 在paas-cc上注册 mesos流程:新建包含仓库账号信息的sercret配置文件并下发 -> 在paas-cc上注册 """ serializer = NamespaceSLZ(data=request.data, context={ 'request': request, 'project_id': project_id }) serializer.is_valid(raise_exception=True) data = serializer.data # 判断权限 perm = bcs_perm.Namespace(request, project_id, bcs_perm.NO_RES, data['cluster_id']) perm.can_create(raise_exception=is_validate_perm) data = serializer.data access_token = request.user.token.access_token project_kind = request.project.kind project_code = request.project.english_name if ClusterType.get(project_kind) == 'Kubernetes': # k8s 集群需要调用 bcs api 初始化数据 self.init_namespace_by_bcs(access_token, project_id, project_code, data) has_image_secret = None else: ns_name = data['name'] cluster_id = data['cluster_id'] self.init_mesos_ns_by_bcs(access_token, project_id, project_code, cluster_id, ns_name) has_image_secret = True result = paas_cc.create_namespace( access_token, project_id, data['cluster_id'], data['name'], None, # description 现在没有用到 request.user.username, data['env_type'], has_image_secret) if result.get('code') != 0: if ClusterType.get(project_kind) != 'Kubernetes': self.delete_secret_for_mesos(access_token, project_id, cluster_id, ns_name) if 'Duplicate entry' in result.get('message', ''): message = "创建失败,namespace名称已经在其他项目存在" else: message = result.get('message', '') return response.Response({ 'code': result['code'], 'data': None, 'message': message }) else: # 注册资源到权限中心 perm.register(result['data']['id'], result['data']['name']) # 创建成功后需要保存变量信息 result_data = result.get('data') if data.get('ns_vars') and result_data: ns_id = result_data.get('id') res, not_exist_vars = NameSpaceVariable.batch_save( ns_id, data['ns_vars']) if not_exist_vars: not_exist_show_msg = [ '%s[id:%s]' % (i['key'], i['id']) for i in not_exist_vars ] result['message'] = u"以下变量不存在:%s" % ";".join( not_exist_show_msg) result['data']['ns_vars'] = NameSpaceVariable.get_ns_vars( ns_id, project_id) return response.Response(result)