2017-12-13 00:59:06 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-06-26 22:24:33 +00:00
|
|
|
"strconv"
|
2019-01-28 20:08:31 +00:00
|
|
|
"strings"
|
2017-12-13 00:59:06 +00:00
|
|
|
"time"
|
|
|
|
|
2019-08-01 20:27:50 +00:00
|
|
|
archiveexporter "github.com/containerd/containerd/images/archive"
|
2019-07-12 18:50:50 +00:00
|
|
|
"github.com/containerd/containerd/leases"
|
2017-12-18 00:20:19 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-12-13 00:59:06 +00:00
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/exporter/containerimage"
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/compression"
|
|
|
|
"github.com/moby/buildkit/util/contentutil"
|
2020-04-23 04:46:43 +00:00
|
|
|
"github.com/moby/buildkit/util/grpcerrors"
|
2019-07-12 18:50:50 +00:00
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2017-12-13 00:59:06 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-12-18 00:20:19 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
2019-07-30 00:50:54 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2017-12-13 00:59:06 +00:00
|
|
|
)
|
|
|
|
|
2017-12-18 00:20:19 +00:00
|
|
|
type ExporterVariant string
|
|
|
|
|
2017-12-13 00:59:06 +00:00
|
|
|
const (
|
2019-11-30 11:46:11 +00:00
|
|
|
keyImageName = "name"
|
|
|
|
keyLayerCompression = "compression"
|
|
|
|
VariantOCI = "oci"
|
|
|
|
VariantDocker = "docker"
|
|
|
|
ociTypes = "oci-mediatypes"
|
2017-12-13 00:59:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Opt struct {
|
|
|
|
SessionManager *session.Manager
|
|
|
|
ImageWriter *containerimage.ImageWriter
|
2017-12-18 00:20:19 +00:00
|
|
|
Variant ExporterVariant
|
2019-07-12 18:50:50 +00:00
|
|
|
LeaseManager leases.Manager
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type imageExporter struct {
|
|
|
|
opt Opt
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(opt Opt) (exporter.Exporter, error) {
|
|
|
|
im := &imageExporter{opt: opt}
|
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
|
2018-06-26 22:24:33 +00:00
|
|
|
var ot *bool
|
2019-11-30 11:46:11 +00:00
|
|
|
i := &imageExporterInstance{
|
|
|
|
imageExporter: e,
|
2020-05-28 20:46:33 +00:00
|
|
|
layerCompression: compression.Default,
|
2019-11-30 11:46:11 +00:00
|
|
|
}
|
2017-12-13 00:59:06 +00:00
|
|
|
for k, v := range opt {
|
|
|
|
switch k {
|
2017-12-18 00:20:19 +00:00
|
|
|
case keyImageName:
|
2018-08-30 13:52:19 +00:00
|
|
|
i.name = v
|
2019-11-30 11:46:11 +00:00
|
|
|
case keyLayerCompression:
|
|
|
|
switch v {
|
|
|
|
case "gzip":
|
2020-05-28 20:46:33 +00:00
|
|
|
i.layerCompression = compression.Gzip
|
2019-11-30 11:46:11 +00:00
|
|
|
case "uncompressed":
|
2020-05-28 20:46:33 +00:00
|
|
|
i.layerCompression = compression.Uncompressed
|
2019-11-30 11:46:11 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported layer compression type: %v", v)
|
|
|
|
}
|
2018-06-26 22:24:33 +00:00
|
|
|
case ociTypes:
|
|
|
|
ot = new(bool)
|
|
|
|
if v == "" {
|
|
|
|
*ot = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
|
|
|
}
|
|
|
|
*ot = b
|
2017-12-13 00:59:06 +00:00
|
|
|
default:
|
2018-07-13 18:28:36 +00:00
|
|
|
if i.meta == nil {
|
|
|
|
i.meta = make(map[string][]byte)
|
|
|
|
}
|
2018-08-06 09:23:46 +00:00
|
|
|
i.meta[k] = []byte(v)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 22:24:33 +00:00
|
|
|
if ot == nil {
|
|
|
|
i.ociTypes = e.opt.Variant == VariantOCI
|
|
|
|
} else {
|
|
|
|
i.ociTypes = *ot
|
|
|
|
}
|
2017-12-13 00:59:06 +00:00
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageExporterInstance struct {
|
|
|
|
*imageExporter
|
2019-11-30 11:46:11 +00:00
|
|
|
meta map[string][]byte
|
|
|
|
name string
|
|
|
|
ociTypes bool
|
2020-05-28 20:46:33 +00:00
|
|
|
layerCompression compression.Type
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *imageExporterInstance) Name() string {
|
|
|
|
return "exporting to oci image format"
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source, sessionID string) (map[string]string, error) {
|
2018-07-13 18:28:36 +00:00
|
|
|
if e.opt.Variant == VariantDocker && len(src.Refs) > 0 {
|
|
|
|
return nil, errors.Errorf("docker exporter does not currently support exporting manifest lists")
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:52:21 +00:00
|
|
|
if src.Metadata == nil {
|
|
|
|
src.Metadata = make(map[string][]byte)
|
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
for k, v := range e.meta {
|
|
|
|
src.Metadata[k] = v
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2018-07-13 18:28:36 +00:00
|
|
|
|
2019-10-03 21:11:54 +00:00
|
|
|
ctx, done, err := leaseutil.WithLease(ctx, e.opt.LeaseManager, leaseutil.MakeTemporary)
|
2019-07-12 18:50:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer done(context.TODO())
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes, e.layerCompression, sessionID)
|
2017-12-13 00:59:06 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2017-12-28 19:56:04 +00:00
|
|
|
defer func() {
|
|
|
|
e.opt.ImageWriter.ContentStore().Delete(context.TODO(), desc.Digest)
|
|
|
|
}()
|
2017-12-18 00:20:19 +00:00
|
|
|
if desc.Annotations == nil {
|
|
|
|
desc.Annotations = map[string]string{}
|
|
|
|
}
|
|
|
|
desc.Annotations[ocispec.AnnotationCreated] = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
|
2018-08-31 09:54:41 +00:00
|
|
|
resp := make(map[string]string)
|
2019-07-30 00:50:54 +00:00
|
|
|
resp["containerimage.digest"] = desc.Digest.String()
|
2020-12-14 21:25:34 +00:00
|
|
|
if v, ok := desc.Annotations["config.digest"]; ok {
|
|
|
|
resp["containerimage.config.digest"] = v
|
|
|
|
delete(desc.Annotations, "config.digeest")
|
|
|
|
}
|
2018-08-31 09:54:41 +00:00
|
|
|
|
2018-08-31 16:34:49 +00:00
|
|
|
if n, ok := src.Metadata["image.name"]; e.name == "*" && ok {
|
2019-01-28 20:08:31 +00:00
|
|
|
e.name = string(n)
|
2018-08-15 15:11:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:23:05 +00:00
|
|
|
names, err := normalizedNames(e.name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(names) != 0 {
|
|
|
|
resp["image.name"] = strings.Join(names, ",")
|
2018-08-31 09:54:41 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:04:10 +00:00
|
|
|
expOpts := []archiveexporter.ExportOpt{archiveexporter.WithManifest(*desc, names...)}
|
2019-08-01 20:27:50 +00:00
|
|
|
switch e.opt.Variant {
|
|
|
|
case VariantOCI:
|
|
|
|
expOpts = append(expOpts, archiveexporter.WithAllPlatforms(), archiveexporter.WithSkipDockerManifest())
|
|
|
|
case VariantDocker:
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid variant %q", e.opt.Variant)
|
2017-12-18 00:20:19 +00:00
|
|
|
}
|
2017-12-13 00:59:06 +00:00
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-08-11 00:11:24 +00:00
|
|
|
caller, err := e.opt.SessionManager.Get(timeoutCtx, sessionID, false)
|
2020-06-30 01:06:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := filesync.CopyFileWriter(ctx, resp, caller)
|
2017-12-13 00:59:06 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
|
|
|
|
mprovider := contentutil.NewMultiProvider(e.opt.ImageWriter.ContentStore())
|
|
|
|
if src.Ref != nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
remote, err := src.Ref.GetRemote(ctx, false, e.layerCompression, session.NewGroup(sessionID))
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// unlazy before tar export as the tar writer does not handle
|
|
|
|
// layer blobs in parallel (whereas unlazy does)
|
|
|
|
if unlazier, ok := remote.Provider.(cache.Unlazier); ok {
|
|
|
|
if err := unlazier.Unlazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, desc := range remote.Descriptors {
|
|
|
|
mprovider.Add(desc.Digest, remote.Provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(src.Refs) > 0 {
|
|
|
|
for _, r := range src.Refs {
|
2020-10-27 06:13:39 +00:00
|
|
|
remote, err := r.GetRemote(ctx, false, e.layerCompression, session.NewGroup(sessionID))
|
2020-05-28 20:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if unlazier, ok := remote.Provider.(cache.Unlazier); ok {
|
|
|
|
if err := unlazier.Unlazy(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, desc := range remote.Descriptors {
|
|
|
|
mprovider.Add(desc.Digest, remote.Provider)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 00:59:06 +00:00
|
|
|
report := oneOffProgress(ctx, "sending tarball")
|
2020-05-28 20:46:33 +00:00
|
|
|
if err := archiveexporter.Export(ctx, mprovider, w, expOpts...); err != nil {
|
2017-12-13 00:59:06 +00:00
|
|
|
w.Close()
|
2020-04-23 04:46:43 +00:00
|
|
|
if grpcerrors.Code(err) == codes.AlreadyExists {
|
2019-07-30 00:50:54 +00:00
|
|
|
return resp, report(nil)
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, report(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2019-07-30 00:50:54 +00:00
|
|
|
err = w.Close()
|
2020-04-23 04:46:43 +00:00
|
|
|
if grpcerrors.Code(err) == codes.AlreadyExists {
|
2019-07-30 00:50:54 +00:00
|
|
|
return resp, report(nil)
|
|
|
|
}
|
|
|
|
return resp, report(err)
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func oneOffProgress(ctx context.Context, id string) func(err error) error {
|
|
|
|
pw, _, _ := progress.FromContext(ctx)
|
|
|
|
now := time.Now()
|
|
|
|
st := progress.Status{
|
|
|
|
Started: &now,
|
|
|
|
}
|
|
|
|
pw.Write(id, st)
|
|
|
|
return func(err error) error {
|
|
|
|
// TODO: set error on status
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
pw.Write(id, st)
|
|
|
|
pw.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 00:20:19 +00:00
|
|
|
|
2019-03-19 00:23:05 +00:00
|
|
|
func normalizedNames(name string) ([]string, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
names := strings.Split(name, ",")
|
|
|
|
var tagNames = make([]string, len(names))
|
|
|
|
for i, name := range names {
|
|
|
|
parsed, err := reference.ParseNormalizedNamed(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to parse %s", name)
|
|
|
|
}
|
|
|
|
tagNames[i] = reference.TagNameOnly(parsed).String()
|
|
|
|
}
|
|
|
|
return tagNames, nil
|
|
|
|
}
|