Skip to content

Commit e614ea8

Browse files
committed
refactor: review rework
1 parent 5a72d28 commit e614ea8

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
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;
2324
import io.kubernetes.client.util.generic.options.ListOptions;
2425

2526
import javax.annotation.Nullable;
@@ -91,8 +92,15 @@ public PodMetricsList getPodMetrics(String namespace, @Nullable String labelSele
9192
GenericKubernetesApi<PodMetrics, PodMetricsList> metricsClient =
9293
new GenericKubernetesApi<>(
9394
PodMetrics.class, PodMetricsList.class, Metrics.API_GROUP, Metrics.API_VERSION, Metrics.PODS, apiClient);
94-
final ListOptions listOptions = new ListOptions();
95-
listOptions.setLabelSelector(labelSelector);
96-
return metricsClient.list(namespace, listOptions).throwsApiException().getObject();
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();
97105
}
98106
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
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;
1917
import static org.junit.jupiter.api.Assertions.assertThrows;
2018

2119
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
@@ -53,20 +51,6 @@ void getPodMetricsThrowsAPIExceptionWhenServerReturnsError() {
5351
assertThrows(ApiException.class, () -> metrics.getPodMetrics(namespace));
5452
}
5553

56-
@Test
57-
void getPodMetricsWithLabelSelectorThrowsAPIExceptionWhenServerReturnsError() {
58-
String namespace = "default";
59-
Metrics metrics = new Metrics(client);
60-
apiServer.stubFor(
61-
get(urlPathMatching("^/apis/metrics.k8s.io/v1beta1/namespaces/" + namespace + "/pods.*"))
62-
.willReturn(
63-
aResponse()
64-
.withStatus(503)
65-
.withHeader("Content-Type", "text/plain")
66-
.withBody("Service Unavailable")));
67-
assertThrows(ApiException.class, () -> metrics.getPodMetrics(namespace, "foo=bar"));
68-
}
69-
7054
@Test
7155
void getNodeMetricsThrowsAPIExceptionWhenServerReturnsError() {
7256
Metrics metrics = new Metrics(client);

0 commit comments

Comments
 (0)