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());
}
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());
}
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;
}
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;
}
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();
}
}
}
Aggregations