This commit is contained in:
Captain.B 2020-03-25 15:01:55 +08:00
parent 7daaa7022c
commit ec093c669f
9 changed files with 215 additions and 120 deletions

View File

@ -1,38 +1,10 @@
package io.metersphere.commons.constants; package io.metersphere.commons.constants;
/**
* Author: chunxing
* Date: 2018/6/26 下午3:44
* Description:
*/
public interface ParamConstants { public interface ParamConstants {
String getValue(); String getValue();
enum KeyCloak implements ParamConstants {
USERNAME("keycloak.username"),
PASSWORD("keycloak.password"),
REALM("keycloak.realm"),
AUTH_SERVER_URL("keycloak.auth-server-url"),
ADDRESS("keycloak-server-address");
private String value;
KeyCloak(String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
enum Type implements ParamConstants { enum Type implements ParamConstants {
PASSWORD("password"), PASSWORD("password"),
@ -57,9 +29,6 @@ public interface ParamConstants {
enum Classify implements ParamConstants { enum Classify implements ParamConstants {
KEYCLOAK("keycloak"),
MAIL("smtp"),
UI("ui"),
REGISTRY("registry"); REGISTRY("registry");
private String value; private String value;
@ -78,77 +47,6 @@ public interface ParamConstants {
} }
} }
enum UI implements ParamConstants {
LOGO("ui.logo"),
SYSTEM_NAME("ui.system.name"),
THEME_PRIMARY("ui.theme.primary"),
THEME_ACCENT("ui.theme.accent"),
FAVICON("ui.favicon"),
LOGIN_TITLE("ui.login.title"),
LOGIN_IMG("ui.login.img"),
SUPPORT_NAME("ui.support.name"),
SUPPORT_URL("ui.support.url"),
TITLE("ui.title");
private String value;
UI(String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
enum MAIL {
SERVER("smtp.server", 1),
PORT("smtp.port", 2),
ACCOUNT("smtp.account", 3),
PASSWORD("smtp.password", 4),
SSL("smtp.ssl", 5),
TLS("smtp.tls", 6);
private String key;
private Integer value;
MAIL(String key, Integer value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public Integer getValue() {
return value;
}
}
enum Log implements ParamConstants {
KEEP_MONTHS("log.keep.months");
private String value;
Log(String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
enum Registry implements ParamConstants { enum Registry implements ParamConstants {
URL("registry.url"), URL("registry.url"),

View File

@ -41,11 +41,9 @@ public class TestResourcePoolController {
return PageUtils.setPageInfo(page, testResourcePoolService.listResourcePools(request)); return PageUtils.setPageInfo(page, testResourcePoolService.listResourcePools(request));
} }
@GetMapping("list/all") @GetMapping("list/all/valid")
public List<TestResourcePoolDTO> listResourcePools() { public List<TestResourcePool> listValidResourcePools() {
PageHelper.startPage(1, 10000, true); return testResourcePoolService.listValidResourcePools();
QueryResourcePoolRequest request = new QueryResourcePoolRequest();
return testResourcePoolService.listResourcePools(request);
} }

View File

@ -6,6 +6,7 @@ import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.engine.AbstractEngine; import io.metersphere.engine.AbstractEngine;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
@ -14,15 +15,19 @@ import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter;
import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec; import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec;
import io.metersphere.engine.kubernetes.provider.ClientCredential; import io.metersphere.engine.kubernetes.provider.ClientCredential;
import io.metersphere.engine.kubernetes.provider.KubernetesProvider; import io.metersphere.engine.kubernetes.provider.KubernetesProvider;
import io.metersphere.engine.kubernetes.registry.RegistryService;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import java.util.HashMap; import java.util.HashMap;
public class KubernetesTestEngine extends AbstractEngine { public class KubernetesTestEngine extends AbstractEngine {
private RegistryService registryService;
@Override @Override
public boolean init(LoadTestWithBLOBs loadTest) { public boolean init(LoadTestWithBLOBs loadTest) {
super.init(loadTest); super.init(loadTest);
this.registryService = CommonBeanFactory.getBean(RegistryService.class);
return true; return true;
} }
@ -44,15 +49,18 @@ public class KubernetesTestEngine extends AbstractEngine {
EngineContext context = EngineFactory.createContext(loadTest, threadNum); EngineContext context = EngineFactory.createContext(loadTest, threadNum);
runTest(context, clientCredential, 1); runTest(context, clientCredential, 1);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); MSException.throwException(e);
} }
}); });
} }
private void runTest(EngineContext context, ClientCredential credential, int replicas) { private void runTest(EngineContext context, ClientCredential credential, int replicas) {
KubernetesProvider kubernetesProvider = new KubernetesProvider(JSON.toJSONString(credential)); KubernetesProvider kubernetesProvider = new KubernetesProvider(JSON.toJSONString(credential));
// create namespace // create namespace
kubernetesProvider.confirmNamespace(context.getNamespace()); kubernetesProvider.confirmNamespace(context.getNamespace());
// docker registry
registryService.dockerRegistry(kubernetesProvider, context.getNamespace());
// create cm // create cm
try (KubernetesClient client = kubernetesProvider.getKubernetesClient()) { try (KubernetesClient client = kubernetesProvider.getKubernetesClient()) {
String configMapName = context.getTestId() + "-files"; String configMapName = context.getTestId() + "-files";
@ -81,12 +89,12 @@ public class KubernetesTestEngine extends AbstractEngine {
}}); }});
jmeter.setSpec(new JmeterSpec() {{ jmeter.setSpec(new JmeterSpec() {{
setReplicas(replicas); setReplicas(replicas);
setImage("registry.fit2cloud.com/metersphere/jmeter-master:0.0.2"); setImage(registryService.getRegistry() + "jmeter-master:0.0.2");
}}); }});
LogUtil.info("Load test started. " + context.getTestId()); LogUtil.info("Load test started. " + context.getTestId());
kubernetesProvider.applyCustomResource(jmeter); kubernetesProvider.applyCustomResource(jmeter);
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); MSException.throwException(e);
} }
} }

View File

@ -0,0 +1,94 @@
package io.metersphere.engine.kubernetes.registry;
import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.domain.SystemParameterExample;
import io.metersphere.base.mapper.SystemParameterMapper;
import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.utils.EncryptUtils;
import io.metersphere.engine.kubernetes.provider.AbstractClientProvider;
import io.metersphere.engine.kubernetes.provider.DockerRegistry;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class RegistryService {
@Resource
private SystemParameterMapper parameterMapper;
/**
* 获取镜像仓库地址
* 地址+项目
*
* @return eg: registry.demo.com/demo/
*/
public String getRegistry() {
StringBuilder sb = new StringBuilder();
Map<String, String> paramMap = getRegistryInfo();
String url = paramMap.getOrDefault(ParamConstants.Registry.URL.getValue(), "");
String project = paramMap.getOrDefault(ParamConstants.Registry.REPO.getValue(), "");
if (url.startsWith("http://") || url.startsWith("https://")) {
url = url.replace("http://", "");
url = url.replace("https://", "");
}
sb.append(url);
if (!url.endsWith("/")) {
sb.append("/");
}
if (StringUtils.isNotEmpty(project)) {
sb.append(project);
sb.append("/");
}
return sb.toString();
}
public String getRegistryUrl() {
Map<String, String> paramMap = getRegistryInfo();
String url = paramMap.get(ParamConstants.Registry.URL.getValue());
if (url.startsWith("http://") || url.startsWith("https://")) {
url = url.replace("http://", "");
url = url.replace("https://", "");
}
return url;
}
public String getRegistryUsername() {
Map<String, String> paramMap = getRegistryInfo();
return paramMap.get(ParamConstants.Registry.USERNAME.getValue());
}
public String getRegistryPassword() {
Map<String, String> paramMap = getRegistryInfo();
return paramMap.get(ParamConstants.Registry.PASSWORD.getValue());
}
public void dockerRegistry(AbstractClientProvider clientProvider, String namespace) {
DockerRegistry registry = new DockerRegistry();
registry.setUrl(this.getRegistryUrl());
registry.setUsername(this.getRegistryUsername());
registry.setPassword(this.getRegistryPassword());
registry.setNamespace(namespace);
clientProvider.dockerRegistry(registry);
}
private Map<String, String> getRegistryInfo() {
Map<String, String> map = new HashMap<>();
SystemParameterExample example = new SystemParameterExample();
example.createCriteria().andParamKeyLike(ParamConstants.Classify.REGISTRY.getValue() + "%");
List<SystemParameter> parameters = parameterMapper.selectByExample(example);
for (SystemParameter parameter : parameters) {
if (StringUtils.equalsIgnoreCase(ParamConstants.Type.PASSWORD.getValue(), parameter.getType())) {
parameter.setParamValue(EncryptUtils.aesDecrypt(parameter.getParamValue()).toString());
}
parameters.forEach(param -> map.put(param.getParamKey(), param.getParamValue()));
}
return map;
}
}

View File

@ -0,0 +1,47 @@
package io.metersphere.service;
import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.domain.SystemParameterExample;
import io.metersphere.base.mapper.SystemParameterMapper;
import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.utils.EncryptUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class RegistryParamService {
@Resource
private SystemParameterMapper parameterMapper;
public List<SystemParameter> getRegistry(String type) {
List<SystemParameter> paramList = this.getParamList(type);
for (SystemParameter parameter : paramList) {
if (StringUtils.equalsIgnoreCase(ParamConstants.Type.PASSWORD.getValue(), parameter.getType())) {
parameter.setParamValue(EncryptUtils.aesDecrypt(parameter.getParamValue()).toString());
}
}
paramList.sort(Comparator.comparingInt(SystemParameter::getSort));
return paramList;
}
public void updateRegistry(List<SystemParameter> parameters) {
for (SystemParameter parameter : parameters) {
if (StringUtils.equalsIgnoreCase(ParamConstants.Type.PASSWORD.getValue(), parameter.getType())) {
parameter.setParamValue(EncryptUtils.aesEncrypt(parameter.getParamValue()).toString());
}
parameterMapper.updateByPrimaryKey(parameter);
}
}
public List<SystemParameter> getParamList(String type) {
SystemParameterExample example = new SystemParameterExample();
example.createCriteria().andParamKeyLike(type + "%");
return parameterMapper.selectByExample(example);
}
}

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.TestResource; import io.metersphere.base.domain.TestResource;
import io.metersphere.base.domain.TestResourceExample; import io.metersphere.base.domain.TestResourceExample;
import io.metersphere.base.domain.TestResourcePool; import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.base.domain.TestResourcePoolExample;
import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.base.mapper.ext.ExtTestReourcePoolMapper; import io.metersphere.base.mapper.ext.ExtTestReourcePoolMapper;
@ -25,6 +26,8 @@ import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static io.metersphere.commons.constants.ResourceStatusEnum.VALID;
/** /**
* @author dongbin * @author dongbin
*/ */
@ -45,7 +48,7 @@ public class TestResourcePoolService {
testResourcePool.setId(UUID.randomUUID().toString()); testResourcePool.setId(UUID.randomUUID().toString());
testResourcePool.setCreateTime(System.currentTimeMillis()); testResourcePool.setCreateTime(System.currentTimeMillis());
testResourcePool.setUpdateTime(System.currentTimeMillis()); testResourcePool.setUpdateTime(System.currentTimeMillis());
testResourcePool.setStatus(ResourceStatusEnum.VALID.name()); testResourcePool.setStatus(VALID.name());
validateTestResourcePool(testResourcePool); validateTestResourcePool(testResourcePool);
testResourcePoolMapper.insertSelective(testResourcePool); testResourcePoolMapper.insertSelective(testResourcePool);
return testResourcePool; return testResourcePool;
@ -87,7 +90,7 @@ public class TestResourcePoolService {
testResourcePool.setStatus(ResourceStatusEnum.INVALID.name()); testResourcePool.setStatus(ResourceStatusEnum.INVALID.name());
resource.setStatus(ResourceStatusEnum.INVALID.name()); resource.setStatus(ResourceStatusEnum.INVALID.name());
} else { } else {
resource.setStatus(ResourceStatusEnum.VALID.name()); resource.setStatus(VALID.name());
} }
resource.setTestResourcePoolId(testResourcePool.getId()); resource.setTestResourcePoolId(testResourcePool.getId());
updateTestResource(resource); updateTestResource(resource);
@ -116,7 +119,7 @@ public class TestResourcePoolService {
try { try {
KubernetesProvider provider = new KubernetesProvider(testResource.getConfiguration()); KubernetesProvider provider = new KubernetesProvider(testResource.getConfiguration());
provider.validateCredential(); provider.validateCredential();
testResource.setStatus(ResourceStatusEnum.VALID.name()); testResource.setStatus(VALID.name());
} catch (Exception e) { } catch (Exception e) {
testResource.setStatus(ResourceStatusEnum.INVALID.name()); testResource.setStatus(ResourceStatusEnum.INVALID.name());
testResourcePool.setStatus(ResourceStatusEnum.INVALID.name()); testResourcePool.setStatus(ResourceStatusEnum.INVALID.name());
@ -142,4 +145,10 @@ public class TestResourcePoolService {
public TestResourcePool getResourcePool(String resourcePoolId) { public TestResourcePool getResourcePool(String resourcePoolId) {
return testResourcePoolMapper.selectByPrimaryKey(resourcePoolId); return testResourcePoolMapper.selectByPrimaryKey(resourcePoolId);
} }
public List<TestResourcePool> listValidResourcePools() {
TestResourcePoolExample example = new TestResourcePoolExample();
example.createCriteria().andStatusEqualTo(ResourceStatusEnum.VALID.name());
return testResourcePoolMapper.selectByExample(example);
}
} }

View File

@ -9,3 +9,8 @@ INSERT INTO role (id, name, description, type, create_time, update_time) VALUES
INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_manager', '测试经理', null, null, 1581576575948, 1581576575948); INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_manager', '测试经理', null, null, 1581576575948, 1581576575948);
INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_user', '测试人员', null, null, 1581576575948, 1581576575948); INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_user', '测试人员', null, null, 1581576575948, 1581576575948);
INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_viewer', 'Viewer', null, null, 1581576575948, 1581576575948); INSERT INTO role (id, name, description, type, create_time, update_time) VALUES ('test_viewer', 'Viewer', null, null, 1581576575948, 1581576575948);
INSERT INTO `system_parameter`(`param_key`, `param_value`, `type`, `sort`) VALUES ('registry.password', '', 'password', 4);
INSERT INTO `system_parameter`(`param_key`, `param_value`, `type`, `sort`) VALUES ('registry.repo', '', 'text', 2);
INSERT INTO `system_parameter`(`param_key`, `param_value`, `type`, `sort`) VALUES ('registry.url', '', 'text', 1);
INSERT INTO `system_parameter`(`param_key`, `param_value`, `type`, `sort`) VALUES ('registry.username', '', 'text', 3);

View File

@ -1,9 +1,12 @@
package io.metersphere; package io.metersphere;
import io.metersphere.base.domain.User; import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.UserMapper;
import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.utils.CompressUtils; import io.metersphere.commons.utils.CompressUtils;
import io.metersphere.engine.kubernetes.registry.RegistryService;
import io.metersphere.service.RegistryParamService;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -12,11 +15,15 @@ import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
//@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests { public class ApplicationTests {
// @Resource @Resource
// UserMapper userMapper; UserMapper userMapper;
@Resource
private RegistryParamService registryParamService;
@Resource
private RegistryService registryService;
@Test @Test
public void test1() { public void test1() {
@ -24,4 +31,33 @@ public class ApplicationTests {
final Object unzip = CompressUtils.unzip(test); final Object unzip = CompressUtils.unzip(test);
System.out.println(new String((byte[]) unzip)); System.out.println(new String((byte[]) unzip));
} }
@Test
public void test2() {
List<SystemParameter> registry = registryParamService.getRegistry(ParamConstants.Classify.REGISTRY.getValue());
System.out.println(registry);
for (SystemParameter sp : registry) {
if ("registry.password".equals(sp.getParamKey())) {
sp.setParamValue("Calong@2015");
}
if ("registry.url".equals(sp.getParamKey())) {
sp.setParamValue("registry.fit2cloud.com");
}
if ("registry.repo".equals(sp.getParamKey())) {
sp.setParamValue("metersphere");
}
if ("registry.username".equals(sp.getParamKey())) {
sp.setParamValue("developer");
}
}
registryParamService.updateRegistry(registry);
}
@Test
public void test3() {
String registry = registryService.getRegistry();
System.out.println(registry);
}
} }

View File

@ -148,7 +148,7 @@
}, },
methods: { methods: {
getResourcePools() { getResourcePools() {
this.$get('/testresourcepool/list/all', response => { this.$get('/testresourcepool/list/all/valid', response => {
this.resourcePools = response.data; this.resourcePools = response.data;
}) })
}, },