|
|
|
@@ -1,5 +1,6 @@ |
|
|
|
package com.ruoyi.platform.utils; |
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON; |
|
|
|
import com.ruoyi.platform.constant.Constant; |
|
|
|
import com.ruoyi.platform.domain.DevEnvironment; |
|
|
|
import com.ruoyi.platform.mapper.ComputingResourceDao; |
|
|
|
@@ -8,6 +9,7 @@ import io.kubernetes.client.custom.IntOrString; |
|
|
|
import io.kubernetes.client.custom.Quantity; |
|
|
|
import io.kubernetes.client.openapi.ApiClient; |
|
|
|
import io.kubernetes.client.openapi.ApiException; |
|
|
|
import io.kubernetes.client.openapi.apis.AppsV1Api; |
|
|
|
import io.kubernetes.client.openapi.apis.CoreV1Api; |
|
|
|
import io.kubernetes.client.openapi.models.*; |
|
|
|
import io.kubernetes.client.util.Config; |
|
|
|
@@ -33,8 +35,8 @@ import java.util.*; |
|
|
|
@Component |
|
|
|
public class K8sClientUtil { |
|
|
|
|
|
|
|
// @Value("${jupyter.hostPath}") |
|
|
|
// private String hostPath; |
|
|
|
@Value("${jupyter.hostPath}") |
|
|
|
private String hostPath; |
|
|
|
|
|
|
|
private String http; |
|
|
|
private String token; |
|
|
|
@@ -314,8 +316,8 @@ public class K8sClientUtil { |
|
|
|
* @param image 镜像 |
|
|
|
* @return 创建成功的pod,的nodePort端口 |
|
|
|
*/ |
|
|
|
public Integer createPodWithSubPath(String podName, String namespace, Integer port, String mountPath, String subPath, String pvcName, String image) { |
|
|
|
// public Integer createPodWithSubPath(String podName, String namespace, Integer port, String mountPath, String subPath, String image) { |
|
|
|
// public Integer createPodWithSubPath(String podName, String namespace, Integer port, String mountPath, String subPath, String pvcName, String image) { |
|
|
|
public Integer createPodWithSubPath(String podName, String namespace, Integer port, String mountPath, String subPath, String image) { |
|
|
|
|
|
|
|
Map<String, String> selector = new LinkedHashMap<>(); |
|
|
|
selector.put("k8s-jupyter", podName); |
|
|
|
@@ -339,6 +341,9 @@ public class K8sClientUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int lastIndex = hostPath.lastIndexOf('/'); |
|
|
|
String newPath = hostPath.substring(0, lastIndex); |
|
|
|
|
|
|
|
V1Pod pod = new V1PodBuilder() |
|
|
|
.withNewMetadata() |
|
|
|
.withName(podName) |
|
|
|
@@ -366,8 +371,8 @@ public class K8sClientUtil { |
|
|
|
.endContainer() |
|
|
|
.addNewVolume() |
|
|
|
.withName("workspace") |
|
|
|
// .withHostPath(new V1HostPathVolumeSource().path(hostPath).type("DirectoryOrCreate")) |
|
|
|
.withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) |
|
|
|
.withHostPath(new V1HostPathVolumeSource().path(newPath).type("DirectoryOrCreate")) |
|
|
|
// .withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) |
|
|
|
.endVolume() |
|
|
|
.withTerminationGracePeriodSeconds(14400L) |
|
|
|
.endSpec() |
|
|
|
@@ -449,46 +454,29 @@ public class K8sClientUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 配置卷和卷挂载 |
|
|
|
// List<V1VolumeMount> volumeMounts = new ArrayList<>(); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("workspace").mountPath("/opt/notebooks")); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("data").mountPath("/opt/dataset").subPath(datasetPath).readOnly(true)); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("data").mountPath("/opt/model").subPath(modelPath).readOnly(true)); |
|
|
|
// |
|
|
|
// List<V1Volume> volumes = new ArrayList<>(); |
|
|
|
// volumes.add(new V1Volume().name("workspace").hostPath(new V1HostPathVolumeSource().path(hostPath + "/" + podName + "/notebooks").type("DirectoryOrCreate"))); |
|
|
|
// volumes.add(new V1Volume().name("data").hostPath(new V1HostPathVolumeSource().path(hostPath).type("DirectoryOrCreate"))); |
|
|
|
|
|
|
|
// 配置卷和卷挂载 |
|
|
|
List<V1VolumeMount> volumeMounts = new ArrayList<>(); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("workspace").mountPath(mountPath)); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/data").subPath(datasetPath).readOnly(true)); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/model").subPath(modelPath).readOnly(true)); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("workspace").mountPath("/opt/notebooks")); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("data").mountPath("/opt/dataset").subPath(datasetPath).readOnly(true)); |
|
|
|
volumeMounts.add(new V1VolumeMount().name("data").mountPath("/opt/model").subPath(modelPath).readOnly(true)); |
|
|
|
|
|
|
|
List<V1Volume> volumes = new ArrayList<>(); |
|
|
|
volumes.add(new V1Volume().name("workspace").persistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvc.getMetadata().getName()))); |
|
|
|
volumes.add(new V1Volume().name("minio-pvc").persistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(dataPvcName))); |
|
|
|
|
|
|
|
volumes.add(new V1Volume().name("workspace").hostPath(new V1HostPathVolumeSource().path(hostPath + "/" + podName + "/notebooks").type("DirectoryOrCreate"))); |
|
|
|
volumes.add(new V1Volume().name("data").hostPath(new V1HostPathVolumeSource().path(hostPath).type("DirectoryOrCreate"))); |
|
|
|
|
|
|
|
//配置资源 |
|
|
|
JSONObject standardJson = new JSONObject(devEnvironment.getStandard()); |
|
|
|
JSONObject valueJson = (JSONObject) standardJson.get("value"); |
|
|
|
int cpu = (int) valueJson.get("cpu"); |
|
|
|
String memory = (String) valueJson.get("memory"); |
|
|
|
memory = memory.substring(0, memory.length() - 1).concat("i"); |
|
|
|
int gpu = (int) valueJson.get("gpu"); |
|
|
|
// 配置卷和卷挂载 |
|
|
|
// List<V1VolumeMount> volumeMounts = new ArrayList<>(); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("workspace").mountPath(mountPath)); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/data").subPath(datasetPath).readOnly(true)); |
|
|
|
// volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/model").subPath(modelPath).readOnly(true)); |
|
|
|
// |
|
|
|
// List<V1Volume> volumes = new ArrayList<>(); |
|
|
|
// volumes.add(new V1Volume().name("workspace").persistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvc.getMetadata().getName()))); |
|
|
|
// volumes.add(new V1Volume().name("minio-pvc").persistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(dataPvcName))); |
|
|
|
|
|
|
|
HashMap<String, Quantity> limitMap = new HashMap<>(); |
|
|
|
if (Constant.Computing_Resource_GPU.equals(devEnvironment.getComputingResource())) { |
|
|
|
limitMap.put("nvidia.com/gpu", new Quantity(String.valueOf(gpu))); |
|
|
|
} |
|
|
|
limitMap.put("cpu", new Quantity(String.valueOf(cpu))); |
|
|
|
limitMap.put("memory", new Quantity(memory)); |
|
|
|
limitMap.put("ephemeral-storage", new Quantity("100Gi")); |
|
|
|
|
|
|
|
V1ResourceRequirements v1ResourceRequirements = new V1ResourceRequirements(); |
|
|
|
v1ResourceRequirements.setRequests(limitMap); |
|
|
|
v1ResourceRequirements.setLimits(limitMap); |
|
|
|
//配置资源 |
|
|
|
V1ResourceRequirements v1ResourceRequirements = setPodResource(devEnvironment.getStandard()); |
|
|
|
|
|
|
|
V1Pod pod = new V1PodBuilder() |
|
|
|
.withNewMetadata() |
|
|
|
@@ -688,6 +676,30 @@ public class K8sClientUtil { |
|
|
|
return pod; |
|
|
|
} |
|
|
|
|
|
|
|
V1ResourceRequirements setPodResource(String resource) { |
|
|
|
//配置pod资源 |
|
|
|
JSONObject standardJson = new JSONObject(resource); |
|
|
|
JSONObject valueJson = (JSONObject) standardJson.get("value"); |
|
|
|
int cpu = (int) valueJson.get("cpu"); |
|
|
|
String memory = (String) valueJson.get("memory"); |
|
|
|
memory = memory.substring(0, memory.length() - 1).concat("i"); |
|
|
|
Integer gpu = (Integer) valueJson.get("gpu"); |
|
|
|
|
|
|
|
HashMap<String, Quantity> limitMap = new HashMap<>(); |
|
|
|
if (gpu != null && gpu != 0) { |
|
|
|
limitMap.put("nvidia.com/gpu", new Quantity(String.valueOf(gpu))); |
|
|
|
} |
|
|
|
limitMap.put("cpu", new Quantity(String.valueOf(cpu))); |
|
|
|
limitMap.put("memory", new Quantity(memory)); |
|
|
|
limitMap.put("ephemeral-storage", new Quantity("100Gi")); |
|
|
|
|
|
|
|
V1ResourceRequirements v1ResourceRequirements = new V1ResourceRequirements(); |
|
|
|
v1ResourceRequirements.setRequests(limitMap); |
|
|
|
v1ResourceRequirements.setLimits(limitMap); |
|
|
|
|
|
|
|
return v1ResourceRequirements; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 删除 Pod |
|
|
|
* |
|
|
|
|