Browse Source

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

pull/106/head
西大锐 1 year ago
parent
commit
9f2d2be545
2 changed files with 25 additions and 3 deletions
  1. +5
    -2
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java
  2. +20
    -1
      ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java

+ 5
- 2
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java View File

@@ -101,7 +101,6 @@ public class JupyterServiceImpl implements JupyterService {


// 调用修改后的 createPod 方法,传入额外的参数 // 调用修改后的 createPod 方法,传入额外的参数
Integer podPort = k8sClientUtil.createConfiguredPod(podName, namespace, port, mountPath, pvc, image, minioPvcName, datasetPath, modelPath); Integer podPort = k8sClientUtil.createConfiguredPod(podName, namespace, port, mountPath, pvc, image, minioPvcName, datasetPath, modelPath);

String url = masterIp + ":" + podPort; String url = masterIp + ":" + podPort;
redisService.setCacheObject(podName,masterIp + ":" + podPort); redisService.setCacheObject(podName,masterIp + ":" + podPort);
devEnvironment.setStatus("Pending"); devEnvironment.setStatus("Pending");
@@ -119,8 +118,9 @@ public class JupyterServiceImpl implements JupyterService {
throw new Exception("开发环境配置不存在"); throw new Exception("开发环境配置不存在");
} }
LoginUser loginUser = SecurityUtils.getLoginUser(); LoginUser loginUser = SecurityUtils.getLoginUser();
//手动构造pod名称
//构造pod和svc名称
String podName = loginUser.getUsername().toLowerCase() +"-editor-pod" + "-" + id; String podName = loginUser.getUsername().toLowerCase() +"-editor-pod" + "-" + id;
String svcName = loginUser.getUsername().toLowerCase() + "-editor-pod" + "-" + id + "-svc";
//得到pod //得到pod
V1Pod pod = k8sClientUtil.getNSPodList(namespace, podName); V1Pod pod = k8sClientUtil.getNSPodList(namespace, podName);
if(pod == null){ if(pod == null){
@@ -128,10 +128,13 @@ public class JupyterServiceImpl implements JupyterService {
} }
// 使用 Kubernetes API 删除 Pod // 使用 Kubernetes API 删除 Pod
String deleteResult = k8sClientUtil.deletePod(podName, namespace); String deleteResult = k8sClientUtil.deletePod(podName, namespace);
// 删除service
k8sClientUtil.deleteService(svcName, namespace);


devEnvironment.setStatus("Terminated"); devEnvironment.setStatus("Terminated");
this.devEnvironmentService.update(devEnvironment); this.devEnvironmentService.update(devEnvironment);
return deleteResult + ",编辑器已停止"; return deleteResult + ",编辑器已停止";

} }


@Override @Override


+ 20
- 1
ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/K8sClientUtil.java View File

@@ -423,7 +423,6 @@ public class K8sClientUtil {
.withVolumeMounts(volumeMounts) .withVolumeMounts(volumeMounts)
.endContainer() .endContainer()
.withVolumes(volumes) .withVolumes(volumes)
.withTerminationGracePeriodSeconds(14400L)
.endSpec() .endSpec()
.build(); .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 是否存在 * 检查 Pod 是否存在
* *


Loading…
Cancel
Save