Search in sources :

Example 1 with NodeMetricsList

use of io.kubernetes.client.custom.NodeMetricsList 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();
        }
    }
}
Also used : Metrics(io.kubernetes.client.Metrics) PodMetrics(io.kubernetes.client.custom.PodMetrics) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) ContainerMetrics(io.kubernetes.client.custom.ContainerMetrics) NodeMetricsList(io.kubernetes.client.custom.NodeMetricsList) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) PodMetrics(io.kubernetes.client.custom.PodMetrics) ApiClient(io.kubernetes.client.openapi.ApiClient) ContainerMetrics(io.kubernetes.client.custom.ContainerMetrics)

Example 2 with NodeMetricsList

use of io.kubernetes.client.custom.NodeMetricsList 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;
}
Also used : V1Node(io.kubernetes.client.openapi.models.V1Node) ArrayList(java.util.ArrayList) Quantity(io.kubernetes.client.custom.Quantity) Metrics(io.kubernetes.client.Metrics) NodeMetrics(io.kubernetes.client.custom.NodeMetrics) PodMetrics(io.kubernetes.client.custom.PodMetrics) ContainerMetrics(io.kubernetes.client.custom.ContainerMetrics) NodeMetricsList(io.kubernetes.client.custom.NodeMetricsList) V1NodeList(io.kubernetes.client.openapi.models.V1NodeList) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

Metrics (io.kubernetes.client.Metrics)2 ContainerMetrics (io.kubernetes.client.custom.ContainerMetrics)2 NodeMetrics (io.kubernetes.client.custom.NodeMetrics)2 NodeMetricsList (io.kubernetes.client.custom.NodeMetricsList)2 PodMetrics (io.kubernetes.client.custom.PodMetrics)2 Quantity (io.kubernetes.client.custom.Quantity)1 ApiClient (io.kubernetes.client.openapi.ApiClient)1 V1Node (io.kubernetes.client.openapi.models.V1Node)1 V1NodeList (io.kubernetes.client.openapi.models.V1NodeList)1 ArrayList (java.util.ArrayList)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1