diff --git a/cri/v1alpha1/cri_utils.go b/cri/v1alpha1/cri_utils.go index 9d4679301..c47e1cef1 100644 --- a/cri/v1alpha1/cri_utils.go +++ b/cri/v1alpha1/cri_utils.go @@ -891,9 +891,9 @@ func (c *CriManager) attachLog(logPath string, containerID string, openStdin boo func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Container) (*runtime.ContainerStats, error) { var usedBytes, inodesUsed uint64 - stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID) + metadata, err := parseContainerName(meta.Name) if err != nil { - return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err) + return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err) } // snapshot key may not equals container ID later @@ -913,11 +913,6 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe InodesUsed: &runtime.UInt64Value{inodesUsed}, } - metadata, err := parseContainerName(meta.Name) - if err != nil { - return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err) - } - labels, annotations := extractLabels(meta.Config.Labels) cs.Attributes = &runtime.ContainerAttributes{ @@ -927,6 +922,11 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe Annotations: annotations, } + stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID) + if err != nil { + return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err) + } + if stats != nil { s, err := typeurl.UnmarshalAny(stats.Data) if err != nil { diff --git a/cri/v1alpha2/cri_utils.go b/cri/v1alpha2/cri_utils.go index 8d8cb1ef4..73c991d1c 100644 --- a/cri/v1alpha2/cri_utils.go +++ b/cri/v1alpha2/cri_utils.go @@ -918,9 +918,9 @@ func (c *CriManager) attachLog(logPath string, containerID string, openStdin boo func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Container) (*runtime.ContainerStats, error) { var usedBytes, inodesUsed uint64 - stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID) + metadata, err := parseContainerName(meta.Name) if err != nil { - return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err) + return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err) } sn, err := c.SnapshotStore.Get(meta.ID) @@ -938,12 +938,6 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe UsedBytes: &runtime.UInt64Value{usedBytes}, InodesUsed: &runtime.UInt64Value{inodesUsed}, } - - metadata, err := parseContainerName(meta.Name) - if err != nil { - return nil, fmt.Errorf("failed to get metadata of container %q: %v", meta.ID, err) - } - labels, annotations := extractLabels(meta.Config.Labels) cs.Attributes = &runtime.ContainerAttributes{ @@ -953,6 +947,11 @@ func (c *CriManager) getContainerMetrics(ctx context.Context, meta *mgr.Containe Annotations: annotations, } + stats, _, err := c.ContainerMgr.Stats(ctx, meta.ID) + if err != nil { + return nil, fmt.Errorf("failed to get stats of container %q: %v", meta.ID, err) + } + if stats != nil { s, err := typeurl.UnmarshalAny(stats.Data) if err != nil {