fix(接口测试): 解决 Kubernetes client 被提前关闭问题

This commit is contained in:
fit2-zhao 2024-10-14 10:56:33 +08:00 committed by Craftsman
parent 6433312de3
commit 60a7acd9d2
1 changed files with 9 additions and 6 deletions

View File

@ -70,8 +70,8 @@ public class KubernetesProvider {
* @param apiPath * @param apiPath
*/ */
protected static void exec(TestResourceDTO resource, Object runRequest, String apiPath, String optToken) { protected static void exec(TestResourceDTO resource, Object runRequest, String apiPath, String optToken) {
try (KubernetesClient client = getKubernetesClient(resource)) { KubernetesClient client = getKubernetesClient(resource);
try {
if (runRequest instanceof TaskBatchRequestDTO request) { if (runRequest instanceof TaskBatchRequestDTO request) {
// 均分给每一个 Pod // 均分给每一个 Pod
List<Pod> pods = getPods(client, resource); List<Pod> pods = getPods(client, resource);
@ -107,7 +107,9 @@ public class KubernetesProvider {
} }
} }
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("Failed to execute tasks on Kubernetes.", e); LogUtils.error("Failed to execute command", e);
client.close();
throw new MSException("Failed to execute command", e);
} }
} }
@ -149,14 +151,14 @@ public class KubernetesProvider {
.writingOutput(System.out) .writingOutput(System.out)
.writingError(System.err) .writingError(System.err)
.withTTY() .withTTY()
.usingListener(new SimpleListener(runRequest)) .usingListener(new SimpleListener(runRequest, client))
.exec(SHELL_COMMAND, "-c", command); .exec(SHELL_COMMAND, "-c", command);
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("Failed to execute command on pod {} ", pod.getMetadata().getName(), e); LogUtils.error("Failed to execute command on pod {} ", pod.getMetadata().getName(), e);
} }
} }
private record SimpleListener(Object runRequest) implements ExecListener { private record SimpleListener(Object runRequest, KubernetesClient client) implements ExecListener {
@Override @Override
public void onOpen() { public void onOpen() {
LogUtils.info("K8s 开启监听"); LogUtils.info("K8s 开启监听");
@ -173,7 +175,8 @@ public class KubernetesProvider {
@Override @Override
public void onClose(int code, String reason) { public void onClose(int code, String reason) {
LogUtils.info("K8s 监听关闭code=" + code + ", reason=" + reason); LogUtils.info("K8s 监听关闭code=" + code + ", reason=" + reason);
// No additional actions needed for now // 关闭客户端
client.close();
} }
} }