增加容器集群创建功能接口

develop
tanshaolong 2024-09-06 16:02:36 +08:00
parent 1c9cfb6a85
commit 5e0fdf4016
16 changed files with 210 additions and 31 deletions

View File

@ -1,6 +1,6 @@
package com.bocloud.ctstack.plugin.controller;
import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.entity.ContainerCluster;
import com.bocloud.ctstack.plugin.service.ContainerService;
import com.megatron.common.model.GeneralResult;
@ -41,10 +41,10 @@ public class ContainerController {
* @param context
* @return
*/
// @Operation(tags = {"CMC", "CSC"}, summary = "创建集群")
// @PostMapping
// public GeneralResult<ContainerCluster> create(@RequestBody ContainerClusterModel model,
// @Value(Common.REQ_CONTEXT) RequestContext context) {
// return containerService.create(model, context);
// }
@Operation(tags = {"CMC", "CSC"}, summary = "创建集群")
@PostMapping
public GeneralResult<ContainerCluster> create(@RequestBody ContainerClusterModel model,
@Value(Common.REQ_CONTEXT) RequestContext context) {
return containerService.create(model, context);
}
}

View File

@ -1,5 +1,6 @@
package com.bocloud.ctstack.plugin.domain.model;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.database.RdsModel;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,6 +1,5 @@
package com.bocloud.ctstack.plugin.domain.model;
package com.bocloud.ctstack.plugin.domain.model.container;
import cn.hutool.core.date.DateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -32,4 +31,28 @@ public class ContainerClusterModel {
private Long businessId;
private List<ContainerNodeModel> nodeModels;
private List<ContainerNodePoolModel> nodePoolModels;
// 平台相同数据
private Long vendorId;
private String subnetUuid;
private String networkPlugin;
private String clusterDomain;
private String securityGroupUuid;
private Long startPort;
private Long endPort;
private String elbProdCode;
private String billMode; // 计费模式1为包周期2为按需
private String containerRuntime;
private String timezone;
private String deployType;
private String azInfo;
private String kubeProxy;
private String hostType;
private String mirrorCategoryName;
private String clusterSeries;
// 主节点信息
private ContainerHostModel masterHost;
// 从节点信息
private ContainerHostModel slaveHost;
}

View File

@ -0,0 +1,16 @@
package com.bocloud.ctstack.plugin.domain.model.container;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContainerEBSVoModel {
private Long itemDefId; // 系统盘id
private String itemDefName; // 系统盘名字
private String itemDefType; // 系统盘类型
private String resType; // 主机类型
private Integer size; // 系统盘大小单位为Gs
}

View File

@ -0,0 +1,18 @@
package com.bocloud.ctstack.plugin.domain.model.container;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContainerHostModel {
private Long cpu;
private Long memory;
private String itemDefType;
private String resType;
private String size;
private ContainerEBSVoModel sysEBSVo;
private ContainerEBSVoModel dataEBSVo;
}

View File

@ -1,4 +1,4 @@
package com.bocloud.ctstack.plugin.domain.model;
package com.bocloud.ctstack.plugin.domain.model.container;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -1,4 +1,4 @@
package com.bocloud.ctstack.plugin.domain.model;
package com.bocloud.ctstack.plugin.domain.model.container;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -5,6 +5,9 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.domain.Butler;
import com.bocloud.ctstack.plugin.domain.model.*;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.domain.model.database.ProjectModel;
import com.bocloud.ctstack.plugin.domain.model.database.RdsModel;
import com.bocloud.ctstack.plugin.provider.TianyiProvider;

View File

@ -3,7 +3,7 @@ package com.bocloud.ctstack.plugin.provider.container;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.domain.Butler;
import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.provider.TianyiProvider;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerClusterConvertor;
@ -80,12 +80,53 @@ public class TianYiContainerClusterProvider extends TianyiProvider {
body.put("resPoolId", model.getResourceGroupUuid());
// clusterBaseV2VO参数
JSONObject clusterBaseV2VO = new JSONObject();
clusterBaseV2VO.put("subnetUuid", model.getSubnetUuid());
clusterBaseV2VO.put("networkPlugin", model.getNetworkPlugin());
clusterBaseV2VO.put("clusterDomain", model.getClusterDomain());
clusterBaseV2VO.put("securityGroupUuid", model.getSecurityGroupUuid());
clusterBaseV2VO.put("startPort", model.getStartPort());
clusterBaseV2VO.put("endPort", model.getEndPort());
clusterBaseV2VO.put("elbProdCode", model.getElbProdCode());
clusterBaseV2VO.put("billMode", model.getBillMode());
clusterBaseV2VO.put("containerRuntime", model.getContainerRuntime());
clusterBaseV2VO.put("timezone", model.getTimezone());
clusterBaseV2VO.put("clusterVersion", model.getVersion());
clusterBaseV2VO.put("deployType", model.getDeployType());
clusterBaseV2VO.put("azInfo", model.getAzInfo());
clusterBaseV2VO.put("vpcUuid", model.getVpcUuid());
clusterBaseV2VO.put("clusterName", model.getClusterUuid());
clusterBaseV2VO.put("kubeProxy", model.getKubeProxy());
clusterBaseV2VO.put("hostType", model.getHostType());
clusterBaseV2VO.put("mirrorCategoryName", model.getMirrorCategoryName());
clusterBaseV2VO.put("clusterSeries", model.getClusterSeries());
body.put("clusterBaseV2VO", clusterBaseV2VO);
// masterHost参数
JSONObject masterHost = new JSONObject();
masterHost.put("cpu", model.getMasterHost().getCpu());
masterHost.put("mem", model.getMasterHost().getMemory());
masterHost.put("itemDefType", model.getMasterHost().getItemDefType());
masterHost.put("resType", model.getMasterHost().getResType());
masterHost.put("size", model.getMasterHost().getSize());
JSONObject sysEBSVo = new JSONObject();
sysEBSVo.put("size", model.getMasterHost().getSysEBSVo().getSize());
masterHost.put("sysEBSVo", sysEBSVo);
JSONObject dataEBSVo = new JSONObject();
dataEBSVo.put("size", model.getMasterHost().getDataEBSVo().getSize());
masterHost.put("dataEBSVo", dataEBSVo);
body.put("masterHost", masterHost);
// slaveHost参数
JSONObject slaveHost = new JSONObject();
slaveHost.put("cpu", model.getSlaveHost().getCpu());
slaveHost.put("mem", model.getSlaveHost().getMemory());
slaveHost.put("itemDefType", model.getSlaveHost().getItemDefType());
slaveHost.put("resType", model.getSlaveHost().getResType());
slaveHost.put("size", model.getSlaveHost().getSize());
sysEBSVo = new JSONObject();
sysEBSVo.put("size", model.getSlaveHost().getSysEBSVo().getSize());
slaveHost.put("sysEBSVo", sysEBSVo);
dataEBSVo = new JSONObject();
dataEBSVo.put("size", model.getSlaveHost().getDataEBSVo().getSize());
slaveHost.put("dataEBSVo", dataEBSVo);
body.put("slaveHost", slaveHost);
Result result = doPost(url, headers, body);

View File

@ -3,8 +3,8 @@ package com.bocloud.ctstack.plugin.provider.container;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.domain.Butler;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.provider.TianyiProvider;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodeConvertor;

View File

@ -1,11 +1,8 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ContainerClusterConvertor implements BeanConvertor<ContainerClusterModel, JSONObject> {

View File

@ -1,7 +1,7 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodeModel;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,7 +1,7 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,11 +1,15 @@
package com.bocloud.ctstack.plugin.service;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.entity.ContainerCluster;
import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.GridBean;
import com.megatron.common.model.Pager;
import com.megatron.common.model.RequestContext;
public interface ContainerService {
GeneralResult<GridBean<ContainerCluster>> list(Pager pager);
GeneralResult<ContainerCluster> create(ContainerClusterModel model, RequestContext context);
}

View File

@ -1,27 +1,33 @@
package com.bocloud.ctstack.plugin.service.impl;
import com.bocloud.cmp.entity.Server;
import com.bocloud.cmp.entity.ServerConfig;
import com.bocloud.ctstack.plugin.entity.Cluster;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.CloudVendor;
import com.bocloud.cmp.repository.CloudVendorRepository;
import com.bocloud.cmp.util.ResourceEventPublisher;
import com.bocloud.ctstack.plugin.config.ButlerConfig;
import com.bocloud.ctstack.plugin.domain.model.VpcModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.database.CreateRdsModel;
import com.bocloud.ctstack.plugin.domain.model.database.RdsModel;
import com.bocloud.ctstack.plugin.entity.ContainerCluster;
import com.bocloud.ctstack.plugin.provider.container.TianYiContainerClusterProvider;
import com.bocloud.ctstack.plugin.provider.database.TianyiRdsProvider;
import com.bocloud.ctstack.plugin.repository.ContainerClusterRepository;
import com.bocloud.ctstack.plugin.service.ContainerService;
import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.GridBean;
import com.megatron.common.model.Pager;
import com.megatron.common.model.SimpleBean;
import com.megatron.common.model.*;
import com.megatron.common.utils.Common;
import com.megatron.common.utils.GridHelper;
import com.megatron.common.utils.ListTool;
import com.megatron.common.utils.MapTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service("containerService")
@ -30,6 +36,21 @@ public class ContainerServiceImpl implements ContainerService {
@Autowired
private ContainerClusterRepository containerClusterRepository;
@Autowired
private ResourceEventPublisher resourceEventPublisher;
@Autowired
private CloudVendorRepository cloudVendorRepository;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
@Autowired
private ButlerConfig butlerConfig;
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public GeneralResult<GridBean<ContainerCluster>> list(Pager pager) {
GridBean gridBean = null;
@ -49,4 +70,56 @@ public class ContainerServiceImpl implements ContainerService {
return new GeneralResult<>(false, "查询容器集群失败");
}
}
public GeneralResult<ContainerCluster> create(ContainerClusterModel model, RequestContext context) {
try {
CloudVendor vendor = cloudVendorRepository.query(model.getVendorId());
Assert.notNull(vendor, "指定云平台不存在");
taskExecutor.submit(() -> {
String operate = vendor.getType().toLowerCase() + ".cloudrds";
String content = "容器集群创建成功!";
boolean result = true;
try {
TianYiContainerClusterProvider provider =
new TianYiContainerClusterProvider(butlerConfig.regionButler(vendor.getUuid(), model.getRegionId()));
GeneralResult createResult = provider.create(model);
if (createResult.isFailed()) {
result = false;
content = createResult.getMessage();
} else {
// 保存新创建的容器集群信息。
ContainerClusterModel clusterModel =
JSONObject.parseObject(JSONObject.toJSONString(createResult.getData()), ContainerClusterModel.class);
ContainerCluster containerCluster = new ContainerCluster();
BeanUtils.copyProperties(clusterModel, containerCluster);
containerCluster.setVendorId(vendor.getId());
containerCluster.setRegionId(model.getRegionId());
containerCluster.setMenderId(context.getTarget());
containerCluster.setCreatorId(context.getTarget());
containerCluster.setTenantId(context.getTenant());
containerCluster.setProjectId(context.getProject());
containerClusterRepository.save(containerCluster);
// TODO: 更新容器集群下的节点和节点池信息
// 发送给云平台统计队列对云平台重新统计
JSONObject jsonObject = new JSONObject();
jsonObject.put(Common.VENDORID, vendor.getId());
amqpTemplate.convertAndSend("vendor.statistics.queue", jsonObject);
}
} catch (Exception e) {
log.error("create cloud container failure:", e);
result = false;
content = e.getMessage();
}
resourceEventPublisher.send(new OperateResult(result, content, operate,
MapTools.simpleMap(Common.VENDORID, vendor.getId()),
OperateResult.OperateCategory.ResourceEvent, context));
});
return new GeneralResult<>(true, "任务已下发,正在执行...");
} catch (Exception e) {
log.error("create container cluster failure:", e);
return new GeneralResult<ContainerCluster>(false, "创建容器集群失败", null, null);
}
}
}

View File

@ -8,6 +8,9 @@ import com.bocloud.cmp.entity.PhysicalServer;
import com.bocloud.cmp.entity.Server;
import com.bocloud.cmp.entity.ServerConfig;
import com.bocloud.cmp.enums.ResourceCategory;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.repository.PhysicalServerRepository;
import com.bocloud.cmp.repository.ServerRepository;
import com.bocloud.ctstack.plugin.config.ButlerConfig;