Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-0.5] cherrypick #1300 kernel bug lead to incorrect cpu usage #1403

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type MetricsPoint struct {
}

func resourceUsage(last, prev MetricsPoint) (corev1.ResourceList, api.TimeInfo, error) {
if last.StartTime.Before(prev.StartTime) {
return corev1.ResourceList{}, api.TimeInfo{}, fmt.Errorf("unexpected decrease in startTime of node/container")
}
if last.CumulativeCpuUsed < prev.CumulativeCpuUsed {
return corev1.ResourceList{}, api.TimeInfo{}, fmt.Errorf("unexpected decrease in cumulative CPU usage value")
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/storage/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ package storage

import (
"math"
"reflect"
"testing"
"time"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/metrics-server/pkg/api"
)

func TestUint64Quantity(t *testing.T) {
Expand All @@ -41,3 +45,55 @@ func TestUint64Quantity(t *testing.T) {
})
}
}

func Test_resourceUsage(t *testing.T) {
start := time.Now()
tcs := []struct {
name string
last MetricsPoint
prev MetricsPoint
wantResourceList v1.ResourceList
wantTimeInfo api.TimeInfo
wantErr bool
}{
{
name: "get resource usage successfully",
last: newMetricsPoint(start, start.Add(20*time.Millisecond), 500, 600),
prev: newMetricsPoint(start, start.Add(10*time.Millisecond), 300, 400),
wantResourceList: v1.ResourceList{v1.ResourceCPU: uint64Quantity(uint64(20000), resource.DecimalSI, -9),
v1.ResourceMemory: uint64Quantity(600, resource.BinarySI, 0)},
wantTimeInfo: api.TimeInfo{Timestamp: start.Add(20 * time.Millisecond), Window: 10 * time.Millisecond},
},
{
name: "get resource usage failed because of unexpected decrease in startTime",
last: newMetricsPoint(start, start.Add(20*time.Millisecond), 500, 600),
prev: newMetricsPoint(start.Add(20*time.Millisecond), start.Add(10*time.Millisecond), 300, 400),
wantResourceList: v1.ResourceList{},
wantTimeInfo: api.TimeInfo{},
wantErr: true,
},
{
name: "get resource usage failed because of unexpected decrease in cumulative CPU usage value",
last: newMetricsPoint(start, start.Add(20*time.Millisecond), 100, 600),
prev: newMetricsPoint(start, start.Add(10*time.Millisecond), 300, 400),
wantResourceList: v1.ResourceList{},
wantTimeInfo: api.TimeInfo{},
wantErr: true,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
resourceList, timeInfo, err := resourceUsage(tc.last, tc.prev)
if (err != nil) != tc.wantErr {
t.Errorf("resourceUsage() error = %v, wantErr %v", err, tc.wantErr)
return
}
if !reflect.DeepEqual(resourceList, tc.wantResourceList) {
t.Errorf("resourceUsage() resourceList = %v, want %v", resourceList, tc.wantResourceList)
}
if !reflect.DeepEqual(timeInfo, tc.wantTimeInfo) {
t.Errorf("resourceUsage() timeInfo = %v, want %v", timeInfo, tc.wantTimeInfo)
}
})
}
}