Merge pull request #82300 from lyft/cri-stats-nanocores-overflow-fix

pkg/kubelet: fix uint64 overflow when elapsed UsageCoreNanoSeconds exceeds 18446744073
This commit is contained in:
Kubernetes Prow Robot 2019-09-23 17:31:38 -07:00 committed by GitHub
commit 50196ce088
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -661,7 +661,8 @@ func (p *criStatsProvider) getAndUpdateContainerUsageNanoCores(stats *runtimeapi
if nanoSeconds <= 0 {
return nil, fmt.Errorf("zero or negative interval (%v - %v)", newStats.Timestamp, cachedStats.Timestamp)
}
usageNanoCores := (newStats.UsageCoreNanoSeconds.Value - cachedStats.UsageCoreNanoSeconds.Value) * uint64(time.Second/time.Nanosecond) / uint64(nanoSeconds)
usageNanoCores := uint64(float64(newStats.UsageCoreNanoSeconds.Value-cachedStats.UsageCoreNanoSeconds.Value) /
float64(nanoSeconds) * float64(time.Second/time.Nanosecond))
// Update cache with new value.
usageToUpdate := usageNanoCores

View File

@ -755,6 +755,9 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
var value0 uint64
var value1 uint64 = 10000000000
// Test with a large container of 100+ CPUs
var value2 uint64 = 188427786383
tests := []struct {
desc string
cpuUsageCache map[string]*cpuUsageRecord
@ -853,6 +856,31 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
},
expected: &value1,
},
{
desc: "should return correct value if elapsed UsageCoreNanoSeconds exceeds 18446744073",
stats: &runtimeapi.ContainerStats{
Attributes: &runtimeapi.ContainerAttributes{
Id: "1",
},
Cpu: &runtimeapi.CpuUsage{
Timestamp: int64(time.Second / time.Nanosecond),
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 68172016162105,
},
},
},
cpuUsageCache: map[string]*cpuUsageRecord{
"1": {
stats: &runtimeapi.CpuUsage{
Timestamp: 0,
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
Value: 67983588375722,
},
},
},
},
expected: &value2,
},
}
for _, test := range tests {