|
|
@@ -1,17 +1,18 @@
|
|
|
package nju.seec.SEECdemo.logic.service.impl;
|
|
|
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.DeploymentApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.NamespaceApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.SecretApi;
|
|
|
-import nju.seec.SEECdemo.logic.api.k8s.ServiceApi;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.*;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.dto.DeploymentDTO;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.dto.IngressDTO;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.dto.ServiceDTO;
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.util.SecretTypeEnum;
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.vo.SecretVO;
|
|
|
import nju.seec.SEECdemo.logic.service.ApplicationService;
|
|
|
import nju.seec.SEECdemo.logic.vo.ApplicationVO;
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
import nju.seec.SEECdemo.web.dto.ApplicationDTO;
|
|
|
+import nju.seec.SEECdemo.web.dto.ContainerDTO;
|
|
|
+import nju.seec.SEECdemo.web.dto.PortDTO;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -36,6 +37,9 @@ public class ApplicationServiceImpl implements ApplicationService{
|
|
|
@Autowired
|
|
|
private SecretApi secretApi;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IngressApi ingressApi;
|
|
|
+
|
|
|
@Override
|
|
|
public ApplicationVO create(ApplicationDTO applicationDTO) {
|
|
|
return null;
|
|
|
@@ -60,21 +64,22 @@ public class ApplicationServiceImpl implements ApplicationService{
|
|
|
public void deploy(ApplicationDTO applicationDTO) {
|
|
|
String projectName = applicationDTO.getProjectName();
|
|
|
if (namespaceApi.exists(projectName)) {
|
|
|
- //判断deployment是否存在 存在则删除
|
|
|
- //判断service是否存在,存在则删除
|
|
|
+ try {
|
|
|
+ DeploymentDTO deploymentDTO = buildDeploymentDTO(applicationDTO);
|
|
|
+ LoggerUtil.info(logger, "deploymentDTO={}", deploymentDTO);
|
|
|
+ deploymentApi.createSync(deploymentDTO);
|
|
|
|
|
|
- DeploymentDTO deploymentDTO = buildDeploymentDTO(applicationDTO);
|
|
|
- LoggerUtil.info(logger, "deploymentDTO={}", deploymentDTO);
|
|
|
- deploymentApi.createSync(deploymentDTO);
|
|
|
+ //判断service是否存在
|
|
|
+ ServiceDTO serviceDTO = buildServiceDTO(applicationDTO);
|
|
|
+ serviceApi.create(serviceDTO);
|
|
|
|
|
|
- //判断service是否存在
|
|
|
- ServiceDTO serviceDTO = buildServiceDTO(applicationDTO);
|
|
|
- serviceApi.create(serviceDTO);
|
|
|
+ IngressDTO ingressDTO = buildIngressDTO(applicationDTO);
|
|
|
+ ingressApi.create(ingressDTO);
|
|
|
+ } catch (K8sApiException e) {
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
|
|
|
private DeploymentDTO buildDeploymentDTO(ApplicationDTO applicationDTO) {
|
|
|
@@ -82,8 +87,12 @@ public class ApplicationServiceImpl implements ApplicationService{
|
|
|
|
|
|
//将registry密钥作为imagePullSecrets注入
|
|
|
List<String> registrySecrets = secretApi
|
|
|
- .getSecretListByType(applicationDTO.getProjectName(), SecretTypeEnum.REGISTRY)
|
|
|
- .stream().map(SecretVO::getName).collect(Collectors.toList()
|
|
|
+ .getSecretListByType(
|
|
|
+ applicationDTO.getProjectName(),
|
|
|
+ SecretTypeEnum.REGISTRY)
|
|
|
+ .stream()
|
|
|
+ .map(SecretVO::getName)
|
|
|
+ .collect(Collectors.toList()
|
|
|
);
|
|
|
deploymentDTO.setImagePullSecrets(registrySecrets);
|
|
|
|
|
|
@@ -99,4 +108,37 @@ public class ApplicationServiceImpl implements ApplicationService{
|
|
|
private ServiceDTO buildServiceDTO(ApplicationDTO applicationDTO) {
|
|
|
return applicationDTO.toServiceDTO();
|
|
|
}
|
|
|
+
|
|
|
+ //@todo 构建Ingress
|
|
|
+ //@fixme 潜在bug 如果容器上的IP和Service上的IP并非一一对应,根据容器的IP暴露Service会出现映射错误
|
|
|
+ private IngressDTO buildIngressDTO(ApplicationDTO applicationDTO) {
|
|
|
+ String appName = applicationDTO.getName();
|
|
|
+ String projectName = applicationDTO.getProjectName();
|
|
|
+
|
|
|
+ IngressDTO ingressDTO = new IngressDTO();
|
|
|
+ ingressDTO.setName(appName);
|
|
|
+ ingressDTO.setNamespace(projectName);
|
|
|
+ ingressDTO.setHttpHost("deernowl.cn");
|
|
|
+
|
|
|
+ //@todo lambda表达式解决
|
|
|
+ Set<IngressDTO.IngressDetailDTO> detailDTOS = new HashSet<>();
|
|
|
+ for (ContainerDTO containerDTO : applicationDTO.getContainers()) {
|
|
|
+ String containerName = containerDTO.getName();
|
|
|
+
|
|
|
+ Set<IngressDTO.IngressDetailDTO> ingressDetailDTOS = containerDTO.getPorts()
|
|
|
+ .stream()
|
|
|
+ .filter(PortDTO::isExpose)
|
|
|
+ .map(portDTO ->{
|
|
|
+ IngressDTO.IngressDetailDTO ingressDetailDTO = new IngressDTO.IngressDetailDTO();
|
|
|
+ ingressDetailDTO.setServiceName(applicationDTO.getName());
|
|
|
+ ingressDetailDTO.setPort(portDTO.getPort());
|
|
|
+ ingressDetailDTO.setPath("/" + appName + "/" + containerName);
|
|
|
+ return ingressDetailDTO;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ detailDTOS.addAll(ingressDetailDTOS);
|
|
|
+ }
|
|
|
+ ingressDTO.setHttpRules(detailDTOS);
|
|
|
+ return ingressDTO;
|
|
|
+ }
|
|
|
}
|