ApplicationServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package cn.seecoder.paas.service.impl;
  2. import cn.seecoder.paas.data.dao.ApplicationDAO;
  3. import cn.seecoder.paas.data.dao.ConfigDAO;
  4. import cn.seecoder.paas.data.dao.EnvironmentDAO;
  5. import cn.seecoder.paas.data.entity.Application;
  6. import cn.seecoder.paas.data.entity.Config;
  7. import cn.seecoder.paas.data.entity.Environment;
  8. import cn.seecoder.paas.service.ApplicationService;
  9. import cn.seecoder.paas.service.EnvironmentService;
  10. import cn.seecoder.paas.service.facade.k8s.PersistentVolumeClaimApi;
  11. import cn.seecoder.paas.service.facade.k8s.model.K8sObjectRequest;
  12. import cn.seecoder.paas.service.facade.k8s.model.PersistentVolumeClaim;
  13. import cn.seecoder.paas.service.model.converter.ApplicationConverter;
  14. import cn.seecoder.paas.service.model.converter.ConfigConverter;
  15. import cn.seecoder.paas.service.model.vo.AppVolumeVO;
  16. import cn.seecoder.paas.service.model.vo.ApplicationVO;
  17. import cn.seecoder.paas.service.model.vo.ConfigVO;
  18. import cn.seecoder.paas.util.ApplicationProperties;
  19. import cn.seecoder.paas.util.ServiceException;
  20. import cn.seecoder.paas.util.enums.ConfigBelongsToType;
  21. import cn.seecoder.paas.util.enums.ResourceLabel;
  22. import io.kubernetes.client.custom.Quantity;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.util.CollectionUtils;
  26. import javax.transaction.Transactional;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.stream.Collectors;
  30. @Service
  31. public class ApplicationServiceImpl implements ApplicationService {
  32. private final ApplicationDAO applicationDAO;
  33. private final ConfigDAO configDAO;
  34. private final EnvironmentService environmentService;
  35. private final EnvironmentDAO environmentDAO;
  36. private final PersistentVolumeClaimApi persistentVolumeClaimApi;
  37. private final ApplicationProperties applicationProperties;
  38. @Autowired
  39. public ApplicationServiceImpl(ApplicationDAO applicationDAO, ConfigDAO configDAO, EnvironmentService environmentService, EnvironmentDAO environmentDAO, PersistentVolumeClaimApi persistentVolumeClaimApi, ApplicationProperties applicationProperties) {
  40. this.applicationDAO = applicationDAO;
  41. this.configDAO = configDAO;
  42. this.environmentService = environmentService;
  43. this.environmentDAO = environmentDAO;
  44. this.persistentVolumeClaimApi = persistentVolumeClaimApi;
  45. this.applicationProperties = applicationProperties;
  46. }
  47. @Override
  48. @Transactional
  49. public ApplicationVO createOrUpdate(ApplicationVO vo) throws ServiceException {
  50. ApplicationVO result = ApplicationConverter.convertToVO(applicationDAO.save(ApplicationConverter.convertToEntity(vo)));
  51. Config config = configDAO.findByConfigBelongsAndAndEntityId(ConfigBelongsToType.APPLICATION, result.getId());
  52. if (config == null) {
  53. ConfigVO configVO = ConfigVO.getEmptyConfigVO(ConfigBelongsToType.APPLICATION, result.getId());
  54. ConfigConverter.convertToVO(configDAO.save(ConfigConverter.convertToEntity(configVO)));
  55. }
  56. return result;
  57. }
  58. @Override
  59. @Transactional
  60. public void delete(Integer id) throws ServiceException {
  61. applicationDAO.deleteById(id);
  62. configDAO.deleteByConfigBelongsAndAndEntityId(ConfigBelongsToType.APPLICATION, id);
  63. List<Environment> environments = environmentDAO.findAllByAppId(id);
  64. for (Environment environment : environments) {
  65. environmentService.delete(environment.getId());
  66. }
  67. String labelKey = ResourceLabel.RESOURCE.getCode();
  68. String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("application", String.valueOf(id));
  69. List<PersistentVolumeClaim> pvcs = persistentVolumeClaimApi.getByCondition(
  70. K8sObjectRequest.builder()
  71. .namespace(applicationProperties.getDeploymentNamespace())
  72. .labels(Collections.singletonMap(labelKey, labelValue))
  73. .build());
  74. if (!CollectionUtils.isEmpty(pvcs)) {
  75. for (PersistentVolumeClaim pvc : pvcs) {
  76. persistentVolumeClaimApi.delete(pvc);
  77. }
  78. }
  79. }
  80. @Override
  81. public List<ApplicationVO> getAll() {
  82. return applicationDAO.findAll().stream().map(ApplicationConverter::convertToVO).collect(Collectors.toList());
  83. }
  84. @Override
  85. public ApplicationVO getDetailById(Integer id) throws ServiceException {
  86. Application application = applicationDAO.findById(id).orElse(null);
  87. if (application == null) {
  88. return null;
  89. }
  90. ApplicationVO vo = ApplicationConverter.convertToVO(application);
  91. Config config = configDAO.findByConfigBelongsAndAndEntityId(ConfigBelongsToType.APPLICATION, id);
  92. if (config != null) {
  93. vo.setConfig(ConfigConverter.convertToVO(config));
  94. } else {
  95. vo.setConfig(ConfigVO.getEmptyConfigVO(ConfigBelongsToType.APPLICATION, id));
  96. }
  97. String labelKey = ResourceLabel.RESOURCE.getCode();
  98. String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("application", String.valueOf(application.getId()));
  99. List<PersistentVolumeClaim> pvcs = persistentVolumeClaimApi.getByCondition(
  100. K8sObjectRequest.builder()
  101. .namespace(applicationProperties.getDeploymentNamespace())
  102. .labels(Collections.singletonMap(labelKey, labelValue))
  103. .build());
  104. vo.setPvcs(pvcs.stream().map(pvc -> {
  105. return new AppVolumeVO(pvc.getName(), application.getId(), pvc.getQuantity().toSuffixedString());
  106. }).collect(Collectors.toList()));
  107. return vo;
  108. }
  109. @Override
  110. public void createAppVolume(AppVolumeVO appVolumeVO) throws ServiceException {
  111. Application application = applicationDAO.findById(appVolumeVO.getAppId()).orElse(null);
  112. if (application == null) {
  113. throw ServiceException.INVALID_DATA;
  114. }
  115. PersistentVolumeClaim pvc = getPVCByAppVolume(appVolumeVO);
  116. persistentVolumeClaimApi.create(pvc);
  117. }
  118. @Override
  119. public void updateAppVolume(AppVolumeVO appVolumeVO) throws ServiceException {
  120. Application application = applicationDAO.findById(appVolumeVO.getAppId()).orElse(null);
  121. if (application == null) {
  122. throw ServiceException.INVALID_DATA;
  123. }
  124. String labelKey = ResourceLabel.RESOURCE.getCode();
  125. String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("application", String.valueOf(appVolumeVO.getAppId()));
  126. List<PersistentVolumeClaim> pvcs = persistentVolumeClaimApi.getByCondition(
  127. K8sObjectRequest.builder()
  128. .name("a" + appVolumeVO.getAppId() + "-" + appVolumeVO.getName())
  129. .namespace(applicationProperties.getDeploymentNamespace())
  130. .labels(Collections.singletonMap(labelKey, labelValue))
  131. .build());
  132. if (CollectionUtils.isEmpty(pvcs)) {
  133. throw ServiceException.BAD_REQUEST;
  134. }
  135. PersistentVolumeClaim pvc = getPVCByAppVolume(appVolumeVO);
  136. persistentVolumeClaimApi.update(pvc);
  137. }
  138. private PersistentVolumeClaim getPVCByAppVolume(AppVolumeVO appVolumeVO) {
  139. String labelKey = ResourceLabel.RESOURCE.getCode();
  140. String labelValue = ResourceLabel.RESOURCE.getGenerator().gen("application", String.valueOf(appVolumeVO.getAppId()));
  141. PersistentVolumeClaim pvc = PersistentVolumeClaim.builder()
  142. .storageClassName(applicationProperties.getPvcStorageClassName())
  143. .accessModes(Collections.singletonList("ReadWriteMany"))
  144. .volumeMode("Filesystem")
  145. .quantity(Quantity.fromString(appVolumeVO.getQuantity()))
  146. .build();
  147. pvc.setName("a" + appVolumeVO.getAppId() + "-" + appVolumeVO.getName());
  148. pvc.setNamespace(applicationProperties.getDeploymentNamespace());
  149. pvc.setLabel(labelKey, labelValue);
  150. return pvc;
  151. }
  152. @Override
  153. public void deleteAppVolume(AppVolumeVO appVolumeVO) throws ServiceException {
  154. Application application = applicationDAO.findById(appVolumeVO.getAppId()).orElse(null);
  155. if (application == null) {
  156. throw ServiceException.INVALID_DATA;
  157. }
  158. PersistentVolumeClaim pvc = new PersistentVolumeClaim();
  159. pvc.setNamespace(applicationProperties.getDeploymentNamespace());
  160. pvc.setName(appVolumeVO.getName());
  161. persistentVolumeClaimApi.delete(pvc);
  162. }
  163. }