Skip to content

Commit 3afd5f4

Browse files
committed
fluent short cut for generic api
1 parent 9dae95d commit 3afd5f4

3 files changed

Lines changed: 100 additions & 19 deletions

File tree

examples/src/main/java/io/kubernetes/client/examples/GenericClientExample.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.kubernetes.client.openapi.models.V1PodSpec;
2222
import io.kubernetes.client.util.ClientBuilder;
2323
import io.kubernetes.client.util.generic.GenericKubernetesApi;
24-
import io.kubernetes.client.util.generic.KubernetesApiResponse;
2524
import java.util.Arrays;
2625

2726
public class GenericClientExample {
@@ -35,32 +34,47 @@ public static void main(String[] args) throws Exception {
3534
.spec(
3635
new V1PodSpec()
3736
.containers(Arrays.asList(new V1Container().name("c").image("test"))));
37+
3838
ApiClient apiClient = ClientBuilder.standard().build();
3939
GenericKubernetesApi<V1Pod, V1PodList> podClient =
4040
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
4141

42-
KubernetesApiResponse<V1Pod> createResponse = podClient.create(pod);
43-
if (!createResponse.isSuccess()) {
44-
throw new RuntimeException(createResponse.getStatus().toString());
45-
}
42+
V1Pod latestPod =
43+
podClient
44+
.create(pod)
45+
.onFailure(
46+
errorStatus -> {
47+
System.out.println("Not Created!");
48+
throw new RuntimeException(errorStatus.toString());
49+
})
50+
.getObject();
4651
System.out.println("Created!");
4752

48-
KubernetesApiResponse<V1Pod> patchResponse =
49-
podClient.patch(
50-
"default",
51-
"foo",
52-
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
53-
new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}"));
54-
if (!patchResponse.isSuccess()) {
55-
throw new RuntimeException(patchResponse.getStatus().toString());
56-
}
53+
V1Pod patchedPod =
54+
podClient
55+
.patch(
56+
"default",
57+
"foo",
58+
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
59+
new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}"))
60+
.onFailure(
61+
errorStatus -> {
62+
System.out.println("Not Patched!");
63+
throw new RuntimeException(errorStatus.toString());
64+
})
65+
.getObject();
5766
System.out.println("Patched!");
5867

59-
KubernetesApiResponse<V1Pod> deleteResponse = podClient.delete("default", "foo");
60-
if (!deleteResponse.isSuccess()) {
61-
throw new RuntimeException(deleteResponse.getStatus().toString());
62-
}
63-
if (deleteResponse.getObject() != null) {
68+
V1Pod deletedPod =
69+
podClient
70+
.delete("default", "foo")
71+
.onFailure(
72+
errorStatus -> {
73+
System.out.println("Not Deleted!");
74+
throw new RuntimeException(errorStatus.toString());
75+
})
76+
.getObject();
77+
if (deletedPod != null) {
6478
System.out.println(
6579
"Received after-deletion status of the requested object, will be deleting in background!");
6680
}

util/src/main/java/io/kubernetes/client/util/generic/KubernetesApiResponse.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import io.kubernetes.client.common.KubernetesType;
1616
import io.kubernetes.client.openapi.models.V1Status;
17+
import java.util.function.Consumer;
1718

1819
public class KubernetesApiResponse<DataType extends KubernetesType> {
1920

@@ -48,4 +49,11 @@ public int getHttpStatusCode() {
4849
public boolean isSuccess() {
4950
return this.httpStatusCode < 400;
5051
}
52+
53+
public KubernetesApiResponse<DataType> onFailure(Consumer<V1Status> errorStatusHandler) {
54+
if (!isSuccess()) {
55+
errorStatusHandler.accept(this.status);
56+
}
57+
return this;
58+
}
5159
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.util.generic;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
17+
import static org.junit.Assert.*;
18+
19+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
20+
import com.google.gson.Gson;
21+
import io.kubernetes.client.openapi.ApiClient;
22+
import io.kubernetes.client.openapi.models.*;
23+
import io.kubernetes.client.util.ClientBuilder;
24+
import java.io.IOException;
25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
import org.junit.Before;
27+
import org.junit.Rule;
28+
import org.junit.Test;
29+
30+
public class KubernetesApiResponseTest {
31+
@Rule public WireMockRule wireMockRule = new WireMockRule(8485);
32+
33+
private GenericKubernetesApi<V1Pod, V1PodList> nodeClient;
34+
35+
@Before
36+
public void setup() throws IOException {
37+
ApiClient apiClient = new ClientBuilder().setBasePath("http://localhost:" + 8485).build();
38+
nodeClient =
39+
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
40+
}
41+
42+
@Test
43+
public void testErrorStatusHandler() {
44+
V1Status forbiddenStatus = new V1Status().code(403).message("Forbidden");
45+
wireMockRule.stubFor(
46+
delete(urlEqualTo("/api/v1/namespaces/default/pods/foo"))
47+
.willReturn(aResponse().withStatus(403).withBody(new Gson().toJson(forbiddenStatus))));
48+
AtomicBoolean catched = new AtomicBoolean(false);
49+
assertNull(
50+
nodeClient
51+
.delete("default", "foo")
52+
.onFailure(
53+
errStatus -> {
54+
catched.set(true);
55+
})
56+
.getObject());
57+
assertTrue(catched.get());
58+
}
59+
}

0 commit comments

Comments
 (0)