From 1de5ad8aa5033d80f76657495c9b65b7aec90b19 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 17 Oct 2024 14:32:37 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=A7=BB=E5=8A=A8=20k8s=20=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=88=B0=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/api-test/pom.xml | 12 ---------- backend/services/system-setting/pom.xml | 11 +++++++++ .../system}/engine/KubernetesExecEngine.java | 16 ++++++------- .../system}/engine/KubernetesProvider.java | 23 ++++++++++++++++++- .../service/TestResourcePoolService.java | 7 ++---- 5 files changed, 42 insertions(+), 27 deletions(-) rename backend/services/{api-test/src/main/java/io/metersphere/api => system-setting/src/main/java/io/metersphere/system}/engine/KubernetesExecEngine.java (84%) rename backend/services/{api-test/src/main/java/io/metersphere/api => system-setting/src/main/java/io/metersphere/system}/engine/KubernetesProvider.java (89%) diff --git a/backend/services/api-test/pom.xml b/backend/services/api-test/pom.xml index 6cf54012b7..6cbc840c19 100644 --- a/backend/services/api-test/pom.xml +++ b/backend/services/api-test/pom.xml @@ -36,11 +36,6 @@ test-jar test - - io.metersphere - monitoring-engine - ${monitoring-engine.revision} - io.metersphere metersphere-project-management @@ -94,13 +89,6 @@ - - - - io.fabric8 - kubernetes-client - ${kubernetes-client.version} - diff --git a/backend/services/system-setting/pom.xml b/backend/services/system-setting/pom.xml index ff6cf7357d..37c7409b3f 100644 --- a/backend/services/system-setting/pom.xml +++ b/backend/services/system-setting/pom.xml @@ -166,6 +166,17 @@ springdoc-openapi-starter-webmvc-ui ${springdoc-openapi-ui.version} + + + io.fabric8 + kubernetes-client + ${kubernetes-client.version} + + + io.metersphere + monitoring-engine + ${monitoring-engine.revision} + diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java b/backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesExecEngine.java similarity index 84% rename from backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesExecEngine.java index b403fe8c0d..07fc14401c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesExecEngine.java @@ -1,4 +1,4 @@ -package io.metersphere.api.engine; +package io.metersphere.system.engine; import io.metersphere.engine.ApiEngine; import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO; @@ -14,8 +14,6 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; -import static io.metersphere.api.controller.result.ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR; - public class KubernetesExecEngine implements ApiEngine { /** * 任务请求参数 @LINK TaskRequestDTO or TaskBatchRequestDTO or List @@ -26,7 +24,7 @@ public class KubernetesExecEngine implements ApiEngine { /** * 单调执行构造函数 * - * @param request 任务请求参数 + * @param request 任务请求参数 * @param resource 资源池 */ public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) { @@ -38,7 +36,7 @@ public class KubernetesExecEngine implements ApiEngine { * 批量执行构造函数 * * @param batchRequestDTO 批量任务请求参数 - * @param resource 资源池 + * @param resource 资源池 */ public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) { this.resource = resource; @@ -49,7 +47,7 @@ public class KubernetesExecEngine implements ApiEngine { * 停止执行构造函数 * * @param reportIds 任务ID列表 - * @param resource 资源池 + * @param resource 资源池 */ public KubernetesExecEngine(List reportIds, TestResourceDTO resource) { this.resource = resource; @@ -79,19 +77,19 @@ public class KubernetesExecEngine implements ApiEngine { // 获取错误代码并处理 int errorCode = Optional.ofNullable(e.getResponseBodyAs(ResultHolder.class)) .map(ResultHolder::getCode) - .orElseThrow(() -> new MSException(RESOURCE_POOL_EXECUTE_ERROR, "Unknown error code")); + .orElseThrow(() -> new MSException("Unknown error code")); // 匹配资源池的错误代码并抛出相应异常 TaskRunnerResultCode resultCode = Arrays.stream(TaskRunnerResultCode.values()) .filter(code -> code.getCode() == errorCode) .findFirst() - .orElseThrow(() -> new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage())); + .orElseThrow(() -> new MSException(e.getMessage())); throw new MSException(resultCode, e.getMessage()); } private void handleGeneralError(Exception e) { LogUtils.error("K8S 执行异常:", e); - throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); + throw new MSException(e.getMessage()); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java b/backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesProvider.java similarity index 89% rename from backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java rename to backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesProvider.java index a57110a123..55320693a1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/engine/KubernetesProvider.java @@ -1,4 +1,4 @@ -package io.metersphere.api.engine; +package io.metersphere.system.engine; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.client.ConfigBuilder; @@ -219,4 +219,25 @@ public class KubernetesProvider { return result; } + + public static boolean validateNamespaceExists(TestResourceDTO testResourceDTO) { + LogUtils.info("Test resource config: {}", testResourceDTO); + try (KubernetesClient kubernetesClient = getKubernetesClient(testResourceDTO)) { + // 校验 KubernetesClient 是否为空,避免后续调用产生 NullPointerException + if (kubernetesClient == null) { + throw new IllegalArgumentException("Kubernetes client initialization failed. Please check your configuration."); + } + + // 直接获取 pods 并进行非空判断 + List pods = getPods(kubernetesClient, testResourceDTO); + if (org.apache.commons.collections.CollectionUtils.isEmpty(pods)) { + throw new RuntimeException("No execution pods found for the given resource: " + testResourceDTO.getNamespace()); + } + } catch (Exception e) { + LogUtils.error("Failed to validate namespace exists", e); + return false; + } + return true; + } + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java index 424f4c415a..9b45e56dc4 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/TestResourcePoolService.java @@ -11,6 +11,7 @@ import io.metersphere.system.domain.*; import io.metersphere.system.dto.pool.*; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.QueryResourcePoolRequest; +import io.metersphere.system.engine.KubernetesProvider; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; @@ -95,11 +96,7 @@ public class TestResourcePoolService { return false; } } else { - KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class); - if (resourcePoolService == null) { - return false; - } - return resourcePoolService.validate(testResourceDTO); + return KubernetesProvider.validateNamespaceExists(testResourceDTO); } }