Skip to content

Commit be85706

Browse files
committed
generic kubernetes api example
1 parent 28cfba9 commit be85706

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.kubernetes.client.examples;
2+
3+
import com.google.common.annotations.Beta;
4+
import io.kubernetes.client.extended.generic.GenericKubernetesApi;
5+
import io.kubernetes.client.extended.generic.KubernetesApiResponse;
6+
import io.kubernetes.client.openapi.ApiClient;
7+
import io.kubernetes.client.openapi.models.V1Container;
8+
import io.kubernetes.client.openapi.models.V1ObjectMeta;
9+
import io.kubernetes.client.openapi.models.V1Pod;
10+
import io.kubernetes.client.openapi.models.V1PodList;
11+
import io.kubernetes.client.openapi.models.V1PodSpec;
12+
import io.kubernetes.client.util.ClientBuilder;
13+
import java.util.Arrays;
14+
15+
@Beta
16+
public class GenericClientExample {
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
V1Pod pod =
21+
new V1Pod()
22+
.metadata(new V1ObjectMeta().name("foo").namespace("default"))
23+
.spec(
24+
new V1PodSpec()
25+
.containers(Arrays.asList(new V1Container().name("c").image("test"))));
26+
ApiClient apiClient = ClientBuilder.standard().build();
27+
GenericKubernetesApi<V1Pod, V1PodList> podClient =
28+
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
29+
30+
KubernetesApiResponse<V1Pod> createResponse = podClient.create(pod);
31+
if (!createResponse.isSuccess()) {
32+
throw new RuntimeException(createResponse.getStatus().toString());
33+
}
34+
System.out.println("Created!");
35+
36+
KubernetesApiResponse<V1Pod> deleteResponse = podClient.delete("default", "foo");
37+
if (!deleteResponse.isSuccess()) {
38+
throw new RuntimeException(deleteResponse.getStatus().toString());
39+
}
40+
if (deleteResponse.getObject() != null) {
41+
System.out.println(
42+
"Received after-deletion status of the requested object, will be deleting in background!");
43+
}
44+
System.out.println("Deleted!");
45+
}
46+
}

extended/src/main/java/io/kubernetes/client/extended/generic/GenericKubernetesApi.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import io.kubernetes.client.util.Watch;
2424
import io.kubernetes.client.util.Watchable;
2525
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
26+
27+
import java.io.IOException;
2628
import java.net.SocketTimeoutException;
2729
import okhttp3.Call;
2830
import okhttp3.HttpUrl;
@@ -669,7 +671,7 @@ private <DataType> KubernetesApiResponse<DataType> executeCall(
669671
JsonElement element = apiClient.<JsonElement>execute(call, JsonElement.class).getData();
670672
return getKubernetesApiResponse(dataClass, element, apiClient.getJSON().getGson());
671673
} catch (ApiException e) {
672-
if (e.getCause() instanceof SocketTimeoutException) {
674+
if (e.getCause() instanceof IOException) {
673675
throw new IllegalStateException(e.getCause()); // make this a checked exception?
674676
}
675677
V1Status status = apiClient.getJSON().deserialize(e.getResponseBody(), V1Status.class);

0 commit comments

Comments
 (0)