Search in sources :

Example 1 with Exec

use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class Exec method exec.

/**
 * Method executes external command
 *
 * @param command     arguments for command
 * @param envVars
 * @param timeout     timeout for execution
 * @param logToOutput log output or not
 * @param throwErrors look for errors in output and throws exception if true
 * @return execution results
 */
public static ExecResult exec(String input, List<String> command, Set<EnvVar> envVars, int timeout, boolean logToOutput, boolean throwErrors) {
    int ret = 1;
    ExecResult execResult;
    try {
        Exec executor = new Exec();
        LOGGER.info("Command: {}", String.join(" ", command));
        ret = executor.execute(input, command, envVars, timeout);
        synchronized (LOCK) {
            if (logToOutput) {
                LOGGER.info("RETURN code: {}", ret);
                if (!executor.out().isEmpty()) {
                    LOGGER.info("======STDOUT START=======");
                    LOGGER.info("{}", cutExecutorLog(executor.out()));
                    LOGGER.info("======STDOUT END======");
                }
                if (!executor.err().isEmpty()) {
                    LOGGER.info("======STDERR START=======");
                    LOGGER.info("{}", cutExecutorLog(executor.err()));
                    LOGGER.info("======STDERR END======");
                }
            }
        }
        execResult = new ExecResult(ret, executor.out(), executor.err());
        if (throwErrors && ret != 0) {
            String msg = "`" + join(" ", command) + "` got status code " + ret + " and stderr:\n------\n" + executor.stdErr + "\n------\nand stdout:\n------\n" + executor.stdOut + "\n------";
            Matcher matcher = ERROR_PATTERN.matcher(executor.err());
            KubeClusterException t = null;
            if (matcher.find()) {
                switch(matcher.group(1)) {
                    case "NotFound":
                        t = new KubeClusterException.NotFound(execResult, msg);
                        break;
                    case "AlreadyExists":
                        t = new KubeClusterException.AlreadyExists(execResult, msg);
                        break;
                    default:
                        break;
                }
            }
            matcher = INVALID_PATTERN.matcher(executor.err());
            if (matcher.find()) {
                t = new KubeClusterException.InvalidResource(execResult, msg);
            }
            if (t == null) {
                t = new KubeClusterException(execResult, msg);
            }
            throw t;
        }
        return new ExecResult(ret, executor.out(), executor.err());
    } catch (IOException | ExecutionException e) {
        throw new KubeClusterException(e);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new KubeClusterException(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) IOException(java.io.IOException) KubeClusterException(org.bf2.test.k8s.KubeClusterException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with Exec

use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class LogCollector method saveClusterState.

private static void saveClusterState(Path logpath) throws IOException {
    KubeClient kube = KubeClient.getInstance();
    Files.writeString(logpath.resolve("describe-cluster-nodes.log"), kube.cmdClient().exec(false, false, "describe", "nodes").out());
    Files.writeString(logpath.resolve("all-events.log"), kube.cmdClient().exec(false, false, "get", "events", "--all-namespaces").out());
    Files.writeString(logpath.resolve("pvs.log"), kube.cmdClient().exec(false, false, "describe", "pv").out());
    Files.writeString(logpath.resolve("operator-routes.yml"), kube.cmdClient().exec(false, false, "get", "routes", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
    Files.writeString(logpath.resolve("operator-services.yml"), kube.cmdClient().exec(false, false, "get", "service", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
    Files.writeString(logpath.resolve("kas-fleetshard-operator-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("strimzi-kafka-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app.kubernetes.io/managed-by=strimzi-cluster-operator", "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("managedkafkas.yml"), kube.cmdClient().exec(false, false, "get", "managedkafka", "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("kafkas.yml"), kube.cmdClient().exec(false, false, "get", "kafka", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("pods-managed-by-operator.yml"), kube.cmdClient().exec(false, false, "get", "pods", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
    Files.writeString(logpath.resolve("operator-namespace-events.yml"), kube.cmdClient().exec(false, false, "get", "events", "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    Files.writeString(logpath.resolve("operator.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.OPERATOR_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    Files.writeString(logpath.resolve("sync.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.SYNC_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
    StrimziOperatorManager.getStrimziOperatorPods().forEach(pod -> {
        try {
            Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.cmdClient().exec(false, false, "logs", pod.getMetadata().getName(), "--tail", "-1", "-n", pod.getMetadata().getNamespace()).out());
        } catch (Exception e) {
            LOGGER.warn("Cannot get logs from pod {} in namespace {}", pod.getMetadata().getName(), pod.getMetadata().getNamespace());
        }
    });
}
Also used : KubeClient(org.bf2.test.k8s.KubeClient) IOException(java.io.IOException)

Example 3 with Exec

use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class FleetShardOperatorManager method createEndpoint.

public static String createEndpoint(KubeClient kubeClient) {
    String externalEndpointName = SYNC_NAME + "-external";
    if (kubeClient.isGenericKubernetes()) {
        if (kubeClient.client().services().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
            kubeClient.client().services().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--type=LoadBalancer", "--name", externalEndpointName);
        return new ExecBuilder().withCommand("minikube", "service", "--url", externalEndpointName, "-n", OPERATOR_NS).logToOutput(false).exec().out().trim();
    } else {
        OpenShiftClient openShiftClient = kubeClient.client().adapt(OpenShiftClient.class);
        if (openShiftClient.routes().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
            openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
        }
        kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--name", externalEndpointName);
        Route r = openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).get();
        return String.format("%s://%s:%d", r.getSpec().getPort().getTargetPort().getStrVal(), r.getSpec().getHost(), r.getSpec().getPort().getTargetPort().getStrVal().equals("http") ? 80 : 443);
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ExecBuilder(org.bf2.test.executor.ExecBuilder) Route(io.fabric8.openshift.api.model.Route)

Example 4 with Exec

use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class SuiteUnitTest method testExecutorError.

@SequentialTest
void testExecutorError() {
    ExecBuilder command = Exec.builder().withCommand("ppppeeeepppaaa", "jenda").logToOutput(false).throwErrors(true).timeout(60);
    assertThrows(KubeClusterException.class, command::exec);
}
Also used : ExecBuilder(org.bf2.test.executor.ExecBuilder) SequentialTest(org.bf2.systemtest.framework.SequentialTest)

Example 5 with Exec

use of org.bf2.srs.fleetmanager.it.executor.Exec in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class SuiteUnitTest method testExecutor.

@ParallelTest
void testExecutor() {
    ExecResult result = Exec.builder().withCommand("ls", System.getProperty("user.dir")).logToOutput(false).throwErrors(true).timeout(60).exec();
    assertTrue(result.exitStatus());
}
Also used : ExecResult(org.bf2.test.executor.ExecResult) ParallelTest(org.bf2.systemtest.framework.ParallelTest)

Aggregations

IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ArrayList (java.util.ArrayList)2 ConditionTimeoutException (org.awaitility.core.ConditionTimeoutException)2 Exec (org.bf2.srs.fleetmanager.it.executor.Exec)2 ExecBuilder (org.bf2.test.executor.ExecBuilder)2 ExecResult (org.bf2.test.executor.ExecResult)2 KubeClient (org.bf2.test.k8s.KubeClient)2 Container (io.fabric8.kubernetes.api.model.Container)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)1 PodResource (io.fabric8.kubernetes.client.dsl.PodResource)1 Route (io.fabric8.openshift.api.model.Route)1 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 List (java.util.List)1