Search in sources :

Example 1 with KubeClusterResource

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");
                }
            }
        });
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Files(java.nio.file.Files) Constants(org.bf2.performance.Constants) LifecycleMethodExecutionExceptionHandler(org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Collectors(java.util.stream.Collectors) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Logger(org.apache.logging.log4j.Logger) KubeClient(org.bf2.test.k8s.KubeClient) PodList(io.fabric8.kubernetes.api.model.PodList) LogManager(org.apache.logging.log4j.LogManager) TestExecutionExceptionHandler(org.junit.jupiter.api.extension.TestExecutionExceptionHandler) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) KubeClient(org.bf2.test.k8s.KubeClient) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 2 with KubeClusterResource

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();
        }
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Files(java.nio.file.Files) Constants(org.bf2.performance.Constants) LifecycleMethodExecutionExceptionHandler(org.junit.jupiter.api.extension.LifecycleMethodExecutionExceptionHandler) Pod(io.fabric8.kubernetes.api.model.Pod) IOException(java.io.IOException) AfterTestExecutionCallback(org.junit.jupiter.api.extension.AfterTestExecutionCallback) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Collectors(java.util.stream.Collectors) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Executors(java.util.concurrent.Executors) UncheckedIOException(java.io.UncheckedIOException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) Logger(org.apache.logging.log4j.Logger) KubeClient(org.bf2.test.k8s.KubeClient) PodList(io.fabric8.kubernetes.api.model.PodList) LogManager(org.apache.logging.log4j.LogManager) TestExecutionExceptionHandler(org.junit.jupiter.api.extension.TestExecutionExceptionHandler) Path(java.nio.file.Path) ExecutorService(java.util.concurrent.ExecutorService) Path(java.nio.file.Path) PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) Pod(io.fabric8.kubernetes.api.model.Pod) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Container(io.fabric8.kubernetes.api.model.Container) KubeClient(org.bf2.test.k8s.KubeClient) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

Container (io.fabric8.kubernetes.api.model.Container)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodList (io.fabric8.kubernetes.api.model.PodList)2 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)2 PodResource (io.fabric8.kubernetes.client.dsl.PodResource)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Constants (org.bf2.performance.Constants)2 KubeClient (org.bf2.test.k8s.KubeClient)2 AfterTestExecutionCallback (org.junit.jupiter.api.extension.AfterTestExecutionCallback)2 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)2