mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 04:06:03 +00:00
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:
commit
50196ce088
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user