chore: 移动 k8s 实现到系统设置模块

This commit is contained in:
fit2-zhao 2024-10-17 14:32:37 +08:00 committed by Craftsman
parent d4655268f1
commit 1de5ad8aa5
5 changed files with 42 additions and 27 deletions

View File

@ -36,11 +36,6 @@
<type>test-jar</type> <type>test-jar</type>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>io.metersphere</groupId>
<artifactId>monitoring-engine</artifactId>
<version>${monitoring-engine.revision}</version>
</dependency>
<dependency> <dependency>
<groupId>io.metersphere</groupId> <groupId>io.metersphere</groupId>
<artifactId>metersphere-project-management</artifactId> <artifactId>metersphere-project-management</artifactId>
@ -94,13 +89,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- k8s client -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -166,6 +166,17 @@
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${springdoc-openapi-ui.version}</version> <version>${springdoc-openapi-ui.version}</version>
</dependency> </dependency>
<!-- k8s client -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>
<dependency>
<groupId>io.metersphere</groupId>
<artifactId>monitoring-engine</artifactId>
<version>${monitoring-engine.revision}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,4 +1,4 @@
package io.metersphere.api.engine; package io.metersphere.system.engine;
import io.metersphere.engine.ApiEngine; import io.metersphere.engine.ApiEngine;
import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO; import io.metersphere.sdk.dto.api.task.TaskBatchRequestDTO;
@ -14,8 +14,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static io.metersphere.api.controller.result.ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR;
public class KubernetesExecEngine implements ApiEngine { public class KubernetesExecEngine implements ApiEngine {
/** /**
* 任务请求参数 @LINK TaskRequestDTO or TaskBatchRequestDTO or List<String> * 任务请求参数 @LINK TaskRequestDTO or TaskBatchRequestDTO or List<String>
@ -26,7 +24,7 @@ public class KubernetesExecEngine implements ApiEngine {
/** /**
* 单调执行构造函数 * 单调执行构造函数
* *
* @param request 任务请求参数 * @param request 任务请求参数
* @param resource 资源池 * @param resource 资源池
*/ */
public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) { public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) {
@ -38,7 +36,7 @@ public class KubernetesExecEngine implements ApiEngine {
* 批量执行构造函数 * 批量执行构造函数
* *
* @param batchRequestDTO 批量任务请求参数 * @param batchRequestDTO 批量任务请求参数
* @param resource 资源池 * @param resource 资源池
*/ */
public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) { public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) {
this.resource = resource; this.resource = resource;
@ -49,7 +47,7 @@ public class KubernetesExecEngine implements ApiEngine {
* 停止执行构造函数 * 停止执行构造函数
* *
* @param reportIds 任务ID列表 * @param reportIds 任务ID列表
* @param resource 资源池 * @param resource 资源池
*/ */
public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource) { public KubernetesExecEngine(List<String> reportIds, TestResourceDTO resource) {
this.resource = resource; this.resource = resource;
@ -79,19 +77,19 @@ public class KubernetesExecEngine implements ApiEngine {
// 获取错误代码并处理 // 获取错误代码并处理
int errorCode = Optional.ofNullable(e.getResponseBodyAs(ResultHolder.class)) int errorCode = Optional.ofNullable(e.getResponseBodyAs(ResultHolder.class))
.map(ResultHolder::getCode) .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()) TaskRunnerResultCode resultCode = Arrays.stream(TaskRunnerResultCode.values())
.filter(code -> code.getCode() == errorCode) .filter(code -> code.getCode() == errorCode)
.findFirst() .findFirst()
.orElseThrow(() -> new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage())); .orElseThrow(() -> new MSException(e.getMessage()));
throw new MSException(resultCode, e.getMessage()); throw new MSException(resultCode, e.getMessage());
} }
private void handleGeneralError(Exception e) { private void handleGeneralError(Exception e) {
LogUtils.error("K8S 执行异常:", e); LogUtils.error("K8S 执行异常:", e);
throw new MSException(RESOURCE_POOL_EXECUTE_ERROR, e.getMessage()); throw new MSException(e.getMessage());
} }
} }

View File

@ -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.api.model.Pod;
import io.fabric8.kubernetes.client.ConfigBuilder; import io.fabric8.kubernetes.client.ConfigBuilder;
@ -219,4 +219,25 @@ public class KubernetesProvider {
return result; 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<Pod> 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;
}
} }

View File

@ -11,6 +11,7 @@ import io.metersphere.system.domain.*;
import io.metersphere.system.dto.pool.*; import io.metersphere.system.dto.pool.*;
import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.sdk.QueryResourcePoolRequest; 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.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.log.dto.LogDTO;
@ -95,11 +96,7 @@ public class TestResourcePoolService {
return false; return false;
} }
} else { } else {
KubernetesResourcePoolService resourcePoolService = CommonBeanFactory.getBean(KubernetesResourcePoolService.class); return KubernetesProvider.validateNamespaceExists(testResourceDTO);
if (resourcePoolService == null) {
return false;
}
return resourcePoolService.validate(testResourceDTO);
} }
} }