...

Source file src/k8s.io/kubectl/pkg/metricsutil/metrics_sorter.go

Documentation: k8s.io/kubectl/pkg/metricsutil

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metricsutil
    18  
    19  import (
    20  	"k8s.io/api/core/v1"
    21  	metricsapi "k8s.io/metrics/pkg/apis/metrics"
    22  )
    23  
    24  type NodeMetricsSorter struct {
    25  	metrics []metricsapi.NodeMetrics
    26  	sortBy  string
    27  }
    28  
    29  func (n *NodeMetricsSorter) Len() int {
    30  	return len(n.metrics)
    31  }
    32  
    33  func (n *NodeMetricsSorter) Swap(i, j int) {
    34  	n.metrics[i], n.metrics[j] = n.metrics[j], n.metrics[i]
    35  }
    36  
    37  func (n *NodeMetricsSorter) Less(i, j int) bool {
    38  	switch n.sortBy {
    39  	case "cpu":
    40  		return n.metrics[i].Usage.Cpu().MilliValue() > n.metrics[j].Usage.Cpu().MilliValue()
    41  	case "memory":
    42  		return n.metrics[i].Usage.Memory().Value() > n.metrics[j].Usage.Memory().Value()
    43  	default:
    44  		return n.metrics[i].Name < n.metrics[j].Name
    45  	}
    46  }
    47  
    48  func NewNodeMetricsSorter(metrics []metricsapi.NodeMetrics, sortBy string) *NodeMetricsSorter {
    49  	return &NodeMetricsSorter{
    50  		metrics: metrics,
    51  		sortBy:  sortBy,
    52  	}
    53  }
    54  
    55  type PodMetricsSorter struct {
    56  	metrics       []metricsapi.PodMetrics
    57  	sortBy        string
    58  	withNamespace bool
    59  	podMetrics    []v1.ResourceList
    60  }
    61  
    62  func (p *PodMetricsSorter) Len() int {
    63  	return len(p.metrics)
    64  }
    65  
    66  func (p *PodMetricsSorter) Swap(i, j int) {
    67  	p.metrics[i], p.metrics[j] = p.metrics[j], p.metrics[i]
    68  	p.podMetrics[i], p.podMetrics[j] = p.podMetrics[j], p.podMetrics[i]
    69  }
    70  
    71  func (p *PodMetricsSorter) Less(i, j int) bool {
    72  	switch p.sortBy {
    73  	case "cpu":
    74  		return p.podMetrics[i].Cpu().MilliValue() > p.podMetrics[j].Cpu().MilliValue()
    75  	case "memory":
    76  		return p.podMetrics[i].Memory().Value() > p.podMetrics[j].Memory().Value()
    77  	default:
    78  		if p.withNamespace && p.metrics[i].Namespace != p.metrics[j].Namespace {
    79  			return p.metrics[i].Namespace < p.metrics[j].Namespace
    80  		}
    81  		return p.metrics[i].Name < p.metrics[j].Name
    82  	}
    83  }
    84  
    85  func NewPodMetricsSorter(metrics []metricsapi.PodMetrics, withNamespace bool, sortBy string) *PodMetricsSorter {
    86  	var podMetrics = make([]v1.ResourceList, len(metrics))
    87  	if len(sortBy) > 0 {
    88  		for i, v := range metrics {
    89  			podMetrics[i] = getPodMetrics(&v)
    90  		}
    91  	}
    92  
    93  	return &PodMetricsSorter{
    94  		metrics:       metrics,
    95  		sortBy:        sortBy,
    96  		withNamespace: withNamespace,
    97  		podMetrics:    podMetrics,
    98  	}
    99  }
   100  
   101  type ContainerMetricsSorter struct {
   102  	metrics []metricsapi.ContainerMetrics
   103  	sortBy  string
   104  }
   105  
   106  func (s *ContainerMetricsSorter) Len() int {
   107  	return len(s.metrics)
   108  }
   109  
   110  func (s *ContainerMetricsSorter) Swap(i, j int) {
   111  	s.metrics[i], s.metrics[j] = s.metrics[j], s.metrics[i]
   112  }
   113  
   114  func (s *ContainerMetricsSorter) Less(i, j int) bool {
   115  	switch s.sortBy {
   116  	case "cpu":
   117  		return s.metrics[i].Usage.Cpu().MilliValue() > s.metrics[j].Usage.Cpu().MilliValue()
   118  	case "memory":
   119  		return s.metrics[i].Usage.Memory().Value() > s.metrics[j].Usage.Memory().Value()
   120  	default:
   121  		return s.metrics[i].Name < s.metrics[j].Name
   122  	}
   123  }
   124  
   125  func NewContainerMetricsSorter(metrics []metricsapi.ContainerMetrics, sortBy string) *ContainerMetricsSorter {
   126  	return &ContainerMetricsSorter{
   127  		metrics: metrics,
   128  		sortBy:  sortBy,
   129  	}
   130  }
   131  

View as plain text