From e32ff12d227f5aed7914e96eb6bdb6607f9fdde9 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 15 Dec 2020 13:56:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8k8s=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/pom.xml | 12 ++++++-- .../performance/engine/AbstractEngine.java | 3 +- .../performance/engine/EngineFactory.java | 28 ++++++++++++++++--- .../service/KubernetesTestEngine.java | 6 ++++ backend/src/main/java/io/metersphere/xpack | 2 +- 5 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/service/KubernetesTestEngine.java diff --git a/backend/pom.xml b/backend/pom.xml index 07439ad68f..a227d4f89b 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -272,9 +272,9 @@ - - - + + + @@ -373,6 +373,12 @@ httpclient 4.5.6 + + + net.oneandone.reflections8 + reflections8 + 0.11.7 + diff --git a/backend/src/main/java/io/metersphere/performance/engine/AbstractEngine.java b/backend/src/main/java/io/metersphere/performance/engine/AbstractEngine.java index 60a8e7ffcc..a2fec3c680 100644 --- a/backend/src/main/java/io/metersphere/performance/engine/AbstractEngine.java +++ b/backend/src/main/java/io/metersphere/performance/engine/AbstractEngine.java @@ -58,7 +58,8 @@ public abstract class AbstractEngine implements Engine { if (resourcePool == null) { MSException.throwException("Resource Pool is empty"); } - if (!ResourcePoolTypeEnum.NODE.name().equals(resourcePool.getType())) { + if (!ResourcePoolTypeEnum.K8S.name().equals(resourcePool.getType()) + && !ResourcePoolTypeEnum.NODE.name().equals(resourcePool.getType())) { MSException.throwException("Invalid Resource Pool type."); } this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId()); diff --git a/backend/src/main/java/io/metersphere/performance/engine/EngineFactory.java b/backend/src/main/java/io/metersphere/performance/engine/EngineFactory.java index 4b2d6040b3..7aaefeb9b7 100644 --- a/backend/src/main/java/io/metersphere/performance/engine/EngineFactory.java +++ b/backend/src/main/java/io/metersphere/performance/engine/EngineFactory.java @@ -2,6 +2,7 @@ package io.metersphere.performance.engine; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import io.metersphere.Application; import io.metersphere.base.domain.FileContent; import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.LoadTestWithBLOBs; @@ -16,17 +17,17 @@ import io.metersphere.performance.engine.docker.DockerTestEngine; import io.metersphere.performance.parse.EngineSourceParser; import io.metersphere.performance.parse.EngineSourceParserFactory; import io.metersphere.service.FileService; +import io.metersphere.service.KubernetesTestEngine; import io.metersphere.service.TestResourcePoolService; +import org.apache.commons.beanutils.ConstructorUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.reflections8.Reflections; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.ByteArrayInputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @Service @@ -34,6 +35,17 @@ public class EngineFactory { private static FileService fileService; private static TestResourcePoolService testResourcePoolService; private static KafkaProperties kafkaProperties; + private static Class kubernetesTestEngineClass; + + static { + Reflections reflections = new Reflections(Application.class.getPackage().getName()); + Set> implClass = reflections.getSubTypesOf(KubernetesTestEngine.class); + for (Class aClass : implClass) { + kubernetesTestEngineClass = aClass; + // 第一个 + break; + } + } public static Engine createEngine(LoadTestWithBLOBs loadTest) { String resourcePoolId = loadTest.getTestResourcePoolId(); @@ -51,6 +63,14 @@ public class EngineFactory { if (type == ResourcePoolTypeEnum.NODE) { return new DockerTestEngine(loadTest); } + if (type == ResourcePoolTypeEnum.K8S) { + try { + return ConstructorUtils.invokeConstructor(kubernetesTestEngineClass, loadTest); + } catch (Exception e) { + LogUtil.error(e); + return null; + } + } return null; } diff --git a/backend/src/main/java/io/metersphere/service/KubernetesTestEngine.java b/backend/src/main/java/io/metersphere/service/KubernetesTestEngine.java new file mode 100644 index 0000000000..47f2114ae5 --- /dev/null +++ b/backend/src/main/java/io/metersphere/service/KubernetesTestEngine.java @@ -0,0 +1,6 @@ +package io.metersphere.service; + +import io.metersphere.performance.engine.Engine; + +public interface KubernetesTestEngine extends Engine { +} diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index acb5adf44c..61397c1672 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit acb5adf44cf5f4a3b512abbff1e437957a9fcfb5 +Subproject commit 61397c16728a63493507679f7e0940d9099f337f