|
@@ -3,8 +3,8 @@ package nju.seec.SEECdemo.logic.api.k8s.impl;
|
|
|
import com.google.common.io.ByteStreams;
|
|
import com.google.common.io.ByteStreams;
|
|
|
import io.kubernetes.client.ApiException;
|
|
import io.kubernetes.client.ApiException;
|
|
|
import io.kubernetes.client.Exec;
|
|
import io.kubernetes.client.Exec;
|
|
|
-import io.kubernetes.client.apis.CoreV1Api;
|
|
|
|
|
import nju.seec.SEECdemo.logic.api.k8s.ExecApi;
|
|
import nju.seec.SEECdemo.logic.api.k8s.ExecApi;
|
|
|
|
|
+import nju.seec.SEECdemo.logic.api.k8s.exception.K8sApiException;
|
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
import nju.seec.SEECdemo.util.LoggerUtil;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -13,75 +13,44 @@ import org.springframework.stereotype.Service;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@Service
|
|
@Service
|
|
|
public class ExecApiImpl implements ExecApi{
|
|
public class ExecApiImpl implements ExecApi{
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerUtil.getLogger(ExecApiImpl.class);
|
|
private static final Logger logger = LoggerUtil.getLogger(ExecApiImpl.class);
|
|
|
|
|
+
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- private CoreV1Api coreV1Api;
|
|
|
|
|
|
|
+ private Exec exec;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public void exec(String namespace, String podName, String containerName, List<String> commands) {
|
|
|
|
|
-
|
|
|
|
|
|
|
+ public boolean exec(String namespace, String podName, String containerName, List<String> commands) {
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public void exec(String namespace, String podName, List<String> commands) {
|
|
|
|
|
- Exec exec = new Exec();
|
|
|
|
|
-
|
|
|
|
|
- boolean tty = System.console() != null;
|
|
|
|
|
|
|
+ public boolean exec(String namespace, String podName, List<String> commands) throws K8sApiException{
|
|
|
try {
|
|
try {
|
|
|
final Process proc =
|
|
final Process proc =
|
|
|
exec.exec(
|
|
exec.exec(
|
|
|
namespace,
|
|
namespace,
|
|
|
podName,
|
|
podName,
|
|
|
commands.isEmpty()
|
|
commands.isEmpty()
|
|
|
- ? new String[] {"sh"}
|
|
|
|
|
|
|
+ ? new String[]{"sh"}
|
|
|
: commands.toArray(new String[commands.size()]),
|
|
: commands.toArray(new String[commands.size()]),
|
|
|
true,
|
|
true,
|
|
|
- tty);
|
|
|
|
|
-
|
|
|
|
|
- Thread in =
|
|
|
|
|
- new Thread(
|
|
|
|
|
- new Runnable() {
|
|
|
|
|
- public void run() {
|
|
|
|
|
- try {
|
|
|
|
|
- ByteStreams.copy(System.in, proc.getOutputStream());
|
|
|
|
|
- } catch (IOException ex) {
|
|
|
|
|
- ex.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- in.start();
|
|
|
|
|
-
|
|
|
|
|
- Thread out =
|
|
|
|
|
- new Thread(
|
|
|
|
|
- new Runnable() {
|
|
|
|
|
- public void run() {
|
|
|
|
|
- try {
|
|
|
|
|
- ByteStreams.copy(proc.getInputStream(), System.out);
|
|
|
|
|
- } catch (IOException ex) {
|
|
|
|
|
- ex.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- out.start();
|
|
|
|
|
-
|
|
|
|
|
|
|
+ false);
|
|
|
proc.waitFor();
|
|
proc.waitFor();
|
|
|
-
|
|
|
|
|
- // wait for any last output; no need to wait for input thread
|
|
|
|
|
- out.join();
|
|
|
|
|
-
|
|
|
|
|
proc.destroy();
|
|
proc.destroy();
|
|
|
-
|
|
|
|
|
- System.exit(proc.exitValue());
|
|
|
|
|
|
|
+ return proc.exitValue() == 0;
|
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
|
- e.printStackTrace();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ LoggerUtil.error(logger, e, "指令执行异常, namespace={}, podName={}, commands={}, response={}",
|
|
|
|
|
+ namespace, podName, commands, e.getResponseBody());
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
|
|
+ } catch (InterruptedException | IOException e) {
|
|
|
|
|
+ LoggerUtil.error(logger, e, "指令执行异常, namespace={}, podName={}, commands={}",
|
|
|
|
|
+ namespace, podName, commands);
|
|
|
|
|
+ throw K8sApiException.K8s_SYSTEM_ERROR_EXCEPTION;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|