# 单位转换函数 def convert_unit(value): if isinstance(value, int): return value if isinstance(value, str): if value.endswith('m'): # CPU millicores return float(value[:-1]) / 1000 elif value.endswith('Gi'): # Memory Gi return float(value[:-2]) elif value.endswith('Mi'): # Memory Mi -> Gi return float(value[:-2]) / 1000 return float(value) async def delete_cpcpodyaml(params_kw={}): ns = { 'id':params_kw['id'], } # type参数:更新和新增都是为apply,删除为delete yamlconfig_id = params_kw.get('id') if not yamlconfig_id: return {'status': False, 'msg': '无资源实例配置ID'} db = DBPools() # dbname = await rfexe('get_module_dbname', 'cpcc') dbname = 'kboss' async with db.sqlorContext(dbname) as sor: # 成功完成k8s资源级联删除后才可以删除数据库记录 yaml_configs = await sor.R('yaml_config', {'id':yamlconfig_id}) if len(yaml_configs) < 1: e = Exception(f'yaml_config {yamlconfig_id=} not exists') exception(f'{e}') raise e yaml_config = yaml_configs[0] source_podengine = yaml_config.get("source_podengine") clusterid = yaml_config.get("clusterid") cpcid = yaml_config.get("cpcid") cpu_model = yaml_config.get("cpu_model") gpu_model = yaml_config.get("gpu_model") disk_sys_limit = yaml_config.get("disk_sys_limit") source_storagelimits = yaml_config.get("source_storagelimits") source_cpurate = yaml_config.get("source_cpurate") source_memrate = yaml_config.get("source_memrate") source_gpu = yaml_config.get("source_gpu") debug(f'===== delete {yamlconfig_id=}, {cpu_model=}, {gpu_model=} {disk_sys_limit=} {source_storagelimits=} {source_cpurate=} {source_memrate=} {source_gpu}') cpclusters = await sor.R('cpccluster', {'id':clusterid}) if len(cpclusters) < 1: e = Exception(f'cpclist {clusterid=} not exists') exception(f'{e}') raise e cpcluster = cpclusters[0] # 此处的nodeid即控制节点id kubeconfig = cpcluster.get("kubeconfig") nodeid = cpcluster.get("controllerid") cpcs = await sor.R('cpclist', {'id':cpcid}) if len(cpcs) < 1: e = Exception(f'cpclist {cpcid=} not exists') exception(f'{e}') raise e cpc = cpcs[0] nodes = await sor.R('cpcnode', {'id':nodeid}) # 这里有问题:没有匹配到节点信息!!! if len(nodes) < 1: e = Exception(f'cpcnode {nodeid=} 节点基础信息不匹配') exception(f'{e}') raise e node = nodes[0] url = cpc.pcapi_url + "/pcapi/api/v1/cluster/common/delete_cpcpod" debug(f"请求url: {url=}") debug(f"目标IP认证信息: {node.ip=} {node.sshport=} {node.adminuser=} {node.adminpwd=}") # 请求方式待定,取决于获取参数值方式 orgid = await get_userorgid() if not orgid: orgid = params_kw.get("orgid",'000') debug(f' >>> 当前用户组织ID: {orgid}') if not orgid: return {'status': False, 'msg': '无组织ID'} headers = basic_auth_headers(cpc.api_user, cpc.api_pwd) ns_name = clusterid.replace("_","-") + "-" + orgid # 集群信息+组织信息合成命名空间 ns_name = ns_name.lower() ns_name = ns_name[:-1] + "kube" if ns_name.endswith('-') else ns_name hc = HttpClient() import requests params = { 'host':node.ip, 'port':node.sshport, 'user':node.adminuser, 'password':node.adminpwd, 'kubeconfig':kubeconfig, 'action':'delete', 'namespace_name': ns_name, 'serviceaccount_name': ns_name + "-serviceaccount", 'podcd_name': yamlconfig_id + "-" + source_podengine.lower(), 'service_name': yamlconfig_id + "-service", #取代+ "-" + source_name } params.update(yaml_config) keys_to_remove = ['_webbricks_', 'width', 'height', '_is_mobile'] for key in keys_to_remove: if key in params: del params[key] debug(f"============================== 正在恢复系统盘库存 ==============================") inc_disksys_consumed = - convert_unit(disk_sys_limit) inc_sql_1 = f'''update cpcwidget set consumed = case when cpcid = '{cpcid}' and type = 'disk' and clusterid = '{clusterid}' and model = 'SYS' then greatest(consumed + {inc_disksys_consumed}, 0) else consumed end''' debug(f'库存增加sql-1: {inc_sql_1}') await sor.sqlExe(inc_sql_1, {}) debug(f"============================== 正在恢复数据盘库存 ==============================") inc_diskdata_consumed = - convert_unit(source_storagelimits) inc_sql_2 = f'''update cpcwidget set consumed = case when cpcid = '{cpcid}' and type = 'disk' and clusterid = '{clusterid}' and model = 'DATA' then greatest(consumed + {inc_diskdata_consumed}, 0) else consumed end''' debug(f'库存增加sql-2: {inc_sql_2}') await sor.sqlExe(inc_sql_2, {}) debug(f"============================== 正在恢复CPU库存 ==============================") inc_cpu_consumed = - convert_unit(source_cpurate) inc_sql_3 = f'''update cpcwidget set consumed = case when cpcid = '{cpcid}' and type = 'cpu' and clusterid = '{clusterid}' and model = '{cpu_model}' then greatest(consumed + {inc_cpu_consumed}, 0) else consumed end''' debug(f'库存增加sql-3: {inc_sql_3}') await sor.sqlExe(inc_sql_3, {}) debug(f"============================== 正在恢复内存库存 ==============================") inc_memory_consumed = - convert_unit(source_memrate) inc_sql_4 = f'''update cpcwidget set consumed = case when cpcid = '{cpcid}' and type = 'memory' and clusterid = '{clusterid}' and model = 'STANDARD' then greatest(consumed + {inc_memory_consumed}, 0) else consumed end''' debug(f'库存增加sql-4: {inc_sql_4}') await sor.sqlExe(inc_sql_4, {}) debug(f"============================== 正在恢复GPU库存 ==============================") inc_gpu_consumed = - convert_unit(source_gpu) inc_sql_5 = f'''update cpcwidget set consumed = case when cpcid = '{cpcid}' and type = 'gpu' and clusterid = '{clusterid}' and model = '{gpu_model}' then greatest(consumed + {inc_gpu_consumed}, 0) else consumed end''' debug(f'库存增加sql-5: {inc_sql_5}') await sor.sqlExe(inc_sql_5, {}) debug(f'请求pcapi参数: {params}') # 框架不支持超时时间 await_resp = await async_post( url=url, headers=headers, data=params, timeout=500, verify=False ) status_code = await_resp.get('status_code', 505) if status_code != 200: raise return {'status': False, 'msg': f'算力中心服务操作失败{status_code}'} resp = await_resp.get("content") debug(f'{type(resp)}->{resp}') data = json.loads(resp).get('data') keys_to_remove = ['host', 'port', 'user', 'password', 'kubeconfig'] for key in keys_to_remove: if key in params: del params[key] params['id'] = yamlconfig_id if json.loads(resp).get("status") == True: r = await sor.D('yaml_config', ns) debug('⌛️ 级联删除集群资源成功,资源准备中...'); time.sleep(10) return {'status': True, 'msg': '级联删除集群资源成功,请10秒后查看实时资源实例面板', 'data': ns} raise debug('级联删除集群资源失败'); return {'status': False, 'msg': '删除失败'} ret = await delete_cpcpodyaml(params_kw) return ret