| @@ -10,22 +10,25 @@ 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 State_building = 2; //创建中 | public final static int State_building = 2; //创建中 | ||||
| public final static int State_failed = 3; //运行失败 | |||||
| public final static int Used_State_used = 1; // 已占用 | public final static int Used_State_used = 1; // 已占用 | ||||
| public final static int Used_State_unused = 0; // 未占用 | public final static int Used_State_unused = 0; // 未占用 | ||||
| public final static String Computing_Resource_CPU = "CPU"; // 计算资源_CPU | public final static String Computing_Resource_CPU = "CPU"; // 计算资源_CPU | ||||
| public final static String Computing_Resource_GPU = "GPU"; // 计算资源_GPU | public final static String Computing_Resource_GPU = "GPU"; // 计算资源_GPU | ||||
| public final static int Git_Category_Id = 39; | public final static int Git_Category_Id = 39; | ||||
| public final static String Source_Auto_Export = "auto_export"; | public final static String Source_Auto_Export = "auto_export"; | ||||
| public final static String Source_Hand_Export = "hand_export"; | public final static String Source_Hand_Export = "hand_export"; | ||||
| public final static String Source_Add = "add"; | public final static String Source_Add = "add"; | ||||
| public final static String Running = "Running"; | |||||
| public final static String Failed = "Failed"; | |||||
| public final static String Pending = "Pending"; | |||||
| public final static String Init = "Init"; | |||||
| public final static String Stopped = "Stopped"; | |||||
| } | } | ||||
| @@ -0,0 +1,111 @@ | |||||
| package com.ruoyi.platform.controller.service; | |||||
| import com.ruoyi.common.core.web.controller.BaseController; | |||||
| import com.ruoyi.common.core.web.domain.GenericsAjaxResult; | |||||
| import com.ruoyi.platform.domain.Service; | |||||
| import com.ruoyi.platform.domain.ServiceVersion; | |||||
| import com.ruoyi.platform.service.ServiceService; | |||||
| import io.swagger.annotations.Api; | |||||
| import io.swagger.annotations.ApiOperation; | |||||
| import org.springframework.data.domain.Page; | |||||
| import org.springframework.data.domain.PageRequest; | |||||
| import org.springframework.web.bind.annotation.*; | |||||
| import javax.annotation.Resource; | |||||
| import java.io.IOException; | |||||
| import java.util.Map; | |||||
| @RestController | |||||
| @RequestMapping("service") | |||||
| @Api("服务") | |||||
| public class ServiceController extends BaseController { | |||||
| @Resource | |||||
| private ServiceService serviceService; | |||||
| @GetMapping | |||||
| @ApiOperation("分页查询服务列表") | |||||
| public GenericsAjaxResult<Page<Service>> queryByPageService(Service service, int page, int size) throws IOException { | |||||
| PageRequest pageRequest = PageRequest.of(page, size); | |||||
| return genericsSuccess(serviceService.queryByPageService(service, pageRequest)); | |||||
| } | |||||
| @GetMapping("/serviceVersion") | |||||
| @ApiOperation("分页查询服务版本列表") | |||||
| public GenericsAjaxResult<Page<ServiceVersion>> queryByPageServiceVersion(ServiceVersion serviceVersion, int page, int size) throws IOException { | |||||
| return genericsSuccess(serviceService.queryByPageServiceVersion(serviceVersion, page, size)); | |||||
| } | |||||
| @PostMapping | |||||
| @ApiOperation("新增服务") | |||||
| public GenericsAjaxResult<Service> addService(@RequestBody Service service) { | |||||
| return genericsSuccess(serviceService.addService(service)); | |||||
| } | |||||
| @PostMapping("/serviceVersion") | |||||
| @ApiOperation("新增服务版本") | |||||
| public GenericsAjaxResult<ServiceVersion> addServiceVersion(@RequestBody ServiceVersion serviceVersion) { | |||||
| return genericsSuccess(serviceService.addServiceVersion(serviceVersion)); | |||||
| } | |||||
| @PutMapping | |||||
| @ApiOperation("编辑服务") | |||||
| public GenericsAjaxResult<Service> editService(@RequestBody Service service) { | |||||
| return genericsSuccess(serviceService.editService(service)); | |||||
| } | |||||
| @PutMapping("/serviceVersion") | |||||
| @ApiOperation("编辑服务版本") | |||||
| public GenericsAjaxResult<ServiceVersion> editServiceVersion(@RequestBody ServiceVersion serviceVersion) { | |||||
| return genericsSuccess(serviceService.editServiceVersion(serviceVersion)); | |||||
| } | |||||
| @GetMapping("/serviceVersionDetail/{id}") | |||||
| @ApiOperation("查询服务版本详细信息") | |||||
| public GenericsAjaxResult<ServiceVersion> getServiceVersion(@PathVariable("id") Long id) { | |||||
| return genericsSuccess(serviceService.getServiceVersion(id)); | |||||
| } | |||||
| @DeleteMapping("{id}") | |||||
| @ApiOperation("删除服务") | |||||
| public GenericsAjaxResult<String> deleteService(@PathVariable("id") Long id) { | |||||
| return genericsSuccess(serviceService.deleteService(id)); | |||||
| } | |||||
| @DeleteMapping("/serviceVersion/{id}") | |||||
| @ApiOperation("删除服务版本") | |||||
| public GenericsAjaxResult<String> deleteServiceVersion(@PathVariable("id") Long id) { | |||||
| return genericsSuccess(serviceService.deleteServiceVersion(id)); | |||||
| } | |||||
| @GetMapping("/runServiceVersion/{id}") | |||||
| @ApiOperation("启动服务版本") | |||||
| public GenericsAjaxResult<String> runServiceVersion(@PathVariable("id") Long id) { | |||||
| return genericsSuccess(serviceService.runServiceVersion(id)); | |||||
| } | |||||
| @GetMapping("/stopServiceVersion/{id}") | |||||
| @ApiOperation("停止服务版本") | |||||
| public GenericsAjaxResult<String> stopServiceVersion(@PathVariable("id") Long id) { | |||||
| return genericsSuccess(serviceService.stopServiceVersion(id)); | |||||
| } | |||||
| @PutMapping("/updateServiceVersion") | |||||
| @ApiOperation("更新服务版本") | |||||
| public GenericsAjaxResult<ServiceVersion> updateServiceVersion(@RequestBody ServiceVersion serviceVersion) { | |||||
| return genericsSuccess(serviceService.updateServiceVersion(serviceVersion)); | |||||
| } | |||||
| @GetMapping("/getServiceVersionLog/{id}") | |||||
| @ApiOperation("获取服务版本日志") | |||||
| public GenericsAjaxResult<String> getServiceVersionLog(@PathVariable("id") Long id, | |||||
| @RequestParam String startTime, @RequestParam String endTime){ | |||||
| return genericsSuccess(serviceService.getServiceVersionLog(id,startTime,endTime)); | |||||
| } | |||||
| @GetMapping("/getServiceVersionDocs/{id}") | |||||
| @ApiOperation("获取服务版本文档") | |||||
| public GenericsAjaxResult<Map<String, Object>> getServiceVersionDocs(@PathVariable("id") Long id){ | |||||
| return genericsSuccess(serviceService.getServiceVersionDocs(id)); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,36 @@ | |||||
| package com.ruoyi.platform.domain; | |||||
| import com.baomidou.mybatisplus.annotation.TableField; | |||||
| import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |||||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | |||||
| import io.swagger.annotations.ApiModel; | |||||
| import lombok.Data; | |||||
| import java.io.Serializable; | |||||
| import java.util.Date; | |||||
| @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) | |||||
| @ApiModel("服务") | |||||
| @Data | |||||
| public class Service implements Serializable { | |||||
| private Long id; | |||||
| private String serviceName; | |||||
| private String serviceType; | |||||
| private String description; | |||||
| private String createBy; | |||||
| private String updateBy; | |||||
| private Date createTime; | |||||
| private Date updateTime; | |||||
| private Integer state; | |||||
| @TableField(exist = false) | |||||
| private Integer versionCount; | |||||
| } | |||||
| @@ -0,0 +1,56 @@ | |||||
| package com.ruoyi.platform.domain; | |||||
| import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |||||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | |||||
| import io.swagger.annotations.ApiModel; | |||||
| import lombok.Data; | |||||
| import java.io.Serializable; | |||||
| import java.util.Date; | |||||
| @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) | |||||
| @ApiModel("服务版本") | |||||
| @Data | |||||
| public class ServiceVersion implements Serializable { | |||||
| private Long id; | |||||
| private Long serviceId; | |||||
| private String version; | |||||
| private String description; | |||||
| private String model; | |||||
| private String image; | |||||
| private String resource; | |||||
| private Integer replicas; | |||||
| private String mountPath; | |||||
| private String envVariables; | |||||
| private String codeConfig; | |||||
| private String command; | |||||
| private String url; | |||||
| private String createBy; | |||||
| private String updateBy; | |||||
| private Date createTime; | |||||
| private Date updateTime; | |||||
| private Integer state; | |||||
| private String runState; | |||||
| private String deploymentName; | |||||
| private String svcName; | |||||
| } | |||||
| @@ -0,0 +1,34 @@ | |||||
| package com.ruoyi.platform.mapper; | |||||
| import com.ruoyi.platform.domain.Service; | |||||
| import com.ruoyi.platform.domain.ServiceVersion; | |||||
| import org.apache.ibatis.annotations.Param; | |||||
| import org.springframework.data.domain.Pageable; | |||||
| import java.util.List; | |||||
| public interface ServiceDao { | |||||
| long countService(@Param("service") com.ruoyi.platform.domain.Service service); | |||||
| long countServiceVersion(@Param("serviceVersion") ServiceVersion serviceVersion); | |||||
| List<Service> queryByPageService(@Param("service") com.ruoyi.platform.domain.Service service, @Param("pageable") Pageable pageable); | |||||
| List<ServiceVersion> queryByPageServiceVersion(@Param("serviceVersion") ServiceVersion serviceVersion, @Param("pageable") Pageable pageable); | |||||
| int insertService(@Param("service") com.ruoyi.platform.domain.Service service); | |||||
| int insertServiceVersion(@Param("serviceVersion") ServiceVersion serviceVersion); | |||||
| int updateService(@Param("service") Service service); | |||||
| int updateServiceVersion(@Param("serviceVersion") ServiceVersion serviceVersion); | |||||
| int updateRunState(@Param("deploymentName") String deploymentName, @Param("runState") String runState); | |||||
| Service getServiceById(@Param("id") Long id); | |||||
| ServiceVersion getServiceVersionById(@Param("id") Long id); | |||||
| } | |||||
| @@ -0,0 +1,39 @@ | |||||
| package com.ruoyi.platform.service; | |||||
| import com.ruoyi.platform.domain.Service; | |||||
| import com.ruoyi.platform.domain.ServiceVersion; | |||||
| import org.springframework.data.domain.Page; | |||||
| import org.springframework.data.domain.PageRequest; | |||||
| import java.io.IOException; | |||||
| import java.util.Map; | |||||
| public interface ServiceService { | |||||
| Page<Service> queryByPageService(Service service, PageRequest pageRequest) throws IOException; | |||||
| Page<ServiceVersion> queryByPageServiceVersion(ServiceVersion serviceVersion, int page, int size) throws IOException; | |||||
| Service addService(Service service); | |||||
| ServiceVersion addServiceVersion(ServiceVersion serviceVersion); | |||||
| Service editService(Service service); | |||||
| ServiceVersion editServiceVersion(ServiceVersion serviceVersion); | |||||
| ServiceVersion getServiceVersion(Long id); | |||||
| String deleteService(Long id); | |||||
| String deleteServiceVersion(Long id); | |||||
| String runServiceVersion(Long id); | |||||
| String stopServiceVersion(Long id); | |||||
| ServiceVersion updateServiceVersion(ServiceVersion serviceVersion); | |||||
| String getServiceVersionLog(Long id, String startTime, String endTime); | |||||
| Map<String, Object> getServiceVersionDocs(Long id); | |||||
| } | |||||
| @@ -0,0 +1,263 @@ | |||||
| package com.ruoyi.platform.service.impl; | |||||
| import com.alibaba.fastjson2.JSON; | |||||
| import com.alibaba.fastjson2.JSONObject; | |||||
| import com.ruoyi.common.security.utils.SecurityUtils; | |||||
| import com.ruoyi.platform.constant.Constant; | |||||
| import com.ruoyi.platform.domain.ServiceVersion; | |||||
| import com.ruoyi.platform.mapper.ServiceDao; | |||||
| import com.ruoyi.platform.service.ServiceService; | |||||
| import com.ruoyi.platform.utils.HttpUtils; | |||||
| import com.ruoyi.platform.utils.JacksonUtil; | |||||
| import com.ruoyi.system.api.model.LoginUser; | |||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.springframework.beans.factory.annotation.Value; | |||||
| import org.springframework.data.domain.Page; | |||||
| import org.springframework.data.domain.PageImpl; | |||||
| import org.springframework.data.domain.PageRequest; | |||||
| import org.springframework.stereotype.Service; | |||||
| import javax.annotation.Resource; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| import java.util.stream.Collectors; | |||||
| @Service("serviceService") | |||||
| public class ServiceServiceImpl implements ServiceService { | |||||
| @Value("${argo.url}") | |||||
| private String argoUrl; | |||||
| @Value("${argo.modelService}") | |||||
| private String modelService; | |||||
| @Resource | |||||
| private ServiceDao serviceDao; | |||||
| @Override | |||||
| public Page<com.ruoyi.platform.domain.Service> queryByPageService(com.ruoyi.platform.domain.Service service, PageRequest pageRequest) { | |||||
| long total = serviceDao.countService(service); | |||||
| List<com.ruoyi.platform.domain.Service> services = serviceDao.queryByPageService(service, pageRequest); | |||||
| return new PageImpl<>(services, pageRequest, total); | |||||
| } | |||||
| @Override | |||||
| public Page<ServiceVersion> queryByPageServiceVersion(ServiceVersion serviceVersion, int page, int size) { | |||||
| PageRequest pageRequest; | |||||
| if (StringUtils.isNotEmpty(serviceVersion.getRunState())) { | |||||
| pageRequest = PageRequest.of(page, Integer.MAX_VALUE); | |||||
| List<ServiceVersion> serviceVersions = serviceDao.queryByPageServiceVersion(serviceVersion, pageRequest); | |||||
| List<String> deploymentNames = serviceVersions.stream().map(ServiceVersion::getDeploymentName).collect(Collectors.toList()); | |||||
| Map<String, String> runStates = getRunState(deploymentNames); | |||||
| updateRunState(runStates); | |||||
| List<ServiceVersion> result = serviceVersions.stream().filter(serviceVersion1 -> { | |||||
| String runState = runStates.get(serviceVersion1.getDeploymentName()); | |||||
| return serviceVersion1.getRunState().equals(runState); | |||||
| }).collect(Collectors.toList()); | |||||
| PageRequest.of(page, size); | |||||
| return new PageImpl<>(result, pageRequest, result.size()); | |||||
| } else { | |||||
| pageRequest = PageRequest.of(page, size); | |||||
| long total = serviceDao.countServiceVersion(serviceVersion); | |||||
| List<ServiceVersion> serviceVersions = serviceDao.queryByPageServiceVersion(serviceVersion, pageRequest); | |||||
| List<String> deploymentNames = serviceVersions.stream().map(ServiceVersion::getDeploymentName).collect(Collectors.toList()); | |||||
| Map<String, String> runStates = getRunState(deploymentNames); | |||||
| updateRunState(runStates); | |||||
| return new PageImpl<>(serviceVersions, pageRequest, total); | |||||
| } | |||||
| } | |||||
| Map<String, String> getRunState(List<String> deploymentNames) { | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("deployment_names", deploymentNames); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/getStatus", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); | |||||
| Map<String, Map<String, String>> data = (Map<String, Map<String, String>>) reqMap.get("data"); | |||||
| return data.get("status"); | |||||
| } else { | |||||
| return null; | |||||
| } | |||||
| } | |||||
| void updateRunState(Map<String, String> runStates) { | |||||
| for (Map.Entry<String, String> entry : runStates.entrySet()) { | |||||
| serviceDao.updateRunState(entry.getKey(), entry.getValue()); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| public com.ruoyi.platform.domain.Service addService(com.ruoyi.platform.domain.Service service) { | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| service.setCreateBy(loginUser.getUsername()); | |||||
| service.setUpdateBy(loginUser.getUsername()); | |||||
| serviceDao.insertService(service); | |||||
| return service; | |||||
| } | |||||
| @Override | |||||
| public ServiceVersion addServiceVersion(ServiceVersion serviceVersion) { | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| serviceVersion.setCreateBy(loginUser.getUsername()); | |||||
| serviceVersion.setUpdateBy(loginUser.getUsername()); | |||||
| serviceDao.insertServiceVersion(serviceVersion); | |||||
| return serviceVersion; | |||||
| } | |||||
| @Override | |||||
| public com.ruoyi.platform.domain.Service editService(com.ruoyi.platform.domain.Service service) { | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| service.setUpdateBy(loginUser.getUsername()); | |||||
| serviceDao.updateService(service); | |||||
| return serviceDao.getServiceById(service.getId()); | |||||
| } | |||||
| @Override | |||||
| public ServiceVersion editServiceVersion(ServiceVersion serviceVersion) { | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| serviceVersion.setUpdateBy(loginUser.getUsername()); | |||||
| serviceDao.updateServiceVersion(serviceVersion); | |||||
| return serviceDao.getServiceVersionById(serviceVersion.getId()); | |||||
| } | |||||
| @Override | |||||
| public ServiceVersion getServiceVersion(Long id) { | |||||
| return serviceDao.getServiceVersionById(id); | |||||
| } | |||||
| @Override | |||||
| public String deleteService(Long id) { | |||||
| com.ruoyi.platform.domain.Service service = serviceDao.getServiceById(id); | |||||
| if (service == null) { | |||||
| return "服务不存在"; | |||||
| } | |||||
| LoginUser loginUser = SecurityUtils.getLoginUser(); | |||||
| String username = loginUser.getUsername(); | |||||
| String createBy = service.getCreateBy(); | |||||
| if (!(StringUtils.equals(username, "admin") || StringUtils.equals(username, createBy))) { | |||||
| return "无权限删除该服务"; | |||||
| } | |||||
| ServiceVersion serviceVersion = new ServiceVersion(); | |||||
| serviceVersion.setServiceId(id); | |||||
| long versionCount = serviceDao.countServiceVersion(serviceVersion); | |||||
| if (versionCount != 0) { | |||||
| return "该服务下还有版本,不能删除"; | |||||
| } | |||||
| service.setState(Constant.State_invalid); | |||||
| return serviceDao.updateService(service) > 0 ? "删除成功" : "删除失败"; | |||||
| } | |||||
| @Override | |||||
| public String deleteServiceVersion(Long id) { | |||||
| ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); | |||||
| serviceVersion.setState(Constant.State_invalid); | |||||
| return serviceDao.updateServiceVersion(serviceVersion) > 0 ? "删除成功" : "删除失败"; | |||||
| } | |||||
| @Override | |||||
| public String runServiceVersion(Long id) { | |||||
| ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); | |||||
| com.ruoyi.platform.domain.Service service = serviceDao.getServiceById(serviceVersion.getServiceId()); | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("service_name", service.getServiceName()); | |||||
| paramMap.put("description", serviceVersion.getDescription()); | |||||
| paramMap.put("resource", serviceVersion.getResource()); | |||||
| paramMap.put("mount_path", serviceVersion.getMountPath()); | |||||
| paramMap.put("replicas", serviceVersion.getReplicas()); | |||||
| paramMap.put("env", JSONObject.parseObject(serviceVersion.getEnvVariables())); | |||||
| paramMap.put("code_config", JSONObject.parseObject(serviceVersion.getCodeConfig())); | |||||
| paramMap.put("image", serviceVersion.getImage()); | |||||
| paramMap.put("model", JSONObject.parseObject(serviceVersion.getModel())); | |||||
| paramMap.put("service_type", service.getServiceType()); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/create", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); | |||||
| if ((Integer) reqMap.get("code") == 200) { | |||||
| Map<String, String> data = (Map<String, String>) reqMap.get("data"); | |||||
| serviceVersion.setUrl(data.get("url")); | |||||
| serviceVersion.setDeploymentName(data.get("deployment_name")); | |||||
| serviceVersion.setSvcName(data.get("svc_name")); | |||||
| serviceVersion.setRunState(Constant.Init); | |||||
| editServiceVersion(serviceVersion); | |||||
| return "启动成功"; | |||||
| } else { | |||||
| throw new RuntimeException("启动失败"); | |||||
| } | |||||
| } else { | |||||
| throw new RuntimeException("启动失败"); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| public String stopServiceVersion(Long id) { | |||||
| ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("deployment_name", serviceVersion.getDeploymentName()); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/stop", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| serviceVersion.setRunState(Constant.Stopped); | |||||
| editServiceVersion(serviceVersion); | |||||
| return "停止成功"; | |||||
| } else { | |||||
| throw new RuntimeException("停止失败"); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| public ServiceVersion updateServiceVersion(ServiceVersion serviceVersion) { | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("deployment_name", serviceVersion.getDeploymentName()); | |||||
| HashMap<String, Object> updateMap = new HashMap<>(); | |||||
| updateMap.put("replicas", serviceVersion.getReplicas()); | |||||
| updateMap.put("resource", serviceVersion.getResource()); | |||||
| paramMap.put("update_model", JSON.toJSONString(updateMap)); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/update", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| return editServiceVersion(serviceVersion); | |||||
| } else { | |||||
| throw new RuntimeException("更新失败"); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| public String getServiceVersionLog(Long id, String startTime, String endTime) { | |||||
| ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("deployment_name", serviceVersion.getDeploymentName()); | |||||
| paramMap.put("start_time", startTime); | |||||
| paramMap.put("end_time", endTime); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/getLog", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); | |||||
| HashMap<String, String> data = (HashMap<String, String>) reqMap.get("data"); | |||||
| return data.get("log_content"); | |||||
| } else { | |||||
| throw new RuntimeException("获取日志失败"); | |||||
| } | |||||
| } | |||||
| @Override | |||||
| public Map<String, Object> getServiceVersionDocs(Long id) { | |||||
| ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); | |||||
| HashMap<String, Object> paramMap = new HashMap<>(); | |||||
| paramMap.put("deployment_name", serviceVersion.getDeploymentName()); | |||||
| String req = HttpUtils.sendPost(argoUrl + modelService + "/getDocs", JSON.toJSONString(paramMap)); | |||||
| if (StringUtils.isNotEmpty(req)) { | |||||
| Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); | |||||
| Map<String, Object> data = (Map<String, Object>) reqMap.get("data"); | |||||
| return data; | |||||
| } else { | |||||
| throw new RuntimeException("获取日志失败"); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,5 +1,6 @@ | |||||
| package com.ruoyi.platform.utils; | package com.ruoyi.platform.utils; | ||||
| import com.alibaba.fastjson2.JSON; | |||||
| import com.ruoyi.platform.constant.Constant; | import com.ruoyi.platform.constant.Constant; | ||||
| import com.ruoyi.platform.domain.DevEnvironment; | import com.ruoyi.platform.domain.DevEnvironment; | ||||
| import com.ruoyi.platform.mapper.ComputingResourceDao; | 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.custom.Quantity; | ||||
| import io.kubernetes.client.openapi.ApiClient; | import io.kubernetes.client.openapi.ApiClient; | ||||
| import io.kubernetes.client.openapi.ApiException; | 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.apis.CoreV1Api; | ||||
| import io.kubernetes.client.openapi.models.*; | import io.kubernetes.client.openapi.models.*; | ||||
| import io.kubernetes.client.util.ClientBuilder; | import io.kubernetes.client.util.ClientBuilder; | ||||
| @@ -471,24 +473,7 @@ public class K8sClientUtil { | |||||
| //配置资源 | //配置资源 | ||||
| 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"); | |||||
| 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() | V1Pod pod = new V1PodBuilder() | ||||
| .withNewMetadata() | .withNewMetadata() | ||||
| @@ -674,6 +659,98 @@ public class K8sClientUtil { | |||||
| return pod; | return pod; | ||||
| } | } | ||||
| public Integer createDeployment(String dpName, String namespace, Integer replicas, String model, String image, Integer port, String resource, String mountPath | |||||
| , String envVariables, String codeConfig) { | |||||
| AppsV1Api api = new AppsV1Api(apiClient); | |||||
| //配置标签选择 | |||||
| HashMap<String, String> selector = new HashMap<>(); | |||||
| selector.put("app", dpName); | |||||
| //配置资源 | |||||
| V1ResourceRequirements v1ResourceRequirements = setPodResource(resource); | |||||
| //配置环境变量 | |||||
| List<V1EnvVar> env = new ArrayList<>(); | |||||
| if (StringUtils.isNotEmpty(envVariables)) { | |||||
| HashMap<String, String> envMap = JSON.parseObject(envVariables, HashMap.class); | |||||
| for (String key : envMap.keySet()) { | |||||
| V1EnvVar envVar = new V1EnvVar().name(key).value(envMap.get(key)); | |||||
| env.add(envVar); | |||||
| } | |||||
| } | |||||
| // 配置卷和卷挂载 | |||||
| // List<V1VolumeMount> volumeMounts = new ArrayList<>(); | |||||
| // volumeMounts.add(new V1VolumeMount().name("workspace").mountPath(mountPath)); | |||||
| // volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/code").subPath(codeConfig).readOnly(true)); | |||||
| // volumeMounts.add(new V1VolumeMount().name("minio-pvc").mountPath("/opt/model").subPath(model).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))); | |||||
| //创建deployment | |||||
| V1Deployment deployment = new V1DeploymentBuilder().withNewMetadata() | |||||
| .withName(dpName) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .withReplicas(replicas) | |||||
| .withSelector(new V1LabelSelector().matchLabels(selector)) | |||||
| .withNewTemplate() | |||||
| .withNewMetadata() | |||||
| .addToLabels("app", dpName) | |||||
| .endMetadata() | |||||
| .withNewSpec() | |||||
| .addNewContainer() | |||||
| .withName(dpName) | |||||
| .withImage(image) | |||||
| .withEnv(env) | |||||
| .withPorts(new V1ContainerPort().containerPort(port).protocol("TCP")) | |||||
| .withResources(v1ResourceRequirements) | |||||
| .endContainer() | |||||
| .endSpec() | |||||
| .endTemplate() | |||||
| .endSpec() | |||||
| .build(); | |||||
| try { | |||||
| api.createNamespacedDeployment(namespace, deployment, null, null, null); | |||||
| } catch (ApiException e) { | |||||
| throw new RuntimeException("创建deployment异常:" + e.getResponseBody()); | |||||
| } | |||||
| V1Service service = createService(namespace, dpName + "-svc", port, selector); | |||||
| return service.getSpec().getPorts().get(0).getNodePort(); | |||||
| } | |||||
| 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 | * 删除 Pod | ||||
| * | * | ||||
| @@ -0,0 +1,156 @@ | |||||
| <?xml version="1.0" encoding="UTF-8"?> | |||||
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||||
| <mapper namespace="com.ruoyi.platform.mapper.ServiceDao"> | |||||
| <select id="countService" resultType="java.lang.Long"> | |||||
| select count(1) from service | |||||
| <where> | |||||
| state = 1 | |||||
| <if test="service.serviceName != null and service.serviceName != ''"> | |||||
| and service_name like concat('%', #{service.serviceName}, '%') | |||||
| </if> | |||||
| <if test="service.serviceType != null"> | |||||
| and service_type = #{service.serviceType} | |||||
| </if> | |||||
| </where> | |||||
| </select> | |||||
| <select id="queryByPageService" resultType="com.ruoyi.platform.domain.Service"> | |||||
| select a.*,count(b.id) as version_count from service a | |||||
| left join (select * from service_version where state = 1) b on a.id = b.service_id | |||||
| <where> | |||||
| a.state = 1 | |||||
| <if test="service.serviceName != null and service.serviceName != ''"> | |||||
| and a.service_name like concat('%', #{service.serviceName}, '%') | |||||
| </if> | |||||
| <if test="service.serviceType != null"> | |||||
| and a.service_type = #{service.serviceType} | |||||
| </if> | |||||
| </where> | |||||
| group by a.id | |||||
| order by a.update_time DESC | |||||
| limit #{pageable.offset}, #{pageable.pageSize} | |||||
| </select> | |||||
| <select id="countServiceVersion" resultType="java.lang.Long"> | |||||
| select count(1) from | |||||
| ( <include refid="queryServiceVersion"></include> ) t | |||||
| </select> | |||||
| <select id="queryByPageServiceVersion" resultType="com.ruoyi.platform.domain.ServiceVersion"> | |||||
| <include refid="queryServiceVersion"></include> | |||||
| limit #{pageable.offset}, #{pageable.pageSize} | |||||
| </select> | |||||
| <sql id="queryServiceVersion"> | |||||
| select * from service_version | |||||
| <where> | |||||
| state = 1 | |||||
| <if test="serviceVersion.version != null and serviceVersion.version !=''"> | |||||
| and version like concat('%', #{serviceVersion.version}, '%') | |||||
| </if> | |||||
| <if test="serviceVersion.serviceId != null and serviceVersion.serviceId !=''"> | |||||
| and service_id = #{serviceVersion.serviceId} | |||||
| </if> | |||||
| </where> | |||||
| order by update_time DESC | |||||
| </sql> | |||||
| <select id="getServiceById" resultType="com.ruoyi.platform.domain.Service"> | |||||
| select * | |||||
| from service | |||||
| where id = #{id} | |||||
| </select> | |||||
| <select id="getServiceVersionById" resultType="com.ruoyi.platform.domain.ServiceVersion"> | |||||
| select * | |||||
| from service_version | |||||
| where id = #{id} | |||||
| </select> | |||||
| <insert id="insertService"> | |||||
| insert into service(service_name, service_type, description, create_by, update_by) | |||||
| values (#{service.serviceName}, #{service.serviceType}, #{service.description}, #{service.createBy}, #{service.updateBy}) | |||||
| </insert> | |||||
| <insert id="insertServiceVersion"> | |||||
| insert into service_version(service_id, version, model, description, image, resource, replicas, mount_path, env_variables, | |||||
| code_config, command, create_by, update_by) | |||||
| values (#{serviceVersion.serviceId}, #{serviceVersion.version}, #{serviceVersion.model}, #{serviceVersion.description}, #{serviceVersion.image}, | |||||
| #{serviceVersion.replicas}, #{serviceVersion.mountPath}, #{serviceVersion.envVariables}, | |||||
| #{serviceVersion.codeConfig}, #{serviceVersion.command}, #{serviceVersion.createBy}, #{serviceVersion.updateBy}) | |||||
| </insert> | |||||
| <update id="updateService"> | |||||
| update service | |||||
| <set> | |||||
| <if test="service.serviceName != null and service.serviceName !=''"> | |||||
| service_name = #{service.serviceName}, | |||||
| </if> | |||||
| <if test="service.serviceType != null and service.serviceType !=''"> | |||||
| service_type = #{service.serviceType}, | |||||
| </if> | |||||
| <if test="service.description != null and service.description !=''"> | |||||
| description = #{service.description}, | |||||
| </if> | |||||
| <if test="service.state != null"> | |||||
| state = #{service.state}, | |||||
| </if> | |||||
| </set> | |||||
| where id = #{service.id} | |||||
| </update> | |||||
| <update id="updateServiceVersion"> | |||||
| update service_version | |||||
| <set> | |||||
| <if test="serviceVersion.description != null and serviceVersion.description !=''"> | |||||
| description = #{serviceVersion.description}, | |||||
| </if> | |||||
| <if test="serviceVersion.model != null and serviceVersion.model !=''"> | |||||
| model = #{serviceVersion.model}, | |||||
| </if> | |||||
| <if test="serviceVersion.image != null and serviceVersion.image !=''"> | |||||
| image = #{serviceVersion.image}, | |||||
| </if> | |||||
| <if test="serviceVersion.resource != null and serviceVersion.resource !=''"> | |||||
| resource = #{serviceVersion.resource}, | |||||
| </if> | |||||
| <if test="serviceVersion.replicas != null"> | |||||
| replicas = #{serviceVersion.replicas}, | |||||
| </if> | |||||
| <if test="serviceVersion.mountPath != null and serviceVersion.mountPath !=''"> | |||||
| mount_path = #{serviceVersion.mountPath}, | |||||
| </if> | |||||
| <if test="serviceVersion.envVariables != null and serviceVersion.envVariables !=''"> | |||||
| env_variables = #{serviceVersion.envVariables}, | |||||
| </if> | |||||
| <if test="serviceVersion.codeConfig != null and serviceVersion.codeConfig !=''"> | |||||
| code_config = #{serviceVersion.codeConfig}, | |||||
| </if> | |||||
| <if test="serviceVersion.command != null and serviceVersion.command !=''"> | |||||
| command = #{serviceVersion.command}, | |||||
| </if> | |||||
| <if test="serviceVersion.url != null and serviceVersion.url !=''"> | |||||
| url = #{serviceVersion.url}, | |||||
| </if> | |||||
| <if test="serviceVersion.state != null"> | |||||
| state = #{serviceVersion.state}, | |||||
| </if> | |||||
| <if test="serviceVersion.runState != null and serviceVersion.runState !=''"> | |||||
| run_state = #{serviceVersion.runState}, | |||||
| </if> | |||||
| <if test="serviceVersion.deploymentName != null and serviceVersion.deploymentName !=''"> | |||||
| deployment_name = #{serviceVersion.deploymentName}, | |||||
| </if> | |||||
| <if test="serviceVersion.svcName != null and serviceVersion.svcName !=''"> | |||||
| svc_name = #{serviceVersion.svcName}, | |||||
| </if> | |||||
| </set> | |||||
| where id = #{serviceVersion.id} | |||||
| </update> | |||||
| <update id="updateRunState"> | |||||
| update service_version | |||||
| set run_state = #{runState} | |||||
| where deployment_name = #{deploymentName} | |||||
| </update> | |||||
| </mapper> | |||||