Skip to content

Commit

Permalink
Merge pull request #74866 from littleroad/automated-cherry-pick-of-#7…
Browse files Browse the repository at this point in the history
…4336-origin-release-1.12

Automated cherry pick of #74336: cri_stats_provider: overload nil as 0 for exited containers
  • Loading branch information
k8s-ci-robot committed Mar 5, 2019
2 parents a85aa81 + ca57f4b commit 9a063aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pkg/kubelet/stats/cri_stats_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,18 @@ func (p *criStatsProvider) makeContainerStats(
if stats.Cpu.UsageCoreNanoSeconds != nil {
result.CPU.UsageCoreNanoSeconds = &stats.Cpu.UsageCoreNanoSeconds.Value
}
} else {
result.CPU.Time = metav1.NewTime(time.Unix(0, time.Now().UnixNano()))
result.CPU.UsageCoreNanoSeconds = Uint64Ptr(0)
}
if stats.Memory != nil {
result.Memory.Time = metav1.NewTime(time.Unix(0, stats.Memory.Timestamp))
if stats.Memory.WorkingSetBytes != nil {
result.Memory.WorkingSetBytes = &stats.Memory.WorkingSetBytes.Value
}
} else {
result.Memory.Time = metav1.NewTime(time.Unix(0, time.Now().UnixNano()))
result.Memory.WorkingSetBytes = Uint64Ptr(0)
}
if stats.WritableLayer != nil {
result.Rootfs.Time = metav1.NewTime(time.Unix(0, stats.WritableLayer.Timestamp))
Expand Down
29 changes: 25 additions & 4 deletions pkg/kubelet/stats/cri_stats_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,22 @@ func TestCRIListPodStats(t *testing.T) {
seedContainer2 = 5000
seedSandbox2 = 6000
seedContainer3 = 7000
seedContainer5 = 9000
)

const (
pName0 = "pod0"
pName1 = "pod1"
pName2 = "pod2"
pName3 = "pod3"
)

const (
cName0 = "container0-name"
cName1 = "container1-name"
cName2 = "container2-name"
cName3 = "container3-name"
cName5 = "container5-name"
)

var (
Expand Down Expand Up @@ -101,6 +104,11 @@ func TestCRIListPodStats(t *testing.T) {
container4 = makeFakeContainer(sandbox2, cName3, 1, false)
containerStats4 = makeFakeContainerStats(container4, imageFsMountpoint)
containerLogStats4 = makeFakeLogStats(4000)

sandbox3 = makeFakePodSandbox("sandbox3-name", "sandbox3-uid", "sandbox3-ns")
container5 = makeFakeContainer(sandbox3, cName5, 0, true)
containerStats5 = makeFakeContainerStats(container5, imageFsMountpoint)
containerLogStats5 = makeFakeLogStats(5000)
)

var (
Expand Down Expand Up @@ -140,13 +148,13 @@ func TestCRIListPodStats(t *testing.T) {
On("GetDirFsInfo", imageFsMountpoint).Return(imageFsInfo, nil).
On("GetDirFsInfo", unknownMountpoint).Return(cadvisorapiv2.FsInfo{}, cadvisorfs.ErrNoSuchDevice)
fakeRuntimeService.SetFakeSandboxes([]*critest.FakePodSandbox{
sandbox0, sandbox1, sandbox2,
sandbox0, sandbox1, sandbox2, sandbox3,
})
fakeRuntimeService.SetFakeContainers([]*critest.FakeContainer{
container0, container1, container2, container3, container4,
container0, container1, container2, container3, container4, container5,
})
fakeRuntimeService.SetFakeContainerStats([]*runtimeapi.ContainerStats{
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4,
containerStats0, containerStats1, containerStats2, containerStats3, containerStats4, containerStats5,
})

ephemeralVolumes := makeFakeVolumeStats([]string{"ephVolume1, ephVolumes2"})
Expand All @@ -161,6 +169,7 @@ func TestCRIListPodStats(t *testing.T) {
kuberuntime.BuildContainerLogsDirectory(types.UID("sandbox0-uid"), cName1): containerLogStats1,
kuberuntime.BuildContainerLogsDirectory(types.UID("sandbox1-uid"), cName2): containerLogStats2,
kuberuntime.BuildContainerLogsDirectory(types.UID("sandbox2-uid"), cName3): containerLogStats4,
kuberuntime.BuildContainerLogsDirectory(types.UID("sandbox3-uid"), cName5): containerLogStats5,
}
fakeLogStatsProvider := NewFakeLogMetricsService(fakeLogStats)

Expand All @@ -177,7 +186,7 @@ func TestCRIListPodStats(t *testing.T) {
stats, err := provider.ListPodStats()
assert := assert.New(t)
assert.NoError(err)
assert.Equal(3, len(stats))
assert.Equal(4, len(stats))

podStatsMap := make(map[statsapi.PodReference]statsapi.PodStats)
for _, s := range stats {
Expand Down Expand Up @@ -239,6 +248,18 @@ func TestCRIListPodStats(t *testing.T) {
checkCRINetworkStats(assert, p2.Network, infos[sandbox2.PodSandboxStatus.Id].Stats[0].Network)
checkCRIPodCPUAndMemoryStats(assert, p2, infos[sandbox2Cgroup].Stats[0])

p3 := podStatsMap[statsapi.PodReference{Name: "sandbox3-name", UID: "sandbox3-uid", Namespace: "sandbox3-ns"}]
assert.Equal(sandbox3.CreatedAt, p3.StartTime.UnixNano())
assert.Equal(1, len(p3.Containers))

c5 := p3.Containers[0]
assert.Equal(cName5, c5.Name)
assert.Equal(container5.CreatedAt, c5.StartTime.UnixNano())
assert.NotNil(c5.CPU.Time)
assert.Zero(*c5.CPU.UsageCoreNanoSeconds)
assert.NotNil(c5.Memory.Time)
assert.Zero(*c5.Memory.WorkingSetBytes)

mockCadvisor.AssertExpectations(t)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/kubelet/stats/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,7 @@ func getUint64Value(value *uint64) uint64 {

return *value
}

func Uint64Ptr(i uint64) *uint64 {
return &i
}

0 comments on commit 9a063aa

Please sign in to comment.