Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: k8s plugin #773 #774

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}