Skip to content

Commit 7d4f555

Browse files
committed
correctly handle resetting cpuacct in a live container
1 parent 064458d commit 7d4f555

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/kubelet/stats/cri_stats_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ func (p *criStatsProvider) getAndUpdateContainerUsageNanoCores(stats *runtimeapi
649649
defer p.mutex.Unlock()
650650

651651
cached, ok := p.cpuUsageCache[id]
652-
if !ok || cached.stats.UsageCoreNanoSeconds == nil {
652+
if !ok || cached.stats.UsageCoreNanoSeconds == nil || stats.Cpu.UsageCoreNanoSeconds.Value < cached.stats.UsageCoreNanoSeconds.Value {
653653
// Cannot compute the usage now, but update the cached stats anyway
654654
p.cpuUsageCache[id] = &cpuUsageRecord{stats: stats.Cpu, usageNanoCores: nil}
655655
return nil, nil

pkg/kubelet/stats/cri_stats_provider_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,32 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
881881
},
882882
expected: &value2,
883883
},
884+
{
885+
desc: "should return nil if cpuacct is reset to 0 in a live container",
886+
stats: &runtimeapi.ContainerStats{
887+
Attributes: &runtimeapi.ContainerAttributes{
888+
Id: "1",
889+
},
890+
Cpu: &runtimeapi.CpuUsage{
891+
Timestamp: 2,
892+
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
893+
Value: 0,
894+
},
895+
},
896+
},
897+
cpuUsageCache: map[string]*cpuUsageRecord{
898+
"1": {
899+
stats: &runtimeapi.CpuUsage{
900+
Timestamp: 1,
901+
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
902+
Value: 10000000000,
903+
},
904+
},
905+
},
906+
},
907+
expected: nil,
908+
},
884909
}
885-
886910
for _, test := range tests {
887911
provider := &criStatsProvider{cpuUsageCache: test.cpuUsageCache}
888912
// Before the update, the cached value should be nil

0 commit comments

Comments
 (0)