去掉 registry

This commit is contained in:
Captain.B 2020-04-01 11:11:04 +08:00
parent 742a082689
commit 812328b907
9 changed files with 13 additions and 157 deletions

View File

@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils;
import java.util.List;
public abstract class AbstractEngine implements Engine {
public static final String REGISTRY = "registry.fit2cloud.com/metersphere/";
public static final String JMETER_IMAGE = "jmeter-master:0.0.2";
protected LoadTestWithBLOBs loadTest;

View File

@ -10,9 +10,8 @@ import io.metersphere.dto.NodeDTO;
import io.metersphere.engine.AbstractEngine;
import io.metersphere.engine.EngineContext;
import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.docker.request.DockerLoginRequest;
import io.metersphere.engine.docker.request.BaseRequest;
import io.metersphere.engine.docker.request.TestRequest;
import io.metersphere.engine.kubernetes.registry.RegistryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.RestTemplate;
@ -22,9 +21,7 @@ import java.util.stream.Collectors;
public class DockerTestEngine extends AbstractEngine {
private static final String BASE_URL = "http://%s:%d";
private RestTemplate restTemplate;
private RegistryService registryService;
public DockerTestEngine(LoadTestWithBLOBs loadTest) {
this.init(loadTest);
@ -34,7 +31,6 @@ public class DockerTestEngine extends AbstractEngine {
protected void init(LoadTestWithBLOBs loadTest) {
super.init(loadTest);
this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class);
this.registryService = CommonBeanFactory.getBean(RegistryService.class);
// todo 初始化操作
}
@ -84,11 +80,9 @@ public class DockerTestEngine extends AbstractEngine {
testRequest.setSize(1);
testRequest.setTestId(testId);
testRequest.setFileString(content);
testRequest.setImage(registryService.getRegistry() + JMETER_IMAGE);
testRequest.setImage(REGISTRY + JMETER_IMAGE);
testRequest.setTestData(context.getTestData());
testRequest.setRegistry(registryService.getRegistryUrl());
testRequest.setPassword(registryService.getRegistryPassword());
testRequest.setUsername(registryService.getRegistryUsername());
// todo 判断测试状态
String taskStatusUri = String.format(BASE_URL + "/jmeter/task/status/" + testId, nodeIp, port);
List containerList = restTemplate.postForObject(taskStatusUri, testRequest, List.class);
@ -106,7 +100,7 @@ public class DockerTestEngine extends AbstractEngine {
public void stop() {
// TODO 停止运行测试
String testId = loadTest.getId();
DockerLoginRequest request = new DockerLoginRequest();
BaseRequest request = new BaseRequest();
this.resourceList.forEach(r -> {
NodeDTO node = JSON.parseObject(r.getConfiguration(), NodeDTO.class);
String ip = node.getIp();

View File

@ -0,0 +1,5 @@
package io.metersphere.engine.docker.request;
public class BaseRequest {
}

View File

@ -1,31 +0,0 @@
package io.metersphere.engine.docker.request;
public class DockerLoginRequest {
private String username;
private String password;
private String registry;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRegistry() {
return registry;
}
public void setRegistry(String registry) {
this.registry = registry;
}
}

View File

@ -3,7 +3,7 @@ package io.metersphere.engine.docker.request;
import java.util.HashMap;
import java.util.Map;
public class TestRequest extends DockerLoginRequest {
public class TestRequest extends BaseRequest {
private int size;
private String fileString;

View File

@ -7,7 +7,6 @@ import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.constants.FileType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.engine.AbstractEngine;
import io.metersphere.engine.EngineContext;
@ -16,14 +15,12 @@ import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter;
import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec;
import io.metersphere.engine.kubernetes.provider.ClientCredential;
import io.metersphere.engine.kubernetes.provider.KubernetesProvider;
import io.metersphere.engine.kubernetes.registry.RegistryService;
import org.apache.commons.collections.MapUtils;
import java.util.HashMap;
public class KubernetesTestEngine extends AbstractEngine {
private RegistryService registryService;
public KubernetesTestEngine(LoadTestWithBLOBs loadTest) {
this.init(loadTest);
@ -32,7 +29,6 @@ public class KubernetesTestEngine extends AbstractEngine {
@Override
public void init(LoadTestWithBLOBs loadTest) {
super.init(loadTest);
this.registryService = CommonBeanFactory.getBean(RegistryService.class);
}
@ -63,8 +59,6 @@ public class KubernetesTestEngine extends AbstractEngine {
// create namespace
kubernetesProvider.confirmNamespace(context.getNamespace());
// docker registry
registryService.dockerRegistry(kubernetesProvider, context.getNamespace());
// create cm
try (KubernetesClient client = kubernetesProvider.getKubernetesClient()) {
String configMapName = context.getTestId() + "-files";
@ -93,7 +87,7 @@ public class KubernetesTestEngine extends AbstractEngine {
}});
jmeter.setSpec(new JmeterSpec() {{
setReplicas(1);
setImage(registryService.getRegistry() + JMETER_IMAGE);
setImage(REGISTRY + JMETER_IMAGE);
}});
LogUtil.info("Load test started. " + context.getTestId());
kubernetesProvider.applyCustomResource(jmeter);
@ -117,7 +111,7 @@ public class KubernetesTestEngine extends AbstractEngine {
}});
jmeter.setSpec(new JmeterSpec() {{
setReplicas(1);
setImage(registryService.getRegistry() + JMETER_IMAGE);
setImage(REGISTRY + JMETER_IMAGE);
}});
provider.deleteCustomResource(jmeter);
} catch (Exception e) {

View File

@ -1,94 +0,0 @@
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

@ -10,7 +10,3 @@ 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_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 `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

@ -5,7 +5,6 @@ import io.metersphere.base.domain.SystemParameter;
import io.metersphere.base.mapper.UserMapper;
import io.metersphere.commons.constants.ParamConstants;
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.runner.RunWith;
@ -22,8 +21,6 @@ public class ApplicationTests {
UserMapper userMapper;
@Resource
private RegistryParamService registryParamService;
@Resource
private RegistryService registryService;
@Test
public void test1() {
@ -54,10 +51,4 @@ public class ApplicationTests {
}
@Test
public void test3() {
String registry = registryService.getRegistry();
System.out.println(registry);
}
}