use of org.bf2.performance.framework.KubeClusterResource in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class TestExceptionCallbackListener method afterTestExecution.
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
for (KubeClusterResource kubeClusterResource : KubeClusterResource.getCurrentConnectedClusters()) {
KubeClient kubeClient = kubeClusterResource.kubeClient();
kubeClient.client().namespaces().list().getItems().stream().filter(ns -> checkAnnotation(ns, Constants.ORG_BF2_PERFORMANCE_CHECKRESTARTEDCONTAINERS)).forEach(ns -> {
List<Pod> podsWithRestartedContainers = kubeClient.client().pods().inNamespace(ns.getMetadata().getName()).list().getItems().stream().filter(p -> p.getStatus().getContainerStatuses().stream().anyMatch(cs -> cs.getRestartCount() > 0)).collect(Collectors.toList());
if (!podsWithRestartedContainers.isEmpty()) {
LOGGER.error("Found {} pod(s) with containers that had restarted at least once", podsWithRestartedContainers.size());
podsWithRestartedContainers.forEach(p -> {
p.getStatus().getContainerStatuses().stream().filter(cs -> cs.getRestartCount() > 0).forEach(cs -> {
LOGGER.error("Pod {} container {} restart count {}", p.getMetadata().getName(), cs.getName(), cs.getRestartCount());
});
});
if (context.getExecutionException().isEmpty()) {
// Fail the test
throw new RuntimeException("Found {} pod(s) with containers that had restarted at least once");
}
}
});
}
}
use of org.bf2.performance.framework.KubeClusterResource in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class TestExceptionCallbackListener method storeClusterInfo.
/**
* Stores cluster specific information in case of failed test in test callback
*
* @param cluster
* @param logPath
* @throws IOException
*/
private void storeClusterInfo(KubeClusterResource cluster, Path logPath) throws IOException {
Files.createDirectories(logPath);
LOGGER.info("Storing cluster info for {}", cluster.kubeClient().client().getConfiguration().getMasterUrl());
Files.writeString(logPath.resolve("describe_cluster.log"), cluster.cmdKubeClient().exec(false, false, "describe", "nodes").out());
Files.writeString(logPath.resolve("events.log"), cluster.cmdKubeClient().exec(false, false, "get", "events", "--all-namespaces").out());
ExecutorService executorService = Executors.newFixedThreadPool(4);
try {
KubeClient kubeClient = cluster.kubeClient();
cluster.kubeClient().client().namespaces().list().getItems().stream().filter(ns -> checkAnnotation(ns, Constants.ORG_BF2_KAFKA_PERFORMANCE_COLLECTPODLOG)).forEach(ns -> {
try {
Files.writeString(logPath.resolve(String.format("describe_%s_pods.log", ns.getMetadata().getName())), cluster.cmdKubeClient().exec(false, false, "describe", "pods", "-n", ns.getMetadata().getName()).out());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
NonNamespaceOperation<Pod, PodList, PodResource<Pod>> podsOp = kubeClient.client().pods().inNamespace(ns.getMetadata().getName());
List<Pod> pods = podsOp.list().getItems();
for (Pod p : pods) {
try {
List<Container> containers = podsOp.withName(p.getMetadata().getName()).get().getSpec().getContainers();
for (Container c : containers) {
executorService.submit(() -> {
Path filePath = logPath.resolve(String.format("%s_%s.log", p.getMetadata().getName(), c.getName()));
try {
Files.writeString(filePath, podsOp.withName(p.getMetadata().getName()).inContainer(c.getName()).getLog());
} catch (IOException e) {
LOGGER.warn("Cannot write file {}", filePath, e);
}
});
}
} catch (Exception ex) {
LOGGER.warn("Cannot access logs from pod {} ", p.getMetadata().getName(), ex);
}
p.getStatus().getContainerStatuses().stream().filter(cs -> cs.getRestartCount() > 0).forEach(cs -> {
executorService.submit(() -> {
Path filePath = logPath.resolve(String.format("%s_%s_terminated.log", p.getMetadata().getName(), cs.getName()));
try {
Files.writeString(filePath, podsOp.withName(p.getMetadata().getName()).inContainer(cs.getName()).terminated().getLog());
} catch (IOException e) {
LOGGER.warn("Cannot write file {}", filePath, e);
}
});
});
}
});
} finally {
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.HOURS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Aggregations