2017-10-12 18:24:15 +00:00
|
|
|
package blobs
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-11-07 23:04:16 +00:00
|
|
|
"time"
|
2017-10-12 18:24:15 +00:00
|
|
|
|
2017-11-07 23:04:16 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2017-11-06 09:44:23 +00:00
|
|
|
"github.com/containerd/containerd/diff"
|
2017-10-12 18:24:15 +00:00
|
|
|
"github.com/containerd/containerd/mount"
|
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2018-07-15 00:07:39 +00:00
|
|
|
"github.com/moby/buildkit/util/winlayers"
|
2017-10-12 18:24:15 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
var g flightcontrol.Group
|
|
|
|
|
2017-11-07 23:04:16 +00:00
|
|
|
const containerdUncompressed = "containerd.io/uncompressed"
|
|
|
|
|
2017-10-12 18:24:15 +00:00
|
|
|
type DiffPair struct {
|
|
|
|
DiffID digest.Digest
|
|
|
|
Blobsum digest.Digest
|
|
|
|
}
|
|
|
|
|
2018-05-08 21:05:08 +00:00
|
|
|
var ErrNoBlobs = errors.Errorf("no blobs for snapshot")
|
|
|
|
|
|
|
|
func GetDiffPairs(ctx context.Context, contentStore content.Store, snapshotter snapshot.Snapshotter, differ diff.Comparer, ref cache.ImmutableRef, createBlobs bool) ([]DiffPair, error) {
|
2017-12-04 22:58:00 +00:00
|
|
|
if ref == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2017-10-12 18:24:15 +00:00
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
if err := ref.Finalize(ctx, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-15 00:07:39 +00:00
|
|
|
if isTypeWindows(ref) {
|
|
|
|
ctx = winlayers.UseWindowsLayerMode(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return getDiffPairs(ctx, contentStore, snapshotter, differ, ref, createBlobs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDiffPairs(ctx context.Context, contentStore content.Store, snapshotter snapshot.Snapshotter, differ diff.Comparer, ref cache.ImmutableRef, createBlobs bool) ([]DiffPair, error) {
|
|
|
|
if ref == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-10-12 18:24:15 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
var diffPairs []DiffPair
|
|
|
|
var currentPair DiffPair
|
|
|
|
parent := ref.Parent()
|
|
|
|
if parent != nil {
|
|
|
|
defer parent.Release(context.TODO())
|
|
|
|
eg.Go(func() error {
|
2018-07-15 00:07:39 +00:00
|
|
|
dp, err := getDiffPairs(ctx, contentStore, snapshotter, differ, parent, createBlobs)
|
2017-10-12 18:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
diffPairs = dp
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
eg.Go(func() error {
|
|
|
|
dp, err := g.Do(ctx, ref.ID(), func(ctx context.Context) (interface{}, error) {
|
2017-12-28 07:07:13 +00:00
|
|
|
diffID, blob, err := snapshotter.GetBlob(ctx, ref.ID())
|
2017-10-12 18:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if blob != "" {
|
|
|
|
return DiffPair{DiffID: diffID, Blobsum: blob}, nil
|
2018-05-08 21:05:08 +00:00
|
|
|
} else if !createBlobs {
|
|
|
|
return nil, errors.WithStack(ErrNoBlobs)
|
2017-10-12 18:24:15 +00:00
|
|
|
}
|
|
|
|
// reference needs to be committed
|
|
|
|
parent := ref.Parent()
|
|
|
|
var lower []mount.Mount
|
|
|
|
if parent != nil {
|
|
|
|
defer parent.Release(context.TODO())
|
2018-04-16 22:23:10 +00:00
|
|
|
m, err := parent.Mount(ctx, true)
|
2017-10-12 18:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 22:23:10 +00:00
|
|
|
lower, err = m.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer m.Release()
|
|
|
|
}
|
|
|
|
m, err := ref.Mount(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-12 18:24:15 +00:00
|
|
|
}
|
2018-04-16 22:23:10 +00:00
|
|
|
upper, err := m.Mount()
|
2017-10-12 18:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 22:23:10 +00:00
|
|
|
defer m.Release()
|
2018-02-09 19:39:48 +00:00
|
|
|
descr, err := differ.Compare(ctx, lower, upper,
|
2017-11-07 23:04:16 +00:00
|
|
|
diff.WithMediaType(ocispec.MediaTypeImageLayerGzip),
|
|
|
|
diff.WithReference(ref.ID()),
|
|
|
|
diff.WithLabels(map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
info, err := contentStore.Info(ctx, descr.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
diffIDStr, ok := info.Labels[containerdUncompressed]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid differ response with no diffID")
|
|
|
|
}
|
|
|
|
diffIDDigest, err := digest.Parse(diffIDStr)
|
2017-10-12 18:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-28 07:07:13 +00:00
|
|
|
if err := snapshotter.SetBlob(ctx, ref.ID(), diffIDDigest, descr.Digest); err != nil {
|
2017-10-12 18:24:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-07 23:04:16 +00:00
|
|
|
return DiffPair{DiffID: diffIDDigest, Blobsum: descr.Digest}, nil
|
2017-10-12 18:24:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentPair = dp.(DiffPair)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
err := eg.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return append(diffPairs, currentPair), nil
|
|
|
|
}
|
2018-07-15 00:07:39 +00:00
|
|
|
|
|
|
|
func isTypeWindows(ref cache.ImmutableRef) bool {
|
|
|
|
if cache.GetLayerType(ref) == "windows" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if parent := ref.Parent(); parent != nil {
|
|
|
|
defer parent.Release(context.TODO())
|
|
|
|
return isTypeWindows(parent)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|