Skip to content

Commit

Permalink
fix: remove the imageExists check before Pull (#359)
Browse files Browse the repository at this point in the history
Containerd does not need us to check for the existence of an image for
it; it will download any layers which do not exist and wont re-download
the same image twice.

This check is causing us trouble now because with our capmvm base images
we push new shas to the same tags. When we run this `imageExists` check
using the ref (name+label) we don't get new changes on that version.
This fix just leaves containerd to get on with it.
  • Loading branch information
Callisto13 authored Jan 24, 2022
1 parent f7bc8d6 commit 48a1ae5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 68 deletions.
30 changes: 10 additions & 20 deletions infrastructure/containerd/image_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,9 @@ func (im *imageService) PullAndMount(ctx context.Context, input *ports.ImageMoun
return nil, fmt.Errorf("getting lease for image pulling and mounting: %w", err)
}

var image containerd.Image

exists, image, err := im.imageExists(leaseCtx, input.ImageName, input.Owner)
image, err := im.pullImage(leaseCtx, input.ImageName, input.Owner)
if err != nil {
return nil, fmt.Errorf("checking if image %s exists for owner %s: %w", input.ImageName, input.Owner, err)
}

if !exists {
image, err = im.pullImage(leaseCtx, input.ImageName, input.Owner)
if err != nil {
return nil, fmt.Errorf("getting image %s for owner %s: %w", input.ImageName, input.Owner, err)
}
return nil, fmt.Errorf("getting image %s for owner %s: %w", input.ImageName, input.Owner, err)
}

ss := im.getSnapshotter(input.Use)
Expand All @@ -94,7 +85,7 @@ func (im *imageService) Exists(ctx context.Context, input *ports.ImageSpec) (boo

nsCtx := namespaces.WithNamespace(ctx, im.config.Namespace)

exists, _, err := im.imageExists(nsCtx, input.ImageName, input.Owner)
exists, err := im.imageExists(nsCtx, input.ImageName, input.Owner)
if err != nil {
return false, fmt.Errorf("checking image exists: %w", err)
}
Expand All @@ -109,7 +100,7 @@ func (im *imageService) IsMounted(ctx context.Context, input *ports.ImageMountSp

nsCtx := namespaces.WithNamespace(ctx, im.config.Namespace)

exists, _, err := im.imageExists(nsCtx, input.ImageName, input.Owner)
exists, err := im.imageExists(nsCtx, input.ImageName, input.Owner)
if err != nil {
return false, fmt.Errorf("checking image exists: %w", err)
}
Expand All @@ -130,22 +121,21 @@ func (im *imageService) IsMounted(ctx context.Context, input *ports.ImageMountSp
return snapshotExists, nil
}

func (im *imageService) imageExists(ctx context.Context, imageName, owner string) (bool, containerd.Image, error) {
func (im *imageService) imageExists(ctx context.Context, imageName, owner string) (bool, error) {
leaseCtx, err := withOwnerLease(ctx, owner, im.client)
if err != nil {
return false, nil, fmt.Errorf("getting lease for owner: %w", err)
return false, fmt.Errorf("getting lease for owner: %w", err)
}

image, err := im.client.GetImage(leaseCtx, imageName)
if err != nil {
if _, err := im.client.GetImage(leaseCtx, imageName); err != nil {
if errdefs.IsNotFound(err) {
return false, nil, nil
return false, nil
}

return false, nil, fmt.Errorf("getting image from containerd %s: %w", imageName, err)
return false, fmt.Errorf("getting image from containerd %s: %w", imageName, err)
}

return true, image, nil
return true, nil
}

func (im *imageService) pullImage(ctx context.Context, imageName string, owner string) (containerd.Image, error) {
Expand Down
56 changes: 8 additions & 48 deletions infrastructure/containerd/image_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestImageService_PullAndMount(t *testing.T) {
RootFS(gomock.Any()).
Return([]digest.Digest{digest.Digest(hash)}, nil)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Pull(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
Expand Down Expand Up @@ -224,46 +224,6 @@ func TestImageService_PullAndMount_failedLeaseOnImageCheck(t *testing.T) {
g.Expect(err).To(g.HaveOccurred())
}

// TestImageService_PullAndMount_failedImageCheck tests what happens when
// something goes wrong with imageExists (and not Leases).
func TestImageService_PullAndMount_failedImageCheck(t *testing.T) {
g.RegisterTestingT(t)

mockCtrl := gomock.NewController(t)
containerdClient := mock.NewMockClient(mockCtrl)
leasesManager := mock.NewMockManager(mockCtrl)
svcConfig := containerd.Config{
SnapshotterKernel: "native",
SnapshotterVolume: "devmapper",
SocketPath: "/something",
Namespace: "unit_test_ns",
}
ctx := context.Background()
client := containerd.NewImageServiceWithClient(&svcConfig, containerdClient)

leasesManager.EXPECT().
List(gomock.Any(), fmt.Sprintf("id==flintlock/%s", testOwner)).
Times(2)
leasesManager.EXPECT().
Create(gomock.Any(), gomock.Any()).
Times(2)
containerdClient.EXPECT().
LeasesService().
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Return(nil, errors.New("nope"))

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
ImageName: testImage,
Owner: testOwner,
Use: models.ImageUseVolume,
OwnerUsageID: testOwnerID,
})
g.Expect(err).To(g.HaveOccurred())
}

// TestImageService_PullAndMount_failedUnpackCheck tests what happens when
// something goes wrong with unpack check.
func TestImageService_PullAndMount_failedUnpackCheck(t *testing.T) {
Expand Down Expand Up @@ -299,7 +259,7 @@ func TestImageService_PullAndMount_failedUnpackCheck(t *testing.T) {
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Pull(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
Expand Down Expand Up @@ -350,7 +310,7 @@ func TestImageService_PullAndMount_failedUnpack(t *testing.T) {
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Pull(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
Expand Down Expand Up @@ -386,6 +346,9 @@ func TestImageService_PullAndMount_failedRootFS(t *testing.T) {
leasesManager.EXPECT().
Create(gomock.Any(), gomock.Any()).
Times(2)
containerdClient.EXPECT().
Pull(gomock.Any(), testImage).
Return(image, nil)
image.EXPECT().
Name().
Return(testImage).
Expand All @@ -403,9 +366,6 @@ func TestImageService_PullAndMount_failedRootFS(t *testing.T) {
LeasesService().
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
ImageName: testImage,
Expand Down Expand Up @@ -467,7 +427,7 @@ func TestImageService_PullAndMount_failedSnapshotCheck(t *testing.T) {
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Pull(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
Expand Down Expand Up @@ -537,7 +497,7 @@ func TestImageService_PullAndMount_failedPrepare(t *testing.T) {
Return(leasesManager).
Times(2)
containerdClient.EXPECT().
GetImage(gomock.Any(), testImage).
Pull(gomock.Any(), testImage).
Return(image, nil)

_, err := client.PullAndMount(ctx, &ports.ImageMountSpec{
Expand Down

0 comments on commit 48a1ae5

Please sign in to comment.