diff --git a/jcommon/docean-plugin/docean-plugin-k8s/pom.xml b/jcommon/docean-plugin/docean-plugin-k8s/pom.xml index 9c7e6c766..4cb47ef56 100644 --- a/jcommon/docean-plugin/docean-plugin-k8s/pom.xml +++ b/jcommon/docean-plugin/docean-plugin-k8s/pom.xml @@ -8,6 +8,7 @@ 1.5.0-jdk21 docean-plugin-k8s + 1.5.1-jdk21 8 5.11.2 @@ -23,10 +24,10 @@ kubernetes-client ${version.fabric8.client} - - io.kubernetes - client-java - 15.0.1 - + + + + + diff --git a/jcommon/docean-plugin/docean-plugin-k8s/src/main/java/run/mone/docean/plugin/k8s/K8sPlugin.java b/jcommon/docean-plugin/docean-plugin-k8s/src/main/java/run/mone/docean/plugin/k8s/K8sPlugin.java index 22f27a0ec..7e5979022 100644 --- a/jcommon/docean-plugin/docean-plugin-k8s/src/main/java/run/mone/docean/plugin/k8s/K8sPlugin.java +++ b/jcommon/docean-plugin/docean-plugin-k8s/src/main/java/run/mone/docean/plugin/k8s/K8sPlugin.java @@ -33,14 +33,12 @@ import io.fabric8.kubernetes.api.model.batch.v1.JobList; import io.fabric8.kubernetes.client.DefaultKubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.dsl.ExecWatch; import io.fabric8.kubernetes.client.dsl.MixedOperation; import io.fabric8.kubernetes.client.informers.ResourceEventHandler; import io.fabric8.kubernetes.client.informers.SharedIndexInformer; import io.fabric8.kubernetes.client.informers.SharedInformerFactory; -import io.kubernetes.client.Exec; -import io.kubernetes.client.openapi.ApiClient; import io.fabric8.kubernetes.api.model.autoscaling.v1.HorizontalPodAutoscaler; -import io.kubernetes.client.util.Streams; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -60,14 +58,10 @@ public class K8sPlugin implements IPlugin { private KubernetesClient client; - private static final ApiClient cli; + private static final KubernetesClient DEFAULT_CLIENT; static { - try { - cli = io.kubernetes.client.util.Config.defaultClient(); - } catch (IOException e) { - throw new RuntimeException(e); - } + DEFAULT_CLIENT = new DefaultKubernetesClient(); } /** @@ -162,27 +156,23 @@ public boolean start(Ioc ioc) { } @SneakyThrows - private static Pair execContainer(String ns, String name, String container, String[] command) { - final Process proc = new Exec(cli).exec(ns, name, command, container, false, false); - ByteArrayOutputStream sb = new ByteArrayOutputStream(); - Thread out = - new Thread( - () -> { - try { - Streams.copy(proc.getInputStream(), sb); - } catch (IOException ex) { - ex.printStackTrace(); - } - }); - out.start(); - - proc.waitFor(); - - // wait for any last output; no need to wait for input thread - out.join(); - - proc.destroy(); - return Pair.of(proc.exitValue(), sb.toString()); + private static Pair execContainer(KubernetesClient client, String ns, String name, String container, String[] command) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ByteArrayOutputStream error = new ByteArrayOutputStream(); + try (ExecWatch execWatch = client.pods().inNamespace(ns).withName(name) + .inContainer(container) + .writingOutput(out) + .writingError(error) + .exec(command)) { + // 等待命令执行完成 + execWatch.close(); + } + // 返回执行结果 + String result = out.toString(); + System.out.println("result:" + result); + // 这里没有直接的退出值,你可能需要根据输出结果来判断命令是否成功执行 + return Pair.of(0, result); + } @SneakyThrows @@ -190,7 +180,7 @@ public static Pair KillContainer(String ns, String name, String String[] shs = new String[]{"bash", "ash", "sh"}; Pair end = null; for (String sh : shs) { - end = execContainer(ns, name, container, new String[]{sh, "-c", "trap \"exit\" SIGINT SIGTERM ; kill -s SIGINT 1"}); + end = execContainer(DEFAULT_CLIENT, ns, name, container, new String[]{sh, "-c", "trap \"exit\" SIGINT SIGTERM ; kill -s SIGINT 1"}); if (end.getKey().equals(0)) { break; } diff --git a/jcommon/file/pom.xml b/jcommon/file/pom.xml index 8f4a84f9a..ee1a2bb1e 100644 --- a/jcommon/file/pom.xml +++ b/jcommon/file/pom.xml @@ -9,7 +9,7 @@ 1.4-jdk20-SNAPSHOT file - 1.4-jdk21-SNAPSHOT + ${submodule-release.version} diff --git a/jcommon/hera/pom.xml b/jcommon/hera/pom.xml index 21e44d88c..9aba37a4e 100644 --- a/jcommon/hera/pom.xml +++ b/jcommon/hera/pom.xml @@ -8,6 +8,7 @@ 1.4-jdk20-SNAPSHOT hera + ${submodule-release.version} pom hera-trace