Skip to content

Commit db2bb61

Browse files
committed
Add node metrics.
1 parent 3419b73 commit db2bb61

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.examples;
14+
15+
import io.kubernetes.client.Metrics;
16+
import io.kubernetes.client.custom.NodeMetrics;
17+
import io.kubernetes.client.custom.NodeMetricsList;
18+
import io.kubernetes.client.openapi.ApiClient;
19+
import io.kubernetes.client.openapi.ApiException;
20+
import io.kubernetes.client.openapi.Configuration;
21+
import io.kubernetes.client.util.Config;
22+
import java.io.IOException;
23+
24+
/**
25+
* A simple example of how to use the Java API
26+
*
27+
* <p>Easiest way to run this: mvn exec:java
28+
* -Dexec.mainClass="io.kubernetes.client.examples.MetricsExample"
29+
*
30+
* <p>From inside $REPO_DIR/examples
31+
*/
32+
public class MetricsExample {
33+
public static void main(String[] args) throws IOException, ApiException {
34+
ApiClient client = Config.defaultClient();
35+
Configuration.setDefaultApiClient(client);
36+
37+
Metrics metrics = new Metrics(client);
38+
NodeMetricsList list = metrics.getNodeMetrics();
39+
for (NodeMetrics item : list.getItems()) {
40+
System.out.println(item.getMetadata().getName());
41+
System.out.println("------------------------------");
42+
for (String key : item.getUsage().keySet()) {
43+
System.out.println("\t" + key);
44+
System.out.println("\t" + item.getUsage().get(key));
45+
}
46+
System.out.println();
47+
}
48+
}
49+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.HashMap;
18+
import java.util.Map;
19+
20+
public class NodeMetrics implements KubernetesObject {
21+
private V1ObjectMeta metadata;
22+
private String kind;
23+
private String apiVersion;
24+
private String timestamp;
25+
private String window;
26+
private Map<String, Quantity> usage;
27+
28+
public NodeMetrics() {
29+
this.metadata = new V1ObjectMeta();
30+
this.usage = new HashMap<>();
31+
}
32+
33+
public V1ObjectMeta getMetadata() {
34+
return metadata;
35+
}
36+
37+
public String getTimestamp() {
38+
return timestamp;
39+
}
40+
41+
public String getWindow() {
42+
return window;
43+
}
44+
45+
public String getKind() {
46+
return kind;
47+
}
48+
49+
public String getApiVersion() {
50+
return apiVersion;
51+
}
52+
53+
public Map<String, Quantity> getUsage() {
54+
return usage;
55+
}
56+
57+
public void setKind(String kind) {
58+
this.kind = kind;
59+
}
60+
61+
public void setApiVersion(String apiVersion) {
62+
this.apiVersion = apiVersion;
63+
}
64+
65+
public void setMetadata(V1ObjectMeta metadata) {
66+
this.metadata = metadata;
67+
}
68+
69+
public void setTimestamp(String timestamp) {
70+
this.timestamp = timestamp;
71+
}
72+
73+
public void setWindow(String window) {
74+
this.window = window;
75+
}
76+
77+
public void setUsage(Map<String, Quantity> usage) {
78+
this.usage = usage;
79+
}
80+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.ArrayList;
18+
import java.util.List;
19+
20+
public class NodeMetricsList implements KubernetesListObject {
21+
private String apiVersion;
22+
private String kind;
23+
private V1ListMeta metadata;
24+
private List<NodeMetrics> items;
25+
26+
public NodeMetricsList() {
27+
this.metadata = new V1ListMeta();
28+
this.items = new ArrayList<>();
29+
}
30+
31+
public String getApiVersion() {
32+
return apiVersion;
33+
}
34+
35+
public String getKind() {
36+
return kind;
37+
}
38+
39+
public V1ListMeta getMetadata() {
40+
return metadata;
41+
}
42+
43+
public List<NodeMetrics> getItems() {
44+
return items;
45+
}
46+
47+
public void setApiVersion(String apiVersion) {
48+
this.apiVersion = apiVersion;
49+
}
50+
51+
public void setKind(String kind) {
52+
this.kind = kind;
53+
}
54+
55+
public void setMetadata(V1ListMeta metadata) {
56+
this.metadata = metadata;
57+
}
58+
59+
public void setItems(List<NodeMetrics> items) {
60+
this.items = items;
61+
}
62+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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;
14+
15+
import io.kubernetes.client.custom.NodeMetrics;
16+
import io.kubernetes.client.custom.NodeMetricsList;
17+
import io.kubernetes.client.openapi.ApiClient;
18+
import io.kubernetes.client.openapi.ApiException;
19+
import io.kubernetes.client.openapi.Configuration;
20+
import io.kubernetes.client.util.generic.GenericKubernetesApi;
21+
22+
public class Metrics {
23+
private ApiClient apiClient;
24+
25+
/** Simple Metrics API constructor, uses default configuration */
26+
public Metrics() {
27+
this(Configuration.getDefaultApiClient());
28+
}
29+
30+
/**
31+
* Metrics API Constructor
32+
*
33+
* @param apiClient The api client to use.
34+
*/
35+
public Metrics(ApiClient apiClient) {
36+
this.apiClient = apiClient;
37+
}
38+
39+
/**
40+
* Get the API client for these Attach operations.
41+
*
42+
* @return The API client that will be used.
43+
*/
44+
public ApiClient getApiClient() {
45+
return apiClient;
46+
}
47+
48+
/**
49+
* Set the API client for subsequent Attach operations.
50+
*
51+
* @param apiClient The new API client to use.
52+
*/
53+
public void setApiClient(ApiClient apiClient) {
54+
this.apiClient = apiClient;
55+
}
56+
57+
public NodeMetricsList getNodeMetrics() throws ApiException {
58+
GenericKubernetesApi<NodeMetrics, NodeMetricsList> metricsClient =
59+
new GenericKubernetesApi<>(
60+
NodeMetrics.class,
61+
NodeMetricsList.class,
62+
"metrics.k8s.io",
63+
"v1beta1",
64+
"nodes",
65+
apiClient);
66+
return metricsClient.list().getObject();
67+
}
68+
}

0 commit comments

Comments
 (0)