添加校验资源池 K8s 和 node 是否有效
This commit is contained in:
parent
a62eaa55e0
commit
10b005ba0b
|
@ -1,6 +1,6 @@
|
|||
package io.metersphere.commons.constants;
|
||||
|
||||
public enum ResourceTypeEnum {
|
||||
public enum ResourcePoolTypeEnum {
|
||||
/**
|
||||
* k8s 资源池
|
||||
*/
|
|
@ -0,0 +1,41 @@
|
|||
package io.metersphere.dto;
|
||||
|
||||
public class KubernetesDTO {
|
||||
|
||||
private String masterUrl;
|
||||
private String token;
|
||||
private Integer maxConcurrency;
|
||||
private Boolean validate;
|
||||
|
||||
public String getMasterUrl() {
|
||||
return masterUrl;
|
||||
}
|
||||
|
||||
public void setMasterUrl(String masterUrl) {
|
||||
this.masterUrl = masterUrl;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public Integer getMaxConcurrency() {
|
||||
return maxConcurrency;
|
||||
}
|
||||
|
||||
public void setMaxConcurrency(Integer maxConcurrency) {
|
||||
this.maxConcurrency = maxConcurrency;
|
||||
}
|
||||
|
||||
public Boolean getValidate() {
|
||||
return validate;
|
||||
}
|
||||
|
||||
public void setValidate(Boolean validate) {
|
||||
this.validate = validate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package io.metersphere.dto;
|
||||
|
||||
public class NodeDTO {
|
||||
private String ip;
|
||||
private Integer port;
|
||||
private Integer maxConcurrency;
|
||||
private Boolean validate;
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Integer getMaxConcurrency() {
|
||||
return maxConcurrency;
|
||||
}
|
||||
|
||||
public void setMaxConcurrency(Integer maxConcurrency) {
|
||||
this.maxConcurrency = maxConcurrency;
|
||||
}
|
||||
|
||||
public Boolean getValidate() {
|
||||
return validate;
|
||||
}
|
||||
|
||||
public void setValidate(Boolean validate) {
|
||||
this.validate = validate;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,23 @@
|
|||
package io.metersphere.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.base.domain.TestResourcePool;
|
||||
import io.metersphere.base.domain.TestResourcePoolExample;
|
||||
import io.metersphere.base.mapper.TestResourcePoolMapper;
|
||||
import io.metersphere.commons.constants.ResourcePoolTypeEnum;
|
||||
import io.metersphere.commons.utils.BeanUtils;
|
||||
import io.metersphere.controller.request.resourcepool.QueryResourcePoolRequest;
|
||||
import io.metersphere.dto.KubernetesDTO;
|
||||
import io.metersphere.dto.NodeDTO;
|
||||
import io.metersphere.engine.kubernetes.provider.ClientCredential;
|
||||
import io.metersphere.engine.kubernetes.provider.KubernetesProvider;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
@ -19,6 +30,8 @@ import java.util.UUID;
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
public class TestResourcePoolService {
|
||||
|
||||
private final static String nodeControllerUrl = "%s:%s/status";
|
||||
|
||||
@Resource
|
||||
private TestResourcePoolMapper testResourcePoolMapper;
|
||||
|
||||
|
@ -27,6 +40,7 @@ public class TestResourcePoolService {
|
|||
testResourcePool.setCreateTime(System.currentTimeMillis());
|
||||
testResourcePool.setUpdateTime(System.currentTimeMillis());
|
||||
testResourcePool.setStatus("1");
|
||||
validateTestResourcePool(testResourcePool);
|
||||
testResourcePoolMapper.insertSelective(testResourcePool);
|
||||
return testResourcePool;
|
||||
}
|
||||
|
@ -37,6 +51,7 @@ public class TestResourcePoolService {
|
|||
|
||||
public void updateTestResourcePool(TestResourcePool testResourcePool) {
|
||||
testResourcePool.setUpdateTime(System.currentTimeMillis());
|
||||
validateTestResourcePool(testResourcePool);
|
||||
testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool);
|
||||
}
|
||||
|
||||
|
@ -47,4 +62,55 @@ public class TestResourcePoolService {
|
|||
}
|
||||
return testResourcePoolMapper.selectByExample(example);
|
||||
}
|
||||
|
||||
private void validateTestResourcePool(TestResourcePool testResourcePool) {
|
||||
if (StringUtils.equalsIgnoreCase(testResourcePool.getType(), ResourcePoolTypeEnum.K8S.name())) {
|
||||
validateK8s(testResourcePool);
|
||||
return;
|
||||
}
|
||||
validateNodes(testResourcePool);
|
||||
}
|
||||
|
||||
private void validateNodes(TestResourcePool testResourcePool) {
|
||||
List<NodeDTO> nodes = JSON.parseArray(testResourcePool.getInfo(), NodeDTO.class);
|
||||
|
||||
if (CollectionUtils.isEmpty(nodes)) {
|
||||
throw new RuntimeException("没有节点信息");
|
||||
}
|
||||
|
||||
for (NodeDTO node : nodes) {
|
||||
boolean isValidate = validateNode(node);
|
||||
if (!isValidate) {
|
||||
testResourcePool.setStatus("0");
|
||||
}
|
||||
node.setValidate(isValidate);
|
||||
}
|
||||
testResourcePool.setInfo(JSON.toJSONString(nodes));
|
||||
}
|
||||
|
||||
private boolean validateNode(NodeDTO dto) {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> entity = restTemplate.getForEntity(String.format(nodeControllerUrl, dto.getIp(), dto.getPort()), String.class);
|
||||
return entity.getStatusCode().value() == HttpStatus.SC_OK;
|
||||
}
|
||||
|
||||
private void validateK8s(TestResourcePool testResourcePool) {
|
||||
List<KubernetesDTO> dtos = JSON.parseArray(testResourcePool.getInfo(), KubernetesDTO.class);
|
||||
|
||||
if (CollectionUtils.isEmpty(dtos) || dtos.size() != 1) {
|
||||
throw new RuntimeException("只能添加一个 K8s");
|
||||
}
|
||||
|
||||
ClientCredential clientCredential = new ClientCredential();
|
||||
BeanUtils.copyBean(clientCredential, dtos.get(0));
|
||||
try {
|
||||
KubernetesProvider provider = new KubernetesProvider(JSON.toJSONString(clientCredential));
|
||||
provider.validateCredential();
|
||||
dtos.get(0).setValidate(true);
|
||||
} catch (Exception e) {
|
||||
dtos.get(0).setValidate(false);
|
||||
testResourcePool.setStatus("0");
|
||||
}
|
||||
testResourcePool.setInfo(JSON.toJSONString(dtos));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue