Skip to content

Commit f57f6e3

Browse files
committed
feat: add label selector to metrics call
1 parent 6fd0e71 commit f57f6e3

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import io.kubernetes.client.openapi.ApiException;
2121
import io.kubernetes.client.openapi.Configuration;
2222
import io.kubernetes.client.util.generic.GenericKubernetesApi;
23+
import io.kubernetes.client.util.generic.options.ListOptions;
2324

2425
public class Metrics {
26+
private static final String API_GROUP = "metrics.k8s.io";
27+
private static final String API_VERSION = "v1beta1";
2528
private ApiClient apiClient;
2629

2730
/** Simple Metrics API constructor, uses default configuration */
@@ -61,17 +64,30 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
6164
new GenericKubernetesApi<>(
6265
NodeMetrics.class,
6366
NodeMetricsList.class,
64-
"metrics.k8s.io",
65-
"v1beta1",
67+
Metrics.API_GROUP,
68+
Metrics.API_VERSION,
6669
"nodes",
6770
apiClient);
6871
return metricsClient.list().throwsApiException().getObject();
6972
}
7073

7174
public PodMetricsList getPodMetrics(String namespace) throws ApiException {
75+
return getPodMetrics(namespace, null);
76+
}
77+
78+
/**
79+
* Obtain Pod Metrics in the given Namespace with an optional label selector.
80+
* @param namespace The Namespace to look in.
81+
* @param labelSelector The label selector, optional. Use comma-delimited for multiple labels.
82+
* @return PodMetricList, never null.
83+
* @throws ApiException If the ApiClient cannot complete the request.
84+
*/
85+
public PodMetricsList getPodMetrics(String namespace, String labelSelector) throws ApiException {
7286
GenericKubernetesApi<PodMetrics, PodMetricsList> metricsClient =
73-
new GenericKubernetesApi<>(
74-
PodMetrics.class, PodMetricsList.class, "metrics.k8s.io", "v1beta1", "pods", apiClient);
75-
return metricsClient.list(namespace).throwsApiException().getObject();
87+
new GenericKubernetesApi<>(
88+
PodMetrics.class, PodMetricsList.class, Metrics.API_GROUP, Metrics.API_VERSION, "pods", apiClient);
89+
final ListOptions listOptions = new ListOptions();
90+
listOptions.setLabelSelector(labelSelector);
91+
return metricsClient.list(namespace, listOptions).throwsApiException().getObject();
7692
}
7793
}

util/src/test/java/io/kubernetes/client/MetricsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
5757
}
5858
}
5959

60+
@Test
61+
void getPodMetricsWithLabelSelectorThrowsAPIExceptionWhenServerReturnsError() {
62+
String namespace = "default";
63+
Metrics metrics = new Metrics(client);
64+
apiServer.stubFor(
65+
get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*"))
66+
.willReturn(
67+
aResponse()
68+
.withStatus(503)
69+
.withHeader("Content-Type", "text/plain")
70+
.withBody("Service Unavailable")));
71+
try {
72+
metrics.getPodMetrics(namespace, "foo=bar");
73+
failBecauseExceptionWasNotThrown(ApiException.class);
74+
} catch (ApiException ex) {
75+
assertThat(ex.getCode()).isEqualTo(503);
76+
}
77+
}
78+
6079
@Test
6180
void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
6281
Metrics metrics = new Metrics(client);

0 commit comments

Comments
 (0)