Skip to content

Commit

Permalink
fix missing name container stats
Browse files Browse the repository at this point in the history
Signed-off-by: fahed dorgaa <[email protected]>
  • Loading branch information
fahedouch committed Jun 29, 2024
1 parent 6b372ea commit 3cece1b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
22 changes: 2 additions & 20 deletions pkg/cmd/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/container/list_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/container/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ""
}
23 changes: 15 additions & 8 deletions pkg/statsutil/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package statsutil
import (
"fmt"
"strconv"
"strings"
"sync"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(),
Expand All @@ -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
}
Expand Down

0 comments on commit 3cece1b

Please sign in to comment.