Merge remote-tracking branch 'origin/dev-czh' into dev

This commit is contained in:
chenzhihang 2024-09-02 18:14:27 +08:00
commit 973ef2c851
4 changed files with 27 additions and 21 deletions

View File

@ -45,7 +45,7 @@ public class ImageController extends BaseController {
@RequestParam("size") int size,
@RequestParam(value = "image_type") int imageType) {
image.setImageType(imageType);
PageRequest pageRequest = PageRequest.of(page,size);
PageRequest pageRequest = PageRequest.of(page, size);
return genericsSuccess(this.imageService.queryByPage(image, pageRequest));
}
@ -72,6 +72,7 @@ public class ImageController extends BaseController {
public GenericsAjaxResult<Page<Image>> queryByName(@PathVariable("name") String name) {
return genericsSuccess(this.imageService.queryByName(name));
}
/**
* 新增数据
*
@ -84,9 +85,8 @@ public class ImageController extends BaseController {
}
/**
*
* @param imageVo 实体
* 新增镜像和版本 @PostMapping
* 新增镜像和版本 @PostMapping
* @return 新增结果
*/
@PostMapping("/addImageAndVersion")
@ -122,21 +122,20 @@ public class ImageController extends BaseController {
@PostMapping("/net")
@ApiOperation("从网络上传构建镜像")
public GenericsAjaxResult<Map<String, String>> createImageFromNet(@RequestParam("name") String imageName,
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromNet(imageName,imageTag,path));
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromNet(imageName, imageTag, path));
}
@PostMapping("/local")
@ApiOperation("从本地上传构建镜像")
public GenericsAjaxResult<Map<String, String>> createImageFromLocal(@RequestParam("name") String imageName,
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromLocal(imageName,imageTag,path));
@RequestParam("tag") String imageTag,
@RequestParam("path") String path) throws Exception {
return genericsSuccess(this.imageService.createImageFromLocal(imageName, imageTag, path));
}
/**
* 镜像上传
*
@ -145,14 +144,14 @@ public class ImageController extends BaseController {
@PostMapping("/upload")
@ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径")
public GenericsAjaxResult<Map<String, String>> uploadImageFiles(@RequestParam("file") MultipartFile file) throws Exception {
return genericsSuccess(this.imageService.uploadImageFiles(file));
return genericsSuccess(this.imageService.uploadImageFiles(file));
}
@PostMapping("/saveImage")
@ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存并推送到horbor")
public GenericsAjaxResult<String> saveImage(@RequestBody ImageVo imageVo){
return genericsSuccess(this.imageService.saveImage(imageVo));
public void saveImage(@RequestBody ImageVo imageVo) {
this.imageService.saveImage(imageVo);
}
}

View File

@ -93,5 +93,5 @@ public interface ImageService {
Map<String, String> createImageFromNet(String imageName, String imageTag, String NetPath) throws Exception;
Map<String, String> uploadImageFiles(MultipartFile file) throws Exception;
String saveImage(ImageVo imageVo);
void saveImage(ImageVo imageVo);
}

View File

@ -24,6 +24,7 @@ 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.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -367,10 +368,18 @@ public class ImageServiceImpl implements ImageService {
@Override
@Transactional
public String saveImage(ImageVo imageVo) {
if (imageDao.getByName(imageVo.getName()) != null) {
throw new IllegalStateException("镜像名称已存在");
@Async
public void saveImage(ImageVo imageVo) {
Image oldImage = imageDao.getByName(imageVo.getName());
if (oldImage != null) {
List<ImageVersion> oldImageVersions = imageVersionDao.queryByImageId(oldImage.getId());
for (ImageVersion oldImageVersion : oldImageVersions) {
if(oldImageVersion.getTagName().equals(imageVo.getTagName())){
throw new IllegalStateException("镜像tag不能重复");
}
}
}
LoginUser loginUser = SecurityUtils.getLoginUser();
String username = loginUser.getUsername().toLowerCase();
String podName = username + "-editor-pod" + "-" + imageVo.getDevEnvironmentId().toString();
@ -412,8 +421,6 @@ public class ImageServiceImpl implements ImageService {
devEnvironment.setId(imageVo.getDevEnvironmentId());
devEnvironment.setImage(resultMap.get("imageName"));
devEnvironmentDao.update(devEnvironment);
return "保存镜像成功";
} catch (Exception e) {
throw new RuntimeException("保存镜像失败:" + e);
}

View File

@ -501,9 +501,9 @@ public class K8sClientUtil {
computingResourceDao.updateUsedStateByNode(nodeName, Constant.Used_State_used);
}
} catch (ApiException e) {
log.error("创建pod异常:" + e.getResponseBody(), e);
throw new RuntimeException("创建pod异常:" + e.getResponseBody());
} catch (Exception e) {
log.error("创建pod系统异常:", e);
throw new RuntimeException("创建pod系统异常:", e);
}
V1Service service = createService(namespace, podName + "-svc", port, selector);