Search in sources :

Example 1 with ExecListener

use of io.fabric8.kubernetes.client.dsl.ExecListener 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)

Aggregations

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