Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[24.0 backport] c8d: Make sure the content isn't removed while we export #46021

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 39 additions & 3 deletions daemon/containerd/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"io"

"github.com/containerd/containerd"
"github.com/containerd/containerd/content"
cerrdefs "github.com/containerd/containerd/errdefs"
containerdimages "github.com/containerd/containerd/images"
"github.com/containerd/containerd/images/archive"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/mount"
cplatforms "github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
Expand Down Expand Up @@ -57,18 +59,28 @@ func (i *ImageService) ExportImage(ctx context.Context, names []string, outStrea
archive.WithPlatform(platform),
}

ctx, release, err := i.client.WithLease(ctx)
contentStore := i.client.ContentStore()
leasesManager := i.client.LeasesService()
lease, err := leasesManager.Create(ctx, leases.WithRandomID())
if err != nil {
return errdefs.System(err)
}
defer release(ctx)
defer func() {
if err := leasesManager.Delete(ctx, lease); err != nil {
logrus.WithError(err).Warn("cleaning up lease")
Copy link
Member Author

@thaJeztah thaJeztah Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a minor conflict in the imports, due to this branch not yet using the containerd log-package; I decided to change this to logrus (instead of the log.G(ctx)

}
}()

for _, name := range names {
target, err := i.resolveDescriptor(ctx, name)
if err != nil {
return err
}

if err = leaseContent(ctx, contentStore, leasesManager, lease, target); err != nil {
return err
}

// We may not have locally all the platforms that are specified in the index.
// Export only those manifests that we have.
// TODO(vvoland): Reconsider this when `--platform` is added.
Expand Down Expand Up @@ -100,6 +112,30 @@ func (i *ImageService) ExportImage(ctx context.Context, names []string, outStrea
return i.client.Export(ctx, outStream, opts...)
}

// leaseContent will add a resource to the lease for each child of the descriptor making sure that it and
// its children won't be deleted while the lease exists
func leaseContent(ctx context.Context, store content.Store, leasesManager leases.Manager, lease leases.Lease, desc ocispec.Descriptor) error {
return containerdimages.Walk(ctx, containerdimages.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
_, err := store.Info(ctx, desc.Digest)
if err != nil {
if errors.Is(err, cerrdefs.ErrNotFound) {
return nil, nil
}
return nil, errdefs.System(err)
}

r := leases.Resource{
ID: desc.Digest.String(),
Type: "content",
}
if err := leasesManager.AddResource(ctx, lease, r); err != nil {
return nil, errdefs.System(err)
}

return containerdimages.Children(ctx, store, desc)
}), desc)
}

// LoadImage uploads a set of images into the repository. This is the
// complement of ExportImage. The input stream is an uncompressed tar
// ball containing images and metadata.
Expand All @@ -110,7 +146,7 @@ func (i *ImageService) LoadImage(ctx context.Context, inTar io.ReadCloser, outSt

// Create an additional image with dangling name for imported images...
containerd.WithDigestRef(danglingImageName),
/// ... but only if they don't have a name or it's invalid.
// / ... but only if they don't have a name or it's invalid.
containerd.WithSkipDigestRef(func(nameFromArchive string) bool {
if nameFromArchive == "" {
return false
Expand Down