| @@ -7,4 +7,7 @@ public class Constant { | |||||
| public final static int State_valid = 1; // 有效 | public final static int State_valid = 1; // 有效 | ||||
| public final static int State_invalid = 0; // 无效 | public final static int State_invalid = 0; // 无效 | ||||
| public final static int Used_State_used = 1; // 已占用 | |||||
| public final static int Used_State_unused = 0; // 未占用 | |||||
| } | } | ||||
| @@ -46,7 +46,11 @@ private Integer id; | |||||
| @ApiModelProperty(value = "状态标识", notes = "0表示失效,1表示生效") | @ApiModelProperty(value = "状态标识", notes = "0表示失效,1表示生效") | ||||
| private Integer state; | private Integer state; | ||||
| @ApiModelProperty(value = "占用情况(1-占用,0-未占用)") | |||||
| private Integer usedState; | |||||
| @ApiModelProperty(value = "节点") | |||||
| private String node; | |||||
| public Integer getId() { | public Integer getId() { | ||||
| return id; | return id; | ||||
| @@ -122,5 +126,20 @@ private Integer id; | |||||
| this.state = state; | this.state = state; | ||||
| } | } | ||||
| public Integer getUsedState() { | |||||
| return usedState; | |||||
| } | |||||
| public void setUsedState(Integer usedState) { | |||||
| this.usedState = usedState; | |||||
| } | |||||
| public String getNode() { | |||||
| return node; | |||||
| } | |||||
| public void setNode(String node) { | |||||
| this.node = node; | |||||
| } | |||||
| } | } | ||||
| @@ -73,6 +73,8 @@ public interface ComputingResourceDao { | |||||
| */ | */ | ||||
| int update(@Param("computingResource") ComputingResource computingResource); | int update(@Param("computingResource") ComputingResource computingResource); | ||||
| int updateUsedStateByNode(@Param("node")String node, @Param("usedState") Integer usedState); | |||||
| /** | /** | ||||
| * 通过主键删除数据 | * 通过主键删除数据 | ||||
| * | * | ||||
| @@ -2,8 +2,10 @@ package com.ruoyi.platform.service.impl; | |||||
| import com.ruoyi.common.redis.service.RedisService; | import com.ruoyi.common.redis.service.RedisService; | ||||
| import com.ruoyi.common.security.utils.SecurityUtils; | import com.ruoyi.common.security.utils.SecurityUtils; | ||||
| import com.ruoyi.platform.constant.Constant; | |||||
| import com.ruoyi.platform.domain.DevEnvironment; | import com.ruoyi.platform.domain.DevEnvironment; | ||||
| import com.ruoyi.platform.domain.PodStatus; | import com.ruoyi.platform.domain.PodStatus; | ||||
| import com.ruoyi.platform.mapper.ComputingResourceDao; | |||||
| import com.ruoyi.platform.mapper.DevEnvironmentDao; | import com.ruoyi.platform.mapper.DevEnvironmentDao; | ||||
| import com.ruoyi.platform.service.DevEnvironmentService; | import com.ruoyi.platform.service.DevEnvironmentService; | ||||
| import com.ruoyi.platform.service.JupyterService; | import com.ruoyi.platform.service.JupyterService; | ||||
| @@ -54,6 +56,9 @@ public class JupyterServiceImpl implements JupyterService { | |||||
| @Resource | @Resource | ||||
| private DevEnvironmentDao devEnvironmentDao; | private DevEnvironmentDao devEnvironmentDao; | ||||
| @Resource | |||||
| private ComputingResourceDao computingResourceDao; | |||||
| @Resource | @Resource | ||||
| @Lazy | @Lazy | ||||
| private DevEnvironmentService devEnvironmentService; | private DevEnvironmentService devEnvironmentService; | ||||
| @@ -126,6 +131,9 @@ public class JupyterServiceImpl implements JupyterService { | |||||
| if(pod == null){ | if(pod == null){ | ||||
| return "pod不存在!"; | return "pod不存在!"; | ||||
| } | } | ||||
| computingResourceDao.updateUsedStateByNode(pod.getSpec().getNodeName(), Constant.Used_State_unused); | |||||
| // 使用 Kubernetes API 删除 Pod | // 使用 Kubernetes API 删除 Pod | ||||
| String deleteResult = k8sClientUtil.deletePod(podName, namespace); | String deleteResult = k8sClientUtil.deletePod(podName, namespace); | ||||
| // 删除service | // 删除service | ||||
| @@ -1,6 +1,8 @@ | |||||
| package com.ruoyi.platform.utils; | package com.ruoyi.platform.utils; | ||||
| import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken; | import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken; | ||||
| import com.ruoyi.platform.constant.Constant; | |||||
| import com.ruoyi.platform.mapper.ComputingResourceDao; | |||||
| import io.kubernetes.client.Exec; | import io.kubernetes.client.Exec; | ||||
| import io.kubernetes.client.custom.IntOrString; | import io.kubernetes.client.custom.IntOrString; | ||||
| import io.kubernetes.client.custom.Quantity; | import io.kubernetes.client.custom.Quantity; | ||||
| @@ -20,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; | |||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import javax.annotation.PostConstruct; | import javax.annotation.PostConstruct; | ||||
| import javax.annotation.Resource; | |||||
| import java.io.BufferedReader; | import java.io.BufferedReader; | ||||
| import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
| import java.util.*; | import java.util.*; | ||||
| @@ -40,6 +43,8 @@ public class K8sClientUtil { | |||||
| */ | */ | ||||
| private static ApiClient apiClient; | private static ApiClient apiClient; | ||||
| @Resource | |||||
| private ComputingResourceDao computingResourceDao; | |||||
| /** | /** | ||||
| * 构建集群POD内通过SA访问的客户端 | * 构建集群POD内通过SA访问的客户端 | ||||
| * loading the in-cluster config, including: | * loading the in-cluster config, including: | ||||
| @@ -409,8 +414,6 @@ public class K8sClientUtil { | |||||
| .namespaces(Collections.singletonList(namespace)) | .namespaces(Collections.singletonList(namespace)) | ||||
| .topologyKey("kubernetes.io/hostname"); | .topologyKey("kubernetes.io/hostname"); | ||||
| // V1WeightedPodAffinityTerm weightedPodAffinityTerm = new V1WeightedPodAffinityTerm().weight(100).podAffinityTerm(podAffinityTerm); | |||||
| V1PodAntiAffinity podAntiAffinity = new V1PodAntiAffinity() | V1PodAntiAffinity podAntiAffinity = new V1PodAntiAffinity() | ||||
| .requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(antiPodAffinityTerm)); | .requiredDuringSchedulingIgnoredDuringExecution(Collections.singletonList(antiPodAffinityTerm)); | ||||
| @@ -469,6 +472,8 @@ public class K8sClientUtil { | |||||
| try { | try { | ||||
| pod = api.createNamespacedPod(namespace, pod, null, null, null); | pod = api.createNamespacedPod(namespace, pod, null, null, null); | ||||
| String nodeName = getNodeName(podName, namespace); | |||||
| computingResourceDao.updateUsedStateByNode(nodeName, Constant.Used_State_used); | |||||
| } catch (ApiException e) { | } catch (ApiException e) { | ||||
| log.error("创建pod异常:" + e.getResponseBody(), e); | log.error("创建pod异常:" + e.getResponseBody(), e); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| @@ -569,6 +574,12 @@ public class K8sClientUtil { | |||||
| return pod.getStatus().getHostIP(); | return pod.getStatus().getHostIP(); | ||||
| } | } | ||||
| public String getNodeName(String podName, String namespace) throws Exception { | |||||
| CoreV1Api api = new CoreV1Api(apiClient); | |||||
| V1Pod pod = api.readNamespacedPod(podName, namespace, null, null, null); | |||||
| return pod.getSpec().getNodeName(); | |||||
| } | |||||
| public String getPodLogs(String podName,String namespace,String container,int line) { | public String getPodLogs(String podName,String namespace,String container,int line) { | ||||
| CoreV1Api api = new CoreV1Api(apiClient); | CoreV1Api api = new CoreV1Api(apiClient); | ||||
| try { | try { | ||||
| @@ -156,6 +156,10 @@ computing_resource = values(computing_resource)standard = values(standard)descri | |||||
| where id = #{computingResource.id} | where id = #{computingResource.id} | ||||
| </update> | </update> | ||||
| <update id="updateUsedStateByNode"> | |||||
| update computing_resource set used_state = #{usedState} where node = #{node} | |||||
| </update> | |||||
| <!--通过主键删除--> | <!--通过主键删除--> | ||||
| <delete id="deleteById"> | <delete id="deleteById"> | ||||
| delete from computing_resource where id = #{id} | delete from computing_resource where id = #{id} | ||||