Search in sources :

Example 1 with Metrics

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();
        }
    }
}
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 Metrics

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;
}
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)

Example 3 with Metrics

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

Aggregations

Metrics (io.kubernetes.client.Metrics)3 ContainerMetrics (io.kubernetes.client.custom.ContainerMetrics)3 NodeMetrics (io.kubernetes.client.custom.NodeMetrics)3 PodMetrics (io.kubernetes.client.custom.PodMetrics)3 NodeMetricsList (io.kubernetes.client.custom.NodeMetricsList)2 ArrayList (java.util.ArrayList)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 PodMetricsList (io.kubernetes.client.custom.PodMetricsList)1 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 V1Pod (io.kubernetes.client.openapi.models.V1Pod)1 V1PodList (io.kubernetes.client.openapi.models.V1PodList)1