|
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.KubernetesApiResponse; |
| 24 | +import io.kubernetes.client.util.generic.options.ListOptions; |
| 25 | + |
| 26 | +import javax.annotation.Nullable; |
23 | 27 |
|
24 | 28 | public class Metrics { |
| 29 | + private static final String API_GROUP = "metrics.k8s.io"; |
| 30 | + private static final String API_VERSION = "v1beta1"; |
| 31 | + private static final String PODS = "pods"; |
| 32 | + private static final String NODES = "nodes"; |
| 33 | + |
25 | 34 | private ApiClient apiClient; |
26 | 35 |
|
27 | 36 | /** Simple Metrics API constructor, uses default configuration */ |
@@ -61,17 +70,37 @@ public NodeMetricsList getNodeMetrics() throws ApiException { |
61 | 70 | new GenericKubernetesApi<>( |
62 | 71 | NodeMetrics.class, |
63 | 72 | NodeMetricsList.class, |
64 | | - "metrics.k8s.io", |
65 | | - "v1beta1", |
66 | | - "nodes", |
| 73 | + Metrics.API_GROUP, |
| 74 | + Metrics.API_VERSION, |
| 75 | + Metrics.NODES, |
67 | 76 | apiClient); |
68 | 77 | return metricsClient.list().throwsApiException().getObject(); |
69 | 78 | } |
70 | 79 |
|
71 | 80 | public PodMetricsList getPodMetrics(String namespace) throws ApiException { |
| 81 | + return getPodMetrics(namespace, null); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Obtain Pod Metrics in the given Namespace with an optional label selector. |
| 86 | + * @param namespace The Namespace to look in. |
| 87 | + * @param labelSelector The label selector, optional. Use comma-delimited for multiple labels. |
| 88 | + * @return PodMetricList, never null. |
| 89 | + * @throws ApiException If the ApiClient cannot complete the request. |
| 90 | + */ |
| 91 | + public PodMetricsList getPodMetrics(String namespace, @Nullable String labelSelector) throws ApiException { |
72 | 92 | 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(); |
| 93 | + new GenericKubernetesApi<>( |
| 94 | + PodMetrics.class, PodMetricsList.class, Metrics.API_GROUP, Metrics.API_VERSION, Metrics.PODS, apiClient); |
| 95 | + final KubernetesApiResponse<PodMetricsList> response; |
| 96 | + if (labelSelector == null || labelSelector.trim().isEmpty()) { |
| 97 | + response = metricsClient.list(namespace); |
| 98 | + } else { |
| 99 | + final ListOptions listOptions = new ListOptions(); |
| 100 | + listOptions.setLabelSelector(labelSelector); |
| 101 | + response = metricsClient.list(namespace, listOptions); |
| 102 | + } |
| 103 | + |
| 104 | + return response.throwsApiException().getObject(); |
76 | 105 | } |
77 | 106 | } |
0 commit comments