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

Enhance LogCollector to store logs from all contaners in pod when tes… #43

Merged
merged 1 commit into from
Jan 3, 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
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
Loading