@@ -93,7 +93,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
93
93
closeChan := make (chan error )
94
94
95
95
var err error
96
- var w = options .Stdout
96
+ w : = options .Stdout
97
97
var tmpl * template.Template
98
98
switch options .Format {
99
99
case "" , "table" :
@@ -128,7 +128,6 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
128
128
return
129
129
}
130
130
}
131
-
132
131
}
133
132
134
133
// getContainerList get all existing containers (only used when calling `nerdctl stats` without arguments).
@@ -145,7 +144,9 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
145
144
continue
146
145
}
147
146
}
148
- s := statsutil .NewStats (c .ID ())
147
+ // if an error occurs when getting labels, the ID alone is sufficient for the stats screen.
148
+ clabels , _ := c .Labels (ctx )
149
+ s := statsutil .NewStats (c .ID (), containerutil .GetContainerName (clabels ))
149
150
if cStats .add (s ) {
150
151
waitFirst .Add (1 )
151
152
go collect (ctx , options .GOptions , s , waitFirst , c .ID (), ! options .NoStream )
@@ -176,7 +177,11 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
176
177
return
177
178
}
178
179
}
179
- s := statsutil .NewStats (datacc .ID )
180
+ // Load the container to get its ID for retrieving the container name from Labels.
181
+ // if an error occurs, the ID alone is sufficient for the stats screen.
182
+ container , _ := client .LoadContainer (ctx , datacc .ID )
183
+ clabels , _ := container .Labels (ctx )
184
+ s := statsutil .NewStats (datacc .ID , containerutil .GetContainerName (clabels ))
180
185
if cStats .add (s ) {
181
186
waitFirst .Add (1 )
182
187
go collect (ctx , options .GOptions , s , waitFirst , datacc .ID , ! options .NoStream )
@@ -219,7 +224,9 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
219
224
walker := & containerwalker.ContainerWalker {
220
225
Client : client ,
221
226
OnFound : func (ctx context.Context , found containerwalker.Found ) error {
222
- s := statsutil .NewStats (found .Container .ID ())
227
+ // if an error occurs when getting labels, the ID alone is sufficient for the stats screen.
228
+ clabels , _ := found .Container .Labels (ctx )
229
+ s := statsutil .NewStats (found .Container .ID (), containerutil .GetContainerName (clabels ))
223
230
if cStats .add (s ) {
224
231
waitFirst .Add (1 )
225
232
go collect (ctx , options .GOptions , s , waitFirst , found .Container .ID (), ! options .NoStream )
@@ -249,7 +256,7 @@ func Stats(ctx context.Context, client *containerd.Client, containerIDs []string
249
256
250
257
// firstTick is for creating distant CPU readings.
251
258
// firstTick stats are not displayed.
252
- var firstTick = true
259
+ firstTick : = true
253
260
for range ticker .C {
254
261
cleanScreen ()
255
262
ccstats := []statsutil.StatsEntry {}
@@ -353,25 +360,17 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
353
360
s .SetError (err )
354
361
return
355
362
}
356
-
357
363
go func () {
358
364
previousStats := new (statsutil.ContainerStats )
359
365
firstSet := true
360
366
for {
361
- //task is in the for loop to avoid nil task just after Container creation
367
+ // task is in the for loop to avoid nil task just after Container creation
362
368
task , err := container .Task (ctx , nil )
363
369
if err != nil {
364
370
u <- err
365
371
continue
366
372
}
367
373
368
- //labels is in the for loop to avoid nil labels just after Container creation
369
- clabels , err := container .Labels (ctx )
370
- if err != nil {
371
- u <- err
372
- continue
373
- }
374
-
375
374
metric , err := task .Metrics (ctx )
376
375
if err != nil {
377
376
u <- err
@@ -395,16 +394,14 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
395
394
u <- err
396
395
continue
397
396
}
398
- statsEntry .Name = containerutil .GetContainerName (clabels )
399
- statsEntry .ID = container .ID ()
400
397
401
398
if firstSet {
402
399
firstSet = false
403
400
} else {
404
401
s .SetStatistics (statsEntry )
405
402
}
406
403
u <- nil
407
- //sleep to create distant CPU readings
404
+ // sleep to create distant CPU readings
408
405
time .Sleep (500 * time .Millisecond )
409
406
}
410
407
}()
@@ -421,7 +418,7 @@ func collect(ctx context.Context, globalOptions types.GlobalCommandOptions, s *s
421
418
}
422
419
case err := <- u :
423
420
if err != nil {
424
- if ! errdefs .IsNotFound (err ) {
421
+ if ! errdefs .IsNotFound (err ) && ! strings . Contains ( err . Error (), "no such file or directory" ) {
425
422
s .SetError (err )
426
423
continue
427
424
}
0 commit comments