def register_default_ns(access_token, username, project_id, project_code, cluster_id): """注册默认的命名空间(针对k8s集群) 1. 创建存储镜像账号的secret 2. 将 default 命名空间注册到paas_cc 上 project_code = request.project.english_name """ # 组装创建ns的数据 data = {'env_type': 'dev', 'name': 'default', 'cluster_id': cluster_id} ns_base = NamespaceBase() # 1. 创建存储镜像账号的secret client = K8SClient(access_token, project_id, data['cluster_id'], env=None) ns_base.create_jfrog_secret(client, access_token, project_id, project_code, data) # 2. 将 default 命名空间注册到paas_cc 上 result = paas_cc.create_namespace(access_token, project_id, data['cluster_id'], data['name'], None, username, data['env_type']) if result.get('code') != 0: if 'Duplicate entry' in result.get('message', ''): message = _("创建失败,namespace名称已经在其他项目存在") else: message = result.get('message', '') return False, message # 注册资源到权限中心 request = RequestClass(username=username, access_token=access_token, project_code=project_code) perm = bcs_perm.Namespace(request, project_id, bcs_perm.NO_RES, data['cluster_id']) perm.register(str(result['data']['id']), result['data']['name']) return True, _("命名空间[default]注册成功")
def _delete_old_init_templates(template_qsets, project_id, project_code, access_token, username): request = RequestClass(username=username, access_token=access_token, project_code=project_code) for template in template_qsets: template_id = template.id perm = bcs_perm.Templates(request, project_id, template_id, template.name) perm.can_delete(raise_exception=True) perm.delete() VersionedEntity.objects.filter(template_id=template_id).delete() ShowVersion.objects.filter(template_id=template_id, name='init_version').delete() template.name = f'[deleted_{int(time.time())}]{template.name}' template.is_deleted = True template.deleted_time = timezone.now() template.save(update_fields=['name', 'is_deleted', 'deleted_time'])
def init_template(project_id, project_code, project_kind, access_token, username): """创建项目时,初始化示例模板集 request.project.english_name """ # 判断模板集是否已经创建, 如果已经创建, 删除旧模板 exit_templates = Template.objects.filter(project_id=project_id, name=TEMPLATE_NAME) if exit_templates.exists(): _delete_old_init_templates(exit_templates, project_id, project_code, access_token, username) template_data = {} if project_kind == K8S_KIND: template_data = K8S_TEMPLATE.get('data', {}) elif project_kind == MESOS_KIND: template_data = MESOS_TEMPLATE.get('data', {}) logger.info(f'init_template [begin] project_id: {project_id}, project_kind: {ClusterType.get(project_kind)}') # 新建模板集 init_template = Template.objects.create( project_id=project_id, name=TEMPLATE_NAME, desc=TEMPLATE_DESC, creator=username, updator=username, ) new_entity = {} for cate in template_data: new_item_id_list = [] data_list = template_data[cate] for _data in data_list: _save_data = {} for _d_key in _data: # 新建,忽略 id 字段 if _d_key == 'id': continue # 目前只有 dict、list这两类非字符格式 _d_value = _data[_d_key] if isinstance(_d_value, list) or isinstance(_d_value, dict): _save_data[_d_key] = json.dumps(_d_value) else: _save_data[_d_key] = _d_value _ins = MODULE_DICT.get(cate).objects.create(**_save_data) new_item_id_list.append(str(_ins.id)) new_entity[cate] = ','.join(new_item_id_list) # 新建version new_ver = VersionedEntity.objects.create( template_id=init_template.id, entity=json.dumps(new_entity), version=get_default_version(), creator=username, updator=username, ) # 新建可见版本 ShowVersion.objects.create( template_id=init_template.id, real_version_id=new_ver.id, name='init_version', ) # 将模板集注册到权限中心 request = RequestClass(username=username, access_token=access_token, project_code=project_code) perm = bcs_perm.Templates(request, project_id, bcs_perm.NO_RES) perm.register(str(init_template.id), str(init_template.name)) logger.info(f'init_template [end] project_id: {project_id}, project_kind: {ClusterType.get(project_kind)}') return