Skip to content

Commit

Permalink
fix: k8s plugin #773 (#774)
Browse files Browse the repository at this point in the history
* fix mvc request method (#749)

* fix: license pom #773
  • Loading branch information
shanwb authored Jan 12, 2024
1 parent cde9274 commit 58f8807
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
11 changes: 6 additions & 5 deletions jcommon/docean-plugin/docean-plugin-k8s/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>docean-plugin-k8s</artifactId>
<version>1.4.0-v1-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand All @@ -29,11 +30,11 @@
<artifactId>kubernetes-client</artifactId>
<version>${version.fabric8.client}</version>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>15.0.1</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>io.kubernetes</groupId>-->
<!-- <artifactId>client-java</artifactId>-->
<!-- <version>15.0.1</version>-->
<!-- </dependency>-->

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -162,35 +156,31 @@ public boolean start(Ioc ioc) {
}

@SneakyThrows
private static Pair<Integer, String> 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<Integer, String> 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
public static Pair<Integer, String> KillContainer(String ns, String name, String container) {
String[] shs = new String[]{"bash", "ash", "sh"};
Pair<Integer, String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public void testCrd() {
@SneakyThrows
@Test
public void testKill() {
String podName = "wangzhidong1-nacostestclient-930391-f44c9db7d-zxnb2";
String namespace = "wangzhidong1";
K8sPlugin.KillContainer(namespace,podName,"toolbox");
String podName = "kibana-7f5fb9c49b-4gzck";
String namespace = "xxx";
K8sPlugin.KillContainer(namespace, podName,"kibana");
}
}

0 comments on commit 58f8807

Please sign in to comment.