2017-05-27 05:10:26 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-04-24 21:00:58 +00:00
|
|
|
"encoding/json"
|
2018-07-15 00:07:39 +00:00
|
|
|
"runtime"
|
2017-05-27 05:10:26 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2017-11-06 09:44:23 +00:00
|
|
|
"github.com/containerd/containerd/diff"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2019-05-21 18:32:21 +00:00
|
|
|
"github.com/containerd/containerd/leases"
|
2018-06-24 05:52:19 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2018-04-24 21:00:58 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2018-07-25 06:18:21 +00:00
|
|
|
gw "github.com/moby/buildkit/frontend/gateway/client"
|
2017-10-15 06:49:55 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-12-28 07:07:13 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/source"
|
2017-12-12 21:58:12 +00:00
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2017-09-05 18:26:03 +00:00
|
|
|
"github.com/moby/buildkit/util/imageutil"
|
2018-05-17 20:09:11 +00:00
|
|
|
"github.com/moby/buildkit/util/pull"
|
2018-09-07 20:45:59 +00:00
|
|
|
"github.com/moby/buildkit/util/resolver"
|
2018-07-15 00:07:39 +00:00
|
|
|
"github.com/moby/buildkit/util/winlayers"
|
2017-05-31 22:36:04 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/opencontainers/image-spec/identity"
|
2018-06-24 05:52:19 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: break apart containerd specifics like contentstore so the resolver
|
|
|
|
// code can be used with any implementation
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
type SourceOpt struct {
|
2019-02-23 12:56:04 +00:00
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
|
|
|
Applier diff.Applier
|
|
|
|
CacheAccessor cache.Accessor
|
|
|
|
ImageStore images.Store // optional
|
|
|
|
ResolverOpt resolver.ResolveOptionsFunc
|
2019-05-21 18:32:21 +00:00
|
|
|
LeaseManager leases.Manager
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type imageSource struct {
|
2017-05-27 06:12:13 +00:00
|
|
|
SourceOpt
|
2017-12-12 21:58:12 +00:00
|
|
|
g flightcontrol.Group
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
func NewSource(opt SourceOpt) (source.Source, error) {
|
2017-05-27 05:10:26 +00:00
|
|
|
is := &imageSource{
|
2017-05-27 06:12:13 +00:00
|
|
|
SourceOpt: opt,
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
2017-05-31 22:36:04 +00:00
|
|
|
|
2017-05-27 05:10:26 +00:00
|
|
|
return is, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *imageSource) ID() string {
|
2017-05-27 06:12:13 +00:00
|
|
|
return source.DockerImageScheme
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 12:56:04 +00:00
|
|
|
func (is *imageSource) ResolveImageConfig(ctx context.Context, ref string, opt gw.ResolveImageConfigOpt, sm *session.Manager) (digest.Digest, []byte, error) {
|
2017-12-12 21:58:12 +00:00
|
|
|
type t struct {
|
|
|
|
dgst digest.Digest
|
|
|
|
dt []byte
|
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
key := ref
|
2018-07-25 06:18:21 +00:00
|
|
|
if platform := opt.Platform; platform != nil {
|
2018-07-13 18:28:36 +00:00
|
|
|
key += platforms.Format(*platform)
|
|
|
|
}
|
2018-07-25 06:18:21 +00:00
|
|
|
|
|
|
|
rm, err := source.ParseImageResolveMode(opt.ResolveMode)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-13 18:28:36 +00:00
|
|
|
res, err := is.g.Do(ctx, key, func(ctx context.Context) (interface{}, error) {
|
2019-02-23 12:56:04 +00:00
|
|
|
dgst, dt, err := imageutil.Config(ctx, ref, pull.NewResolver(ctx, is.ResolverOpt, sm, is.ImageStore, rm, ref), is.ContentStore, opt.Platform)
|
2017-12-12 21:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &t{dgst: dgst, dt: dt}, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
typed := res.(*t)
|
|
|
|
return typed.dgst, typed.dt, nil
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2019-02-23 12:56:04 +00:00
|
|
|
func (is *imageSource) Resolve(ctx context.Context, id source.Identifier, sm *session.Manager) (source.SourceInstance, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
imageIdentifier, ok := id.(*source.ImageIdentifier)
|
2017-05-27 05:10:26 +00:00
|
|
|
if !ok {
|
2017-07-07 21:35:10 +00:00
|
|
|
return nil, errors.Errorf("invalid image identifier %v", id)
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
platform := platforms.DefaultSpec()
|
|
|
|
if imageIdentifier.Platform != nil {
|
|
|
|
platform = *imageIdentifier.Platform
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:09:11 +00:00
|
|
|
pullerUtil := &pull.Puller{
|
|
|
|
Snapshotter: is.Snapshotter,
|
|
|
|
ContentStore: is.ContentStore,
|
|
|
|
Applier: is.Applier,
|
|
|
|
Src: imageIdentifier.Reference,
|
2019-02-23 12:56:04 +00:00
|
|
|
Resolver: pull.NewResolver(ctx, is.ResolverOpt, sm, is.ImageStore, imageIdentifier.ResolveMode, imageIdentifier.Reference.String()),
|
2018-06-24 05:52:19 +00:00
|
|
|
Platform: &platform,
|
2019-05-21 18:32:21 +00:00
|
|
|
LeaseManager: is.LeaseManager,
|
2018-05-17 20:09:11 +00:00
|
|
|
}
|
2017-07-06 19:07:42 +00:00
|
|
|
p := &puller{
|
2018-05-17 20:09:11 +00:00
|
|
|
CacheAccessor: is.CacheAccessor,
|
|
|
|
Puller: pullerUtil,
|
2018-06-24 05:52:19 +00:00
|
|
|
Platform: platform,
|
2018-07-26 20:51:28 +00:00
|
|
|
id: imageIdentifier,
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:26:03 +00:00
|
|
|
type puller struct {
|
2018-05-17 20:09:11 +00:00
|
|
|
CacheAccessor cache.Accessor
|
2018-06-24 05:52:19 +00:00
|
|
|
Platform specs.Platform
|
2018-07-26 20:51:28 +00:00
|
|
|
id *source.ImageIdentifier
|
2018-05-17 20:09:11 +00:00
|
|
|
*pull.Puller
|
2017-09-05 18:26:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
func mainManifestKey(ctx context.Context, desc specs.Descriptor, platform specs.Platform) (digest.Digest, error) {
|
2018-04-24 21:00:58 +00:00
|
|
|
dt, err := json.Marshal(struct {
|
2018-06-24 05:52:19 +00:00
|
|
|
Digest digest.Digest
|
|
|
|
OS string
|
|
|
|
Arch string
|
|
|
|
Variant string `json:",omitempty"`
|
2018-04-24 21:00:58 +00:00
|
|
|
}{
|
2018-06-24 05:52:19 +00:00
|
|
|
Digest: desc.Digest,
|
|
|
|
OS: platform.OS,
|
|
|
|
Arch: platform.Architecture,
|
|
|
|
Variant: platform.Variant,
|
2018-04-24 21:00:58 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return digest.FromBytes(dt), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) CacheKey(ctx context.Context, index int) (string, bool, error) {
|
2018-05-17 20:09:11 +00:00
|
|
|
_, desc, err := p.Puller.Resolve(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
if index == 0 || desc.Digest == "" {
|
2018-06-24 05:52:19 +00:00
|
|
|
k, err := mainManifestKey(ctx, desc, p.Platform)
|
2018-04-24 21:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
return k.String(), false, nil
|
|
|
|
}
|
2018-05-17 20:09:11 +00:00
|
|
|
ref, err := reference.ParseNormalizedNamed(p.Src.String())
|
2018-04-24 21:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
2018-05-17 20:09:11 +00:00
|
|
|
ref, err = reference.WithDigest(ref, desc.Digest)
|
2018-04-24 21:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
2018-07-16 17:04:31 +00:00
|
|
|
_, dt, err := imageutil.Config(ctx, ref.String(), p.Resolver, p.ContentStore, &p.Platform)
|
2018-04-24 21:00:58 +00:00
|
|
|
if err != nil {
|
2019-02-26 01:16:12 +00:00
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k := cacheKeyFromConfig(dt).String()
|
|
|
|
if k == "" {
|
2018-06-24 05:52:19 +00:00
|
|
|
k, err := mainManifestKey(ctx, desc, p.Platform)
|
2018-04-24 21:00:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
return k.String(), true, nil
|
|
|
|
}
|
2019-02-26 01:16:12 +00:00
|
|
|
return k, true, nil
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) Snapshot(ctx context.Context) (cache.ImmutableRef, error) {
|
2018-07-15 00:07:39 +00:00
|
|
|
layerNeedsTypeWindows := false
|
|
|
|
if platform := p.Puller.Platform; platform != nil {
|
|
|
|
if platform.OS == "windows" && runtime.GOOS != "windows" {
|
|
|
|
ctx = winlayers.UseWindowsLayerMode(ctx)
|
|
|
|
layerNeedsTypeWindows = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:09:11 +00:00
|
|
|
pulled, err := p.Puller.Pull(ctx)
|
2017-05-27 05:10:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-22 21:49:21 +00:00
|
|
|
if pulled.ChainID == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-07-15 00:07:39 +00:00
|
|
|
ref, err := p.CacheAccessor.GetFromSnapshotter(ctx, string(pulled.ChainID), cache.WithDescription("pulled from "+pulled.Ref))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if layerNeedsTypeWindows && ref != nil {
|
|
|
|
if err := markRefLayerTypeWindows(ref); err != nil {
|
|
|
|
ref.Release(context.TODO())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 20:51:28 +00:00
|
|
|
|
|
|
|
if p.id.RecordType != "" && cache.GetRecordType(ref) == "" {
|
|
|
|
if err := cache.SetRecordType(ref, p.id.RecordType); err != nil {
|
|
|
|
ref.Release(context.TODO())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 00:07:39 +00:00
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func markRefLayerTypeWindows(ref cache.ImmutableRef) error {
|
|
|
|
if parent := ref.Parent(); parent != nil {
|
|
|
|
defer parent.Release(context.TODO())
|
|
|
|
if err := markRefLayerTypeWindows(parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cache.SetLayerType(ref, "windows")
|
2017-06-15 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 21:00:58 +00:00
|
|
|
// cacheKeyFromConfig returns a stable digest from image config. If image config
|
|
|
|
// is a known oci image we will use chainID of layers.
|
|
|
|
func cacheKeyFromConfig(dt []byte) digest.Digest {
|
2018-06-24 05:52:19 +00:00
|
|
|
var img specs.Image
|
2018-04-24 21:00:58 +00:00
|
|
|
err := json.Unmarshal(dt, &img)
|
|
|
|
if err != nil {
|
|
|
|
return digest.FromBytes(dt)
|
|
|
|
}
|
2019-02-26 01:16:12 +00:00
|
|
|
if img.RootFS.Type != "layers" || len(img.RootFS.DiffIDs) == 0 {
|
|
|
|
return ""
|
2018-04-24 21:00:58 +00:00
|
|
|
}
|
|
|
|
return identity.ChainID(img.RootFS.DiffIDs)
|
|
|
|
}
|