Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit f8b1c97

Browse files
authored
Merge pull request containerd#3142 from fahedouch/fix-container-name-stats
fix missing name container stats
2 parents 51645bc + 3cece1b commit f8b1c97

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

pkg/cmd/container/list.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import (
3131
"github.com/containerd/log"
3232
"github.com/containerd/nerdctl/v2/pkg/api/types"
3333
"github.com/containerd/nerdctl/v2/pkg/containerdutil"
34+
"github.com/containerd/nerdctl/v2/pkg/containerutil"
3435
"github.com/containerd/nerdctl/v2/pkg/formatter"
3536
"github.com/containerd/nerdctl/v2/pkg/imgutil"
3637
"github.com/containerd/nerdctl/v2/pkg/labels"
37-
"github.com/containerd/nerdctl/v2/pkg/labels/k8slabels"
3838
)
3939

4040
// List prints containers according to `options`.
@@ -138,7 +138,7 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
138138
ID: id,
139139
Image: info.Image,
140140
Platform: info.Labels[labels.Platform],
141-
Names: getContainerName(info.Labels),
141+
Names: containerutil.GetContainerName(info.Labels),
142142
Ports: formatter.FormatPorts(info.Labels),
143143
Status: formatter.ContainerStatus(ctx, c),
144144
Runtime: info.Runtime.Name,
@@ -162,24 +162,6 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container
162162
return listItems, nil
163163
}
164164

165-
func getContainerName(containerLabels map[string]string) string {
166-
if name, ok := containerLabels[labels.Name]; ok {
167-
return name
168-
}
169-
170-
if ns, ok := containerLabels[k8slabels.PodNamespace]; ok {
171-
if podName, ok := containerLabels[k8slabels.PodName]; ok {
172-
if containerName, ok := containerLabels[k8slabels.ContainerName]; ok {
173-
// Container
174-
return fmt.Sprintf("k8s://%s/%s/%s", ns, podName, containerName)
175-
}
176-
// Pod sandbox
177-
return fmt.Sprintf("k8s://%s/%s", ns, podName)
178-
}
179-
}
180-
return ""
181-
}
182-
183165
func getContainerNetworks(containerLables map[string]string) []string {
184166
var networks []string
185167
if names, ok := containerLables[labels.Networks]; ok {

pkg/cmd/container/list_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (cl *containerFilterContext) matchesNameFilter(info containers.Container) b
289289
if len(cl.nameFilterFuncs) == 0 {
290290
return true
291291
}
292-
cName := getContainerName(info.Labels)
292+
cName := containerutil.GetContainerName(info.Labels)
293293
for _, nameFilterFunc := range cl.nameFilterFuncs {
294294
if !nameFilterFunc(cName) {
295295
continue
@@ -368,7 +368,7 @@ func idOrNameFilter(ctx context.Context, containers []containerd.Container, valu
368368
if err != nil {
369369
return nil, err
370370
}
371-
if strings.HasPrefix(info.ID, value) || strings.Contains(getContainerName(info.Labels), value) {
371+
if strings.HasPrefix(info.ID, value) || strings.Contains(containerutil.GetContainerName(info.Labels), value) {
372372
return &info, nil
373373
}
374374
}

pkg/cmd/container/stats.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ import (
3535
"github.com/containerd/nerdctl/v2/pkg/api/types"
3636
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3737
"github.com/containerd/nerdctl/v2/pkg/containerinspector"
38+
"github.com/containerd/nerdctl/v2/pkg/containerutil"
3839
"github.com/containerd/nerdctl/v2/pkg/eventutil"
3940
"github.com/containerd/nerdctl/v2/pkg/formatter"
4041
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
4142
"github.com/containerd/nerdctl/v2/pkg/infoutil"
42-
"github.com/containerd/nerdctl/v2/pkg/labels"
4343
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
4444
"github.com/containerd/nerdctl/v2/pkg/statsutil"
4545
"github.com/containerd/typeurl/v2"
@@ -54,7 +54,7 @@ type stats struct {
5454
func (s *stats) add(cs *statsutil.Stats) bool {
5555
s.mu.Lock()
5656
defer s.mu.Unlock()
57-
if _, exists := s.isKnownContainer(cs.Container); !exists {
57+
if _, exists := s.isKnownContainer(cs.ID); !exists {
5858
s.cs = append(s.cs, cs)
5959
return true
6060
}
@@ -73,7 +73,7 @@ func (s *stats) remove(id string) {
7373
// isKnownContainer is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/stats_helpers.go#L44-L51
7474
func (s *stats) isKnownContainer(cid string) (int, bool) {
7575
for i, c := range s.cs {
76-
if c.Container == cid {
76+
if c.ID == cid {
7777
return i, true
7878
}
7979
}
@@ -325,7 +325,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
325325
}
326326

327327
func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *statsutil.Stats, waitFirst *sync.WaitGroup, id string, noStream bool) {
328-
log.G(ctx).Debugf("collecting stats for %s", s.Container)
328+
log.G(ctx).Debugf("collecting stats for %s", s.ID)
329329
var (
330330
getFirst = true
331331
u = make(chan error, 1)
@@ -394,7 +394,7 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
394394
u <- err
395395
continue
396396
}
397-
statsEntry.Name = clabels[labels.Name]
397+
statsEntry.Name = containerutil.GetContainerName(clabels)
398398
statsEntry.ID = container.ID()
399399

400400
if firstSet {

pkg/containerutil/containerutil.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/containerd/nerdctl/v2/pkg/formatter"
4141
"github.com/containerd/nerdctl/v2/pkg/ipcutil"
4242
"github.com/containerd/nerdctl/v2/pkg/labels"
43+
"github.com/containerd/nerdctl/v2/pkg/labels/k8slabels"
4344
"github.com/containerd/nerdctl/v2/pkg/nsutil"
4445
"github.com/containerd/nerdctl/v2/pkg/portutil"
4546
"github.com/containerd/nerdctl/v2/pkg/rootlessutil"
@@ -562,3 +563,21 @@ func GetContainerVolumes(containerLabels map[string]string) []*ContainerVolume {
562563
}
563564
return vols
564565
}
566+
567+
func GetContainerName(containerLabels map[string]string) string {
568+
if name, ok := containerLabels[labels.Name]; ok {
569+
return name
570+
}
571+
572+
if ns, ok := containerLabels[k8slabels.PodNamespace]; ok {
573+
if podName, ok := containerLabels[k8slabels.PodName]; ok {
574+
if containerName, ok := containerLabels[k8slabels.ContainerName]; ok {
575+
// Container
576+
return fmt.Sprintf("k8s://%s/%s/%s", ns, podName, containerName)
577+
}
578+
// Pod sandbox
579+
return fmt.Sprintf("k8s://%s/%s", ns, podName)
580+
}
581+
}
582+
return ""
583+
}

pkg/statsutil/stats.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package statsutil
1919
import (
2020
"fmt"
2121
"strconv"
22+
"strings"
2223
"sync"
2324
"time"
2425

@@ -27,7 +28,6 @@ import (
2728

2829
// StatsEntry represents the statistics data collected from a container
2930
type StatsEntry struct {
30-
Container string
3131
Name string
3232
ID string
3333
CPUPercentage float64
@@ -69,15 +69,14 @@ type ContainerStats struct {
6969
}
7070

7171
// NewStats is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L113-L116
72-
func NewStats(container string) *Stats {
73-
return &Stats{StatsEntry: StatsEntry{Container: container}}
72+
func NewStats(containerID string) *Stats {
73+
return &Stats{StatsEntry: StatsEntry{ID: containerID}}
7474
}
7575

7676
// SetStatistics is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L87-L93
7777
func (cs *Stats) SetStatistics(s StatsEntry) {
7878
cs.mutex.Lock()
7979
defer cs.mutex.Unlock()
80-
s.Container = cs.Container
8180
cs.StatsEntry = s
8281
}
8382

@@ -134,7 +133,7 @@ func calculateMemPercent(limit float64, usedNo float64) float64 {
134133
// Rendering a FormattedStatsEntry from StatsEntry
135134
func RenderEntry(in *StatsEntry, noTrunc bool) FormattedStatsEntry {
136135
return FormattedStatsEntry{
137-
Name: in.EntryName(),
136+
Name: in.EntryName(noTrunc),
138137
ID: in.EntryID(noTrunc),
139138
CPUPerc: in.CPUPerc(),
140139
MemUsage: in.MemUsage(),
@@ -148,10 +147,18 @@ func RenderEntry(in *StatsEntry, noTrunc bool) FormattedStatsEntry {
148147
/*
149148
a set of functions to format container stats
150149
*/
151-
func (s *StatsEntry) EntryName() string {
150+
func (s *StatsEntry) EntryName(noTrunc bool) string {
152151
if len(s.Name) > 1 {
153-
if len(s.Name) > 12 {
154-
return s.Name[:12]
152+
if !noTrunc {
153+
var truncLen int
154+
if strings.HasPrefix(s.Name, "k8s://") {
155+
truncLen = 24
156+
} else {
157+
truncLen = 12
158+
}
159+
if len(s.Name) > truncLen {
160+
return s.Name[:truncLen]
161+
}
155162
}
156163
return s.Name
157164
}

0 commit comments

Comments
 (0)