use of io.kubernetes.client.Metrics in project java by kubernetes-client.
the class MetricsExample method main.
public static void main(String[] args) throws IOException, ApiException {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
Metrics metrics = new Metrics(client);
NodeMetricsList list = metrics.getNodeMetrics();
for (NodeMetrics item : list.getItems()) {
System.out.println(item.getMetadata().getName());
System.out.println("------------------------------");
for (String key : item.getUsage().keySet()) {
System.out.println("\t" + key);
System.out.println("\t" + item.getUsage().get(key));
}
System.out.println();
}
for (PodMetrics item : metrics.getPodMetrics("default").getItems()) {
System.out.println(item.getMetadata().getName());
System.out.println("------------------------------");
if (item.getContainers() == null) {
continue;
}
for (ContainerMetrics container : item.getContainers()) {
System.out.println(container.getName());
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
for (String key : container.getUsage().keySet()) {
System.out.println("\t" + key);
System.out.println("\t" + container.getUsage().get(key));
}
System.out.println();
}
}
}
use of io.kubernetes.client.Metrics in project java by kubernetes-client.
the class KubectlTop method topNodes.
private List<Pair<ApiType, MetricsType>> topNodes(CoreV1Api api, ApiClient apiClient, String metricName) throws KubectlException, ApiException, IOException {
V1NodeList nodes = api.listNode(null, null, null, null, null, null, null, null, null, null);
NodeMetricsList metrics = new Metrics(apiClient).getNodeMetrics();
List<V1Node> items = nodes.getItems();
Collections.sort(items, new Comparator<V1Node>() {
@Override
public int compare(V1Node arg0, V1Node arg1) {
Quantity m0 = findNodeMetric(arg0.getMetadata().getName(), metrics).getUsage().get(metricName);
Quantity m1 = findNodeMetric(arg1.getMetadata().getName(), metrics).getUsage().get(metricName);
double p0 = findNodePercentage(arg0, m0, metricName);
double p1 = findNodePercentage(arg1, m1, metricName);
// sort high to low
return Double.compare(p0, p1) * -1;
}
});
List<Pair<ApiType, MetricsType>> result = new ArrayList<>();
for (V1Node node : items) {
result.add(new ImmutablePair<>((ApiType) node, (MetricsType) findNodeMetric(node, metrics)));
}
return result;
}
use of io.kubernetes.client.Metrics in project java by kubernetes-client.
the class KubectlTop method topPods.
private List<Pair<ApiType, MetricsType>> topPods(CoreV1Api api, ApiClient apiClient, String metricName) throws KubectlException, ApiException, IOException {
V1PodList pods = api.listNamespacedPod(namespace, null, null, null, null, null, null, null, null, null, null);
PodMetricsList metrics = new Metrics(apiClient).getPodMetrics(namespace);
List<V1Pod> items = pods.getItems();
Collections.sort(items, new Comparator<V1Pod>() {
@Override
public int compare(V1Pod arg0, V1Pod arg1) {
double m0 = podMetricSum(findPodMetric(arg0.getMetadata().getName(), metrics), metricName);
double m1 = podMetricSum(findPodMetric(arg1.getMetadata().getName(), metrics), metricName);
// sort high to low
return Double.compare(m0, m1) * -1;
}
});
List<Pair<ApiType, MetricsType>> result = new ArrayList<>();
for (V1Pod pod : items) {
result.add(new ImmutablePair<>((ApiType) pod, (MetricsType) findPodMetric(pod, metrics)));
}
return result;
}
Aggregations