package nju.seec.SEECdemo.web.dto; import lombok.Data; import nju.seec.SEECdemo.logic.api.k8s.dto.DeploymentDTO; import nju.seec.SEECdemo.logic.api.k8s.dto.ServiceDTO; import org.hibernate.validator.constraints.Range; import javax.validation.constraints.NotEmpty; import java.util.*; import java.util.stream.Collectors; import static nju.seec.SEECdemo.util.Constants.APPLICATION_LABEL; @Data public class ApplicationDTO { @NotEmpty private String name; @NotEmpty private String projectName; @Range(min = 1, max = 3) private int replicas; private List containers = new ArrayList<>(); @Range(min = 0, max = 100) private int cpuRequest; /** * 存储的默认单位为M */ @Range(min = 0) private int memoryRequest; @Range(min = 0) private int storageRequest; public DeploymentDTO toDeploymentDTO(){ DeploymentDTO deploymentDTO = new DeploymentDTO(); deploymentDTO.setNamespace(projectName); deploymentDTO.setName(name); deploymentDTO.setContainers(containers.stream().map(ContainerDTO::toContainerDTO).collect(Collectors.toList())); deploymentDTO.setReplicas(replicas); deploymentDTO.setLabels(Collections.singletonMap(APPLICATION_LABEL, name)); //@todo 设置资源限额 return deploymentDTO; } public ServiceDTO toServiceDTO() { ServiceDTO serviceDTO = new ServiceDTO(); serviceDTO.setNamespace(projectName); serviceDTO.setName(name); serviceDTO.setSelectors(Collections.singletonMap(APPLICATION_LABEL, name)); serviceDTO.setServicePorts( containers .stream() .map(ContainerDTO::getPorts) .flatMap(Collection::stream) .map(PortDTO::toServicePortDTO) .collect(Collectors.toSet()) ); return serviceDTO; } }