def delete_instance( self, request, project_id, cluster_id, ns, instance_name, category="application", kind=2, inst_id_list=None, enforce=0, ): """删除instance""" if kind == 2: client = MesosClient(request.user.token.access_token, project_id, cluster_id, None) if category == "application": resp = client.delete_mesos_app_instance(ns, instance_name, enforce=enforce) elif category == "deployment": resp = client.delete_deployment(ns, instance_name, enforce=enforce) elif category == "secret": resp = client.delete_secret(ns, instance_name) elif category == "configmap": resp = client.delete_configmap(ns, instance_name) else: resp = client.delete_service(ns, instance_name) if inst_id_list: delete_instance_task.delay(request.user.token.access_token, inst_id_list, kind) else: client = K8SClient(request.user.token.access_token, project_id, cluster_id, None) # deployment 需要级联删除 res\pod; daemonset/job/statefulset 需要级联删除 pod if FUNC_MAP[category] in ['%s_deployment', '%s_daemonset', '%s_job', '%s_statefulset']: fun_prefix = 'deep_delete' else: fun_prefix = 'delete' curr_func = getattr(client, FUNC_MAP[category] % fun_prefix) resp = curr_func(ns, instance_name) # 级联删除,会返回空 if resp is None: # 启动后台任务,轮训任务状态 if inst_id_list: delete_instance_task.delay(request.user.token.access_token, inst_id_list, kind) return APIResponse({"code": ErrorCode.NoError, "message": _("删除成功")}) # response msg = resp.get("message") # message中有not found或者node does not exist时,认为已经删除成功 # 状态码为正常或者满足不存在条件时,认为已经删除成功 if ( ( resp.get("code") in [ErrorCode.NoError, ErrorCode.MesosDeploymentNotFound, ErrorCode.MesosApplicationNotFound] ) or ("not found" in msg) or ("node does not exist" in msg) ): return APIResponse({"code": ErrorCode.NoError, "message": _("删除成功")}) return APIResponse({"code": resp.get("code"), "message": msg})
def delete_instance(self, request, project_id, cluster_id, ns, instance_name, category="application", kind=2, inst_id_list=None, enforce=0): """删除instance """ if kind == 2: client = MesosClient(request.user.token.access_token, project_id, cluster_id, None) if category == "application": resp = client.delete_mesos_app_instance(ns, instance_name, enforce=enforce) elif category == "deployment": resp = client.delete_deployment(ns, instance_name, enforce=enforce) elif category == "secret": resp = client.delete_secret(ns, instance_name) elif category == "configmap": resp = client.delete_configmap(ns, instance_name) else: resp = client.delete_service(ns, instance_name) if inst_id_list: delete_instance_task.delay(request.user.token.access_token, inst_id_list, kind) else: client = K8SClient(request.user.token.access_token, project_id, cluster_id, None) # deployment 需要级联删除 res\pod; daemonset/job/statefulset 需要级联删除 pod if FUNC_MAP[category] in [ '%s_deployment', '%s_daemonset', '%s_job', '%s_statefulset' ]: fun_prefix = 'deep_delete' else: fun_prefix = 'delete' curr_func = getattr(client, FUNC_MAP[category] % fun_prefix) resp = curr_func(ns, instance_name) # 级联删除,会返回空 if resp is None: # 启动后台任务,轮训任务状态 if inst_id_list: delete_instance_task.delay(request.user.token.access_token, inst_id_list, kind) return APIResponse({ "code": ErrorCode.NoError, "message": _("删除成功") }) curr_msg = resp.get("message") if "not found" in curr_msg or "node does not exist" in curr_msg: return APIResponse({ "code": ErrorCode.NoError, "message": _("删除成功") }) if resp.get("code") != ErrorCode.NoError: return APIResponse({ "code": resp.get("code") or DEFAULT_ERROR_CODE, "message": curr_msg }) return APIResponse({"code": resp.get("code"), "message": curr_msg})