kubelet/stats: take container log stats into account when checking ephemeral stats

this commit updates checkEphemeralStorage to be able to add container log stats, if applicable.

It also updates the old check when container log stats aren't found to be more accurate.
Specifically, this check previously worked because of a fluke programming accident:

according to this block in pkg/kubelet/stats/helper.go:113
```
if result.Rootfs != nil {
    rootfsUsage := *cfs.BaseUsageBytes
    result.Rootfs.UsedBytes = &rootfsUsage
}
```

BaseUsageBytes should be the value added, not TotalUsageBytes. However, since in this case
one also needs to account for the calculated log size, which is TotalUsageBytes - BaseUsageBytes
using TotalUsageBytes value accidentally worked.

Updating the case to use the correct value AND log offset fixes this accident and makes
the behavior more in line with what happens when calculating ephemeral storage.

Signed-off-by: Peter Hunt <pehunt@redhat.com>
This commit is contained in:
Peter Hunt 2022-02-15 16:28:17 -05:00
parent ab0f274a6f
commit 1c3357db76
2 changed files with 16 additions and 4 deletions

View File

@ -270,7 +270,7 @@ func TestCadvisorListPodStats(t *testing.T) {
assert.EqualValues(t, p0Time.Unix(), ps.StartTime.Time.Unix())
checkNetworkStats(t, "Pod0", seedPod0Infra, ps.Network)
checkEphemeralStats(t, "Pod0", []int{seedPod0Container0, seedPod0Container1}, []int{seedEphemeralVolume1, seedEphemeralVolume2}, ps.EphemeralStorage)
checkEphemeralStats(t, "Pod0", []int{seedPod0Container0, seedPod0Container1}, []int{seedEphemeralVolume1, seedEphemeralVolume2}, nil, ps.EphemeralStorage)
if ps.CPU != nil {
checkCPUStats(t, "Pod0", seedPod0Infra, ps.CPU)
}

View File

@ -37,6 +37,7 @@ import (
kubepodtest "k8s.io/kubernetes/pkg/kubelet/pod/testing"
serverstats "k8s.io/kubernetes/pkg/kubelet/server/stats"
kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
"k8s.io/kubernetes/pkg/volume"
)
const (
@ -699,16 +700,27 @@ func checkFsStats(t *testing.T, label string, seed int, stats *statsapi.FsStats)
assert.EqualValues(t, seed+offsetFsInodesFree, *stats.InodesFree, label+".InodesFree")
}
func checkEphemeralStats(t *testing.T, label string, containerSeeds []int, volumeSeeds []int, stats *statsapi.FsStats) {
func checkEphemeralStats(t *testing.T, label string, containerSeeds []int, volumeSeeds []int, containerLogStats []*volume.Metrics, stats *statsapi.FsStats) {
var usedBytes, inodeUsage int
for _, cseed := range containerSeeds {
usedBytes = usedBytes + cseed + offsetFsTotalUsageBytes
usedBytes += cseed + offsetFsBaseUsageBytes
inodeUsage += cseed + offsetFsInodeUsage
// If containerLogStats is nil, then the log stats calculated from cAdvisor
// information is used. Since it's Total - Base, and these values are
// set to the offset, we can use the calculated difference in the offset
// to account for this.
if containerLogStats == nil {
usedBytes += offsetFsTotalUsageBytes - offsetFsBaseUsageBytes
}
}
for _, vseed := range volumeSeeds {
usedBytes = usedBytes + vseed + offsetFsUsage
usedBytes += vseed + offsetFsUsage
inodeUsage += vseed + offsetFsInodeUsage
}
for _, logStats := range containerLogStats {
usedBytes += int(logStats.Used.Value())
inodeUsage += int(logStats.InodesUsed.Value())
}
assert.EqualValues(t, usedBytes, int(*stats.UsedBytes), label+".UsedBytes")
assert.EqualValues(t, inodeUsage, int(*stats.InodesUsed), label+".InodesUsed")
}