Commit b72113de authored by wutu's avatar wutu

完成并测试集群创建和删除的功能模块

parent 72eff040
......@@ -13,4 +13,6 @@ public interface CloudService {
String initCluster(ClusterConfig clusterConfig);
List<SeparatedClusterConfig> sendClusterConfigToEdgeNode(List<SeparatedClusterConfig> clusterConfigs);
boolean deleteClusterFromEdgeNode(long clusterId);
}
......@@ -21,6 +21,7 @@ public class CloudServiceImpl implements CloudService {
private static final Logger LOG = LoggerFactory.getLogger(CloudServiceImpl.class);
private static final Random randomInt = new Random(14);
private static final String CREATE_CLUSTER = "/createCluster";
private static final String DELETE_CLUSTER = "/delCluster";
// 用于存储配置文件
private ConcurrentHashMap<Long, List<SeparatedClusterConfig>> allClusterConfig
= new ConcurrentHashMap<>();
......@@ -199,8 +200,29 @@ public class CloudServiceImpl implements CloudService {
if(!response.getStatusCode().is2xxSuccessful()) {
LOG.error("集群配置下发失败:{}:{}", c.getEdgeNodeId(), response.getBody());
}
if(!allClusterConfig.containsKey(c.getClusterConfig().getId())) {
allClusterConfig.put(c.getClusterConfig().getId(), new ArrayList<>());
}
allClusterConfig.get(c.getClusterConfig().getId()).add(c);
});
return clusterConfigs;
}
/**
* 删除全部的容器集群
* @param clusterId
* @return
*/
@Override
public boolean deleteClusterFromEdgeNode(long clusterId) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("clusterId", clusterId);
List<SeparatedClusterConfig> separatedClusterConfigs = allClusterConfig.get(clusterId);
separatedClusterConfigs.forEach(c -> {
// 调用远程借口删除服务
restTemplate.getForEntity("http://" + c.getEdgeNodeId() + DELETE_CLUSTER + "?clusterId=" + clusterId, String.class);
});
return true;
}
}
......@@ -11,6 +11,7 @@ import top.ninwoo.cloudcenter.service.CloudService;
import top.ninwoo.common.entity.ClusterConfig;
import top.ninwoo.common.entity.ContainerDescription;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.common.entity.NetworkTopology;
import java.util.ArrayList;
import java.util.List;
......@@ -53,14 +54,21 @@ public class CloudServiceImplTest {
cds.add(containerDescription1);
clusterConfig.setDockers(cds);
NetworkTopology topo = new NetworkTopology();
topo.setAppNames(new String[]{"Run", "APP", "br:ovs1"});
// 这个参数好像没啥用
topo.setTopologyId(11);
topo.setTopology(new int[][]{{0,0,0},{0,0,0},{1,1,0}});
clusterConfig.setTopology(topo);
separatedClusterConfig.setClusterConfig(clusterConfig);
clusterConfigs.add(separatedClusterConfig);
cloudService.sendClusterConfigToEdgeNode(clusterConfigs);
cloudService.deleteClusterFromEdgeNode(11111l);
}
@Test
public void deleteClusterFromEdgeNodeTest() {
}
}
......@@ -105,7 +105,9 @@ public class IndexController {
public long createCluster(@RequestBody ClusterConfig clusterConfig) {
// 创建一个解析器
long l = clusterService.initContainers(clusterConfig);
// 使用json进行解析
// 创建网络
// 使用topo创建工具
topologyService.createTopo(l, clusterConfig.getTopology());
return l;
}
......@@ -165,7 +167,10 @@ public class IndexController {
@RequestMapping(value = "/delCluster", method = RequestMethod.GET)
public String delCluster(long clusterId) {
// 删除集群
topologyService.removeTopoByCluterId(clusterId);
clusterService.removeContainersByClusterId(clusterId);
return "success";
}
......
......@@ -27,4 +27,6 @@ public interface TopologyService {
// 调整网络拓扑,这里限定条件为,网络节点不改变
String modifyTopology(long clusterId, NetworkTopology topology);
void removeTopoByCluterId(long clusterId);
}
......@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.edgecenter.entity.ClusterConfig;
import top.ninwoo.edgecenter.entity.ContainerDescription;
import top.ninwoo.edgecenter.entity.NetworkTopology;
import top.ninwoo.edgecenter.service.ClusterService;
import top.ninwoo.edgecenter.service.TopologyService;
import top.ninwoo.utils.entity.NetworkInfo;
......
......@@ -358,4 +358,30 @@ public class TopologyServiceImpl implements TopologyService {
throw new RuntimeException("Physical topology does not support c2c link.");
}
}
@Override
public void removeTopoByCluterId(long clusterId) {
if(!clustersTopo.containsKey(clusterId)) {
return;
}
NetworkTopology topo = clustersTopo.get(clusterId);
setZeroTopo(topo);
// 先将全部网络调整为0
modifyTopology(clusterId, topo);
String[] appNames = clustersTopo.get(clusterId).getAppNames();
for (String appName : appNames) {
if(appName.contains("br") && !appName.contains("remote")) {
ovsService.delBridge(appName.substring(3));
}
}
}
private void setZeroTopo(NetworkTopology topo) {
int[][] topology = topo.getTopology();
for (int i = 0; i < topology.length; i++) {
for (int j = 0; j < topology.length ; j++) {
topology[i][j] = 0;
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment