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 index 0bd9120d..7330ecb6 100644 --- 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 @@ -6,6 +6,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; +import java.util.List; @Data @AllArgsConstructor @@ -29,7 +30,6 @@ public class ContainerClusterModel { 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/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/common/TianyiLocationProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java index 5bc04eec..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.setContainerClusterModels((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/TianYiContainerNodeProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java index 1067a028..b9210e77 100644 --- 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 @@ -43,7 +43,6 @@ public class TianYiContainerNodeProvider extends TianyiProvider { 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); 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 a000183e..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 @@ -26,6 +26,8 @@ public class ContainerClusterConvertor implements BeanConvertor { + + public ContainerClusterRepository(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 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 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/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 {