|
@@ -1,13 +1,23 @@
|
|
|
package io.renren.common.utils;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import io.argoproj.workflow.models.NodeStatus;
|
|
|
+import io.argoproj.workflow.models.Workflow;
|
|
|
import io.kubernetes.client.PodLogs;
|
|
|
import io.kubernetes.client.openapi.ApiException;
|
|
|
+import io.kubernetes.client.openapi.apis.CoreV1Api;
|
|
|
+import io.kubernetes.client.openapi.models.V1Container;
|
|
|
+import io.kubernetes.client.openapi.models.V1Pod;
|
|
|
import io.kubernetes.client.util.ClientBuilder;
|
|
|
import io.kubernetes.client.util.KubeConfig;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import java.io.FileReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
import io.kubernetes.client.openapi.ApiClient;
|
|
|
import io.kubernetes.client.openapi.Configuration;
|
|
|
/**
|
|
@@ -83,5 +93,168 @@ public class KubernetesUtils {
|
|
|
return inputStream;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据podname 获取pod ,获取pod中容器个数,获取每个容器的日志,返回日志结果
|
|
|
+ * @param namespace 命名空间
|
|
|
+ * @param podname pod名称
|
|
|
+ * @return 日志结果
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public static Map<String,String> getPodLog2(String namespace, String podname,Map<String,String> podcontent) throws IOException{
|
|
|
+ PodLogs podLogs = new PodLogs(kubeClient);
|
|
|
+ CoreV1Api apiInstance = new CoreV1Api(kubeClient);
|
|
|
+ V1Pod resultpod;
|
|
|
+ try {
|
|
|
+ resultpod = apiInstance.readNamespacedPod(podname, namespace, "true", true, true);
|
|
|
+ List<V1Container> containers1 = resultpod.getSpec().getContainers();
|
|
|
+ for(V1Container container:containers1){
|
|
|
+// container.getName();
|
|
|
+// System.out.println(container.getName());
|
|
|
+ InputStream inputStream1 = podLogs.streamNamespacedPodLog(namespace, podname, container.getName());
|
|
|
+ String podcontentmain1= new BufferedReader(new InputStreamReader(inputStream1)).lines().collect(Collectors.joining(System.lineSeparator()));
|
|
|
+ podcontent.put(container.getName(),podcontentmain1);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (ApiException e) {
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+ return podcontent;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据podname 获取pod
|
|
|
+ * @param namespace 命名空间
|
|
|
+ * @param podname pod名称
|
|
|
+ * @return 日志结果
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public static V1Pod getPod(String namespace, String podname) {
|
|
|
+ CoreV1Api apiInstance = new CoreV1Api(kubeClient);
|
|
|
+ V1Pod resultpod = null;
|
|
|
+ try {
|
|
|
+ resultpod = apiInstance.readNamespacedPod(podname, namespace, "true", true, true);
|
|
|
+ } catch (ApiException e) {
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+ return resultpod;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据podname 获取pod 中镜像列表
|
|
|
+ * @param namespace 命名空间
|
|
|
+ * @param podname pod名称
|
|
|
+ * @return 日志结果
|
|
|
+
|
|
|
+ */
|
|
|
+ public static List<V1Container> getPodContainers(String namespace, String podname) {
|
|
|
+ System.out.println("podnameL "+podname+" namespace: "+namespace);
|
|
|
+ CoreV1Api apiInstance = new CoreV1Api(kubeClient);
|
|
|
+ V1Pod resultpod;
|
|
|
+ List<V1Container> containers1 = null;
|
|
|
+ try {
|
|
|
+ resultpod = apiInstance.readNamespacedPod(podname, namespace, "true", true, true);
|
|
|
+ System.out.println(resultpod);
|
|
|
+ containers1 = resultpod.getSpec().getContainers();
|
|
|
+ System.out.println("containers");
|
|
|
+ System.out.println(containers1);
|
|
|
+
|
|
|
+ } catch (ApiException e) {
|
|
|
+ System.out.println(e);
|
|
|
+ }
|
|
|
+ return containers1;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据podname 获取pod ,获取pod中容器个数,获取每个容器的日志,返回日志结果
|
|
|
+ * @param namespace 命名空间
|
|
|
+ * @return 日志结果
|
|
|
+ * @throws IOException
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public static void watchWorkflowlog(String namespace,String workflowName,OutputStream outputStream) throws IOException{
|
|
|
+ Map<String,String> workflowcontent = new HashMap<String,String>();//日志
|
|
|
+ StringBuilder sbmain = new StringBuilder();
|
|
|
+ StringBuilder sbinit = new StringBuilder();
|
|
|
+ StringBuilder sbwait = new StringBuilder();
|
|
|
+
|
|
|
+ //根据提交后的工作流名称获取该工作流完整信息
|
|
|
+ Workflow successworkflow = null;
|
|
|
+ try {
|
|
|
+ successworkflow = ArgoUtils.getWorkflow(namespace, workflowName);
|
|
|
+ //创建一个api对象
|
|
|
+ CoreV1Api api = new CoreV1Api();
|
|
|
+ Map<String, NodeStatus> nodeStatusMap = successworkflow.getStatus().getNodes();
|
|
|
+ for (Map.Entry<String, NodeStatus> entry : nodeStatusMap.entrySet()) {
|
|
|
+ //
|
|
|
+ if (entry.getValue().getType().equals("Pod")) {
|
|
|
+// String templateName = entry.getValue().getTemplateName();
|
|
|
+// String displayName = entry.getValue().getDisplayName(); //dag taskname唯一
|
|
|
+ String podname = entry.getValue().getId();
|
|
|
+ System.out.println("podname");
|
|
|
+ System.out.println(podname);
|
|
|
+ String podphase = entry.getValue().getPhase();
|
|
|
+ if(podphase.equals("Pending")){
|
|
|
+// sbmain.append("pod初始化中");
|
|
|
+// sbmain.append("\n");
|
|
|
+ }else{
|
|
|
+ List<V1Container> podContainers = KubernetesUtils.getPodContainers(namespace, podname);
|
|
|
+ if(podContainers!=null){
|
|
|
+ for(V1Container container:podContainers){
|
|
|
+// container.getName();
|
|
|
+// System.out.println(container.getName());
|
|
|
+ InputStream inputStream1 = getPodLog(namespace,podname,container.getName());
|
|
|
+ String podcontentmain1= new BufferedReader(new InputStreamReader(inputStream1)).lines().collect(Collectors.joining(System.lineSeparator()));
|
|
|
+ if(container.getName().equals("main")){
|
|
|
+ System.out.println("main: " +podcontentmain1);
|
|
|
+ sbmain.append(podcontentmain1);
|
|
|
+ sbmain.append("\n");
|
|
|
+ }else if(container.getName().equals("init")){
|
|
|
+ System.out.println("init: " +podcontentmain1);
|
|
|
+ sbinit.append(podcontentmain1);
|
|
|
+ sbinit.append("\n");
|
|
|
+ }else if(container.getName().equals("wait")){
|
|
|
+ System.out.println("wait: " +podcontentmain1);
|
|
|
+ sbwait.append(podcontentmain1);
|
|
|
+ sbwait.append("\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ System.out.println("当前pod中镜像为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (io.argoproj.workflow.ApiException | FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (io.kubernetes.client.openapi.ApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ workflowcontent.put("main",sbmain.toString());
|
|
|
+ workflowcontent.put("init",sbinit.toString());
|
|
|
+ workflowcontent.put("wait",sbwait.toString());
|
|
|
+ System.out.println(workflowcontent);
|
|
|
+ String paramjson= JSONObject.toJSONString(workflowcontent);
|
|
|
+
|
|
|
+ outputStream.write(paramjson.getBytes());
|
|
|
+// MyWebSocket.sendMessage(paramjson);
|
|
|
+// if(successworkflow.getStatus().getPhase().equals("Succeed"))
|
|
|
+
|
|
|
+// kubeClient.pods().inNamespace(namespace).withName(podName).tailingLines(10).watchLog(System.out);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
}
|