Search in sources :

Example 6 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class BaseOperationTest method testNoHttpRetryWithDefaultConfig.

@Test
void testNoHttpRetryWithDefaultConfig() throws MalformedURLException, IOException {
    final AtomicInteger httpExecutionCounter = new AtomicInteger(0);
    HttpClient mockClient = newHttpClientWithSomeFailures(httpExecutionCounter, 1000);
    BaseOperation<Pod, PodList, Resource<Pod>> baseOp = new BaseOperation(new OperationContext().withConfig(new ConfigBuilder().withMasterUrl("https://172.17.0.2:8443").build()).withPlural("pods").withName("test-pod").withHttpClient(mockClient));
    baseOp.setType(Pod.class);
    // When
    Exception exception = assertThrows(KubernetesClientException.class, () -> {
        Pod result = baseOp.get();
    });
    // Then
    assertTrue(exception.getCause().getMessage().contains("For example java.net.ConnectException"), "As the first failure is an IOException the message of the causedBy expected to contain the given text: 'For example java.net.ConnectException'!");
    assertEquals(1, httpExecutionCounter.get());
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.internal.OperationContext) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) Resource(io.fabric8.kubernetes.client.dsl.Resource) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) BaseOperation(io.fabric8.kubernetes.client.dsl.internal.BaseOperation) KubernetesClientTimeoutException(io.fabric8.kubernetes.client.KubernetesClientTimeoutException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 7 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class BaseOperationTest method testWaitUntilFailureCompletion.

@Test
void testWaitUntilFailureCompletion() throws MalformedURLException, IOException {
    final AtomicInteger httpExecutionCounter = new AtomicInteger(0);
    HttpClient mockClient = newHttpClientWithSomeFailures(httpExecutionCounter, 2);
    CompletableFuture<List<Pod>> future = new CompletableFuture<>();
    BaseOperation<Pod, PodList, Resource<Pod>> baseOp = new BaseOperation(new OperationContext().withConfig(new ConfigBuilder().withMasterUrl("https://172.17.0.2:8443").build()).withPlural("pods").withName("test-pod").withHttpClient(mockClient)) {

        @Override
        public CompletableFuture<List<Pod>> informOnCondition(Predicate condition) {
            return future;
        }
    };
    baseOp.setType(Pod.class);
    // When
    try {
        baseOp.waitUntilCondition(Objects::isNull, 1, TimeUnit.MILLISECONDS);
        fail("should timeout");
    } catch (KubernetesClientTimeoutException e) {
    }
    // Then
    assertTrue(future.isCancelled());
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.internal.OperationContext) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) Resource(io.fabric8.kubernetes.client.dsl.Resource) BaseOperation(io.fabric8.kubernetes.client.dsl.internal.BaseOperation) Predicate(java.util.function.Predicate) KubernetesClientTimeoutException(io.fabric8.kubernetes.client.KubernetesClientTimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) Objects(java.util.Objects) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) Test(org.junit.jupiter.api.Test)

Example 8 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class LogWatchCallback method callAndWait.

public LogWatchCallback callAndWait(HttpClient client, URL url) {
    HttpRequest request = client.newHttpRequestBuilder().url(url).build();
    HttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    CompletableFuture<HttpResponse<InputStream>> future = clone.sendAsync(request, InputStream.class).whenComplete(this);
    if (!Utils.waitUntilReady(future, config.getRequestTimeout(), TimeUnit.MILLISECONDS)) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Log watch request has not been opened within: {} millis.", config.getRequestTimeout());
        }
    }
    return this;
}
Also used : HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) HttpResponse(io.fabric8.kubernetes.client.http.HttpResponse)

Example 9 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class PodUpload method initWebSocket.

private static PodUploadWebSocketListener initWebSocket(URL url, HttpClient client) {
    final PodUploadWebSocketListener podUploadWebSocketListener = new PodUploadWebSocketListener();
    final HttpClient clone = client.newBuilder().readTimeout(0, TimeUnit.MILLISECONDS).build();
    CompletableFuture<WebSocket> startedFuture = clone.newWebSocketBuilder().subprotocol("v4.channel.k8s.io").uri(URI.create(url.toString())).buildAsync(podUploadWebSocketListener);
    startedFuture.whenComplete((w, t) -> {
        if (t != null) {
            podUploadWebSocketListener.onError(w, t);
        }
    });
    return podUploadWebSocketListener;
}
Also used : HttpClient(io.fabric8.kubernetes.client.http.HttpClient) WebSocket(io.fabric8.kubernetes.client.http.WebSocket)

Example 10 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class PodUpload method uploadDirectory.

private static boolean uploadDirectory(HttpClient client, PodOperationContext context, OperationSupport operationSupport, Path pathToUpload) throws IOException, InterruptedException {
    final String command = String.format("mkdir -p %1$s && base64 -d - | tar -C %1$s -xzf -", shellQuote(context.getDir()));
    final PodUploadWebSocketListener podUploadWebSocketListener = initWebSocket(buildCommandUrl(command, context, operationSupport), client);
    try (final PipedOutputStream pos = new PipedOutputStream();
        final PipedInputStream pis = new PipedInputStream(pos);
        final Base64.OutputStream b64Out = new Base64.OutputStream(pos, Base64.ENCODE);
        final GZIPOutputStream gzip = new GZIPOutputStream(b64Out)) {
        podUploadWebSocketListener.waitUntilReady(operationSupport.getConfig().getRequestConfig().getUploadConnectionTimeout());
        final Callable<?> readFiles = () -> {
            try (final TarArchiveOutputStream tar = new TarArchiveOutputStream(gzip)) {
                tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
                for (File file : pathToUpload.toFile().listFiles()) {
                    addFileToTar(null, file, tar);
                }
                tar.flush();
            }
            return null;
        };
        final ExecutorService es = Executors.newSingleThreadExecutor();
        Future<?> readFilesFuture = es.submit(readFiles);
        InputStreamPumper.transferTo(pis, podUploadWebSocketListener::send);
        podUploadWebSocketListener.waitUntilComplete(operationSupport.getConfig().getRequestConfig().getUploadRequestTimeout());
        try {
            readFilesFuture.get(100, TimeUnit.SECONDS);
            return true;
        } catch (ExecutionException ex) {
            if (ex.getCause() instanceof IOException) {
                throw (IOException) ex.getCause();
            }
            throw new IOException(ex.getMessage(), ex.getCause());
        } catch (TimeoutException e) {
            return false;
        } finally {
            es.shutdown();
        }
    }
}
Also used : Base64(io.fabric8.kubernetes.client.utils.internal.Base64) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) PipedOutputStream(java.io.PipedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ExecutorService(java.util.concurrent.ExecutorService) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

IOException (java.io.IOException)17 HttpClient (io.fabric8.kubernetes.client.http.HttpClient)16 URL (java.net.URL)10 Pod (io.fabric8.kubernetes.api.model.Pod)8 Test (org.junit.jupiter.api.Test)8 HttpRequest (io.fabric8.kubernetes.client.http.HttpRequest)7 Vertx (io.vertx.core.Vertx)7 File (java.io.File)7 OkHttpClient (okhttp3.OkHttpClient)7 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)6 HttpClient (io.vertx.core.http.HttpClient)6 TreeMap (java.util.TreeMap)6 Service (io.fabric8.kubernetes.api.model.Service)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 Resource (io.fabric8.kubernetes.client.dsl.Resource)5 OperationContext (io.fabric8.kubernetes.client.dsl.internal.OperationContext)5 Route (io.fabric8.openshift.api.model.Route)5 RestAssured (io.restassured.RestAssured)5 AbstractTestClass (io.vertx.it.openshift.utils.AbstractTestClass)5 Ensure (io.vertx.it.openshift.utils.Ensure)5