Search in sources :

Example 16 with Namespaces

use of org.bf2.cos.fleetshard.support.resources.Namespaces 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

Namespace (io.fabric8.kubernetes.api.model.Namespace)7 ConnectorNamespace (org.bf2.cos.fleet.manager.model.ConnectorNamespace)6 Test (org.junit.jupiter.api.Test)6 List (java.util.List)5 Objects (java.util.Objects)5 Pod (io.fabric8.kubernetes.api.model.Pod)4 QuarkusTest (io.quarkus.test.junit.QuarkusTest)4 IOException (java.io.IOException)4 KubeClient (org.bf2.test.k8s.KubeClient)4 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)3 PodList (io.fabric8.kubernetes.api.model.PodList)3 Quantity (io.fabric8.kubernetes.api.model.Quantity)3 ResourceQuota (io.fabric8.kubernetes.api.model.ResourceQuota)3 RestAssured (io.restassured.RestAssured)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)2 ContentTypeHeader (com.github.tomakehurst.wiremock.http.ContentTypeHeader)2 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)2 Container (io.fabric8.kubernetes.api.model.Container)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 TestProfile (io.quarkus.test.junit.TestProfile)2