From 3125f7e1a035bf7d46c6be61c89dc42f05190a80 Mon Sep 17 00:00:00 2001 From: Danielle Lancashire Date: Fri, 22 Jul 2022 11:54:51 +0000 Subject: [PATCH] cri_stats: handle missing cpu stats Signed-off-by: Danielle Lancashire --- pkg/cri/server/container_stats_list.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/cri/server/container_stats_list.go b/pkg/cri/server/container_stats_list.go index c0cffba92..9459b64b1 100644 --- a/pkg/cri/server/container_stats_list.go +++ b/pkg/cri/server/container_stats_list.go @@ -65,12 +65,14 @@ func (c *criService) toCRIContainerStats( return nil, fmt.Errorf("failed to decode container metrics for %q: %w", cntr.ID, err) } - // this is a calculated value and should be computed for all OSes - nanoUsage, err := c.getUsageNanoCores(cntr.Metadata.ID, false, cs.Cpu.UsageCoreNanoSeconds.Value, time.Unix(0, cs.Cpu.Timestamp)) - if err != nil { - return nil, fmt.Errorf("failed to get usage nano cores, containerID: %s: %w", cntr.Metadata.ID, err) + if cs.Cpu != nil && cs.Cpu.UsageCoreNanoSeconds != nil { + // this is a calculated value and should be computed for all OSes + nanoUsage, err := c.getUsageNanoCores(cntr.Metadata.ID, false, cs.Cpu.UsageCoreNanoSeconds.Value, time.Unix(0, cs.Cpu.Timestamp)) + if err != nil { + return nil, fmt.Errorf("failed to get usage nano cores, containerID: %s: %w", cntr.Metadata.ID, err) + } + cs.Cpu.UsageNanoCores = &runtime.UInt64Value{Value: nanoUsage} } - cs.Cpu.UsageNanoCores = &runtime.UInt64Value{Value: nanoUsage} containerStats.Stats = append(containerStats.Stats, cs) }