Skip to content

Commit b90470d

Browse files
authored
Merge pull request #1937 from munim-db/surface-api-exception-in-metrics-client
Surface api exception in metrics client
2 parents 71418f0 + 4726803 commit b90470d

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

util/src/main/java/io/kubernetes/client/Metrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
6565
"v1beta1",
6666
"nodes",
6767
apiClient);
68-
return metricsClient.list().getObject();
68+
return metricsClient.list().throwsApiException().getObject();
6969
}
7070

7171
public PodMetricsList getPodMetrics(String namespace) throws ApiException {
7272
GenericKubernetesApi<PodMetrics, PodMetricsList> metricsClient =
7373
new GenericKubernetesApi<>(
7474
PodMetrics.class, PodMetricsList.class, "metrics.k8s.io", "v1beta1", "pods", apiClient);
75-
return metricsClient.list(namespace).getObject();
75+
return metricsClient.list(namespace).throwsApiException().getObject();
7676
}
7777
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Copyright 2021 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;
14+
15+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
16+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
17+
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.fail;
19+
20+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
21+
import io.kubernetes.client.openapi.ApiClient;
22+
import io.kubernetes.client.openapi.ApiException;
23+
import io.kubernetes.client.util.ClientBuilder;
24+
import java.io.IOException;
25+
import org.junit.Before;
26+
import org.junit.Rule;
27+
import org.junit.Test;
28+
29+
public class MetricsTest {
30+
31+
private ApiClient client;
32+
33+
@Rule public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort());
34+
35+
@Before
36+
public void setup() throws IOException {
37+
client = new ClientBuilder().setBasePath("http://localhost:" + wireMockRule.port()).build();
38+
}
39+
40+
@Test
41+
public void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
42+
String namespace = "default";
43+
Metrics metrics = new Metrics(client);
44+
wireMockRule.stubFor(
45+
get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*"))
46+
.willReturn(
47+
aResponse()
48+
.withStatus(503)
49+
.withHeader("Content-Type", "text/plain")
50+
.withBody("Service Unavailable")));
51+
try {
52+
metrics.getPodMetrics(namespace);
53+
fail("Expected ApiException to be thrown");
54+
} catch (ApiException ex) {
55+
assertEquals(503, ex.getCode());
56+
}
57+
}
58+
59+
@Test
60+
public void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
61+
Metrics metrics = new Metrics(client);
62+
wireMockRule.stubFor(
63+
get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/nodes.*"))
64+
.willReturn(
65+
aResponse()
66+
.withStatus(503)
67+
.withHeader("Content-Type", "text/plain")
68+
.withBody("Service Unavailable")));
69+
try {
70+
metrics.getNodeMetrics();
71+
fail("Expected ApiException to be thrown");
72+
} catch (ApiException ex) {
73+
assertEquals(503, ex.getCode());
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)