From 3cece1b1f92d73ac682460fc1db42c03f727900b Mon Sep 17 00:00:00 2001 From: fahed dorgaa Date: Tue, 25 Jun 2024 23:09:14 +0200 Subject: [PATCH] fix missing name container stats Signed-off-by: fahed dorgaa --- pkg/cmd/container/list.go | 22 ++-------------------- pkg/cmd/container/list_util.go | 4 ++-- pkg/cmd/container/stats.go | 10 +++++----- pkg/containerutil/containerutil.go | 19 +++++++++++++++++++ pkg/statsutil/stats.go | 23 +++++++++++++++-------- 5 files changed, 43 insertions(+), 35 deletions(-) diff --git a/pkg/cmd/container/list.go b/pkg/cmd/container/list.go index c7dc778d5de..c4279ad40d6 100644 --- a/pkg/cmd/container/list.go +++ b/pkg/cmd/container/list.go @@ -31,10 +31,10 @@ import ( "github.com/containerd/log" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/containerdutil" + "github.com/containerd/nerdctl/v2/pkg/containerutil" "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/imgutil" "github.com/containerd/nerdctl/v2/pkg/labels" - "github.com/containerd/nerdctl/v2/pkg/labels/k8slabels" ) // List prints containers according to `options`. @@ -138,7 +138,7 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container ID: id, Image: info.Image, Platform: info.Labels[labels.Platform], - Names: getContainerName(info.Labels), + Names: containerutil.GetContainerName(info.Labels), Ports: formatter.FormatPorts(info.Labels), Status: formatter.ContainerStatus(ctx, c), Runtime: info.Runtime.Name, @@ -162,24 +162,6 @@ func prepareContainers(ctx context.Context, client *containerd.Client, container return listItems, nil } -func getContainerName(containerLabels map[string]string) string { - if name, ok := containerLabels[labels.Name]; ok { - return name - } - - if ns, ok := containerLabels[k8slabels.PodNamespace]; ok { - if podName, ok := containerLabels[k8slabels.PodName]; ok { - if containerName, ok := containerLabels[k8slabels.ContainerName]; ok { - // Container - return fmt.Sprintf("k8s://%s/%s/%s", ns, podName, containerName) - } - // Pod sandbox - return fmt.Sprintf("k8s://%s/%s", ns, podName) - } - } - return "" -} - func getContainerNetworks(containerLables map[string]string) []string { var networks []string if names, ok := containerLables[labels.Networks]; ok { diff --git a/pkg/cmd/container/list_util.go b/pkg/cmd/container/list_util.go index 4647faa213a..50f1f8b7251 100644 --- a/pkg/cmd/container/list_util.go +++ b/pkg/cmd/container/list_util.go @@ -289,7 +289,7 @@ func (cl *containerFilterContext) matchesNameFilter(info containers.Container) b if len(cl.nameFilterFuncs) == 0 { return true } - cName := getContainerName(info.Labels) + cName := containerutil.GetContainerName(info.Labels) for _, nameFilterFunc := range cl.nameFilterFuncs { if !nameFilterFunc(cName) { continue @@ -368,7 +368,7 @@ func idOrNameFilter(ctx context.Context, containers []containerd.Container, valu if err != nil { return nil, err } - if strings.HasPrefix(info.ID, value) || strings.Contains(getContainerName(info.Labels), value) { + if strings.HasPrefix(info.ID, value) || strings.Contains(containerutil.GetContainerName(info.Labels), value) { return &info, nil } } diff --git a/pkg/cmd/container/stats.go b/pkg/cmd/container/stats.go index c81165a4773..de44e7226e0 100644 --- a/pkg/cmd/container/stats.go +++ b/pkg/cmd/container/stats.go @@ -35,11 +35,11 @@ import ( "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/containerinspector" + "github.com/containerd/nerdctl/v2/pkg/containerutil" "github.com/containerd/nerdctl/v2/pkg/eventutil" "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" "github.com/containerd/nerdctl/v2/pkg/infoutil" - "github.com/containerd/nerdctl/v2/pkg/labels" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/statsutil" "github.com/containerd/typeurl/v2" @@ -54,7 +54,7 @@ type stats struct { func (s *stats) add(cs *statsutil.Stats) bool { s.mu.Lock() defer s.mu.Unlock() - if _, exists := s.isKnownContainer(cs.Container); !exists { + if _, exists := s.isKnownContainer(cs.ID); !exists { s.cs = append(s.cs, cs) return true } @@ -73,7 +73,7 @@ func (s *stats) remove(id string) { // isKnownContainer is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/stats_helpers.go#L44-L51 func (s *stats) isKnownContainer(cid string) (int, bool) { for i, c := range s.cs { - if c.Container == cid { + if c.ID == cid { return i, true } } @@ -325,7 +325,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string } func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *statsutil.Stats, waitFirst *sync.WaitGroup, id string, noStream bool) { - log.G(ctx).Debugf("collecting stats for %s", s.Container) + log.G(ctx).Debugf("collecting stats for %s", s.ID) var ( getFirst = true u = make(chan error, 1) @@ -394,7 +394,7 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s u <- err continue } - statsEntry.Name = clabels[labels.Name] + statsEntry.Name = containerutil.GetContainerName(clabels) statsEntry.ID = container.ID() if firstSet { diff --git a/pkg/containerutil/containerutil.go b/pkg/containerutil/containerutil.go index 85a4ddc53c9..d61a478ccd3 100644 --- a/pkg/containerutil/containerutil.go +++ b/pkg/containerutil/containerutil.go @@ -40,6 +40,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/ipcutil" "github.com/containerd/nerdctl/v2/pkg/labels" + "github.com/containerd/nerdctl/v2/pkg/labels/k8slabels" "github.com/containerd/nerdctl/v2/pkg/nsutil" "github.com/containerd/nerdctl/v2/pkg/portutil" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" @@ -562,3 +563,21 @@ func GetContainerVolumes(containerLabels map[string]string) []*ContainerVolume { } return vols } + +func GetContainerName(containerLabels map[string]string) string { + if name, ok := containerLabels[labels.Name]; ok { + return name + } + + if ns, ok := containerLabels[k8slabels.PodNamespace]; ok { + if podName, ok := containerLabels[k8slabels.PodName]; ok { + if containerName, ok := containerLabels[k8slabels.ContainerName]; ok { + // Container + return fmt.Sprintf("k8s://%s/%s/%s", ns, podName, containerName) + } + // Pod sandbox + return fmt.Sprintf("k8s://%s/%s", ns, podName) + } + } + return "" +} diff --git a/pkg/statsutil/stats.go b/pkg/statsutil/stats.go index 05436b84f45..bfbf8b6e1c0 100644 --- a/pkg/statsutil/stats.go +++ b/pkg/statsutil/stats.go @@ -19,6 +19,7 @@ package statsutil import ( "fmt" "strconv" + "strings" "sync" "time" @@ -27,7 +28,6 @@ import ( // StatsEntry represents the statistics data collected from a container type StatsEntry struct { - Container string Name string ID string CPUPercentage float64 @@ -69,15 +69,14 @@ type ContainerStats struct { } // NewStats is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L113-L116 -func NewStats(container string) *Stats { - return &Stats{StatsEntry: StatsEntry{Container: container}} +func NewStats(containerID string) *Stats { + return &Stats{StatsEntry: StatsEntry{ID: containerID}} } // SetStatistics is from https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/command/container/formatter_stats.go#L87-L93 func (cs *Stats) SetStatistics(s StatsEntry) { cs.mutex.Lock() defer cs.mutex.Unlock() - s.Container = cs.Container cs.StatsEntry = s } @@ -134,7 +133,7 @@ func calculateMemPercent(limit float64, usedNo float64) float64 { // Rendering a FormattedStatsEntry from StatsEntry func RenderEntry(in *StatsEntry, noTrunc bool) FormattedStatsEntry { return FormattedStatsEntry{ - Name: in.EntryName(), + Name: in.EntryName(noTrunc), ID: in.EntryID(noTrunc), CPUPerc: in.CPUPerc(), MemUsage: in.MemUsage(), @@ -148,10 +147,18 @@ func RenderEntry(in *StatsEntry, noTrunc bool) FormattedStatsEntry { /* a set of functions to format container stats */ -func (s *StatsEntry) EntryName() string { +func (s *StatsEntry) EntryName(noTrunc bool) string { if len(s.Name) > 1 { - if len(s.Name) > 12 { - return s.Name[:12] + if !noTrunc { + var truncLen int + if strings.HasPrefix(s.Name, "k8s://") { + truncLen = 24 + } else { + truncLen = 12 + } + if len(s.Name) > truncLen { + return s.Name[:truncLen] + } } return s.Name }