Skip to content

Commit c0b1c8f

Browse files
lengrongfukiashok
authored andcommitted
fix ci Linux Integration test fail
Signed-off-by: rongfu.leng <[email protected]> (cherry picked from commit 38f9bc3) Signed-off-by: Kirtana Ashok <[email protected]>
1 parent e631554 commit c0b1c8f

2 files changed

Lines changed: 113 additions & 0 deletions

File tree

pkg/cri/sbserver/container_stats_list.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/containerd/api/services/tasks/v1"
3030
"github.com/containerd/containerd/api/types"
3131
"github.com/containerd/containerd/errdefs"
32+
"github.com/containerd/containerd/log"
3233
"github.com/containerd/containerd/pkg/cri/store/stats"
3334
"github.com/containerd/containerd/protobuf"
3435
"github.com/containerd/typeurl/v2"
@@ -115,6 +116,14 @@ func (c *criService) toCRIContainerStats(
115116
if !ok {
116117
handler, err = c.getMetricsHandler(ctx, cntr.SandboxID)
117118
if err != nil {
119+
// If the sandbox is not found, it may have been removed. we need to check container whether it is still exist
120+
if errdefs.IsNotFound(err) {
121+
_, err = c.containerStore.Get(cntr.ID)
122+
if err != nil && errdefs.IsNotFound(err) {
123+
log.G(ctx).Warnf("container %q is not found, skip it", cntr.ID)
124+
continue
125+
}
126+
}
118127
return nil, fmt.Errorf("failed to get metrics handler for container %q: %w", cntr.ID, err)
119128
}
120129
sandboxToMetricsHandler[cntr.SandboxID] = handler

pkg/cri/sbserver/container_stats_list_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
package sbserver
1818

1919
import (
20+
"context"
2021
"math"
22+
"reflect"
2123
"testing"
2224
"time"
2325

2426
v1 "github.com/containerd/cgroups/v3/cgroup1/stats"
2527
v2 "github.com/containerd/cgroups/v3/cgroup2/stats"
28+
"github.com/containerd/containerd/api/types"
2629
containerstore "github.com/containerd/containerd/pkg/cri/store/container"
30+
sandboxstore "github.com/containerd/containerd/pkg/cri/store/sandbox"
2731
"github.com/stretchr/testify/assert"
2832
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
2933
)
@@ -328,3 +332,103 @@ func TestContainerMetricsMemory(t *testing.T) {
328332
})
329333
}
330334
}
335+
336+
func TestListContainerStats(t *testing.T) {
337+
c := newTestCRIService()
338+
type args struct {
339+
ctx context.Context
340+
stats []*types.Metric
341+
containers []containerstore.Container
342+
}
343+
tests := []struct {
344+
name string
345+
args args
346+
before func()
347+
after func()
348+
want *runtime.ListContainerStatsResponse
349+
wantErr bool
350+
}{
351+
{
352+
name: "args containers having c1,but containerStore not found c1, so filter c1",
353+
args: args{
354+
ctx: context.Background(),
355+
stats: []*types.Metric{
356+
{
357+
ID: "c1",
358+
},
359+
},
360+
containers: []containerstore.Container{
361+
{
362+
Metadata: containerstore.Metadata{
363+
ID: "c1",
364+
SandboxID: "s1",
365+
},
366+
},
367+
},
368+
},
369+
want: &runtime.ListContainerStatsResponse{},
370+
},
371+
{
372+
name: "args containers having c1,c2, but containerStore not found c1, so filter c1",
373+
args: args{
374+
ctx: context.Background(),
375+
stats: []*types.Metric{
376+
{
377+
ID: "c1",
378+
},
379+
{
380+
ID: "c2",
381+
},
382+
},
383+
containers: []containerstore.Container{
384+
{
385+
Metadata: containerstore.Metadata{
386+
ID: "c1",
387+
SandboxID: "s1",
388+
},
389+
},
390+
{
391+
Metadata: containerstore.Metadata{
392+
ID: "c2",
393+
SandboxID: "s2",
394+
},
395+
},
396+
},
397+
},
398+
before: func() {
399+
c.containerStore.Add(containerstore.Container{
400+
Metadata: containerstore.Metadata{
401+
ID: "c2",
402+
},
403+
})
404+
c.sandboxStore.Add(sandboxstore.Sandbox{
405+
Metadata: sandboxstore.Metadata{
406+
ID: "s2",
407+
},
408+
})
409+
},
410+
wantErr: true,
411+
want: nil,
412+
},
413+
}
414+
415+
for _, tt := range tests {
416+
t.Run(tt.name, func(t *testing.T) {
417+
if tt.before != nil {
418+
tt.before()
419+
}
420+
got, err := c.toCRIContainerStats(tt.args.ctx, tt.args.stats, tt.args.containers)
421+
if tt.after != nil {
422+
tt.after()
423+
}
424+
if (err != nil) != tt.wantErr {
425+
t.Errorf("ListContainerStats() error = %v, wantErr %v", err, tt.wantErr)
426+
return
427+
}
428+
if !reflect.DeepEqual(got, tt.want) {
429+
t.Errorf("ListContainerStats() = %v, want %v", got, tt.want)
430+
}
431+
})
432+
}
433+
434+
}

0 commit comments

Comments
 (0)