From 1937665cea9c72d19e9034719fec8c079c506db4 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 11 Oct 2024 19:13:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E8=B0=83=E6=95=B4=E6=8C=87=E4=BB=A4=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/engine/KubernetesProvider.java | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) 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 7f5953f73f..eb484e8902 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 @@ -10,11 +10,9 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.system.dto.pool.TestResourceDTO; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; -import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.util.List; import java.util.concurrent.ThreadLocalRandom; @@ -22,7 +20,7 @@ import java.util.concurrent.ThreadLocalRandom; public class KubernetesProvider { private static final String RUNNING_PHASE = "Running"; - private static final String SHELL_COMMAND = "sh"; + private static final String SHELL_COMMAND = "#!/bin/bash"; public static KubernetesClient getKubernetesClient(TestResourceDTO credential) { ConfigBuilder configBuilder = new ConfigBuilder() @@ -67,38 +65,28 @@ public class KubernetesProvider { Pod pod = getExecPod(client, resource); LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】"); // 创建文件 - String createFile = "echo -e \"" + JSON.toJSONString(runRequest) + "\" > " + scriptId; + String parameters = "cat < " + scriptId + StringUtils.LF + JSON.toFormatJSONString(runRequest) + StringUtils.LF + "EOF" + StringUtils.LF; // 删除文件 - String deleteFile = "rm -f " + scriptId; + String deleteFile = "rm -f " + scriptId + StringUtils.LF; + String commandX = SHELL_COMMAND + StringUtils.LF + parameters + command + StringUtils.LF + deleteFile; + + LogUtils.info("执行命令:【 " + commandX + " 】"); // 同步执行命令 execWatch = client.pods().inNamespace(client.getNamespace()) .withName(pod.getMetadata().getName()) .redirectingInput() .writingOutput(System.out) .writingError(System.err) - .withTTY().exec(SHELL_COMMAND, "-c", createFile + " && " + command + " && " + deleteFile); + .withTTY().exec(commandX); // 等待命令执行完成,获取结果 - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); - - try (InputStream inputStream = execWatch.getOutput(); - InputStream errStream = execWatch.getError()) { - - // 读取标准输出和错误输出 - IOUtils.copy(inputStream, outputStream); - IOUtils.copy(errStream, errorStream); - + try (InputStream error = execWatch.getError()) { // 判断是否有错误输出 - if (errorStream.size() > 0) { - throw new MSException("Kubernetes exec error: " + errorStream); + if (error != null && error.available() > 0) { + throw new MSException("Kubernetes exec error"); } - - // 输出结果 - String result = outputStream.toString(); - LogUtils.info("命令执行结果: " + result); } } finally { // 确保 ExecWatch 被关闭以释放资源