|
20 | 20 | import io.kubernetes.client.openapi.ApiException;
|
21 | 21 | import io.kubernetes.client.openapi.Configuration;
|
22 | 22 | import io.kubernetes.client.util.generic.GenericKubernetesApi;
|
| 23 | +import io.kubernetes.client.util.generic.options.ListOptions; |
23 | 24 |
|
24 | 25 | public class Metrics {
|
| 26 | + private static final String API_GROUP = "metrics.k8s.io"; |
| 27 | + private static final String API_VERSION = "v1beta1"; |
25 | 28 | private ApiClient apiClient;
|
26 | 29 |
|
27 | 30 | /** Simple Metrics API constructor, uses default configuration */
|
@@ -61,17 +64,30 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
|
61 | 64 | new GenericKubernetesApi<>(
|
62 | 65 | NodeMetrics.class,
|
63 | 66 | NodeMetricsList.class,
|
64 |
| - "metrics.k8s.io", |
65 |
| - "v1beta1", |
| 67 | + Metrics.API_GROUP, |
| 68 | + Metrics.API_VERSION, |
66 | 69 | "nodes",
|
67 | 70 | apiClient);
|
68 | 71 | return metricsClient.list().throwsApiException().getObject();
|
69 | 72 | }
|
70 | 73 |
|
71 | 74 | 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 { |
72 | 86 | 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(); |
76 | 92 | }
|
77 | 93 | }
|
0 commit comments