2020-05-28 20:46:33 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-21 22:53:16 +00:00
|
|
|
"fmt"
|
2021-07-26 02:00:48 +00:00
|
|
|
"io"
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/containerd/containerd/diff"
|
|
|
|
"github.com/containerd/containerd/leases"
|
|
|
|
"github.com/containerd/containerd/mount"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/compression"
|
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
|
|
|
"github.com/moby/buildkit/util/winlayers"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
imagespecidentity "github.com/opencontainers/image-spec/identity"
|
2021-07-26 08:53:30 +00:00
|
|
|
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
var g flightcontrol.Group
|
|
|
|
|
|
|
|
const containerdUncompressed = "containerd.io/uncompressed"
|
|
|
|
|
|
|
|
var ErrNoBlobs = errors.Errorf("no blobs for snapshot")
|
|
|
|
|
|
|
|
// computeBlobChain ensures every ref in a parent chain has an associated blob in the content store. If
|
|
|
|
// a blob is missing and createIfNeeded is true, then the blob will be created, otherwise ErrNoBlobs will
|
|
|
|
// be returned. Caller must hold a lease when calling this function.
|
2021-03-30 12:59:03 +00:00
|
|
|
// If forceCompression is specified but the blob of compressionType doesn't exist, this function creates it.
|
|
|
|
func (sr *immutableRef) computeBlobChain(ctx context.Context, createIfNeeded bool, compressionType compression.Type, forceCompression bool, s session.Group) error {
|
2020-05-28 20:46:33 +00:00
|
|
|
if _, ok := leases.FromContext(ctx); !ok {
|
|
|
|
return errors.Errorf("missing lease requirement for computeBlobChain")
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:50:54 +00:00
|
|
|
if err := sr.finalizeLocked(ctx); err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTypeWindows(sr) {
|
|
|
|
ctx = winlayers.UseWindowsLayerMode(ctx)
|
|
|
|
}
|
|
|
|
|
2021-03-30 12:59:03 +00:00
|
|
|
return computeBlobChain(ctx, sr, createIfNeeded, compressionType, forceCompression, s)
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
type compressor func(dest io.Writer, requiredMediaType string) (io.WriteCloser, error)
|
|
|
|
|
2021-03-30 12:59:03 +00:00
|
|
|
func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool, compressionType compression.Type, forceCompression bool, s session.Group) error {
|
2020-05-28 20:46:33 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
if sr.parent != nil {
|
|
|
|
eg.Go(func() error {
|
2021-03-30 12:59:03 +00:00
|
|
|
return computeBlobChain(ctx, sr.parent, createIfNeeded, compressionType, forceCompression, s)
|
2020-05-28 20:46:33 +00:00
|
|
|
})
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
eg.Go(func() error {
|
2021-07-26 02:00:48 +00:00
|
|
|
_, err := g.Do(ctx, fmt.Sprintf("%s-%t", sr.ID(), createIfNeeded), func(ctx context.Context) (interface{}, error) {
|
2021-07-21 22:53:16 +00:00
|
|
|
if getBlob(sr.md) != "" {
|
2021-07-26 02:00:48 +00:00
|
|
|
return nil, nil
|
2021-07-21 22:53:16 +00:00
|
|
|
}
|
|
|
|
if !createIfNeeded {
|
2020-05-28 20:46:33 +00:00
|
|
|
return nil, errors.WithStack(ErrNoBlobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mediaType string
|
2021-07-26 02:00:48 +00:00
|
|
|
var compressorFunc compressor
|
|
|
|
var finalize func(context.Context, content.Store) (map[string]string, error)
|
2020-05-28 20:46:33 +00:00
|
|
|
switch compressionType {
|
|
|
|
case compression.Uncompressed:
|
2021-07-26 08:53:30 +00:00
|
|
|
mediaType = ocispecs.MediaTypeImageLayer
|
2020-05-28 20:46:33 +00:00
|
|
|
case compression.Gzip:
|
2021-07-26 08:53:30 +00:00
|
|
|
mediaType = ocispecs.MediaTypeImageLayerGzip
|
2021-07-26 02:00:48 +00:00
|
|
|
case compression.EStargz:
|
|
|
|
compressorFunc, finalize = writeEStargz()
|
|
|
|
mediaType = ocispecs.MediaTypeImageLayerGzip
|
2020-05-28 20:46:33 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown layer compression type: %q", compressionType)
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
var lower []mount.Mount
|
|
|
|
if sr.parent != nil {
|
|
|
|
m, err := sr.parent.Mount(ctx, true, s)
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
var release func() error
|
|
|
|
lower, release, err = m.Mount()
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if release != nil {
|
|
|
|
defer release()
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
}
|
|
|
|
m, err := sr.Mount(ctx, true, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
upper, release, err := m.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if release != nil {
|
|
|
|
defer release()
|
|
|
|
}
|
|
|
|
desc, err := sr.cm.Differ.Compare(ctx, lower, upper,
|
|
|
|
diff.WithMediaType(mediaType),
|
|
|
|
diff.WithReference(sr.ID()),
|
2021-07-26 02:00:48 +00:00
|
|
|
diff.WithCompressor(compressorFunc),
|
2021-07-21 22:53:16 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
if desc.Annotations == nil {
|
|
|
|
desc.Annotations = map[string]string{}
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
2021-07-26 02:00:48 +00:00
|
|
|
if finalize != nil {
|
|
|
|
a, err := finalize(ctx, sr.cm.ContentStore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to finalize compression")
|
|
|
|
}
|
|
|
|
for k, v := range a {
|
|
|
|
desc.Annotations[k] = v
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
info, err := sr.cm.ContentStore.Info(ctx, desc.Digest)
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if diffID, ok := info.Labels[containerdUncompressed]; ok {
|
2021-07-21 22:53:16 +00:00
|
|
|
desc.Annotations[containerdUncompressed] = diffID
|
|
|
|
} else if mediaType == ocispecs.MediaTypeImageLayer {
|
|
|
|
desc.Annotations[containerdUncompressed] = desc.Digest.String()
|
2020-05-28 20:46:33 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.Errorf("unknown layer compression type")
|
|
|
|
}
|
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
if err := sr.setBlob(ctx, compressionType, desc); err != nil {
|
2021-07-21 22:53:16 +00:00
|
|
|
return nil, err
|
2021-03-30 12:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
return nil, nil
|
2020-05-28 20:46:33 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
if forceCompression {
|
2021-07-26 02:00:48 +00:00
|
|
|
if err := ensureCompression(ctx, sr, compressionType, s); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to ensure compression type of %q", compressionType)
|
2021-07-21 22:53:16 +00:00
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-07-21 22:53:16 +00:00
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
return sr.setChains(ctx)
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setBlob associates a blob with the cache record.
|
|
|
|
// A lease must be held for the blob when calling this function
|
|
|
|
// Caller should call Info() for knowing what current values are actually set
|
2021-07-26 02:00:48 +00:00
|
|
|
func (sr *immutableRef) setBlob(ctx context.Context, compressionType compression.Type, desc ocispecs.Descriptor) error {
|
2020-05-28 20:46:33 +00:00
|
|
|
if _, ok := leases.FromContext(ctx); !ok {
|
|
|
|
return errors.Errorf("missing lease requirement for setBlob")
|
|
|
|
}
|
|
|
|
|
|
|
|
diffID, err := diffIDFromDescriptor(desc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := sr.cm.ContentStore.Info(ctx, desc.Digest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
if compressionType == compression.UnknownCompression {
|
|
|
|
return errors.Errorf("unhandled layer media type: %q", desc.MediaType)
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
if getBlob(sr.md) != "" {
|
2020-05-28 20:46:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-01 02:50:54 +00:00
|
|
|
if err := sr.finalize(ctx); err != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sr.cm.LeaseManager.AddResource(ctx, leases.Lease{ID: sr.ID()}, leases.Resource{
|
|
|
|
ID: desc.Digest.String(),
|
|
|
|
Type: "content",
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queueDiffID(sr.md, diffID.String())
|
|
|
|
queueBlob(sr.md, desc.Digest.String())
|
2021-07-21 22:53:16 +00:00
|
|
|
queueMediaType(sr.md, desc.MediaType)
|
|
|
|
queueBlobSize(sr.md, desc.Size)
|
|
|
|
if err := sr.md.Commit(); err != nil {
|
|
|
|
return err
|
2020-05-28 20:46:33 +00:00
|
|
|
}
|
2021-07-21 22:53:16 +00:00
|
|
|
|
2021-07-26 02:00:48 +00:00
|
|
|
if err := sr.addCompressionBlob(ctx, desc, compressionType); err != nil {
|
2021-07-21 22:53:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sr *immutableRef) setChains(ctx context.Context) error {
|
|
|
|
if _, ok := leases.FromContext(ctx); !ok {
|
|
|
|
return errors.Errorf("missing lease requirement for setChains")
|
|
|
|
}
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
|
|
|
if getChainID(sr.md) != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainIDs []digest.Digest
|
|
|
|
var blobChainIDs []digest.Digest
|
|
|
|
if sr.parent != nil {
|
|
|
|
chainIDs = append(chainIDs, digest.Digest(getChainID(sr.parent.md)))
|
|
|
|
blobChainIDs = append(blobChainIDs, digest.Digest(getBlobChainID(sr.parent.md)))
|
|
|
|
}
|
|
|
|
diffID := digest.Digest(getDiffID(sr.md))
|
|
|
|
chainIDs = append(chainIDs, diffID)
|
|
|
|
blobChainIDs = append(blobChainIDs, imagespecidentity.ChainID([]digest.Digest{digest.Digest(getBlob(sr.md)), diffID}))
|
|
|
|
|
|
|
|
chainID := imagespecidentity.ChainID(chainIDs)
|
|
|
|
blobChainID := imagespecidentity.ChainID(blobChainIDs)
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
queueChainID(sr.md, chainID.String())
|
|
|
|
queueBlobChainID(sr.md, blobChainID.String())
|
|
|
|
if err := sr.md.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTypeWindows(sr *immutableRef) bool {
|
|
|
|
if GetLayerType(sr) == "windows" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if parent := sr.parent; parent != nil {
|
|
|
|
return isTypeWindows(parent)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-03-30 12:59:03 +00:00
|
|
|
|
|
|
|
// ensureCompression ensures the specified ref has the blob of the specified compression Type.
|
2021-07-26 02:00:48 +00:00
|
|
|
func ensureCompression(ctx context.Context, ref *immutableRef, compressionType compression.Type, s session.Group) error {
|
|
|
|
_, err := g.Do(ctx, fmt.Sprintf("%s-%d", ref.ID(), compressionType), func(ctx context.Context) (interface{}, error) {
|
|
|
|
desc, err := ref.ociDesc(ctx, ref.descHandlers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
// Resolve converters
|
2021-07-26 02:00:48 +00:00
|
|
|
layerConvertFunc, err := getConverter(desc, compressionType)
|
2021-07-21 22:53:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if layerConvertFunc == nil {
|
2021-07-26 02:00:48 +00:00
|
|
|
if isLazy, err := ref.isLazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if isLazy {
|
|
|
|
// This ref can be used as the specified compressionType. Keep it lazy.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, ref.addCompressionBlob(ctx, desc, compressionType)
|
2021-07-21 22:53:16 +00:00
|
|
|
}
|
2021-03-30 12:59:03 +00:00
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
// First, lookup local content store
|
|
|
|
if _, err := ref.getCompressionBlob(ctx, compressionType); err == nil {
|
|
|
|
return nil, nil // found the compression variant. no need to convert.
|
|
|
|
}
|
2021-03-30 12:59:03 +00:00
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
// Convert layer compression type
|
|
|
|
if err := (lazyRefProvider{
|
|
|
|
ref: ref,
|
|
|
|
desc: desc,
|
|
|
|
dh: ref.descHandlers[desc.Digest],
|
|
|
|
session: s,
|
|
|
|
}).Unlazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
newDesc, err := layerConvertFunc(ctx, ref.cm.ContentStore, desc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-30 12:59:03 +00:00
|
|
|
|
2021-07-21 22:53:16 +00:00
|
|
|
// Start to track converted layer
|
2021-07-26 02:00:48 +00:00
|
|
|
if err := ref.addCompressionBlob(ctx, *newDesc, compressionType); err != nil {
|
2021-07-21 22:53:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
return err
|
2021-03-30 12:59:03 +00:00
|
|
|
}
|