| @@ -19,8 +19,6 @@ import javax.annotation.Resource; | |||||
| @Api("TensorBoard管理") | @Api("TensorBoard管理") | ||||
| public class TensorBoardController extends BaseController { | public class TensorBoardController extends BaseController { | ||||
| //状态查询接口 | |||||
| @Resource | @Resource | ||||
| private TensorBoardService tensorBoardService; | private TensorBoardService tensorBoardService; | ||||
| @@ -36,5 +34,9 @@ public class TensorBoardController extends BaseController { | |||||
| public GenericsAjaxResult<String> runTensorBoard(@RequestBody FrameLogPathVo frameLogPathVo) throws Exception { | public GenericsAjaxResult<String> runTensorBoard(@RequestBody FrameLogPathVo frameLogPathVo) throws Exception { | ||||
| return genericsSuccess(tensorBoardService.runTensorBoard(frameLogPathVo)); | return genericsSuccess(tensorBoardService.runTensorBoard(frameLogPathVo)); | ||||
| } | } | ||||
| @PostMapping("/getStatus") | |||||
| @ApiResponse | |||||
| public GenericsAjaxResult<String> getStatus(@RequestBody FrameLogPathVo frameLogPathVo) throws Exception { | |||||
| return genericsSuccess(tensorBoardService.getTensorBoardStatus(frameLogPathVo)); | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,32 @@ | |||||
| package com.ruoyi.platform.domain; | |||||
| public enum TensorBoardStatus { | |||||
| Pending("Pending",1), Running("Running",2),Terminated("未运行",3); | |||||
| private String name; | |||||
| private int index; | |||||
| private TensorBoardStatus(String name, int index) { | |||||
| this.name = name; | |||||
| this.index = index; | |||||
| } | |||||
| public static String getName(int index) { | |||||
| for (TensorBoardStatus c : TensorBoardStatus.values()) { | |||||
| if (c.getIndex() == index) { | |||||
| return c.name; | |||||
| } | |||||
| } | |||||
| return null; | |||||
| } | |||||
| // get set 方法 | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| public int getIndex() { | |||||
| return index; | |||||
| } | |||||
| public void setIndex(int index) { | |||||
| this.index = index; | |||||
| } | |||||
| } | |||||
| @@ -3,6 +3,9 @@ package com.ruoyi.platform.service; | |||||
| import com.ruoyi.platform.vo.FrameLogPathVo; | import com.ruoyi.platform.vo.FrameLogPathVo; | ||||
| public interface TensorBoardService { | public interface TensorBoardService { | ||||
| String getTensorBoardStatus(FrameLogPathVo frameLogPathVo); | |||||
| /** | /** | ||||
| * 在集群中启动TensorBoard容器,并且返回地址,4小时后销毁 | * 在集群中启动TensorBoard容器,并且返回地址,4小时后销毁 | ||||
| * @param frameLogPathVo | * @param frameLogPathVo | ||||
| @@ -105,6 +105,9 @@ public class ExperimentInsServiceImpl implements ExperimentInsService { | |||||
| this.update(experimentIns); | this.update(experimentIns); | ||||
| } | } | ||||
| } | } | ||||
| //新增查询tensorBoard容器状态 | |||||
| result.add(experimentIns); | result.add(experimentIns); | ||||
| } | } | ||||
| } | } | ||||
| @@ -2,10 +2,13 @@ package com.ruoyi.platform.service.impl; | |||||
| import com.ruoyi.common.core.utils.StringUtils; | import com.ruoyi.common.core.utils.StringUtils; | ||||
| import com.ruoyi.common.security.utils.SecurityUtils; | import com.ruoyi.common.security.utils.SecurityUtils; | ||||
| import com.ruoyi.platform.domain.TensorBoardStatus; | |||||
| import com.ruoyi.platform.service.TensorBoardService; | import com.ruoyi.platform.service.TensorBoardService; | ||||
| import com.ruoyi.platform.utils.K8sClientUtil; | import com.ruoyi.platform.utils.K8sClientUtil; | ||||
| import com.ruoyi.platform.vo.FrameLogPathVo; | import com.ruoyi.platform.vo.FrameLogPathVo; | ||||
| import com.ruoyi.system.api.model.LoginUser; | import com.ruoyi.system.api.model.LoginUser; | ||||
| import io.kubernetes.client.openapi.models.V1Pod; | |||||
| import net.sf.jsqlparser.schema.Database; | |||||
| import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| @@ -22,6 +25,25 @@ public class TensorBoardServiceImpl implements TensorBoardService { | |||||
| private String mountPath; | private String mountPath; | ||||
| @Value("${tensorBoard.masterIp}") | @Value("${tensorBoard.masterIp}") | ||||
| private String masterIp; | private String masterIp; | ||||
| @Override | |||||
| public String getTensorBoardStatus(FrameLogPathVo frameLogPathVo){ | |||||
| String status = TensorBoardStatus.Terminated.getName(); | |||||
| if (StringUtils.isEmpty(frameLogPathVo.getPath())||StringUtils.isEmpty(frameLogPathVo.getPvcName())){ | |||||
| return status; | |||||
| } | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| String podName = loginUser.getUsername().toLowerCase()+"-"+frameLogPathVo.getPath().split("/")[2]+ "-tensorboard-pod"; | |||||
| try { | |||||
| String podStatus = K8sClientUtil.getPodStatus(podName, StringUtils.isEmpty(frameLogPathVo.getNamespace()) ? "default" : frameLogPathVo.getNamespace()); | |||||
| System.out.println(podStatus); | |||||
| } catch (Exception e) { | |||||
| return TensorBoardStatus.Terminated.getName(); | |||||
| } | |||||
| return status; | |||||
| } | |||||
| @Override | @Override | ||||
| public String runTensorBoard(FrameLogPathVo frameLogPathVo) throws Exception { | public String runTensorBoard(FrameLogPathVo frameLogPathVo) throws Exception { | ||||
| if (StringUtils.isEmpty(frameLogPathVo.getPath())||StringUtils.isEmpty(frameLogPathVo.getPvcName())){ | if (StringUtils.isEmpty(frameLogPathVo.getPath())||StringUtils.isEmpty(frameLogPathVo.getPvcName())){ | ||||
| @@ -247,7 +247,7 @@ public class K8sClientUtil { | |||||
| .withName(podName) | .withName(podName) | ||||
| .withLabels(selector) | .withLabels(selector) | ||||
| .endMetadata() | .endMetadata() | ||||
| .withNewSpec() | |||||
| .withNewSpec().withSchedulerName("0 */4 * * *")//默认不被操作4小时后删除 | |||||
| .addNewContainer() | .addNewContainer() | ||||
| .withName(podName) | .withName(podName) | ||||
| .withImage(image) | .withImage(image) | ||||
| @@ -257,7 +257,7 @@ public class K8sClientUtil { | |||||
| .addNewVolume() | .addNewVolume() | ||||
| .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvc.getMetadata().getName())) | .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvc.getMetadata().getName())) | ||||
| .endVolume() | .endVolume() | ||||
| .withTerminationGracePeriodSeconds(14400L) //默认不被操作4小时后删除 | |||||
| // .withTerminationGracePeriodSeconds(14400L) //默认不被操作4小时后删除 | |||||
| .endSpec() | .endSpec() | ||||
| .build(); | .build(); | ||||
| @@ -317,7 +317,7 @@ public class K8sClientUtil { | |||||
| .withName(podName) | .withName(podName) | ||||
| .withLabels(selector) | .withLabels(selector) | ||||
| .endMetadata() | .endMetadata() | ||||
| .withNewSpec() | |||||
| .withNewSpec().withSchedulerName("0 */1 * * *")//默认不被操作4小时后删除 | |||||
| .addNewContainer() | .addNewContainer() | ||||
| .withName(podName) | .withName(podName) | ||||
| .withImage(image) | .withImage(image) | ||||
| @@ -327,7 +327,7 @@ public class K8sClientUtil { | |||||
| .addNewVolume() | .addNewVolume() | ||||
| .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | .withName("workspace").withPersistentVolumeClaim(new V1PersistentVolumeClaimVolumeSource().claimName(pvcName)) | ||||
| .endVolume() | .endVolume() | ||||
| .withTerminationGracePeriodSeconds(14400L) //默认不被操作4小时后删除 | |||||
| // .withTerminationGracePeriodSeconds(14400L) | |||||
| .endSpec() | .endSpec() | ||||
| .build(); | .build(); | ||||
| @@ -390,4 +390,25 @@ public class K8sClientUtil { | |||||
| throw new RuntimeException("执行命令异常"); | throw new RuntimeException("执行命令异常"); | ||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * 根据Pod的名称和Namespace查询Pod的状态 | |||||
| * @param podName Pod的名称 | |||||
| * @param namespace Pod所在的Namespace | |||||
| */ | |||||
| public static String getPodStatus(String podName, String namespace) throws Exception { | |||||
| CoreV1Api api = new CoreV1Api(apiClient); | |||||
| V1Pod pod = api.readNamespacedPod(podName, namespace, null, null, null); | |||||
| return pod.getStatus().getPhase(); | |||||
| } | |||||
| public static String getPodLogs(String podName,String namespace,int line) { | |||||
| CoreV1Api api = new CoreV1Api(apiClient); | |||||
| try { | |||||
| String log = api.readNamespacedPodLog(podName, namespace, null, null, null, null, null,null, null, line, null); | |||||
| return log; | |||||
| } catch (ApiException e) { | |||||
| throw new RuntimeException("获取Pod日志异常", e); | |||||
| } | |||||
| } | |||||
| } | } | ||||