Skip to content

Commit ac97b2d

Browse files
authored
Merge pull request kubernetes#83507 from lyft/support-resetting-cpuacct
Prevent returning invalid usageNanoCores value when cpuacct is reset in a live container
2 parents eb2d1fd + 7d4f555 commit ac97b2d

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
@@ -891,8 +891,32 @@ func TestGetContainerUsageNanoCores(t *testing.T) {
891891
},
892892
expected: &value2,
893893
},
894+
{
895+
desc: "should return nil if cpuacct is reset to 0 in a live container",
896+
stats: &runtimeapi.ContainerStats{
897+
Attributes: &runtimeapi.ContainerAttributes{
898+
Id: "1",
899+
},
900+
Cpu: &runtimeapi.CpuUsage{
901+
Timestamp: 2,
902+
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
903+
Value: 0,
904+
},
905+
},
906+
},
907+
cpuUsageCache: map[string]*cpuUsageRecord{
908+
"1": {
909+
stats: &runtimeapi.CpuUsage{
910+
Timestamp: 1,
911+
UsageCoreNanoSeconds: &runtimeapi.UInt64Value{
912+
Value: 10000000000,
913+
},
914+
},
915+
},
916+
},
917+
expected: nil,
918+
},
894919
}
895-
896920
for _, test := range tests {
897921
provider := &criStatsProvider{cpuUsageCache: test.cpuUsageCache}
898922
// Before the update, the cached value should be nil

0 commit comments

Comments
 (0)