From f57f6e3b1c447968166dd38716a90e6644b51e8d Mon Sep 17 00:00:00 2001 From: Dustin Jenkins Date: Thu, 3 Apr 2025 09:40:00 -0700 Subject: [PATCH] feat: add label selector to metrics call --- .../java/io/kubernetes/client/Metrics.java | 26 +++++++++++++++---- .../io/kubernetes/client/MetricsTest.java | 19 ++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/Metrics.java b/util/src/main/java/io/kubernetes/client/Metrics.java index a655ea72b3..d884ae82c1 100644 --- a/util/src/main/java/io/kubernetes/client/Metrics.java +++ b/util/src/main/java/io/kubernetes/client/Metrics.java @@ -20,8 +20,11 @@ import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.Configuration; import io.kubernetes.client.util.generic.GenericKubernetesApi; +import io.kubernetes.client.util.generic.options.ListOptions; public class Metrics { + private static final String API_GROUP = "metrics.k8s.io"; + private static final String API_VERSION = "v1beta1"; private ApiClient apiClient; /** Simple Metrics API constructor, uses default configuration */ @@ -61,17 +64,30 @@ public NodeMetricsList getNodeMetrics() throws ApiException { new GenericKubernetesApi<>( NodeMetrics.class, NodeMetricsList.class, - "metrics.k8s.io", - "v1beta1", + Metrics.API_GROUP, + Metrics.API_VERSION, "nodes", apiClient); return metricsClient.list().throwsApiException().getObject(); } public PodMetricsList getPodMetrics(String namespace) throws ApiException { + return getPodMetrics(namespace, null); + } + + /** + * Obtain Pod Metrics in the given Namespace with an optional label selector. + * @param namespace The Namespace to look in. + * @param labelSelector The label selector, optional. Use comma-delimited for multiple labels. + * @return PodMetricList, never null. + * @throws ApiException If the ApiClient cannot complete the request. + */ + public PodMetricsList getPodMetrics(String namespace, String labelSelector) throws ApiException { GenericKubernetesApi metricsClient = - new GenericKubernetesApi<>( - PodMetrics.class, PodMetricsList.class, "metrics.k8s.io", "v1beta1", "pods", apiClient); - return metricsClient.list(namespace).throwsApiException().getObject(); + new GenericKubernetesApi<>( + PodMetrics.class, PodMetricsList.class, Metrics.API_GROUP, Metrics.API_VERSION, "pods", apiClient); + final ListOptions listOptions = new ListOptions(); + listOptions.setLabelSelector(labelSelector); + return metricsClient.list(namespace, listOptions).throwsApiException().getObject(); } } diff --git a/util/src/test/java/io/kubernetes/client/MetricsTest.java b/util/src/test/java/io/kubernetes/client/MetricsTest.java index 39eae73d55..cfdce46776 100644 --- a/util/src/test/java/io/kubernetes/client/MetricsTest.java +++ b/util/src/test/java/io/kubernetes/client/MetricsTest.java @@ -57,6 +57,25 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() { } } + @Test + void getPodMetricsWithLabelSelectorThrowsAPIExceptionWhenServerReturnsError() { + String namespace = "default"; + Metrics metrics = new Metrics(client); + apiServer.stubFor( + get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*")) + .willReturn( + aResponse() + .withStatus(503) + .withHeader("Content-Type", "text/plain") + .withBody("Service Unavailable"))); + try { + metrics.getPodMetrics(namespace, "foo=bar"); + failBecauseExceptionWasNotThrown(ApiException.class); + } catch (ApiException ex) { + assertThat(ex.getCode()).isEqualTo(503); + } + } + @Test void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() { Metrics metrics = new Metrics(client);