Skip to content

Commit 87bbbcd

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

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
14+
import java.util.Arrays;
15+
16+
@Beta
17+
public class GenericClientExample {
18+
19+
public static void main(String[] args) throws Exception {
20+
21+
V1Pod pod = new V1Pod()
22+
.metadata(new V1ObjectMeta()
23+
.name("foo")
24+
.namespace("default")
25+
)
26+
.spec(new V1PodSpec().containers(Arrays.asList(
27+
new V1Container().name("c").image("test")
28+
)));
29+
ApiClient apiClient = ClientBuilder.standard().build();
30+
GenericKubernetesApi<V1Pod, V1PodList> podClient = new GenericKubernetesApi<>(
31+
V1Pod.class,
32+
V1PodList.class,
33+
"",
34+
"v1",
35+
"pods",
36+
apiClient);
37+
38+
KubernetesApiResponse<V1Pod> createResponse = podClient.create(pod);
39+
if (!createResponse.isSuccess()) {
40+
throw new RuntimeException(createResponse.getStatus().toString());
41+
}
42+
System.out.println("Created!");
43+
44+
45+
KubernetesApiResponse<V1Pod> deleteResponse = podClient.delete("default", "foo");
46+
if (!deleteResponse.isSuccess()) {
47+
throw new RuntimeException(deleteResponse.getStatus().toString());
48+
}
49+
if (deleteResponse.getObject() != null) {
50+
System.out.println("Received after-deletion status of the requested object, will be deleting in background!");
51+
}
52+
System.out.println("Deleted!");
53+
}
54+
55+
}

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

Lines changed: 3 additions & 1 deletion
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)