Search in sources :

Example 1 with PodMetricsList

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