@@ -55,7 +55,6 @@ func NewTopCmdPrinter(out io.Writer) *TopCmdPrinter {
55
55
type NodeMetricsSorter struct {
56
56
metrics []metricsapi.NodeMetrics
57
57
sortBy string
58
- usages []v1.ResourceList
59
58
}
60
59
61
60
func (n * NodeMetricsSorter ) Len () int {
@@ -64,37 +63,24 @@ func (n *NodeMetricsSorter) Len() int {
64
63
65
64
func (n * NodeMetricsSorter ) Swap (i , j int ) {
66
65
n .metrics [i ], n .metrics [j ] = n .metrics [j ], n .metrics [i ]
67
- n .usages [i ], n .usages [j ] = n .usages [j ], n .usages [i ]
68
66
}
69
67
70
68
func (n * NodeMetricsSorter ) Less (i , j int ) bool {
71
69
switch n .sortBy {
72
70
case "cpu" :
73
- qi := n.usages [i ][v1.ResourceCPU ]
74
- qj := n.usages [j ][v1.ResourceCPU ]
75
- return qi .MilliValue () > qj .MilliValue ()
71
+ return n .metrics [i ].Usage .Cpu ().MilliValue () > n .metrics [j ].Usage .Cpu ().MilliValue ()
76
72
case "memory" :
77
- qi := n.usages [i ][v1.ResourceMemory ]
78
- qj := n.usages [j ][v1.ResourceMemory ]
79
- return qi .Value () > qj .Value ()
73
+ return n .metrics [i ].Usage .Memory ().Value () > n .metrics [j ].Usage .Memory ().Value ()
80
74
default :
81
75
return n .metrics [i ].Name < n .metrics [j ].Name
82
76
}
83
77
}
84
78
85
- func NewNodeMetricsSorter (metrics []metricsapi.NodeMetrics , sortBy string ) (* NodeMetricsSorter , error ) {
86
- var usages = make ([]v1.ResourceList , len (metrics ))
87
- if len (sortBy ) > 0 {
88
- for i , v := range metrics {
89
- v .Usage .DeepCopyInto (& usages [i ])
90
- }
91
- }
92
-
79
+ func NewNodeMetricsSorter (metrics []metricsapi.NodeMetrics , sortBy string ) * NodeMetricsSorter {
93
80
return & NodeMetricsSorter {
94
81
metrics : metrics ,
95
82
sortBy : sortBy ,
96
- usages : usages ,
97
- }, nil
83
+ }
98
84
}
99
85
100
86
type PodMetricsSorter struct {
@@ -116,13 +102,9 @@ func (p *PodMetricsSorter) Swap(i, j int) {
116
102
func (p * PodMetricsSorter ) Less (i , j int ) bool {
117
103
switch p .sortBy {
118
104
case "cpu" :
119
- qi := p.podMetrics [i ][v1.ResourceCPU ]
120
- qj := p.podMetrics [j ][v1.ResourceCPU ]
121
- return qi .MilliValue () > qj .MilliValue ()
105
+ return p .podMetrics [i ].Cpu ().MilliValue () > p .podMetrics [j ].Cpu ().MilliValue ()
122
106
case "memory" :
123
- qi := p.podMetrics [i ][v1.ResourceMemory ]
124
- qj := p.podMetrics [j ][v1.ResourceMemory ]
125
- return qi .Value () > qj .Value ()
107
+ return p .podMetrics [i ].Memory ().Value () > p .podMetrics [j ].Memory ().Value ()
126
108
default :
127
109
if p .withNamespace && p .metrics [i ].Namespace != p .metrics [j ].Namespace {
128
110
return p .metrics [i ].Namespace < p .metrics [j ].Namespace
@@ -131,11 +113,11 @@ func (p *PodMetricsSorter) Less(i, j int) bool {
131
113
}
132
114
}
133
115
134
- func NewPodMetricsSorter (metrics []metricsapi.PodMetrics , printContainers bool , withNamespace bool , sortBy string ) ( * PodMetricsSorter , error ) {
116
+ func NewPodMetricsSorter (metrics []metricsapi.PodMetrics , withNamespace bool , sortBy string ) * PodMetricsSorter {
135
117
var podMetrics = make ([]v1.ResourceList , len (metrics ))
136
118
if len (sortBy ) > 0 {
137
119
for i , v := range metrics {
138
- podMetrics [i ], _ , _ = getPodMetrics (& v , printContainers )
120
+ podMetrics [i ] = getPodMetrics (& v )
139
121
}
140
122
}
141
123
@@ -144,7 +126,38 @@ func NewPodMetricsSorter(metrics []metricsapi.PodMetrics, printContainers bool,
144
126
sortBy : sortBy ,
145
127
withNamespace : withNamespace ,
146
128
podMetrics : podMetrics ,
147
- }, nil
129
+ }
130
+ }
131
+
132
+ type ContainerMetricsSorter struct {
133
+ metrics []metricsapi.ContainerMetrics
134
+ sortBy string
135
+ }
136
+
137
+ func (s * ContainerMetricsSorter ) Len () int {
138
+ return len (s .metrics )
139
+ }
140
+
141
+ func (s * ContainerMetricsSorter ) Swap (i , j int ) {
142
+ s .metrics [i ], s .metrics [j ] = s .metrics [j ], s .metrics [i ]
143
+ }
144
+
145
+ func (s * ContainerMetricsSorter ) Less (i , j int ) bool {
146
+ switch s .sortBy {
147
+ case "cpu" :
148
+ return s .metrics [i ].Usage .Cpu ().MilliValue () > s .metrics [j ].Usage .Cpu ().MilliValue ()
149
+ case "memory" :
150
+ return s .metrics [i ].Usage .Memory ().Value () > s .metrics [j ].Usage .Memory ().Value ()
151
+ default :
152
+ return s .metrics [i ].Name < s .metrics [j ].Name
153
+ }
154
+ }
155
+
156
+ func NewContainerMetricsSorter (metrics []metricsapi.ContainerMetrics , sortBy string ) * ContainerMetricsSorter {
157
+ return & ContainerMetricsSorter {
158
+ metrics : metrics ,
159
+ sortBy : sortBy ,
160
+ }
148
161
}
149
162
150
163
func (printer * TopCmdPrinter ) PrintNodeMetrics (metrics []metricsapi.NodeMetrics , availableResources map [string ]v1.ResourceList , noHeaders bool , sortBy string ) error {
@@ -154,11 +167,7 @@ func (printer *TopCmdPrinter) PrintNodeMetrics(metrics []metricsapi.NodeMetrics,
154
167
w := printers .GetNewTabWriter (printer .out )
155
168
defer w .Flush ()
156
169
157
- n , err := NewNodeMetricsSorter (metrics , sortBy )
158
- if err != nil {
159
- return err
160
- }
161
- sort .Sort (n )
170
+ sort .Sort (NewNodeMetricsSorter (metrics , sortBy ))
162
171
163
172
if ! noHeaders {
164
173
printColumnNames (w , NodeColumns )
@@ -197,16 +206,14 @@ func (printer *TopCmdPrinter) PrintPodMetrics(metrics []metricsapi.PodMetrics, p
197
206
printColumnNames (w , PodColumns )
198
207
}
199
208
200
- p , err := NewPodMetricsSorter (metrics , printContainers , withNamespace , sortBy )
201
- if err != nil {
202
- return err
203
- }
204
- sort .Sort (p )
209
+ sort .Sort (NewPodMetricsSorter (metrics , withNamespace , sortBy ))
205
210
206
211
for _ , m := range metrics {
207
- err := printSinglePodMetrics (w , & m , printContainers , withNamespace )
208
- if err != nil {
209
- return err
212
+ if printContainers {
213
+ sort .Sort (NewContainerMetricsSorter (m .Containers , sortBy ))
214
+ printSinglePodContainerMetrics (w , & m , withNamespace )
215
+ } else {
216
+ printSinglePodMetrics (w , & m , withNamespace )
210
217
}
211
218
}
212
219
return nil
@@ -219,56 +226,46 @@ func printColumnNames(out io.Writer, names []string) {
219
226
fmt .Fprint (out , "\n " )
220
227
}
221
228
222
- func printSinglePodMetrics (out io.Writer , m * metricsapi.PodMetrics , printContainersOnly bool , withNamespace bool ) error {
223
- podMetrics , containers , err := getPodMetrics (m , printContainersOnly )
224
- if err != nil {
225
- return err
229
+ func printSinglePodMetrics (out io.Writer , m * metricsapi.PodMetrics , withNamespace bool ) {
230
+ podMetrics := getPodMetrics (m )
231
+ if withNamespace {
232
+ printValue ( out , m . Namespace )
226
233
}
227
- if printContainersOnly {
228
- for contName := range containers {
229
- if withNamespace {
230
- printValue (out , m .Namespace )
231
- }
232
- printValue (out , m .Name )
233
- printMetricsLine (out , & ResourceMetricsInfo {
234
- Name : contName ,
235
- Metrics : containers [contName ],
236
- Available : v1.ResourceList {},
237
- })
238
- }
239
- } else {
234
+ printMetricsLine (out , & ResourceMetricsInfo {
235
+ Name : m .Name ,
236
+ Metrics : podMetrics ,
237
+ Available : v1.ResourceList {},
238
+ })
239
+ }
240
+
241
+ func printSinglePodContainerMetrics (out io.Writer , m * metricsapi.PodMetrics , withNamespace bool ) {
242
+ for _ , c := range m .Containers {
240
243
if withNamespace {
241
244
printValue (out , m .Namespace )
242
245
}
246
+ printValue (out , m .Name )
243
247
printMetricsLine (out , & ResourceMetricsInfo {
244
- Name : m .Name ,
245
- Metrics : podMetrics ,
248
+ Name : c .Name ,
249
+ Metrics : c . Usage ,
246
250
Available : v1.ResourceList {},
247
251
})
248
252
}
249
- return nil
250
253
}
251
254
252
- func getPodMetrics (m * metricsapi.PodMetrics , printContainersOnly bool ) (v1.ResourceList , map [string ]v1.ResourceList , error ) {
253
- containers := make (map [string ]v1.ResourceList )
255
+ func getPodMetrics (m * metricsapi.PodMetrics ) v1.ResourceList {
254
256
podMetrics := make (v1.ResourceList )
255
257
for _ , res := range MeasuredResources {
256
258
podMetrics [res ], _ = resource .ParseQuantity ("0" )
257
259
}
258
260
259
- var usage v1.ResourceList
260
261
for _ , c := range m .Containers {
261
- c .Usage .DeepCopyInto (& usage )
262
- containers [c .Name ] = usage
263
- if ! printContainersOnly {
264
- for _ , res := range MeasuredResources {
265
- quantity := podMetrics [res ]
266
- quantity .Add (usage [res ])
267
- podMetrics [res ] = quantity
268
- }
262
+ for _ , res := range MeasuredResources {
263
+ quantity := podMetrics [res ]
264
+ quantity .Add (c .Usage [res ])
265
+ podMetrics [res ] = quantity
269
266
}
270
267
}
271
- return podMetrics , containers , nil
268
+ return podMetrics
272
269
}
273
270
274
271
func printMetricsLine (out io.Writer , metrics * ResourceMetricsInfo ) {
0 commit comments