2017-07-10 20:03:38 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
2017-07-11 00:56:04 +00:00
|
|
|
"time"
|
2017-07-10 20:03:38 +00:00
|
|
|
|
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/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
2017-10-15 06:49:55 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2017-10-13 17:08:47 +00:00
|
|
|
"github.com/moby/buildkit/util/push"
|
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-11-03 00:48:03 +00:00
|
|
|
keyInsecure = "registry.insecure"
|
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
|
2017-12-13 00:58:48 +00:00
|
|
|
ImageWriter *ImageWriter
|
2017-10-15 06:49:55 +00:00
|
|
|
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-11-03 00:48:03 +00:00
|
|
|
case keyInsecure:
|
|
|
|
i.insecure = true
|
2017-12-13 00:59:06 +00:00
|
|
|
case exporterImageConfig:
|
|
|
|
i.config = []byte(v)
|
2017-07-10 20:03:38 +00:00
|
|
|
default:
|
2017-12-08 18:53:17 +00:00
|
|
|
logrus.Warnf("image exporter: unknown option %s", k)
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type imageExporterInstance struct {
|
|
|
|
*imageExporter
|
|
|
|
targetName string
|
2017-10-13 17:08:47 +00:00
|
|
|
push bool
|
2017-11-03 00:48:03 +00:00
|
|
|
insecure bool
|
2017-12-13 00:59:06 +00:00
|
|
|
config []byte
|
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-12-13 00:59:06 +00:00
|
|
|
if config, ok := opt[exporterImageConfig]; ok {
|
|
|
|
e.config = config
|
|
|
|
}
|
|
|
|
desc, err := e.opt.ImageWriter.Commit(ctx, ref, e.config)
|
2017-12-08 18:53:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
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)
|
2017-12-08 18:53:17 +00:00
|
|
|
img := images.Image{
|
2017-12-13 00:58:48 +00:00
|
|
|
Name: e.targetName,
|
|
|
|
Target: *desc,
|
2017-10-13 17:08:47 +00:00
|
|
|
CreatedAt: time.Now(),
|
2017-07-13 06:15:12 +00:00
|
|
|
}
|
2017-12-08 18:53:17 +00:00
|
|
|
|
|
|
|
if _, err := e.opt.Images.Update(ctx, img); err != nil {
|
2017-10-13 17:08:47 +00:00
|
|
|
if !errdefs.IsNotFound(err) {
|
|
|
|
return tagDone(err)
|
|
|
|
}
|
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
if _, err := e.opt.Images.Create(ctx, img); err != nil {
|
2017-10-13 17:08:47 +00:00
|
|
|
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-12-13 00:58:48 +00:00
|
|
|
return push.Push(ctx, e.opt.SessionManager, e.opt.ImageWriter.ContentStore(), desc.Digest, e.targetName, e.insecure)
|
2017-07-11 00:56:04 +00:00
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
return nil
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|