Skip to content

Commit

Permalink
Enhance LogCollector to store logs from all contaners in pod when tes…
Browse files Browse the repository at this point in the history
…t failed
  • Loading branch information
kornys committed Jan 3, 2024
1 parent 93e4933 commit 692792f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
34 changes: 16 additions & 18 deletions src/main/java/io/odh/test/framework/logs/LogCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});
}
}
6 changes: 5 additions & 1 deletion src/main/java/io/odh/test/platform/KubeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <---------
// ==================================
Expand Down

0 comments on commit 692792f

Please sign in to comment.