From 692792fcf63642861036f73214cba00a93b2c031 Mon Sep 17 00:00:00 2001 From: David Kornel Date: Wed, 3 Jan 2024 16:41:32 +0100 Subject: [PATCH] Enhance LogCollector to store logs from all contaners in pod when test failed --- .../odh/test/framework/logs/LogCollector.java | 34 +++++++++---------- .../java/io/odh/test/platform/KubeClient.java | 6 +++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/odh/test/framework/logs/LogCollector.java b/src/main/java/io/odh/test/framework/logs/LogCollector.java index a912fee0..250382ab 100644 --- a/src/main/java/io/odh/test/framework/logs/LogCollector.java +++ b/src/main/java/io/odh/test/framework/logs/LogCollector.java @@ -4,6 +4,7 @@ */ package io.odh.test.framework.logs; +import io.fabric8.kubernetes.api.model.Pod; import io.odh.test.Environment; import io.odh.test.OdhConstants; import io.odh.test.TestUtils; @@ -36,6 +37,18 @@ public static void saveKubernetesState(ExtensionContext extensionContext, Throwa throw throwable; } + private static void writeLogsFromPods(Path logpath, Pod pod) { + pod.getSpec().getContainers().forEach(container -> { + try { + LOGGER.debug("Get logs from pod {}/{} container {}", pod.getMetadata().getNamespace(), pod.getMetadata().getName(), container.getName()); + Files.writeString(logpath.resolve(pod.getMetadata().getNamespace() + "-" + pod.getMetadata().getName() + "-" + container.getName() + ".log"), + ResourceManager.getClient().getLogsFromContainer(pod.getMetadata().getNamespace(), pod.getMetadata().getName(), container.getName())); + } catch (IOException e) { + LOGGER.warn("Cannot get logs for pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); + } + }); + } + private static void saveClusterState(Path logpath) throws IOException { KubeClient kube = ResourceManager.getClient(); KubeCmdClient cmdClient = ResourceManager.getKubeCmdClient(); @@ -45,28 +58,13 @@ private static void saveClusterState(Path logpath) throws IOException { Files.writeString(logpath.resolve("dsc.yml"), cmdClient.exec(false, false, "get", "dsc", "-o", "yaml").out()); Files.writeString(logpath.resolve("dsci.yml"), cmdClient.exec(false, false, "get", "dsci", "-o", "yaml").out()); kube.listPodsByPrefixInName(OdhConstants.BUNDLE_OPERATOR_NAMESPACE, "opendatahub-operator-controller-manager").forEach(pod -> { - try { - LOGGER.debug("Get logs from pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.getLogs(OdhConstants.BUNDLE_OPERATOR_NAMESPACE, pod.getMetadata().getName())); - } catch (IOException e) { - LOGGER.warn("Cannot get logs for pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - } + writeLogsFromPods(logpath, pod); }); kube.listPodsByPrefixInName(OdhConstants.OLM_OPERATOR_NAMESPACE, "opendatahub").forEach(pod -> { - try { - LOGGER.debug("Get logs from pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.getLogs(OdhConstants.OLM_OPERATOR_NAMESPACE, pod.getMetadata().getName())); - } catch (IOException e) { - LOGGER.warn("Cannot get logs for pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - } + writeLogsFromPods(logpath, pod); }); kube.listPods(OdhConstants.CONTROLLERS_NAMESPACE).forEach(pod -> { - try { - LOGGER.debug("Get logs from pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.getLogs(OdhConstants.CONTROLLERS_NAMESPACE, pod.getMetadata().getName())); - } catch (IOException e) { - LOGGER.warn("Cannot get logs for pod {}/{}", pod.getMetadata().getNamespace(), pod.getMetadata().getName()); - } + writeLogsFromPods(logpath, pod); }); } } diff --git a/src/main/java/io/odh/test/platform/KubeClient.java b/src/main/java/io/odh/test/platform/KubeClient.java index a37ff8c7..de26230c 100644 --- a/src/main/java/io/odh/test/platform/KubeClient.java +++ b/src/main/java/io/odh/test/platform/KubeClient.java @@ -233,10 +233,14 @@ public Pod getPod(String name) { return getPod(namespace, name); } - public String getLogs(String namespaceName, String podName) { + public String getLogsFromPod(String namespaceName, String podName) { return client.pods().inNamespace(namespaceName).withName(podName).getLog(); } + public String getLogsFromContainer(String namespaceName, String podName, String containerName) { + return client.pods().inNamespace(namespaceName).withName(podName).inContainer(containerName).getLog(); + } + // ================================== // ---------> STATEFUL SET <--------- // ==================================