diff --git a/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java b/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java index 0d8c10e30b..93815c0694 100644 --- a/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java +++ b/examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java @@ -64,5 +64,22 @@ public static void main(String[] args) throws IOException, ApiException { System.out.println(); } } + + for (PodMetrics item : metrics.getPodMetrics("default", "foo=bar").getItems()) { + System.out.println(item.getMetadata().getName()); + System.out.println("------------------------------"); + if (item.getContainers() == null) { + continue; + } + for (ContainerMetrics container : item.getContainers()) { + System.out.println(container.getName()); + System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"); + for (String key : container.getUsage().keySet()) { + System.out.println("\t" + key); + System.out.println("\t" + container.getUsage().get(key)); + } + System.out.println(); + } + } } } diff --git a/util/src/main/java/io/kubernetes/client/Metrics.java b/util/src/main/java/io/kubernetes/client/Metrics.java index a655ea72b3..320eeeaf36 100644 --- a/util/src/main/java/io/kubernetes/client/Metrics.java +++ b/util/src/main/java/io/kubernetes/client/Metrics.java @@ -20,8 +20,17 @@ 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.KubernetesApiResponse; +import io.kubernetes.client.util.generic.options.ListOptions; + +import javax.annotation.Nullable; public class Metrics { + private static final String API_GROUP = "metrics.k8s.io"; + private static final String API_VERSION = "v1beta1"; + private static final String PODS = "pods"; + private static final String NODES = "nodes"; + private ApiClient apiClient; /** Simple Metrics API constructor, uses default configuration */ @@ -61,17 +70,37 @@ public NodeMetricsList getNodeMetrics() throws ApiException { new GenericKubernetesApi<>( NodeMetrics.class, NodeMetricsList.class, - "metrics.k8s.io", - "v1beta1", - "nodes", + Metrics.API_GROUP, + Metrics.API_VERSION, + Metrics.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, @Nullable 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, Metrics.PODS, apiClient); + final KubernetesApiResponse response; + if (labelSelector == null || labelSelector.trim().isEmpty()) { + response = metricsClient.list(namespace); + } else { + final ListOptions listOptions = new ListOptions(); + listOptions.setLabelSelector(labelSelector); + response = metricsClient.list(namespace, listOptions); + } + + return response.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..c7b36f4ed8 100644 --- a/util/src/test/java/io/kubernetes/client/MetricsTest.java +++ b/util/src/test/java/io/kubernetes/client/MetricsTest.java @@ -14,8 +14,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; +import static org.junit.jupiter.api.Assertions.assertThrows; import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import io.kubernetes.client.openapi.ApiClient; @@ -49,12 +48,7 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() { .withStatus(503) .withHeader("Content-Type", "text/plain") .withBody("Service Unavailable"))); - try { - metrics.getPodMetrics(namespace); - failBecauseExceptionWasNotThrown(ApiException.class); - } catch (ApiException ex) { - assertThat(ex.getCode()).isEqualTo(503); - } + assertThrows(ApiException.class, () -> metrics.getPodMetrics(namespace)); } @Test @@ -67,11 +61,6 @@ void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() { .withStatus(503) .withHeader("Content-Type", "text/plain") .withBody("Service Unavailable"))); - try { - metrics.getNodeMetrics(); - failBecauseExceptionWasNotThrown(ApiException.class); - } catch (ApiException ex) { - assertThat(ex.getCode()).isEqualTo(503); - } + assertThrows(ApiException.class, metrics::getNodeMetrics); } }