fix:开发环境删除pod同时删除svc

This commit is contained in:
西大锐 2024-07-18 21:15:35 +08:00
parent b544f9afbf
commit 9f2d2be545
2 changed files with 25 additions and 3 deletions

View File

@ -101,7 +101,6 @@ public class JupyterServiceImpl implements JupyterService {
// 调用修改后的 createPod 方法传入额外的参数
Integer podPort = k8sClientUtil.createConfiguredPod(podName, namespace, port, mountPath, pvc, image, minioPvcName, datasetPath, modelPath);
String url = masterIp + ":" + podPort;
redisService.setCacheObject(podName,masterIp + ":" + podPort);
devEnvironment.setStatus("Pending");
@ -119,8 +118,9 @@ public class JupyterServiceImpl implements JupyterService {
throw new Exception("开发环境配置不存在");
}
LoginUser loginUser = SecurityUtils.getLoginUser();
//手动构造pod名称
//构造pod和svc名称
String podName = loginUser.getUsername().toLowerCase() +"-editor-pod" + "-" + id;
String svcName = loginUser.getUsername().toLowerCase() + "-editor-pod" + "-" + id + "-svc";
//得到pod
V1Pod pod = k8sClientUtil.getNSPodList(namespace, podName);
if(pod == null){
@ -128,10 +128,13 @@ public class JupyterServiceImpl implements JupyterService {
}
// 使用 Kubernetes API 删除 Pod
String deleteResult = k8sClientUtil.deletePod(podName, namespace);
// 删除service
k8sClientUtil.deleteService(svcName, namespace);
devEnvironment.setStatus("Terminated");
this.devEnvironmentService.update(devEnvironment);
return deleteResult + ",编辑器已停止";
}
@Override

View File

@ -423,7 +423,6 @@ public class K8sClientUtil {
.withVolumeMounts(volumeMounts)
.endContainer()
.withVolumes(volumes)
.withTerminationGracePeriodSeconds(14400L)
.endSpec()
.build();
@ -578,6 +577,26 @@ public class K8sClientUtil {
}
}
/**
* 删除 Service
*
* @param svcName Service 名称
* @param namespace 命名空间
* @throws ApiException 异常
*/
public String deleteService(String svcName, String namespace) throws ApiException {
CoreV1Api api = new CoreV1Api(apiClient);
try {
V1Status result = api.deleteNamespacedService(svcName, namespace, null, null, null, null, null, null);
return "Service " + svcName + " 删除请求已发送";
} catch (ApiException e) {
log.error("删除service异常:" + e.getResponseBody(), e);
throw e;
}
}
/**
* 检查 Pod 是否存在
*