diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java new file mode 100644 index 00000000..03a64145 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java @@ -0,0 +1,50 @@ +package com.bocloud.ctstack.plugin.controller; + +import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel; +import com.bocloud.ctstack.plugin.entity.ContainerCluster; +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.RequestContext; +import com.megatron.common.utils.Common; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +@Tag(name = "容器集群管理") +@RestController +@RequestMapping("/v1/containers") +public class ContainerController { + + @Autowired + private ContainerService containerService; + + /** + * 查看集群列表 + * + * @param pager + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "查询集群列表") + @GetMapping + public GeneralResult> list(Pager pager) { + return containerService.list(pager); + } + + /** + * 添加集群 + * + * @param model + * @param context + * @return + */ +// @Operation(tags = {"CMC", "CSC"}, summary = "创建集群") +// @PostMapping +// public GeneralResult create(@RequestBody ContainerClusterModel model, +// @Value(Common.REQ_CONTEXT) RequestContext context) { +// return containerService.create(model, context); +// } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerClusterModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerClusterModel.java new file mode 100644 index 00000000..7330ecb6 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerClusterModel.java @@ -0,0 +1,35 @@ +package com.bocloud.ctstack.plugin.domain.model; + +import cn.hutool.core.date.DateTime; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ContainerClusterModel { + + private String name; + private String remark; + private String clusterSpec; + private String status; + private String regionId; + private String clusterUuid; + private String clusterType; + private String version; + private Date expiredTime; + private String nodeCidr; + private String vpcUuid; + private Date created; + private String eipUuid; + private String loadBalancerUuid; + private String resourceGroupUuid; + private String serviceCidr; + private Long businessId; + private List nodeModels; + private List nodePoolModels; +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodeModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodeModel.java new file mode 100644 index 00000000..7e2d09b2 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodeModel.java @@ -0,0 +1,19 @@ +package com.bocloud.ctstack.plugin.domain.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ContainerNodeModel { + private String nodeName; + private String nodeStatus; + private String regionId; + private String clusterName; + private String clusterUuid; + private Date created; +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodePoolModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodePoolModel.java new file mode 100644 index 00000000..6cfd1eea --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContainerNodePoolModel.java @@ -0,0 +1,35 @@ +package com.bocloud.ctstack.plugin.domain.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ContainerNodePoolModel { + private String name; + private String nodepoolUuid; + private String remark; + private String status; + private String regionId; + private String clusterUuid; + private String clusterName; + private boolean isDefault; + private String type; + private Long totalNodes; + private Long healthyNodes; + private Long failedNodes; + private Date created; + private String kubernetesConfig; + private String scalingGroup; + // 天翼云的数据 + private Long cpu; + private Long memory; + private String vmSpecName; // 节点规格 + private String vmSpecType; // 节点规格类型 + private String vmSpecId; // 节点规格id + +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContarinerClusterModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContarinerClusterModel.java deleted file mode 100644 index c93a2928..00000000 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/ContarinerClusterModel.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.bocloud.ctstack.plugin.domain.model; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class ContarinerClusterModel { - - private String name; -} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/TianyiSyncModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/TianyiSyncModel.java index 4f5988e0..b5813128 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/TianyiSyncModel.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/TianyiSyncModel.java @@ -35,7 +35,7 @@ public class TianyiSyncModel { private List physicalModels; private ResourcePoolModel resourcePoolModel; private List rdsModels; - private List contarinerClusterModels; + private List containerClusterModels; /** * @param serverModels * @param imageModels diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java new file mode 100644 index 00000000..2c37c80d --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java @@ -0,0 +1,75 @@ +package com.bocloud.ctstack.plugin.entity; + +import com.megatron.entity.annotations.Column; +import com.megatron.entity.annotations.PK; +import com.megatron.entity.annotations.Table; +import com.megatron.entity.bean.GenericEntity; +import com.megatron.entity.meta.PKStrategy; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Table("container_cluster") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ContainerCluster extends GenericEntity { + + @PK(value = PKStrategy.AUTO) + private Long id; + @Column("vendor_id") + private Long vendorId; // 云供应商ID + + @Column("tenant_id") + private Long tenantId; + + @Column("project_id") + private Long projectId; + + @Column("cluster_uuid") + private String clusterUuid; + + @Column("region_id") + private String regionId; + + @Column("cluster_spec") + private String clusterSpec; + + @Column("version") + private String version; + + @Column("service_cidr") + private String serviceCidr; + + @Column("node_cidr") + private String nodeCidr; + + @Column("created") + private Date created; // 创建时间 + + @Column("business_id") + private Long businessId; + + @Column("resource_group_uuid") + private String resourceGroupUuid; + + @Column("eip_uuid") + private String eipUuid; + + @Column("expired_time") + private Date expiredTime; + + @Column("load_balancer_uuid") + private String loadBalancerUuid; + + @Column("vpc_uuid") + private String vpcUuid; + + @Column("recycle_time") + private Date recycleTime; + + @Column("is_recycle") + private Boolean isRecycle; +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java new file mode 100644 index 00000000..34f3241a --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java @@ -0,0 +1,39 @@ +package com.bocloud.ctstack.plugin.entity; + +import com.megatron.entity.annotations.Column; +import com.megatron.entity.annotations.PK; +import com.megatron.entity.annotations.Table; +import com.megatron.entity.bean.GenericEntity; +import com.megatron.entity.meta.PKStrategy; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Table("container_cluster_node") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ContainerClusterNode extends GenericEntity { + + @PK(value = PKStrategy.AUTO) + private Long id; + @Column("vendor_id") + private Long vendorId; // 云供应商ID + + @Column("node_name") + private String nodeName; + + @Column("region_id") + private String regionId; + + @Column("cluster_uuid") + private String clusterUuid; + + @Column("node_status") + private String nodeStatus; + + @Column("created") + private Date created; // 创建时间 +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNodePool.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNodePool.java new file mode 100644 index 00000000..213dcf99 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNodePool.java @@ -0,0 +1,74 @@ +package com.bocloud.ctstack.plugin.entity; + +import com.megatron.entity.annotations.Column; +import com.megatron.entity.annotations.PK; +import com.megatron.entity.annotations.Table; +import com.megatron.entity.bean.GenericEntity; +import com.megatron.entity.meta.PKStrategy; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Table("container_cluster_node_pool") +@Data +@NoArgsConstructor +@AllArgsConstructor +public class ContainerClusterNodePool extends GenericEntity { + + @PK(value = PKStrategy.AUTO) + private Long id; + @Column("vendor_id") + private Long vendorId; // 云供应商ID + + @Column("region_id") + private String regionId; + + @Column("tenant_id") + private Long tenantId; + + @Column("project_id") + private Long projectId; + + @Column("nodepool_uuid") + private String nodePoolUuid; + + @Column("cluster_uuid") + private String clusterUuid; + + @Column("failed_nodes") + private Long failedNodes; + + @Column("healthy_nodes") + private Long healthyNodes; + + @Column("total_nodes") + private Long totalNodes; + + @Column("created") + private Date created; // 创建时间 + + @Column("kubernetes_config") + private String kubernetesConfig; + + @Column("scaling_group") + private String scalingGroup; + + // 天翼云使用字段 开始 + @Column("vm_spec_type") + private String vmSpecType; + + @Column("vm_spec_name") + private String vmSpecName; + + @Column("vm_spec_id") + private String vmSpecId; + + @Column("cpu") + private Long cpu; + + @Column("memory") + private Long memory; + // 天翼云使用字段 结束 +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java index 1d21587b..eec80cad 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java @@ -100,6 +100,16 @@ public class TianyiProvider { return httpClient.get(header, params, uri); } + public Result doGet2(String url, Map headers, Map params) { + HttpClient httpClient = new HttpClient(60 * 1000, PostDataFormat.RAW); + httpClient.setDataFormat(null); + + String uri = "http://ccse-global.ctapi.ctyun.local:31167" + url; + Map header = buildHeader(HttpRequestMethod.GET, url, headers, params); + printCurl(HttpRequestMethod.GET, uri, header, params); + return httpClient.get(header, params, uri); + } + public Result doGetWithBody(String url, Map headers, Map params) { try { //创建httpclient对象 @@ -150,6 +160,20 @@ public class TianyiProvider { return httpClient.post(header, params, uri); } + public Result doPut(String url, Map headers, Map params) { + HttpClient httpClient = new HttpClient(60 * 1000, PostDataFormat.RAW); + String uri = buildUrl(url); + Map header = buildHeader(HttpRequestMethod.PUT, url, headers, params); + printCurl(HttpRequestMethod.PUT, uri, header, params); + return httpClient.post(header, params, uri); + } + + public Result doDelete(String url, Map headers) { + HttpClient httpClient = new HttpClient(20 * 1000, PostDataFormat.RAW); + httpClient.setDataFormat(null); + return httpClient.get(buildHeader(HttpRequestMethod.DELETE, url, headers, null), null, buildUrl(url)); + } + public String getCtUserId() { return ctUserId; } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java index 2040c0e8..ada49d8c 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java @@ -12,6 +12,7 @@ import com.bocloud.ctstack.plugin.provider.compute.TianyiImageProvider; import com.bocloud.ctstack.plugin.provider.compute.TianyiPhysicalProvider; import com.bocloud.ctstack.plugin.provider.compute.TianyiServerProvider; import com.bocloud.ctstack.plugin.provider.container.TianYiContainerClusterProvider; +import com.bocloud.ctstack.plugin.provider.container.TianYiContainerNodeProvider; import com.bocloud.ctstack.plugin.provider.database.TianyiRdsProvider; import com.bocloud.ctstack.plugin.provider.network.TianyiFloatingIpProvider; import com.bocloud.ctstack.plugin.provider.network.TianyiSecurityGroupProvider; @@ -293,14 +294,32 @@ public class TianyiLocationProvider extends TianyiProvider { syncModel.setImageModels((List) imageList.getData()); } - - // 同步镜像 + // 同步容器 log.info("获取容器信息..."); TianYiContainerClusterProvider tianYiContainerClusterProvider = new TianYiContainerClusterProvider(this.getButler()); GeneralResult containerClusterList = tianYiContainerClusterProvider.list(); log.info("同步容器结果:{}",containerClusterList.isSuccess()); if (containerClusterList.isSuccess()) { - syncModel.setContarinerClusterModels((List) containerClusterList.getData()); + List containerClusterModels = (List) containerClusterList.getData(); + for (ContainerClusterModel clusterModel : containerClusterModels) { + // 同步容器节点信息 + log.info("获取容器节点信息..."); + TianYiContainerNodeProvider tianYiContainerNodeProvider = new TianYiContainerNodeProvider(this.getButler()); + GeneralResult clusterNodeList = tianYiContainerNodeProvider.listNodes(clusterModel.getName()); + log.info("同步容器节点结果:{}",clusterNodeList.isSuccess()); + if (clusterNodeList.isSuccess()) { + clusterModel.setNodeModels((List) clusterNodeList.getData()); + } + + // 同步容器节点池信息 + log.info("获取容器节点池信息..."); + GeneralResult clusterNodePoolList = tianYiContainerNodeProvider.listNodePools(clusterModel.getName()); + log.info("同步容器节点池结果:{}",clusterNodePoolList.isSuccess()); + if (clusterNodePoolList.isSuccess()) { + clusterModel.setNodePoolModels((List) clusterNodePoolList.getData()); + } + } + syncModel.setContainerClusterModels(containerClusterModels); } // 同步云硬盘 diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java index 4eec0800..90de90de 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java @@ -1,10 +1,9 @@ package com.bocloud.ctstack.plugin.provider.container; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bocloud.cmp.domain.Butler; -import com.bocloud.ctstack.plugin.domain.model.ContarinerClusterModel; +import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel; import com.bocloud.ctstack.plugin.provider.TianyiProvider; import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor; import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerClusterConvertor; @@ -12,6 +11,7 @@ import com.megatron.common.model.GeneralResult; import com.megatron.common.model.Result; import lombok.extern.slf4j.Slf4j; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -20,7 +20,7 @@ import java.util.Map; public class TianYiContainerClusterProvider extends TianyiProvider { - BeanConvertor containerClusterConvertor = new ContainerClusterConvertor(); + BeanConvertor containerClusterConvertor = new ContainerClusterConvertor(); Butler butler = null; @@ -37,18 +37,112 @@ public class TianYiContainerClusterProvider extends TianyiProvider { public GeneralResult list() { try { - String url = "/v1.1/ccse/clusters/page"; + Integer pageNow = 1; + Integer pageSize = 100; + JSONObject body = new JSONObject(); + body.put("pageSize", pageSize); Map header = new HashMap<>(); header.put("regionId", this.getRegionId()); - Result result = doGet(url, header); - log.info("result:{}", JSON.toJSONString(result)); - JSONObject checkedResult = checkResult(result, "查询容器集群列表"); - log.info("checkedResult:{}", JSON.toJSONString(checkedResult)); - List records = JSONObject.parseObject(checkedResult.getString("records"), List.class); + String url = "/v1.1/ccse/clusters/page"; + List records = new ArrayList<>(); + + while (true) { + body.put("pageNow", pageNow); + Result result = doGet2(url, header, body); + JSONObject returnObj = checkResult(result, "查询容器集群列表"); + log.info("checkedResult:{}", JSON.toJSONString(returnObj)); + records.addAll(JSONObject.parseObject(returnObj.getString("records"), List.class)); + pageNow++; + Integer current = returnObj.getInteger("current"); + Integer pages = returnObj.getInteger("pages"); + if (current == pages) { + break; + } + } + return new GeneralResult(true, containerClusterConvertor.convertList(records), "查询成功"); } catch (Exception e) { log.error("查询容器集群列表失败 : " + e); return new GeneralResult(false, "查询容器集群列表失败", e.getMessage()); } } + + public GeneralResult create(ContainerClusterModel model) { + try { + String url = "/v1.1/ccse/clusters/createByPaas"; + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + + // 容器body参数 + JSONObject body = new JSONObject(); + body.put("reqType", "2"); // 2为天翼云, 1桌面云 + body.put("resPoolId", model.getResourceGroupUuid()); + // clusterBaseV2VO参数 + JSONObject clusterBaseV2VO = new JSONObject(); + body.put("clusterBaseV2VO", clusterBaseV2VO); + // masterHost参数 + JSONObject masterHost = new JSONObject(); + body.put("masterHost", masterHost); + // slaveHost参数 + JSONObject slaveHost = new JSONObject(); + body.put("slaveHost", slaveHost); + + Result result = doPost(url, headers, body); + JSONObject returnObj = checkResult(result, "创建容器集群"); + // TODO: 等待订单完成 + return detail(model.getName()); + } catch (Exception e) { + log.error("创建容器集群失败 : " + e); + return new GeneralResult(false, "创建容器集群失失败", e.getMessage()); + } + } + + public GeneralResult remove(String poolId, String InstanceId) { + try { + String url = "/v1.1/ccse/clusters/deleteByPaas"; + JSONObject body = new JSONObject(); + body.put("resPoolId", poolId); + body.put("prodInstId", InstanceId); + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + Result result = doPost(url, headers, body); + checkResult(result, "删除容器集群"); + return new GeneralResult(true, "删除容器集群成功"); + } catch (Exception e) { + log.error("删除容器集群失败 : " + e); + return new GeneralResult(false, "删除容器集群失败", e.getMessage()); + } + } + + public GeneralResult detail(String clusterName) { + try { + String apiUrl = "/v1.1/ccse/clusters/" + clusterName; + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + Result result = doGet(apiUrl, headers); + JSONObject returnObj = checkResult(result, "查询容器集群详情"); + // TODO: + ContainerClusterModel model = JSONObject.parseObject(JSONObject.toJSONString(returnObj), ContainerClusterModel.class); + return new GeneralResult(true, model, "查询容器集群详情成功"); + } catch (Exception e) { + log.error("查询容器集群详情失败 : " + e); + return new GeneralResult(false, null, "查询容器集群详情失败"); + } + } + + private GeneralResult modifyDesc(String clusterName, String clusterDesc) { + try { + String apiUrl = "/ccse/clusters/" + clusterName; + JSONObject body = new JSONObject(); + body.put("clusterDesc", clusterDesc); + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + Result result = doPut(apiUrl, headers, body); + checkResult(result, "修改集群描述"); + return detail(clusterName); + } catch (Exception e) { + log.error("修改容器集群失败 : " + e); + return new GeneralResult(false, "修改容器集群失败", e.getMessage()); + } + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java new file mode 100644 index 00000000..b9210e77 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java @@ -0,0 +1,136 @@ +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.provider.TianyiProvider; +import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor; +import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodeConvertor; +import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodePoolConvertor; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.Result; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +public class TianYiContainerNodeProvider extends TianyiProvider { + BeanConvertor nodeConvertor = new ContainerNodeConvertor(); + BeanConvertor nodePoolConvertor = new ContainerNodePoolConvertor(); + + Butler butler = null; + + /** + * 构造方法,所有的云平台驱动都必须要实现或重写该构造方法 + * + * @param butler 云平台认证信息 + */ + public TianYiContainerNodeProvider(Butler butler) { + super(butler); + this.butler = butler; + } + + public GeneralResult listNodes(String clusterName) { + try { + String url = "/v1.1/ccse/clusters/" + clusterName + "/nodes/list"; + Map header = new HashMap<>(); + header.put("regionId", this.getRegionId()); + + Result result = doGet(url, header); + JSONObject returnObj = checkResult(result, "容器集群节点列表"); + return new GeneralResult(true, nodeConvertor.convertList(JSONArray.parseArray(returnObj.toJSONString(), JSONObject.class)), "查询容器集群节点列表成功"); + } catch (Exception e) { + log.error("查询容器集群节点列表失败 : " + e); + return new GeneralResult(false, "查询容器集群节点列表失败", e.getMessage()); + } + } + + public GeneralResult createNodePool(ContainerNodePoolModel poolModel) { + try { + String url = "/ccse/clusters/" + poolModel.getClusterName() + "/nodePool"; + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + + // 容器body参数 + JSONObject body = new JSONObject(); + body.put("nodePoolName", poolModel.getName()); + body.put("vmSpecName", poolModel.getVmSpecName()); + body.put("vmSpecId", poolModel.getVmSpecId()); + body.put("cpu", poolModel.getCpu()); + body.put("memory", poolModel.getMemory()); + + Result result = doPost(url, headers, body); + JSONObject returnObj = checkResult(result, "创建容器集群"); + + return detailNodePool(poolModel.getClusterName(), poolModel.getName()); + } catch (Exception e) { + log.error("创建容器集群节点池失败 : " + e); + return new GeneralResult(false, "创建容器集群节点池失败", e.getMessage()); + } + } + + public GeneralResult detailNodePool(String clusterName, String poolName) { + try { + String apiUrl = "/v1.1/ccse/clusters/" + clusterName + "/nodePool/" + poolName; + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + Result result = doGet(apiUrl, headers); + JSONObject returnObj = checkResult(result, "查询容器节点池详情"); + ContainerNodePoolModel poolModel = nodePoolConvertor.convertModel(returnObj); + return new GeneralResult(true, poolModel, "查询容器节点池详情成功"); + } catch (Exception e) { + log.error("查询容器节点池详情失败 : " + e); + return new GeneralResult(false, null, "查询容器节点池详情失败"); + } + } + + public GeneralResult listNodePools(String clusterName) { + try { + Integer pageNow = 1; + Integer pageSize = 100; + String url = "/v1.1/ccse/clusters/" + clusterName + "/nodePool/page"; + JSONObject body = new JSONObject(); + body.put("pageSize", pageSize); + Map header = new HashMap<>(); + header.put("regionId", this.getRegionId()); + List records = new ArrayList<>(); + + while (true) { + body.put("pageNow", pageNow); + Result result = doGet(url, header, body); + JSONObject returnObj = checkResult(result, "容器集群节点池列表"); + records.addAll(JSONObject.parseObject(returnObj.getString("records"), List.class)); + pageNow++; + Integer current = returnObj.getInteger("current"); + Integer pages = returnObj.getInteger("pages"); + if (current == pages) { + break; + } + } + + return new GeneralResult(true, nodePoolConvertor.convertList(records), "查询容器集群节点池列表成功"); + } catch (Exception e) { + log.error("查询容器集群节点池列表失败 : " + e); + return new GeneralResult(false, "查询容器集群节点池列表失败", e.getMessage()); + } + } + + public GeneralResult deleteNodePool(String clusterName, String poolName) { + try { + String url = "/ccse/clusters/" + clusterName + "/nodePool/" + poolName + "/delete"; + Map headers = new HashMap<>(); + headers.put("regionId", this.getRegionId()); + Result result = doDelete(url, headers); + checkResult(result, "删除容器节点资源池"); + return new GeneralResult(true, "删除容器节点资源池成功"); + } catch (Exception e) { + log.error("删除容器节点资源池失败 : " + e); + return new GeneralResult(false, "删除容器节点资源池失败", e.getMessage()); + } + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerClusterConvertor.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerClusterConvertor.java index acbc7704..cedb8c24 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerClusterConvertor.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerClusterConvertor.java @@ -1,31 +1,47 @@ package com.bocloud.ctstack.plugin.provider.tianyiconvertor; import com.alibaba.fastjson.JSONObject; -import com.bocloud.ctstack.plugin.domain.model.ContarinerClusterModel; +import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel; -import java.util.ArrayList; -import java.util.List; - -public class ContainerClusterConvertor implements BeanConvertor { +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; +public class ContainerClusterConvertor implements BeanConvertor { @Override - public List convertList(List list) { - List contarinerClusterModels = new ArrayList<>(); + public List convertList(List list) { + List containerClusterModels = new ArrayList<>(); for (JSONObject object : list) { - contarinerClusterModels.add(convertModel(object)); + containerClusterModels.add(convertModel(object)); } - return contarinerClusterModels; + return containerClusterModels; } @Override - public ContarinerClusterModel convertModel(JSONObject jsonObject) { + public ContainerClusterModel convertModel(JSONObject jsonObject) { if (null == jsonObject) return null; if (jsonObject.isEmpty()) return null; - ContarinerClusterModel contarinerClusterModel = new ContarinerClusterModel(); + ContainerClusterModel containerClusterModel = new ContainerClusterModel(); - contarinerClusterModel.setName(jsonObject.getString("clusterName")); + containerClusterModel.setName(jsonObject.getString("clusterName")); + // 容器集群是没有uuid的,只能把唯一的集群名作为它的UUID + containerClusterModel.setClusterUuid(jsonObject.getString("clusterName")); + containerClusterModel.setRemark(jsonObject.getString("clusterDesc")); + containerClusterModel.setClusterSpec(jsonObject.getString("deployMode")); + containerClusterModel.setVersion(jsonObject.getString("clusterVersion")); + containerClusterModel.setStatus(jsonObject.getString("clusterStatus")); + containerClusterModel.setServiceCidr(jsonObject.getString("serviceCidr")); + containerClusterModel.setNodeCidr(jsonObject.getString("podCidr")); + containerClusterModel.setCreated(jsonObject.getDate("createdTime")); + containerClusterModel.setBusinessId(jsonObject.getLong("bizState")); + containerClusterModel.setResourceGroupUuid(jsonObject.getString("resPoolId")); + containerClusterModel.setEipUuid(jsonObject.getString("eip")); + containerClusterModel.setExpiredTime(jsonObject.getDate("expireTime")); + containerClusterModel.setLoadBalancerUuid(jsonObject.getString("masterSlbIp")); + containerClusterModel.setVpcUuid(jsonObject.getString("vpcId")); - return contarinerClusterModel; + return containerClusterModel; } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodeConvertor.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodeConvertor.java new file mode 100644 index 00000000..54ac320d --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodeConvertor.java @@ -0,0 +1,33 @@ +package com.bocloud.ctstack.plugin.provider.tianyiconvertor; + +import com.alibaba.fastjson.JSONObject; +import com.bocloud.ctstack.plugin.domain.model.ContainerNodeModel; + +import java.util.ArrayList; +import java.util.List; + +public class ContainerNodeConvertor implements BeanConvertor { + + + @Override + public List convertList(List list) { + List containerNodeModels = new ArrayList<>(); + for (JSONObject object : list) { + containerNodeModels.add(convertModel(object)); + } + return containerNodeModels; + } + + @Override + public ContainerNodeModel convertModel(JSONObject jsonObject) { + if (null == jsonObject) return null; + if (jsonObject.isEmpty()) return null; + ContainerNodeModel containerNodeModel = new ContainerNodeModel(); + + containerNodeModel.setNodeName(jsonObject.getString("nodeName")); + containerNodeModel.setNodeStatus(jsonObject.getString("nodeStatus")); + containerNodeModel.setCreated(jsonObject.getDate("createdTime")); + + return containerNodeModel; + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java new file mode 100644 index 00000000..7bb644dc --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java @@ -0,0 +1,48 @@ +package com.bocloud.ctstack.plugin.provider.tianyiconvertor; + +import com.alibaba.fastjson.JSONObject; +import com.bocloud.ctstack.plugin.domain.model.ContainerNodePoolModel; + +import java.util.ArrayList; +import java.util.List; + +public class ContainerNodePoolConvertor implements BeanConvertor { + + + @Override + public List convertList(List list) { + List containerNodePoolModels = new ArrayList<>(); + for (JSONObject object : list) { + containerNodePoolModels.add(convertModel(object)); + } + return containerNodePoolModels; + } + + @Override + public ContainerNodePoolModel convertModel(JSONObject jsonObject) { + if (null == jsonObject) return null; + if (jsonObject.isEmpty()) return null; + ContainerNodePoolModel containerNodePoolModel = new ContainerNodePoolModel(); + + containerNodePoolModel.setName(jsonObject.getString("nodePoolName")); + containerNodePoolModel.setNodepoolUuid(jsonObject.getString("nodePoolName")); + containerNodePoolModel.setRemark(jsonObject.getString("description")); + containerNodePoolModel.setStatus(jsonObject.getString("status")); + //containerNodePoolModel.setIsDefault(jsonObject.getBoolean("isDefault")); + containerNodePoolModel.setVmSpecType(jsonObject.getString("vmType")); + containerNodePoolModel.setVmSpecName(jsonObject.getString("vmSpecName")); + containerNodePoolModel.setVmSpecId(jsonObject.getString("vmSpecId")); + containerNodePoolModel.setCpu(jsonObject.getLong("cpu")); + containerNodePoolModel.setMemory(jsonObject.getLong("memory")); + containerNodePoolModel.setVmSpecId(jsonObject.getString("vmSpecId")); + containerNodePoolModel.setFailedNodes(jsonObject.getLong("unNormalNodeNum")); + containerNodePoolModel.setHealthyNodes(jsonObject.getLong("normalNodeNum")); + containerNodePoolModel.setTotalNodes(jsonObject.getLong("nodeTotalNum")); + containerNodePoolModel.setCreated(jsonObject.getDate("createdTime")); + containerNodePoolModel.setKubernetesConfig(jsonObject.getString("kubeletArgs")); + containerNodePoolModel.setScalingGroup(jsonObject.getString("nodeGroup")); + + return containerNodePoolModel; + } +} + diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/utils/TyHttpUtils.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/utils/TyHttpUtils.java index 961e68ff..44cb887b 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/utils/TyHttpUtils.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/utils/TyHttpUtils.java @@ -37,6 +37,9 @@ public class TyHttpUtils { headersOfAuth.put("hybrid-date", hybridDate); String requestId = UUID.randomUUID().toString().replace("-", ""); headersOfAuth.put("ctyun-hybrid-request-id", requestId); + logger.info("----------------------------------------------------------"); + logger.info(requestId); + logger.info("----------------------------------------------------------"); String forSignature = forSignature(method, headersOfAuth, params); byte[] kdate = kdate(hybridDate, accessKey, secretKey); byte[] signatureBytes = HmacSHA256Util.hmacSHA256(forSignature.getBytes(StandardCharsets.UTF_8), kdate); @@ -49,6 +52,9 @@ public class TyHttpUtils { newHeaders.put("Content-Type", "application/json"); newHeaders.put("hybrid-date", hybridDate); newHeaders.put("ctyun-hybrid-request-id", requestId); + logger.info("----------------------------------------------------------"); + logger.info(requestId); + logger.info("----------------------------------------------------------"); newHeaders.put("Hybrid-Authorization", authorization); newHeaders.put("ctUserId", ctUserId); diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java new file mode 100644 index 00000000..698fc087 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java @@ -0,0 +1,82 @@ +package com.bocloud.ctstack.plugin.repository; + +import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.megatron.common.model.Pager; +import com.megatron.common.model.Param; +import com.megatron.common.model.SimpleBean; +import com.megatron.common.utils.Common; +import com.megatron.common.utils.MapTools; +import com.megatron.database.core.intf.impl.BasicGenericDao; +import com.megatron.framework.core.CurrentService; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Component("containerClusterRepository") +public class ContainerClusterRepository extends BasicGenericDao { + + public ContainerClusterRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate, CurrentService service) { + super(jdbcTemplate, npJdbcTemplate, service); + } + + public int count(List params) { + String sql = "select count(1) from container_cluster a where a.is_deleted = 0"; + sql = this.getQueryBuilder().buildRaw(sql, params, null, "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.countQuery(sql, paramMap).intValue(); + } + + public List list(int page, int rows, List params, Map sorter) { + String sql = "select a.*,b.name vdc_name,c.name vendor_name , r.name region_name from container_cluster a " + + " left join vdc b on a.vdc_id = b.id " + + " left join cloud_vendor c on a.vendor_id = c.id " + + " left join region r on a.region_id = r.id and a.vendor_id = r.vendor_id " + + " where a.is_deleted = 0 "; + sql = this.getQueryBuilder().buildRaw(sql, new Pager(page, rows, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.list(ContainerCluster.class, sql, paramMap); + } + + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { + String sql = "update container_cluster set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + + " and region_id = :regionId "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public List listByVid(Long vendorId, String regionId) throws Exception { + String sql = "select a.*,t.name tenant_name from container_cluster a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId" + + " and a.region_id = :regionId "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + return this.list(ContainerCluster.class, sql, params); + } + + public List list(List params, Map sorter) { + String sql = "select a.id,a.name from container_cluster a where a.is_deleted = 0"; + sql = this.getQueryBuilder().build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + List clusters = this.list(ContainerCluster.class, sql, paramMap); + List beans = new ArrayList(); + for (ContainerCluster cluster : clusters) { + beans.add(new SimpleBean(cluster.getId(), cluster.getName())); + } + return beans; + } + + public boolean remove(Long id, Long userId) throws Exception { + String sql = "update container_cluster set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id"; + Map params = MapTools.simpleMap("id", id); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java new file mode 100644 index 00000000..cc164fc3 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java @@ -0,0 +1,60 @@ +package com.bocloud.ctstack.plugin.repository; + +import com.bocloud.ctstack.plugin.entity.ContainerClusterNodePool; +import com.megatron.common.utils.Common; +import com.megatron.common.utils.MapTools; +import com.megatron.database.core.intf.impl.BasicGenericDao; +import com.megatron.framework.core.CurrentService; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Component("containerNodePoolRepository") +public class ContainerNodePoolRepository extends BasicGenericDao { + + public ContainerNodePoolRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate, CurrentService service) { + super(jdbcTemplate, npJdbcTemplate, service); + } + + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { + String sql = "update container_cluster_node set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + + " and region_id = :regionId "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public boolean removeByClusterUuid(Long vendorId, String regionId, String clusterUuid, Long userId) throws Exception { + String sql = "update container_cluster_node set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + + " and region_id = :regionId and cluster_uuid = :clusterUuid "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put(Common.MENDER_ID, userId); + params.put("clusterUuid", clusterUuid); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public List listByClusterUuid(Long vendorId, String regionId, String clusterUuid) throws Exception { + String sql = "select a.*,t.name tenant_name from container_cluster_node_pool a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId" + + " and a.region_id = :regionId and a.cluster_uuid = :clusterUuid "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put("clusterUuid", clusterUuid); + return this.list(ContainerClusterNodePool.class, sql, params); + } + + public boolean remove(Long id, Long userId) throws Exception { + String sql = "update container_cluster_node_pool set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id"; + Map params = MapTools.simpleMap("id", id); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java new file mode 100644 index 00000000..08c263fa --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java @@ -0,0 +1,60 @@ +package com.bocloud.ctstack.plugin.repository; + +import com.bocloud.ctstack.plugin.entity.ContainerClusterNode; +import com.megatron.common.utils.Common; +import com.megatron.common.utils.MapTools; +import com.megatron.database.core.intf.impl.BasicGenericDao; +import com.megatron.framework.core.CurrentService; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; +import org.springframework.stereotype.Component; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Component("containerNodeRepository") +public class ContainerNodeRepository extends BasicGenericDao { + + public ContainerNodeRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate, CurrentService service) { + super(jdbcTemplate, npJdbcTemplate, service); + } + + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { + String sql = "update container_cluster_node_pool set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + + " and region_id = :regionId "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public boolean removeByClusterUuid(Long vendorId, String regionId, String clusterUuid, Long userId) throws Exception { + String sql = "update container_cluster_node_pool set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + + " and region_id = :regionId and cluster_uuid = :clusterUuid "; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put(Common.MENDER_ID, userId); + params.put("clusterUuid", clusterUuid); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public List listByClusterUuid(Long vendorId, String regionId, String clusterUuid) throws Exception { + String sql = "select a.*,t.name tenant_name from container_cluster_node a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId" + + " and a.region_id = :regionId and a.cluster_uuid = :clusterUuid"; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put("clusterUuid", clusterUuid); + return this.list(ContainerClusterNode.class, sql, params); + } + + public boolean remove(Long id, Long userId) throws Exception { + String sql = "update container_cluster_node set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id"; + Map params = MapTools.simpleMap("id", id); + params.put(Common.MENDER_ID, userId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java new file mode 100644 index 00000000..eb6e9245 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java @@ -0,0 +1,11 @@ +package com.bocloud.ctstack.plugin.service; + +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; + +public interface ContainerService { + + GeneralResult> list(Pager pager); +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java new file mode 100644 index 00000000..4f7a4c8d --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java @@ -0,0 +1,52 @@ +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.bocloud.ctstack.plugin.entity.ContainerCluster; +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.utils.Common; +import com.megatron.common.utils.GridHelper; +import com.megatron.common.utils.ListTool; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +@Service("containerService") +public class ContainerServiceImpl implements ContainerService { + + @Autowired + private ContainerClusterRepository containerClusterRepository; + + @Override + public GeneralResult> list(Pager pager) { + GridBean gridBean = null; + try { + pager.getSorter().put("gmtCreate", Common.ONE); + int total = this.containerClusterRepository.count(pager.getParams()); + if (pager.getSimple()) { + List beans = this.containerClusterRepository.list(pager.getParams(), pager.getSorter()); + gridBean = new GridBean(1, 1, total, beans); + } else { + List list = this.containerClusterRepository.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter()); + gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list); + } + return new GeneralResult<>(true, gridBean, "查询成功"); + } catch (Exception e) { + log.error("Query container cluster list fail:", e); + return new GeneralResult<>(false, "查询容器集群失败"); + } + } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java index 10c882e8..4e468ad4 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java @@ -106,6 +106,12 @@ public class TianyiTransporter { private CloudRdsRepository cloudRdsRepository; @Autowired private PhysicalServerRepository physicalServerRepository; + @Autowired + private ContainerClusterRepository containerClusterRepository; + @Autowired + private ContainerNodeRepository containerNodeRepository; + @Autowired + private ContainerNodePoolRepository containerNodePoolRepository; public Result transport(CloudVendor vendor, Long operator) { @@ -237,6 +243,12 @@ public class TianyiTransporter { for (Keypair keypair : keyPairList) { keyPairMap.put(keypair.getFingerprint(), keypair.getId()); } + + // 同步容器信息 + log.info("Starting sync container ......"); + this.syncContainers(model.getContainerClusterModels(), vendor, userId, region); + log.info("End sync container ......"); + // 同步浮动IP log.info("Starting sync floatIp ......"); this.syncFloatIps(model.getServerModels(), model.getFloatingIpModels(), vendor, userId, region); @@ -564,7 +576,164 @@ public class TianyiTransporter { for (Entry entry : ipMap.entrySet()) { floatingIpRepository.remove(entry.getValue().getId(), userId); } + } + private void syncContainers(List clusterModels, CloudVendor vendor, Long userId, Region region) throws Exception { + if (null == clusterModels) { + return; + } + Long vendorId = vendor.getId(); + // 当容器集群为空时,清空数据库 + if (ListTool.isEmpty(clusterModels)) { + containerClusterRepository.removeByVid(vendorId, region.getRegionId(), userId); + containerNodeRepository.removeByVid(vendorId, region.getRegionId(), userId); + containerNodePoolRepository.removeByVid(vendorId, region.getRegionId(), userId); + return; + } + List listByVid = containerClusterRepository.listByVid(vendorId, region.getRegionId()); + Map clusterMap = Maps.newHashMap(); + for (ContainerCluster containerCluster : listByVid) { + clusterMap.put(containerCluster.getClusterUuid(), containerCluster); + } + + ContainerCluster containerCluster = null; + for (ContainerClusterModel model : clusterModels) { + if (clusterMap.containsKey(model.getClusterUuid())) { + // 更新操作 + containerCluster = clusterMap.get(model.getClusterUuid()); + BeanUtils.copyProperties(model, containerCluster); + containerCluster.setMenderId(userId); + containerCluster.setRegionId(region.getRegionId()); + containerCluster.setGmtModify(new Date()); + containerCluster.setCreatorId(userId); + containerCluster.setVendorId(vendorId); + containerClusterRepository.update(containerCluster); + clusterMap.remove(model.getClusterUuid()); + + // 同步这个集群下的节点和节点池信息 + syncContainerNodes(containerCluster, model.getNodeModels(), vendor, userId, region); + syncContainerNodePools(containerCluster, model.getNodePoolModels(), vendor, userId, region); + continue; + } + containerCluster = new ContainerCluster(); + BeanUtils.copyProperties(model, containerCluster); + containerCluster.setMenderId(userId); + containerCluster.setRegionId(region.getRegionId()); + containerCluster.setGmtModify(new Date()); + containerCluster.setTenantId(0L); + containerCluster.setProjectId(0L); + containerCluster.setCreatorId(userId); + containerCluster.setVendorId(vendorId); + containerClusterRepository.save(containerCluster); + + // 同步这个集群下的节点和节点池信息 + syncContainerNodes(containerCluster, model.getNodeModels(), vendor, userId, region); + syncContainerNodePools(containerCluster, model.getNodePoolModels(), vendor, userId, region); + } + for (Entry entry : clusterMap.entrySet()) { + containerClusterRepository.remove(entry.getValue().getId(), userId); + // 删除这个容器云下面的节点和节点池 + containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), entry.getValue().getClusterUuid(), userId); + containerNodePoolRepository.removeByClusterUuid(vendorId, region.getRegionId(), entry.getValue().getClusterUuid(), userId); + } + } + + private void syncContainerNodes(ContainerCluster containerCluster, List nodeModels, CloudVendor vendor, Long userId, Region region) throws Exception { + if (null == nodeModels) { + return; + } + Long vendorId = vendor.getId(); + // 当容器集群下节点为空时,清空数据库 + if (ListTool.isEmpty(nodeModels)) { + containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), containerCluster.getClusterUuid(), userId); + return; + } + List nodes = containerNodeRepository.listByClusterUuid(vendorId, region.getRegionId(), + containerCluster.getClusterUuid()); + Map nodeMap = Maps.newHashMap(); + for (ContainerClusterNode containerClusterNode : nodes) { + nodeMap.put(containerClusterNode.getNodeName(), containerClusterNode); + } + + ContainerClusterNode containerClusterNode = null; + for (ContainerNodeModel model : nodeModels) { + if (nodeMap.containsKey(model.getNodeName())) { + // 更新操作 + containerClusterNode = nodeMap.get(model.getNodeName()); + BeanUtils.copyProperties(model, containerClusterNode); + containerClusterNode.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNode.setMenderId(userId); + containerClusterNode.setRegionId(region.getRegionId()); + containerClusterNode.setGmtModify(new Date()); + containerClusterNode.setCreatorId(userId); + containerClusterNode.setVendorId(vendorId); + containerNodeRepository.update(containerClusterNode); + nodeMap.remove(model.getClusterUuid()); + continue; + } + containerClusterNode = new ContainerClusterNode(); + BeanUtils.copyProperties(model, containerClusterNode); + containerClusterNode.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNode.setMenderId(userId); + containerClusterNode.setRegionId(region.getRegionId()); + containerClusterNode.setGmtModify(new Date()); + containerClusterNode.setCreatorId(userId); + containerClusterNode.setVendorId(vendorId); + containerNodeRepository.save(containerClusterNode); + } + for (Entry entry : nodeMap.entrySet()) { + containerNodeRepository.remove(entry.getValue().getId(), userId); + } + } + + private void syncContainerNodePools(ContainerCluster containerCluster, List nodePoolModels, CloudVendor vendor, Long userId, Region region) throws Exception { + if (null == nodePoolModels) { + return; + } + Long vendorId = vendor.getId(); + // 当容器集群下节点为空时,清空数据库 + if (ListTool.isEmpty(nodePoolModels)) { + containerNodePoolRepository.removeByClusterUuid(vendorId, region.getRegionId(), containerCluster.getClusterUuid(), userId); + return; + } + List nodePools = containerNodePoolRepository.listByClusterUuid(vendorId, region.getRegionId(), + containerCluster.getClusterUuid()); + Map nodePoolMap = Maps.newHashMap(); + for (ContainerClusterNodePool containerClusterNodePool : nodePools) { + nodePoolMap.put(containerClusterNodePool.getNodePoolUuid(), containerClusterNodePool); + } + + ContainerClusterNodePool containerClusterNodePool = null; + for (ContainerNodePoolModel model : nodePoolModels) { + if (nodePoolMap.containsKey(model.getNodepoolUuid())) { + // 更新操作 + containerClusterNodePool = nodePoolMap.get(model.getNodepoolUuid()); + BeanUtils.copyProperties(model, containerClusterNodePool); + containerClusterNodePool.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNodePool.setMenderId(userId); + containerClusterNodePool.setRegionId(region.getRegionId()); + containerClusterNodePool.setGmtModify(new Date()); + containerClusterNodePool.setCreatorId(userId); + containerClusterNodePool.setVendorId(vendorId); + containerNodePoolRepository.update(containerClusterNodePool); + nodePoolMap.remove(model.getClusterUuid()); + continue; + } + containerClusterNodePool = new ContainerClusterNodePool(); + BeanUtils.copyProperties(model, containerClusterNodePool); + containerClusterNodePool.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNodePool.setMenderId(userId); + containerClusterNodePool.setRegionId(region.getRegionId()); + containerClusterNodePool.setGmtModify(new Date()); + containerClusterNodePool.setTenantId(0L); + containerClusterNodePool.setProjectId(0L); + containerClusterNodePool.setCreatorId(userId); + containerClusterNodePool.setVendorId(vendorId); + containerNodePoolRepository.save(containerClusterNodePool); + } + for (Entry entry : nodePoolMap.entrySet()) { + containerNodePoolRepository.remove(entry.getValue().getId(), userId); + } } private void syncResourceGroups(List resourceGroupModels, CloudVendor vendor, Long userId, Region region) throws Exception {