2017-07-10 20:03:38 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"runtime"
|
2017-07-11 00:56:04 +00:00
|
|
|
"time"
|
2017-07-10 20:03:38 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/content"
|
2017-07-13 06:15:12 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
|
|
|
"github.com/containerd/containerd/rootfs"
|
2017-10-13 17:08:47 +00:00
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-10-12 18:24:15 +00:00
|
|
|
"github.com/moby/buildkit/cache/blobs"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/moby/buildkit/exporter"
|
2017-10-15 06:49:55 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2017-07-11 00:56:04 +00:00
|
|
|
"github.com/moby/buildkit/util/progress"
|
2017-10-13 17:08:47 +00:00
|
|
|
"github.com/moby/buildkit/util/push"
|
2017-07-10 20:03:38 +00:00
|
|
|
"github.com/moby/buildkit/util/system"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-10 20:03:38 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-11 05:35:07 +00:00
|
|
|
keyImageName = "name"
|
2017-10-13 17:08:47 +00:00
|
|
|
keyPush = "push"
|
2017-09-11 05:35:07 +00:00
|
|
|
exporterImageConfig = "containerimage.config"
|
2017-07-10 20:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Opt struct {
|
2017-10-15 06:49:55 +00:00
|
|
|
SessionManager *session.Manager
|
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
ContentStore content.Store
|
|
|
|
Differ rootfs.MountDiffer
|
|
|
|
Images images.Store
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type imageExporter struct {
|
2017-10-12 18:24:15 +00:00
|
|
|
opt Opt
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(opt Opt) (exporter.Exporter, error) {
|
2017-10-12 18:24:15 +00:00
|
|
|
im := &imageExporter{opt: opt}
|
2017-07-10 20:03:38 +00:00
|
|
|
return im, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exporter.ExporterInstance, error) {
|
|
|
|
i := &imageExporterInstance{imageExporter: e}
|
|
|
|
for k, v := range opt {
|
|
|
|
switch k {
|
|
|
|
case keyImageName:
|
|
|
|
i.targetName = v
|
2017-10-13 17:08:47 +00:00
|
|
|
case keyPush:
|
|
|
|
i.push = true
|
2017-07-10 20:03:38 +00:00
|
|
|
default:
|
|
|
|
logrus.Warnf("unknown exporter option %s", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageExporterInstance struct {
|
|
|
|
*imageExporter
|
|
|
|
targetName string
|
2017-10-13 17:08:47 +00:00
|
|
|
push bool
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 00:56:04 +00:00
|
|
|
func (e *imageExporterInstance) Name() string {
|
|
|
|
return "exporting to image"
|
|
|
|
}
|
|
|
|
|
2017-10-03 06:33:25 +00:00
|
|
|
func (e *imageExporterInstance) Export(ctx context.Context, ref cache.ImmutableRef, opt map[string][]byte) error {
|
2017-07-11 00:56:04 +00:00
|
|
|
layersDone := oneOffProgress(ctx, "exporting layers")
|
2017-10-12 18:24:15 +00:00
|
|
|
diffPairs, err := blobs.GetDiffPairs(ctx, e.opt.Snapshotter, e.opt.Differ, ref)
|
2017-07-10 20:03:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-11 00:56:04 +00:00
|
|
|
layersDone(nil)
|
2017-07-10 20:03:38 +00:00
|
|
|
|
|
|
|
diffIDs := make([]digest.Digest, 0, len(diffPairs))
|
|
|
|
for _, dp := range diffPairs {
|
2017-10-12 18:24:15 +00:00
|
|
|
diffIDs = append(diffIDs, dp.DiffID)
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
var dt []byte
|
2017-10-03 06:33:25 +00:00
|
|
|
if config, ok := opt[exporterImageConfig]; ok {
|
|
|
|
dt, err = setDiffIDs(config, diffIDs)
|
2017-09-11 05:35:07 +00:00
|
|
|
if err != nil {
|
2017-10-03 06:33:25 +00:00
|
|
|
return err
|
2017-09-11 05:35:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dt, err = json.Marshal(imageConfig(diffIDs))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal image config")
|
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dgst := digest.FromBytes(dt)
|
2017-07-11 00:56:04 +00:00
|
|
|
configDone := oneOffProgress(ctx, "exporting config "+dgst.String())
|
2017-07-10 20:03:38 +00:00
|
|
|
|
|
|
|
if err := content.WriteBlob(ctx, e.opt.ContentStore, dgst.String(), bytes.NewReader(dt), int64(len(dt)), dgst); err != nil {
|
2017-07-11 00:56:04 +00:00
|
|
|
return configDone(errors.Wrap(err, "error writing config blob"))
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
2017-07-11 00:56:04 +00:00
|
|
|
configDone(nil)
|
2017-07-10 20:03:38 +00:00
|
|
|
|
2017-10-13 17:08:47 +00:00
|
|
|
mfst := schema2.Manifest{
|
|
|
|
Config: distribution.Descriptor{
|
2017-07-10 20:03:38 +00:00
|
|
|
Digest: dgst,
|
|
|
|
Size: int64(len(dt)),
|
2017-10-13 17:08:47 +00:00
|
|
|
MediaType: schema2.MediaTypeImageConfig,
|
2017-07-10 20:03:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
mfst.SchemaVersion = 2
|
2017-10-13 17:08:47 +00:00
|
|
|
mfst.MediaType = schema2.MediaTypeManifest
|
2017-07-10 20:03:38 +00:00
|
|
|
|
|
|
|
for _, dp := range diffPairs {
|
2017-10-12 18:24:15 +00:00
|
|
|
info, err := e.opt.ContentStore.Info(ctx, dp.Blobsum)
|
2017-07-10 20:03:38 +00:00
|
|
|
if err != nil {
|
2017-10-12 18:24:15 +00:00
|
|
|
return configDone(errors.Wrapf(err, "could not get blob %s", dp.Blobsum))
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
2017-10-13 17:08:47 +00:00
|
|
|
mfst.Layers = append(mfst.Layers, distribution.Descriptor{
|
2017-10-12 18:24:15 +00:00
|
|
|
Digest: dp.Blobsum,
|
2017-07-10 20:03:38 +00:00
|
|
|
Size: info.Size,
|
2017-10-13 17:08:47 +00:00
|
|
|
MediaType: schema2.MediaTypeLayer,
|
2017-07-10 20:03:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err = json.Marshal(mfst)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal manifest")
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst = digest.FromBytes(dt)
|
2017-07-11 00:56:04 +00:00
|
|
|
mfstDone := oneOffProgress(ctx, "exporting manifest "+dgst.String())
|
2017-07-10 20:03:38 +00:00
|
|
|
|
|
|
|
if err := content.WriteBlob(ctx, e.opt.ContentStore, dgst.String(), bytes.NewReader(dt), int64(len(dt)), dgst); err != nil {
|
2017-07-11 00:56:04 +00:00
|
|
|
return mfstDone(errors.Wrap(err, "error writing manifest blob"))
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 00:56:04 +00:00
|
|
|
mfstDone(nil)
|
2017-07-10 20:03:38 +00:00
|
|
|
|
2017-10-13 17:08:47 +00:00
|
|
|
if e.targetName != "" {
|
|
|
|
if e.opt.Images != nil {
|
|
|
|
tagDone := oneOffProgress(ctx, "naming to "+e.targetName)
|
|
|
|
imgrec := images.Image{
|
|
|
|
Name: e.targetName,
|
|
|
|
Target: ocispec.Descriptor{
|
|
|
|
Digest: dgst,
|
|
|
|
Size: int64(len(dt)),
|
|
|
|
MediaType: ocispec.MediaTypeImageManifest,
|
|
|
|
},
|
|
|
|
CreatedAt: time.Now(),
|
2017-07-13 06:15:12 +00:00
|
|
|
}
|
2017-10-13 17:08:47 +00:00
|
|
|
_, err := e.opt.Images.Update(ctx, imgrec)
|
2017-07-13 06:15:12 +00:00
|
|
|
if err != nil {
|
2017-10-13 17:08:47 +00:00
|
|
|
if !errdefs.IsNotFound(err) {
|
|
|
|
return tagDone(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := e.opt.Images.Create(ctx, imgrec)
|
|
|
|
if err != nil {
|
|
|
|
return tagDone(err)
|
|
|
|
}
|
2017-07-13 06:15:12 +00:00
|
|
|
}
|
2017-10-13 17:08:47 +00:00
|
|
|
tagDone(nil)
|
|
|
|
}
|
|
|
|
if e.push {
|
2017-10-15 06:49:55 +00:00
|
|
|
return push.Push(ctx, e.opt.SessionManager, e.opt.ContentStore, dgst, e.targetName)
|
2017-07-11 00:56:04 +00:00
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is temporary: should move to dockerfile frontend
|
|
|
|
func imageConfig(diffIDs []digest.Digest) ocispec.Image {
|
|
|
|
img := ocispec.Image{
|
|
|
|
Architecture: runtime.GOARCH,
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
}
|
|
|
|
img.RootFS.Type = "layers"
|
|
|
|
img.RootFS.DiffIDs = diffIDs
|
|
|
|
img.Config.WorkingDir = "/"
|
|
|
|
img.Config.Env = []string{"PATH=" + system.DefaultPathEnv}
|
|
|
|
return img
|
|
|
|
}
|
2017-07-11 00:56:04 +00:00
|
|
|
|
2017-10-03 06:33:25 +00:00
|
|
|
func setDiffIDs(config []byte, diffIDs []digest.Digest) ([]byte, error) {
|
|
|
|
mp := map[string]json.RawMessage{}
|
|
|
|
if err := json.Unmarshal(config, &mp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var rootFS ocispec.RootFS
|
|
|
|
rootFS.Type = "layers"
|
|
|
|
rootFS.DiffIDs = diffIDs
|
|
|
|
dt, err := json.Marshal(rootFS)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mp["rootfs"] = dt
|
|
|
|
return json.Marshal(mp)
|
2017-09-11 05:35:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 00:56:04 +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
|
|
|
|
}
|
|
|
|
}
|