diff --git a/containerm/ctr/ctrd_spi.go b/containerm/ctr/ctrd_spi.go index d4dcd0a..28d0608 100644 --- a/containerm/ctr/ctrd_spi.go +++ b/containerm/ctr/ctrd_spi.go @@ -61,8 +61,8 @@ type containerdSpi interface { UnpackImage(ctx context.Context, image containerd.Image, opts ...containerd.UnpackOpt) error // DeleteImage removes the contents of the provided image from the disk DeleteImage(ctx context.Context, imageRef string) error - // ListImages returns all locally existing images - ListImages(ctx context.Context) ([]containerd.Image, error) + // ListImages returns all locally existing images matching the provided filters or all if no filters are provided + ListImages(ctx context.Context, filters ...string) ([]containerd.Image, error) // Wrapper section for managing the file system of the container and its snapshots // GetSnapshotID generates a new ID for the snapshot to be used for this container diff --git a/containerm/ctr/ctrd_spi_images.go b/containerm/ctr/ctrd_spi_images.go index aafe64d..54e091d 100644 --- a/containerm/ctr/ctrd_spi_images.go +++ b/containerm/ctr/ctrd_spi_images.go @@ -38,9 +38,9 @@ func (spi *ctrdSpi) UnpackImage(ctx context.Context, image containerd.Image, opt } // ListImages returns all locally existing images -func (spi *ctrdSpi) ListImages(ctx context.Context) ([]containerd.Image, error) { +func (spi *ctrdSpi) ListImages(ctx context.Context, filters ...string) ([]containerd.Image, error) { ctx = spi.setContext(ctx, false) - return spi.client.ListImages(ctx) + return spi.client.ListImages(ctx, filters...) } // DeleteImage removes the contents of the provided image from the disk diff --git a/containerm/ctr/ctrd_spi_images_test.go b/containerm/ctr/ctrd_spi_images_test.go index a138280..fb9e461 100644 --- a/containerm/ctr/ctrd_spi_images_test.go +++ b/containerm/ctr/ctrd_spi_images_test.go @@ -189,6 +189,7 @@ func TestUnpackImage(t *testing.T) { func TestListImages(t *testing.T) { const ( testNamespace = "test-ns" + testFilter = "name=test.img/ref:latest" ) testCases := map[string]struct { @@ -197,14 +198,14 @@ func TestListImages(t *testing.T) { "test_no_err": { mockExec: func(ctx context.Context, ctrdWrapperMock *ctrdMocks.MockcontainerClientWrapper, imageMock *containerdMocks.MockImage) ([]containerd.Image, error) { res := []containerd.Image{imageMock} - ctrdWrapperMock.EXPECT().ListImages(ctx).Return(res, nil) + ctrdWrapperMock.EXPECT().ListImages(ctx, testFilter).Return(res, nil) return res, nil }, }, "test_err": { mockExec: func(ctx context.Context, ctrdWrapperMock *ctrdMocks.MockcontainerClientWrapper, imageMock *containerdMocks.MockImage) ([]containerd.Image, error) { err := log.NewError("test pull image error") - ctrdWrapperMock.EXPECT().ListImages(ctx).Return(nil, err) + ctrdWrapperMock.EXPECT().ListImages(ctx, testFilter).Return(nil, err) return nil, err }, }, @@ -230,7 +231,7 @@ func TestListImages(t *testing.T) { expectedImgs, expectedErr := testData.mockExec(namespaces.WithNamespace(ctx, testNamespace), mockCtrdWrapper, mockImg) // test - imgs, actualErr := testSpi.ListImages(ctx) + imgs, actualErr := testSpi.ListImages(ctx, testFilter) testutil.AssertError(t, expectedErr, actualErr) testutil.AssertEqual(t, expectedImgs, imgs) }) diff --git a/containerm/pkg/testutil/mocks/ctrd/mock_ctrd_spi.go b/containerm/pkg/testutil/mocks/ctrd/mock_ctrd_spi.go index 59aeb24..7c2d986 100644 --- a/containerm/pkg/testutil/mocks/ctrd/mock_ctrd_spi.go +++ b/containerm/pkg/testutil/mocks/ctrd/mock_ctrd_spi.go @@ -354,18 +354,23 @@ func (mr *MockcontainerdSpiMockRecorder) GetSnapshotID(containerID interface{}) } // ListImages mocks base method. -func (m *MockcontainerdSpi) ListImages(ctx context.Context) ([]containerd.Image, error) { +func (m *MockcontainerdSpi) ListImages(ctx context.Context, filters ...string) ([]containerd.Image, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListImages", ctx) + varargs := []interface{}{ctx} + for _, a := range filters { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ListImages", varargs...) ret0, _ := ret[0].([]containerd.Image) ret1, _ := ret[1].(error) return ret0, ret1 } // ListImages indicates an expected call of ListImages. -func (mr *MockcontainerdSpiMockRecorder) ListImages(ctx interface{}) *gomock.Call { +func (mr *MockcontainerdSpiMockRecorder) ListImages(ctx interface{}, filters ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListImages", reflect.TypeOf((*MockcontainerdSpi)(nil).ListImages), ctx) + varargs := append([]interface{}{ctx}, filters...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListImages", reflect.TypeOf((*MockcontainerdSpi)(nil).ListImages), varargs...) } // ListSnapshots mocks base method.