From 97c6ddea80f75038ad7185e325b5ac8d57fc1e56 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 11 Oct 2024 10:03:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E5=8D=95=E4=B8=AA=E6=89=A7=E8=A1=8C=E6=94=AF=E6=8C=81k8s?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/engine/KubernetesExecEngine.java | 2 ++ .../api/engine/KubernetesProvider.java | 1 + .../api/service/ApiExecuteService.java | 35 +++++++++++++++---- .../api/k8s/KubernetesExecTests.java | 25 ++++++++++--- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java index 53811f340c..52fe8ee37b 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java @@ -3,6 +3,7 @@ package io.metersphere.api.engine; import io.metersphere.engine.ApiEngine; import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO; import io.metersphere.sdk.dto.api.task.TaskRequestDTO; +import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.dto.pool.TestResourceDTO; @@ -43,6 +44,7 @@ public class KubernetesExecEngine implements ApiEngine { } catch (Exception e) { LogUtils.error("K8S 执行异常:", e); rollbackOnFailure(); // 错误处理逻辑 + throw new MSException("K8S 节点执行错误:" + e.getMessage(), e); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java index da377b2b91..e915a200d6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java @@ -77,6 +77,7 @@ public class KubernetesProvider { if (runRequest != null) { LogUtils.info("请求参数:{}", JSON.toJSONString(runRequest)); // TODO: Add proper error handling based on response or task request details + throw new MSException("K8S 节点执行错误:" + t.getMessage(), t); } else { throw new MSException("K8S 节点执行错误:" + t.getMessage(), t); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java index 5ba0338121..7ccf3a40ad 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiExecuteService.java @@ -10,6 +10,7 @@ import io.metersphere.api.dto.request.controller.MsScriptElement; import io.metersphere.api.parser.TestElementParser; import io.metersphere.api.parser.TestElementParserFactory; import io.metersphere.api.utils.ApiDataUtils; +import io.metersphere.engine.EngineFactory; import io.metersphere.engine.MsHttpClient; import io.metersphere.plugin.api.dto.ParameterConfig; import io.metersphere.plugin.api.spi.AbstractMsTestElement; @@ -31,6 +32,7 @@ import io.metersphere.sdk.util.*; import io.metersphere.system.config.MinioProperties; import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.TestResourcePool; +import io.metersphere.system.dto.pool.TestResourceDTO; import io.metersphere.system.dto.pool.TestResourceNodeDTO; import io.metersphere.system.dto.pool.TestResourcePoolReturnDTO; import io.metersphere.system.service.*; @@ -213,19 +215,38 @@ public class ApiExecuteService { // 获取资源池 TestResourcePoolReturnDTO testResourcePoolDTO = getGetResourcePoolNodeDTO(taskInfo.getRunModeConfig(), taskInfo.getProjectId()); - TestResourceNodeDTO testResourceNodeDTO = getNextExecuteNode(testResourcePoolDTO); if (StringUtils.isNotBlank(testResourcePoolDTO.getServerUrl())) { // 如果资源池配置了当前站点,则使用资源池的 taskInfo.setMsUrl(testResourcePoolDTO.getServerUrl()); } - taskInfo.setPoolSize(testResourceNodeDTO.getConcurrentNumber()); - String endpoint = MsHttpClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort()); - LogUtils.info("开始发送请求【 {}_{} 】到 {} 节点执行", taskItem.getReportId(), taskItem.getResourceId(), endpoint); - if (StringUtils.equalsAny(taskInfo.getRunMode(), ApiExecuteRunMode.FRONTEND_DEBUG.name(), ApiExecuteRunMode.BACKEND_DEBUG.name())) { - MsHttpClient.debugApi(endpoint, taskRequest); + + // 判断是否为 K8S 资源池 + boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.name()); + boolean isDebugMode = StringUtils.equalsAny(taskInfo.getRunMode(), ApiExecuteRunMode.FRONTEND_DEBUG.name(), ApiExecuteRunMode.BACKEND_DEBUG.name()); + + if (isK8SResourcePool) { + TestResourceDTO testResourceDTO = new TestResourceDTO(); + BeanUtils.copyBean(testResourceDTO, testResourcePoolDTO.getTestResourceReturnDTO()); + taskInfo.setPoolSize(testResourceDTO.getConcurrentNumber()); + LogUtils.info("开始发送请求【 {}_{} 】到 K8S 资源池执行", taskItem.getReportId(), taskItem.getResourceId()); + if (isDebugMode) { + EngineFactory.debugApi(taskRequest, testResourceDTO); + } else { + EngineFactory.runApi(taskRequest, testResourceDTO); + } } else { - MsHttpClient.runApi(endpoint, taskRequest); + TestResourceNodeDTO testResourceNodeDTO = getNextExecuteNode(testResourcePoolDTO); + taskInfo.setPoolSize(testResourceNodeDTO.getConcurrentNumber()); + + String endpoint = MsHttpClient.getEndpoint(testResourceNodeDTO.getIp(), testResourceNodeDTO.getPort()); + LogUtils.info("开始发送请求【 {}_{} 】到 {} 节点执行", taskItem.getReportId(), taskItem.getResourceId(), endpoint); + + if (isDebugMode) { + MsHttpClient.debugApi(endpoint, taskRequest); + } else { + MsHttpClient.runApi(endpoint, taskRequest); + } } } catch (HttpServerErrorException e) { diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/k8s/KubernetesExecTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/k8s/KubernetesExecTests.java index cd90406dc2..b292025994 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/k8s/KubernetesExecTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/k8s/KubernetesExecTests.java @@ -24,7 +24,11 @@ public class KubernetesExecTests extends BaseTest { public void debugApi() throws Exception { TaskRequestDTO request = new TaskRequestDTO(); TestResourceDTO resource = new TestResourceDTO(); - EngineFactory.debugApi(request, resource); + try{ + EngineFactory.debugApi(request, resource); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } } @Test @@ -32,7 +36,11 @@ public class KubernetesExecTests extends BaseTest { public void runApi() throws Exception { TaskRequestDTO request = new TaskRequestDTO(); TestResourceDTO resource = new TestResourceDTO(); - EngineFactory.runApi(request, resource); + try { + EngineFactory.runApi(request, resource); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } } @@ -41,7 +49,11 @@ public class KubernetesExecTests extends BaseTest { public void batchRunApi() throws Exception { TaskBatchRequestDTO request = new TaskBatchRequestDTO(); TestResourceDTO resource = new TestResourceDTO(); - EngineFactory.batchRunApi(request, resource); + try { + EngineFactory.batchRunApi(request, resource); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } } @@ -50,8 +62,11 @@ public class KubernetesExecTests extends BaseTest { public void stop() throws Exception { List request = new ArrayList<>(); TestResourceDTO resource = new TestResourceDTO(); - EngineFactory.stopApi(request, resource); + try { + EngineFactory.stopApi(request, resource); + } catch (Exception e) { + System.out.println("Error: " + e.getMessage()); + } } - } \ No newline at end of file