Skip to content

Commit 50312d2

Browse files
committed
Display CPU usage in milli cores instead of nano cores for PodMetrics/NodeMetrics
This change updates the metrics-server to display CPU usage in milli cores (e.g., 67m) instead of nano cores (e.g., 67131864n) in PodMetrics and NodeMetrics outputs. - Updates the scale in resource usage calculation from -9 (nano) to -3 (milli) in pkg/storage/types.go. - Adjusts all related unit tests to expect milli core values. - Improves user experience and aligns with Kubernetes conventions, where milli cores are the standard for CPU resource display. - No API or data structure changes; only the display format is affected. Fixes #1669
1 parent 55b4961 commit 50312d2

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

pkg/storage/node_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var _ = Describe("Node storage", func() {
5151
Expect(ms[0].Window.Duration).Should(BeEquivalentTo(10 * time.Second))
5252
Expect(ms[0].Usage).Should(BeEquivalentTo(
5353
corev1.ResourceList{
54-
corev1.ResourceCPU: *resource.NewScaledQuantity(CoreSecond, -9),
54+
corev1.ResourceCPU: *resource.NewScaledQuantity(CoreSecond, -3),
5555
corev1.ResourceMemory: *resource.NewQuantity(3*MiByte, resource.BinarySI),
5656
},
5757
))
@@ -174,7 +174,7 @@ var _ = Describe("Node storage", func() {
174174
Expect(ms[0].Window.Duration).Should(BeEquivalentTo(10 * time.Second))
175175
Expect(ms[0].Usage).Should(BeEquivalentTo(
176176
corev1.ResourceList{
177-
corev1.ResourceCPU: *resource.NewScaledQuantity(2.5*CoreSecond, -9),
177+
corev1.ResourceCPU: *resource.NewScaledQuantity(2.5*CoreSecond, -3),
178178
corev1.ResourceMemory: *resource.NewQuantity(2*MiByte, resource.BinarySI),
179179
},
180180
))
@@ -203,7 +203,7 @@ var _ = Describe("Node storage", func() {
203203
Expect(ms[0].Window.Duration).Should(BeEquivalentTo(10 * time.Second))
204204
Expect(ms[0].Usage).Should(BeEquivalentTo(
205205
corev1.ResourceList{
206-
corev1.ResourceCPU: *resource.NewScaledQuantity(CoreSecond, -9),
206+
corev1.ResourceCPU: *resource.NewScaledQuantity(CoreSecond, -3),
207207
corev1.ResourceMemory: *resource.NewQuantity(3*MiByte, resource.BinarySI),
208208
},
209209
))

pkg/storage/pod_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var _ = Describe("Pod storage", func() {
5555
Expect(ms[0].Containers).Should(BeEquivalentTo([]metrics.ContainerMetrics{{
5656
Name: "container1",
5757
Usage: corev1.ResourceList{
58-
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -9),
58+
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -3),
5959
corev1.ResourceMemory: *resource.NewQuantity(5*MiByte, resource.BinarySI),
6060
},
6161
}}))
@@ -168,7 +168,7 @@ var _ = Describe("Pod storage", func() {
168168
Expect(ms[0].Containers).Should(BeEquivalentTo([]metrics.ContainerMetrics{{
169169
Name: "container1",
170170
Usage: corev1.ResourceList{
171-
corev1.ResourceCPU: *resource.NewScaledQuantity(500000000, -9),
171+
corev1.ResourceCPU: *resource.NewScaledQuantity(500000000, -3),
172172
corev1.ResourceMemory: *resource.NewQuantity(5*MiByte, resource.BinarySI),
173173
},
174174
}}))
@@ -230,7 +230,7 @@ var _ = Describe("Pod storage", func() {
230230
Expect(ms[0].Containers).Should(BeEquivalentTo([]metrics.ContainerMetrics{{
231231
Name: "container1",
232232
Usage: corev1.ResourceList{
233-
corev1.ResourceCPU: *resource.NewScaledQuantity(5*CoreSecond, -9),
233+
corev1.ResourceCPU: *resource.NewScaledQuantity(5*CoreSecond, -3),
234234
corev1.ResourceMemory: *resource.NewQuantity(5*MiByte, resource.BinarySI),
235235
},
236236
}}))
@@ -262,7 +262,7 @@ var _ = Describe("Pod storage", func() {
262262
Expect(ms[0].Containers).Should(BeEquivalentTo([]metrics.ContainerMetrics{{
263263
Name: "container1",
264264
Usage: corev1.ResourceList{
265-
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -9),
265+
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -3),
266266
corev1.ResourceMemory: *resource.NewQuantity(4*MiByte, resource.BinarySI),
267267
},
268268
}}))
@@ -313,7 +313,7 @@ var _ = Describe("Pod storage", func() {
313313
Expect(ms[0].Containers).Should(BeEquivalentTo([]metrics.ContainerMetrics{{
314314
Name: "container1",
315315
Usage: corev1.ResourceList{
316-
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -9),
316+
corev1.ResourceCPU: *resource.NewScaledQuantity(1*CoreSecond, -3),
317317
corev1.ResourceMemory: *resource.NewQuantity(5*MiByte, resource.BinarySI),
318318
},
319319
}}))

pkg/storage/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func resourceUsage(last, prev MetricsPoint) (corev1.ResourceList, api.TimeInfo,
6060
window := last.Timestamp.Sub(prev.Timestamp)
6161
cpuUsage := float64(last.CumulativeCPUUsed-prev.CumulativeCPUUsed) / window.Seconds()
6262
return corev1.ResourceList{
63-
corev1.ResourceCPU: uint64Quantity(uint64(cpuUsage), resource.DecimalSI, -9),
63+
corev1.ResourceCPU: uint64Quantity(uint64(cpuUsage), resource.DecimalSI, -3),
6464
corev1.ResourceMemory: uint64Quantity(last.MemoryUsage, resource.BinarySI, 0),
6565
}, api.TimeInfo{
6666
Timestamp: last.Timestamp,

pkg/storage/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_resourceUsage(t *testing.T) {
6161
name: "get resource usage successfully",
6262
last: newMetricsPoint(start, start.Add(20*time.Millisecond), 500, 600),
6363
prev: newMetricsPoint(start, start.Add(10*time.Millisecond), 300, 400),
64-
wantResourceList: v1.ResourceList{v1.ResourceCPU: uint64Quantity(uint64(20000), resource.DecimalSI, -9),
64+
wantResourceList: v1.ResourceList{v1.ResourceCPU: uint64Quantity(uint64(20000), resource.DecimalSI, -3),
6565
v1.ResourceMemory: uint64Quantity(600, resource.BinarySI, 0)},
6666
wantTimeInfo: api.TimeInfo{Timestamp: start.Add(20 * time.Millisecond), Window: 10 * time.Millisecond},
6767
},

0 commit comments

Comments
 (0)