Skip to content

Commit 1065775

Browse files
committed
refactor(generic-api): adding throwsApiExcetion method as default error handler
1 parent 02c8b32 commit 1065775

File tree

13 files changed

+57
-158
lines changed

13 files changed

+57
-158
lines changed

docs/generate-model-from-third-party-resources.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ customObjectsApi.createNamespacedCustomObject(
122122
// alternatively use generic kubernetes api, the generic api is aimed to address the drawbacks
123123
// from the CustomObjectsApi.
124124
GenericKubernetesApi<V1CronTab, V1CronTabList> crontabClient =
125-
new GenericKubernetesApi<>(V1CronTab.class, V1CronTabList.class, "com.example.stable", "v1", "crontabs", apiClient);
126-
KubernetesApiResponse<V1CronTab> createResponse = crontabClient.create(crontab);
125+
new GenericKubernetesApi<>(V1CronTab.class, V1CronTabList.class, "com.example.stable", "v1", "crontabs", apiClient)
126+
.create(crontab)
127+
.throwsApiException();
127128
```
128129
129130

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

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

1515
import io.kubernetes.client.custom.V1Patch;
1616
import io.kubernetes.client.openapi.ApiClient;
17-
import io.kubernetes.client.openapi.ApiException;
1817
import io.kubernetes.client.openapi.models.V1Container;
1918
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2019
import io.kubernetes.client.openapi.models.V1Pod;
@@ -40,15 +39,7 @@ public static void main(String[] args) throws Exception {
4039
GenericKubernetesApi<V1Pod, V1PodList> podClient =
4140
new GenericKubernetesApi<>(V1Pod.class, V1PodList.class, "", "v1", "pods", apiClient);
4241

43-
V1Pod latestPod =
44-
podClient
45-
.create(pod)
46-
.onFailure(
47-
errorStatus -> {
48-
System.out.println("Not Created!");
49-
throw new ApiException(errorStatus.toString());
50-
})
51-
.getObject();
42+
V1Pod latestPod = podClient.create(pod).throwsApiException().getObject();
5243
System.out.println("Created!");
5344

5445
V1Pod patchedPod =
@@ -58,23 +49,11 @@ public static void main(String[] args) throws Exception {
5849
"foo",
5950
V1Patch.PATCH_FORMAT_STRATEGIC_MERGE_PATCH,
6051
new V1Patch("{\"metadata\":{\"finalizers\":[\"example.io/foo\"]}}"))
61-
.onFailure(
62-
errorStatus -> {
63-
System.out.println("Not Patched!");
64-
throw new ApiException(errorStatus.toString());
65-
})
52+
.throwsApiException()
6653
.getObject();
6754
System.out.println("Patched!");
6855

69-
V1Pod deletedPod =
70-
podClient
71-
.delete("default", "foo")
72-
.onFailure(
73-
errorStatus -> {
74-
System.out.println("Not Deleted!");
75-
throw new ApiException(errorStatus.toString());
76-
})
77-
.getObject();
56+
V1Pod deletedPod = podClient.delete("default", "foo").throwsApiException().getObject();
7857
if (deletedPod != null) {
7958
System.out.println(
8059
"Received after-deletion status of the requested object, will be deleting in background!");

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
import com.coreos.monitoring.models.V1Prometheus;
1616
import com.coreos.monitoring.models.V1PrometheusList;
1717
import com.coreos.monitoring.models.V1PrometheusSpec;
18+
import io.kubernetes.client.openapi.ApiException;
1819
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1920
import io.kubernetes.client.util.ClientBuilder;
2021
import io.kubernetes.client.util.generic.GenericKubernetesApi;
2122
import java.io.IOException;
2223

2324
public class PromOpExample {
24-
public static void main(String[] args) throws IOException {
25+
public static void main(String[] args) throws IOException, ApiException {
2526
GenericKubernetesApi<V1Prometheus, V1PrometheusList> prometheusApi =
2627
new GenericKubernetesApi<>(
2728
V1Prometheus.class,
@@ -30,11 +31,13 @@ public static void main(String[] args) throws IOException {
3031
"v1",
3132
"prometheuses",
3233
ClientBuilder.defaultClient());
33-
prometheusApi.create(
34-
new V1Prometheus()
35-
.metadata(new V1ObjectMeta().namespace("default").name("my-prometheus"))
36-
.kind("Prometheus")
37-
.apiVersion("monitoring.coreos.com/v1")
38-
.spec(new V1PrometheusSpec()));
34+
prometheusApi
35+
.create(
36+
new V1Prometheus()
37+
.metadata(new V1ObjectMeta().namespace("default").name("my-prometheus"))
38+
.kind("Prometheus")
39+
.apiVersion("monitoring.coreos.com/v1")
40+
.spec(new V1PrometheusSpec()))
41+
.throwsApiException();
3942
}
4043
}

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlAnnotate.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,13 @@ public ApiType execute() throws KubectlException {
4646
final ApiType currentObj;
4747
if (isNamespaced(apiTypeClass)) {
4848
try {
49-
currentObj =
50-
getGenericApi()
51-
.get(namespace, name)
52-
.onFailure(
53-
errorStatus -> {
54-
throw new ApiException(errorStatus.toString());
55-
})
56-
.getObject();
49+
currentObj = getGenericApi().get(namespace, name).throwsApiException().getObject();
5750
} catch (ApiException e) {
5851
throw new KubectlException(e);
5952
}
6053
} else {
6154
try {
62-
currentObj =
63-
getGenericApi()
64-
.get(name)
65-
.onFailure(
66-
errorStatus -> {
67-
throw new ApiException(errorStatus.toString());
68-
})
69-
.getObject();
55+
currentObj = getGenericApi().get(name).throwsApiException().getObject();
7056
} catch (ApiException e) {
7157
throw new KubectlException(e);
7258
}
@@ -76,13 +62,7 @@ public ApiType execute() throws KubectlException {
7662

7763
final KubernetesApiResponse<ApiType> updateResponse;
7864
try {
79-
return getGenericApi()
80-
.update(currentObj)
81-
.onFailure(
82-
errorStatus -> {
83-
throw new ApiException(errorStatus.toString());
84-
})
85-
.getObject();
65+
return getGenericApi().update(currentObj).throwsApiException().getObject();
8666
} catch (ApiException e) {
8767
throw new KubectlException(e);
8868
}

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlApply.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ private KubernetesObject executeServerSideApply() throws KubectlException {
9292
V1Patch.PATCH_FORMAT_APPLY_YAML,
9393
new V1Patch(apiClient.getJSON().serialize(targetObj)),
9494
patchOptions)
95-
.onFailure(
96-
errorStatus -> {
97-
throw new ApiException(errorStatus.toString());
98-
})
95+
.throwsApiException()
9996
.getObject();
10097
} catch (ApiException e) {
10198
throw new KubectlException(e);
@@ -107,10 +104,7 @@ private KubernetesObject executeServerSideApply() throws KubectlException {
107104
V1Patch.PATCH_FORMAT_APPLY_YAML,
108105
new V1Patch(apiClient.getJSON().serialize(targetObj)),
109106
patchOptions)
110-
.onFailure(
111-
errorStatus -> {
112-
throw new ApiException(errorStatus.toString());
113-
})
107+
.throwsApiException()
114108
.getObject();
115109
} catch (ApiException e) {
116110
throw new KubectlException(e);

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlCordon.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ protected V1Node performCordon() throws KubectlException {
4242
try {
4343
return getGenericApi()
4444
.patch(name, V1Patch.PATCH_FORMAT_JSON_PATCH, new V1Patch(patch))
45-
.onFailure(
46-
errorStatus -> {
47-
throw new ApiException(errorStatus.toString());
48-
})
45+
.throwsApiException()
4946
.getObject();
5047
} catch (ApiException e) {
5148
throw new KubectlException(e);

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlCreate.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,14 @@ public KubernetesObject execute() throws KubectlException {
5151
: targetObj.getMetadata().getNamespace();
5252
try {
5353
return api.create(targetNamespace, targetObj, new CreateOptions())
54-
.onFailure(
55-
errorStatus -> {
56-
throw new ApiException(errorStatus.toString());
57-
})
54+
.throwsApiException()
5855
.getObject();
5956
} catch (ApiException e) {
6057
throw new KubectlException(e);
6158
}
6259
} else {
6360
try {
64-
return api.create(targetObj, new CreateOptions())
65-
.onFailure(
66-
errorStatus -> {
67-
throw new ApiException(errorStatus.toString());
68-
})
69-
.getObject();
61+
return api.create(targetObj, new CreateOptions()).throwsApiException().getObject();
7062
} catch (ApiException e) {
7163
throw new KubectlException(e);
7264
}

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlDelete.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,13 @@ public ApiType execute() throws KubectlException {
3333

3434
if (isNamespaced(apiTypeClass)) {
3535
try {
36-
return getGenericApi()
37-
.delete(namespace, name)
38-
.onFailure(
39-
errorStatus -> {
40-
throw new ApiException(errorStatus.toString());
41-
})
42-
.getObject();
36+
return getGenericApi().delete(namespace, name).throwsApiException().getObject();
4337
} catch (ApiException e) {
4438
throw new KubectlException(e);
4539
}
4640
} else {
4741
try {
48-
return getGenericApi()
49-
.delete(name)
50-
.onFailure(
51-
errorStatus -> {
52-
throw new ApiException(errorStatus.toString());
53-
})
54-
.getObject();
42+
return getGenericApi().delete(name).throwsApiException().getObject();
5543
} catch (ApiException e) {
5644
throw new KubectlException(e);
5745
}

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlGet.java

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,10 @@ public List<ApiType> execute() throws KubectlException {
6464
try {
6565
if (isNamespaced()) {
6666
return (List<ApiType>)
67-
api.list(namespace, listOptions)
68-
.onFailure(
69-
errorStatus -> {
70-
throw new ApiException(errorStatus.toString());
71-
})
72-
.getObject()
73-
.getItems();
67+
api.list(namespace, listOptions).throwsApiException().getObject().getItems();
7468

7569
} else {
76-
return (List<ApiType>)
77-
api.list(listOptions)
78-
.onFailure(
79-
errorStatus -> {
80-
throw new ApiException(errorStatus.toString());
81-
})
82-
.getObject()
83-
.getItems();
70+
return (List<ApiType>) api.list(listOptions).throwsApiException().getObject().getItems();
8471
}
8572
} catch (ApiException e) {
8673
throw new KubectlException(e);
@@ -113,19 +100,11 @@ public ApiType execute() throws KubectlException {
113100
try {
114101
if (isNamespaced()) {
115102
return api.get(KubectlGetSingle.this.namespace, KubectlGetSingle.this.name)
116-
.onFailure(
117-
errorStatus -> {
118-
throw new ApiException(errorStatus.toString());
119-
})
103+
.throwsApiException()
120104
.getObject();
121105

122106
} else {
123-
return api.get(KubectlGetSingle.this.name)
124-
.onFailure(
125-
errorStatus -> {
126-
throw new ApiException(errorStatus.toString());
127-
})
128-
.getObject();
107+
return api.get(KubectlGetSingle.this.name).throwsApiException().getObject();
129108
}
130109
} catch (ApiException e) {
131110
throw new KubectlException(e);

extended/src/main/java/io/kubernetes/client/extended/kubectl/KubectlLabel.java

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,13 @@ public ApiType execute() throws KubectlException {
4545
final ApiType currentObj;
4646
if (isNamespaced(apiTypeClass)) {
4747
try {
48-
currentObj =
49-
getGenericApi()
50-
.get(namespace, name)
51-
.onFailure(
52-
errorStatus -> {
53-
throw new ApiException(errorStatus.toString());
54-
})
55-
.getObject();
48+
currentObj = getGenericApi().get(namespace, name).throwsApiException().getObject();
5649
} catch (ApiException e) {
5750
throw new KubectlException(e);
5851
}
5952
} else {
6053
try {
61-
currentObj =
62-
getGenericApi()
63-
.get(name)
64-
.onFailure(
65-
errorStatus -> {
66-
throw new ApiException(errorStatus.toString());
67-
})
68-
.getObject();
54+
currentObj = getGenericApi().get(name).throwsApiException().getObject();
6955
} catch (ApiException e) {
7056
throw new KubectlException(e);
7157
}
@@ -74,13 +60,7 @@ public ApiType execute() throws KubectlException {
7460
Labels.addLabels(currentObj, addingLabels);
7561

7662
try {
77-
return getGenericApi()
78-
.update(currentObj)
79-
.onFailure(
80-
errorStatus -> {
81-
throw new ApiException(errorStatus.toString());
82-
})
83-
.getObject();
63+
return getGenericApi().update(currentObj).throwsApiException().getObject();
8464
} catch (ApiException e) {
8565
throw new KubectlException(e);
8666
}

0 commit comments

Comments
 (0)