Skip to content

Commit f7273dc

Browse files
shawkinsmanusa
authored andcommitted
fix #5041: adding raw methods
1 parent 4bd80e1 commit f7273dc

13 files changed

Lines changed: 144 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#### New Features
3838
* Fix #5037: OkHttp-specific logging interceptor replacement. Introducing a generic HTTP interceptor to log HTTP and WS requests.
39+
* Fix #5041: exposed Client.raw methods for arbitrary calls
3940

4041
#### _**Note**_: Breaking changes
4142
* Fix #4875: Removed unused options from the java-generator

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Client.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,23 @@ default <T extends HasMetadata, L extends KubernetesResourceList<T>> MixedOperat
189189

190190
Config getConfiguration();
191191

192+
/**
193+
* GET the response from the given uri as a String
194+
*
195+
* @param uri must start with / if relative
196+
* @return the response, or null if a 404 code
197+
*/
198+
default String raw(String uri) {
199+
return raw(uri, "GET", null);
200+
}
201+
202+
/**
203+
* The response from the given uri as a String
204+
*
205+
* @param uri must start with / if relative
206+
* @param method an http method verb such as GET, DELETE, PUT, POST
207+
* @param payload a non-String value will be converted to json
208+
* @return the response
209+
*/
210+
String raw(String uri, String method, Object payload);
192211
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/NamespacedKubernetesClientAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,9 @@ public void visitResources(ApiVisitor visitor) {
420420
getClient().visitResources(visitor);
421421
}
422422

423+
@Override
424+
public String raw(String uri, String method, Object payload) {
425+
return getClient().raw(uri, method, payload);
426+
}
427+
423428
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/extension/ClientAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,9 @@ public Client newClient(RequestConfig requestConfig) {
153153

154154
public abstract C newInstance();
155155

156+
@Override
157+
public String raw(String uri, String method, Object payload) {
158+
return client.raw(uri, method, payload);
159+
}
160+
156161
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpRequest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ public HttpRequest.Builder method(String method, String contentType, String body
218218
this.method = method;
219219
this.contentType = contentType;
220220
this.bodyAsString = body;
221-
this.body = new StringBodyContent(body);
221+
if (body != null) {
222+
this.body = new StringBodyContent(body);
223+
}
222224
return this;
223225
}
224226

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/TestHttpResponse.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.fabric8.kubernetes.client.http;
1717

18+
import java.io.ByteArrayInputStream;
19+
import java.io.InputStream;
1820
import java.nio.charset.StandardCharsets;
1921
import java.util.Optional;
2022

@@ -82,7 +84,8 @@ public HttpResponse<T> withPreviousResponse(HttpResponse<T> previousResponse) {
8284
return this;
8385
}
8486

85-
public static TestHttpResponse<byte[]> from(int code, String body) {
86-
return new TestHttpResponse<byte[]>().withCode(code).withBody(body.getBytes(StandardCharsets.UTF_8));
87+
public static TestHttpResponse<InputStream> from(int code, String body) {
88+
return new TestHttpResponse<InputStream>().withCode(code)
89+
.withBody(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)));
8790
}
8891
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/internal/OperationSupport.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.slf4j.Logger;
4545
import org.slf4j.LoggerFactory;
4646

47-
import java.io.ByteArrayInputStream;
4847
import java.io.IOException;
4948
import java.io.InputStream;
5049
import java.io.InterruptedIOException;
@@ -488,11 +487,7 @@ protected <T> T handleGet(URL resourceUrl, Class<T> type) throws InterruptedExce
488487
* Send a raw get - where the type should be one of String, Reader, InputStream
489488
*/
490489
protected <T> T handleRawGet(URL resourceUrl, Class<T> type) throws IOException {
491-
HttpRequest.Builder requestBuilder = httpClient.newHttpRequestBuilder().url(resourceUrl);
492-
HttpRequest request = requestBuilder.build();
493-
HttpResponse<T> response = waitForResult(httpClient.sendAsync(request, type));
494-
assertResponseCode(request, response);
495-
return response.body();
490+
return handleRaw(type, resourceUrl.toString(), "GET", null);
496491
}
497492

498493
/**
@@ -569,11 +564,11 @@ protected <T> CompletableFuture<T> handleResponse(HttpClient client, HttpRequest
569564
VersionUsageUtils.log(this.resourceT, this.apiGroupVersion);
570565
HttpRequest request = requestBuilder.build();
571566

572-
return client.sendAsync(request, byte[].class).thenApply(response -> {
567+
return client.sendAsync(request, InputStream.class).thenApply(response -> {
573568
try {
574569
assertResponseCode(request, response);
575570
if (type != null && type.getType() != null) {
576-
return Serialization.unmarshal(new ByteArrayInputStream(response.body()), type);
571+
return Serialization.unmarshal(response.body(), type);
577572
} else {
578573
return null;
579574
}
@@ -760,4 +755,28 @@ public <R1> R1 restCall(Class<R1> result, String... path) {
760755
}
761756
}
762757

758+
/**
759+
* Send a raw request - where the type should be one of String, Reader, InputStream
760+
*/
761+
public <R1> R1 handleRaw(Class<R1> result, String uri, String method, Object payload) {
762+
try {
763+
if (uri.startsWith("/")) {
764+
// master ends with slash
765+
uri = config.getMasterUrl() + uri.substring(1, uri.length());
766+
}
767+
String body = null;
768+
if (payload instanceof String) {
769+
body = (String) payload;
770+
} else if (payload != null) {
771+
body = Serialization.asJson(payload);
772+
}
773+
HttpRequest request = httpClient.newHttpRequestBuilder().uri(uri).method(method, JSON, body).build();
774+
HttpResponse<R1> response = waitForResult(httpClient.sendAsync(request, result));
775+
assertResponseCode(request, response);
776+
return response.body();
777+
} catch (IOException e) {
778+
throw KubernetesClientException.launderThrowable(e);
779+
}
780+
}
781+
763782
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/impl/BaseClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
4242
import io.fabric8.kubernetes.client.utils.Utils;
4343

44+
import java.net.HttpURLConnection;
4445
import java.net.MalformedURLException;
4546
import java.net.URL;
4647
import java.util.List;
@@ -354,4 +355,21 @@ public Executor getExecutor() {
354355
return executor;
355356
}
356357

358+
@Override
359+
public String raw(String uri) {
360+
try {
361+
return raw(uri, "GET", null);
362+
} catch (KubernetesClientException e) {
363+
if (e.getCode() != HttpURLConnection.HTTP_NOT_FOUND) {
364+
throw e;
365+
}
366+
return null;
367+
}
368+
}
369+
370+
@Override
371+
public String raw(String uri, String method, Object payload) {
372+
return this.getOperationSupport().handleRaw(String.class, uri, method, payload);
373+
}
374+
357375
}

kubernetes-client/src/test/java/io/fabric8/kubernetes/client/dsl/internal/BaseOperationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private HttpClient newHttpClientWithSomeFailures(final AtomicInteger httpExecuti
250250
HttpRequest.Builder mockRequestBuilder = mock(HttpRequest.Builder.class, Mockito.RETURNS_SELF);
251251
when(mockClient.newHttpRequestBuilder()).thenReturn(mockRequestBuilder);
252252
when(mockRequestBuilder.build()).thenReturn(new StandardHttpRequest.Builder().uri("https://k8s.example.com").build());
253-
when(mockClient.sendAsync(Mockito.any(), Mockito.eq(byte[].class))).thenAnswer(
253+
when(mockClient.sendAsync(Mockito.any(), Mockito.eq(InputStream.class))).thenAnswer(
254254
invocation -> {
255255
int count = httpExecutionCounter.getAndIncrement();
256256
if (count < numFailures) {

kubernetes-client/src/test/java/io/fabric8/kubernetes/client/impl/DryRunTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.mockito.Mockito;
3636

3737
import java.io.IOException;
38+
import java.io.InputStream;
3839
import java.net.URL;
3940
import java.util.ArrayList;
4041
import java.util.List;
@@ -58,7 +59,7 @@ public void setUp() throws IOException {
5859
builders = new ArrayList<>();
5960
this.mockClient = Mockito.mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
6061
Config config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
61-
when(mockClient.sendAsync(any(), Mockito.eq(byte[].class)))
62+
when(mockClient.sendAsync(any(), Mockito.eq(InputStream.class)))
6263
.thenReturn(CompletableFuture.completedFuture(TestHttpResponse.from(200,
6364
"{\"kind\":\"Pod\", \"apiVersion\":\"v1\"}")));
6465
kubernetesClient = new KubernetesClientImpl(mockClient, config);

0 commit comments

Comments
 (0)