Search in sources :

Example 1 with ExecWatch

use of io.fabric8.kubernetes.client.dsl.ExecWatch in project zalenium by zalando.

the class KubernetesContainerClient method executeCommand.

@Override
public void executeCommand(String containerId, String[] command, boolean waitForExecution) {
    final CountDownLatch latch = new CountDownLatch(1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    logger.info(String.format("%s %s", containerId, Arrays.toString(command)));
    ExecWatch exec = client.pods().withName(containerId).writingOutput(baos).writingError(baos).usingListener(new ExecListener() {

        @Override
        public void onOpen(Response response) {
        }

        @Override
        public void onFailure(Throwable t, Response response) {
            logger.error(String.format("%s Failed to execute command %s", containerId, Arrays.toString(command)), t);
            latch.countDown();
        }

        @Override
        public void onClose(int code, String reason) {
            latch.countDown();
        }
    }).exec(command);
    Supplier<Void> waitForResultsAndCleanup = () -> {
        try {
            latch.await();
        } catch (InterruptedException e) {
            logger.error(String.format("%s Failed to execute command %s", containerId, Arrays.toString(command)), e);
        } finally {
            exec.close();
        }
        logger.info(String.format("%s %s", containerId, baos.toString()));
        return null;
    };
    if (waitForExecution) {
        // If we're going to wait, let's use the same thread
        waitForResultsAndCleanup.get();
    } else {
        // Let the common ForkJoinPool handle waiting for the results, since we don't care when it finishes.
        CompletableFuture.supplyAsync(waitForResultsAndCleanup);
    }
}
Also used : Response(okhttp3.Response) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) ExecListener(io.fabric8.kubernetes.client.dsl.ExecListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with ExecWatch

use of io.fabric8.kubernetes.client.dsl.ExecWatch in project zalenium by zalando.

the class KubernetesContainerClient method copyFiles.

/**
 * Copy some files by executing a tar command to the stdout and return the InputStream that contains the tar
 * contents.
 * <p>
 * Unfortunately due to the fact that any error handling happens on another thread, if the tar command fails the
 * InputStream will simply be empty and it will close. It won't propagate an Exception to the reader of the
 * InputStream.
 */
@Override
public InputStream copyFiles(String containerId, String folderName) {
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    String[] command = new String[] { "tar", "-C", folderName, "-c", "." };
    CopyFilesExecListener listener = new CopyFilesExecListener(stderr, command, containerId);
    ExecWatch exec = client.pods().withName(containerId).redirectingOutput().writingError(stderr).usingListener(listener).exec(command);
    // FIXME: This is a bit dodgy, but we need the listener to be able to close the ExecWatch in failure conditions,
    // because it doesn't cleanup properly and deadlocks.
    // Needs bugs fixed inside kubernetes-client.
    listener.setExecWatch(exec);
    // When zalenium is under high load sometimes the stdout isn't connected by the time we try to read from it.
    // Let's wait until it is connected before proceeding.
    listener.waitForInputStreamToConnect();
    return exec.getOutput();
}
Also used : ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ExecWatch (io.fabric8.kubernetes.client.dsl.ExecWatch)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ExecListener (io.fabric8.kubernetes.client.dsl.ExecListener)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Response (okhttp3.Response)1