Skip to content

Commit 47c76b7

Browse files
authored
Merge pull request #3999 from at88mph/metrics-label-selector
feat: add label selector to metrics call
2 parents 099007d + dd395f0 commit 47c76b7

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

examples/examples-release-latest/src/main/java/io/kubernetes/client/examples/MetricsExample.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,22 @@ public static void main(String[] args) throws IOException, ApiException {
6464
System.out.println();
6565
}
6666
}
67+
68+
for (PodMetrics item : metrics.getPodMetrics("default", "foo=bar").getItems()) {
69+
System.out.println(item.getMetadata().getName());
70+
System.out.println("------------------------------");
71+
if (item.getContainers() == null) {
72+
continue;
73+
}
74+
for (ContainerMetrics container : item.getContainers()) {
75+
System.out.println(container.getName());
76+
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
77+
for (String key : container.getUsage().keySet()) {
78+
System.out.println("\t" + key);
79+
System.out.println("\t" + container.getUsage().get(key));
80+
}
81+
System.out.println();
82+
}
83+
}
6784
}
6885
}

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
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.KubernetesApiResponse;
24+
import io.kubernetes.client.util.generic.options.ListOptions;
25+
26+
import javax.annotation.Nullable;
2327

2428
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+
2534
private ApiClient apiClient;
2635

2736
/** Simple Metrics API constructor, uses default configuration */
@@ -61,17 +70,37 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
6170
new GenericKubernetesApi<>(
6271
NodeMetrics.class,
6372
NodeMetricsList.class,
64-
"metrics.k8s.io",
65-
"v1beta1",
66-
"nodes",
73+
Metrics.API_GROUP,
74+
Metrics.API_VERSION,
75+
Metrics.NODES,
6776
apiClient);
6877
return metricsClient.list().throwsApiException().getObject();
6978
}
7079

7180
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 {
7292
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();
76105
}
77106
}

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1616
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
17-
import static org.assertj.core.api.Assertions.assertThat;
18-
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
17+
import static org.junit.jupiter.api.Assertions.assertThrows;
1918

2019
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
2120
import io.kubernetes.client.openapi.ApiClient;
@@ -49,12 +48,7 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
4948
.withStatus(503)
5049
.withHeader("Content-Type", "text/plain")
5150
.withBody("Service Unavailable")));
52-
try {
53-
metrics.getPodMetrics(namespace);
54-
failBecauseExceptionWasNotThrown(ApiException.class);
55-
} catch (ApiException ex) {
56-
assertThat(ex.getCode()).isEqualTo(503);
57-
}
51+
assertThrows(ApiException.class, () -> metrics.getPodMetrics(namespace));
5852
}
5953

6054
@Test
@@ -67,11 +61,6 @@ void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
6761
.withStatus(503)
6862
.withHeader("Content-Type", "text/plain")
6963
.withBody("Service Unavailable")));
70-
try {
71-
metrics.getNodeMetrics();
72-
failBecauseExceptionWasNotThrown(ApiException.class);
73-
} catch (ApiException ex) {
74-
assertThat(ex.getCode()).isEqualTo(503);
75-
}
64+
assertThrows(ApiException.class, metrics::getNodeMetrics);
7665
}
7766
}

0 commit comments

Comments
 (0)