Skip to content

Commit a8d9fde

Browse files
brendandburnsbrendanburns
authored andcommitted
Add support for Pod metrics.
1 parent db2bb61 commit a8d9fde

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

examples/src/main/java/io/kubernetes/client/examples/MetricsExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
package io.kubernetes.client.examples;
1414

1515
import io.kubernetes.client.Metrics;
16+
import io.kubernetes.client.custom.ContainerMetrics;
1617
import io.kubernetes.client.custom.NodeMetrics;
1718
import io.kubernetes.client.custom.NodeMetricsList;
19+
import io.kubernetes.client.custom.PodMetrics;
1820
import io.kubernetes.client.openapi.ApiClient;
1921
import io.kubernetes.client.openapi.ApiException;
2022
import io.kubernetes.client.openapi.Configuration;
@@ -45,5 +47,22 @@ public static void main(String[] args) throws IOException, ApiException {
4547
}
4648
System.out.println();
4749
}
50+
51+
for (PodMetrics item : metrics.getPodMetrics("default").getItems()) {
52+
System.out.println(item.getMetadata().getName());
53+
System.out.println("------------------------------");
54+
if (item.getContainers() == null) {
55+
continue;
56+
}
57+
for (ContainerMetrics container : item.getContainers()) {
58+
System.out.println(container.getName());
59+
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
60+
for (String key : container.getUsage().keySet()) {
61+
System.out.println("\t" + key);
62+
System.out.println("\t" + container.getUsage().get(key));
63+
}
64+
System.out.println();
65+
}
66+
}
4867
}
4968
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import java.util.Map;
16+
17+
public class ContainerMetrics {
18+
private String name;
19+
private Map<String, Quantity> usage;
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public Map<String, Quantity> getUsage() {
26+
return usage;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public void setUsage(Map<String, Quantity> usage) {
34+
this.usage = usage;
35+
}
36+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import io.kubernetes.client.common.KubernetesObject;
16+
import io.kubernetes.client.openapi.models.V1ObjectMeta;
17+
import java.util.List;
18+
19+
public class PodMetrics implements KubernetesObject {
20+
private String apiVersion;
21+
private String kind;
22+
private V1ObjectMeta metadata;
23+
private String timestamp;
24+
private String window;
25+
private List<ContainerMetrics> containers;
26+
27+
public String getApiVersion() {
28+
return apiVersion;
29+
}
30+
31+
public String getKind() {
32+
return kind;
33+
}
34+
35+
public V1ObjectMeta getMetadata() {
36+
return metadata;
37+
}
38+
39+
public String getTimestamp() {
40+
return timestamp;
41+
}
42+
43+
public String getWindow() {
44+
return window;
45+
}
46+
47+
public List<ContainerMetrics> getContainers() {
48+
return containers;
49+
}
50+
51+
public void setApiVersion(String apiVersion) {
52+
this.apiVersion = apiVersion;
53+
}
54+
55+
public void setKind(String kind) {
56+
this.kind = kind;
57+
}
58+
59+
public void setMetadata(V1ObjectMeta metadata) {
60+
this.metadata = metadata;
61+
}
62+
63+
public void setTimestamp(String timestamp) {
64+
this.timestamp = timestamp;
65+
}
66+
67+
public void setWindow(String window) {
68+
this.window = window;
69+
}
70+
71+
public void setContainers(List<ContainerMetrics> containers) {
72+
this.containers = containers;
73+
}
74+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import io.kubernetes.client.common.KubernetesListObject;
16+
import io.kubernetes.client.openapi.models.V1ListMeta;
17+
import java.util.List;
18+
19+
public class PodMetricsList implements KubernetesListObject {
20+
private String apiVersion;
21+
private String kind;
22+
private V1ListMeta metadata;
23+
private List<PodMetrics> items;
24+
25+
public String getApiVersion() {
26+
return apiVersion;
27+
}
28+
29+
public String getKind() {
30+
return kind;
31+
}
32+
33+
public V1ListMeta getMetadata() {
34+
return metadata;
35+
}
36+
37+
public List<PodMetrics> getItems() {
38+
return items;
39+
}
40+
41+
public void setApiVersion(String apiVersion) {
42+
this.apiVersion = apiVersion;
43+
}
44+
45+
public void setKind(String kind) {
46+
this.kind = kind;
47+
}
48+
49+
public void setMetadata(V1ListMeta metadata) {
50+
this.metadata = metadata;
51+
}
52+
53+
public void setItems(List<PodMetrics> items) {
54+
this.items = items;
55+
}
56+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import io.kubernetes.client.custom.NodeMetrics;
1616
import io.kubernetes.client.custom.NodeMetricsList;
17+
import io.kubernetes.client.custom.PodMetrics;
18+
import io.kubernetes.client.custom.PodMetricsList;
1719
import io.kubernetes.client.openapi.ApiClient;
1820
import io.kubernetes.client.openapi.ApiException;
1921
import io.kubernetes.client.openapi.Configuration;
@@ -65,4 +67,11 @@ public NodeMetricsList getNodeMetrics() throws ApiException {
6567
apiClient);
6668
return metricsClient.list().getObject();
6769
}
70+
71+
public PodMetricsList getPodMetrics(String namespace) throws ApiException {
72+
GenericKubernetesApi<PodMetrics, PodMetricsList> metricsClient =
73+
new GenericKubernetesApi<>(
74+
PodMetrics.class, PodMetricsList.class, "metrics.k8s.io", "v1beta1", "pods", apiClient);
75+
return metricsClient.list(namespace).getObject();
76+
}
6877
}

0 commit comments

Comments
 (0)