2017-07-10 20:03:38 +00:00
|
|
|
package containerimage
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2018-05-08 05:54:16 +00:00
|
|
|
"strconv"
|
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"
|
2018-05-08 05:54:16 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-07-10 20:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
2018-06-26 22:24:33 +00:00
|
|
|
ociTypes = "oci-mediatypes"
|
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
|
|
|
}
|
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
// New returns a new containerimage exporter instance that supports exporting
|
|
|
|
// to an image store and pushing the image to registry.
|
|
|
|
// This exporter supports following values in returned kv map:
|
|
|
|
// - containerimage.digest - The digest of the root manifest for the image.
|
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:
|
2018-05-08 05:54:16 +00:00
|
|
|
if v == "" {
|
|
|
|
i.push = true
|
2018-05-08 12:45:38 +00:00
|
|
|
continue
|
2018-05-08 05:54:16 +00:00
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
|
|
|
}
|
|
|
|
i.push = b
|
2017-11-03 00:48:03 +00:00
|
|
|
case keyInsecure:
|
2018-05-08 05:54:16 +00:00
|
|
|
if v == "" {
|
|
|
|
i.insecure = true
|
2018-05-08 12:45:38 +00:00
|
|
|
continue
|
2018-05-08 05:54:16 +00:00
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
|
|
|
}
|
|
|
|
i.insecure = b
|
2017-12-13 00:59:06 +00:00
|
|
|
case exporterImageConfig:
|
|
|
|
i.config = []byte(v)
|
2018-06-26 22:24:33 +00:00
|
|
|
case ociTypes:
|
|
|
|
if v == "" {
|
|
|
|
i.ociTypes = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b, err := strconv.ParseBool(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
|
|
|
|
}
|
|
|
|
i.ociTypes = b
|
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
|
2018-06-26 22:24:33 +00:00
|
|
|
ociTypes 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"
|
|
|
|
}
|
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
func (e *imageExporterInstance) Export(ctx context.Context, ref cache.ImmutableRef, opt map[string][]byte) (map[string]string, error) {
|
2017-12-13 00:59:06 +00:00
|
|
|
if config, ok := opt[exporterImageConfig]; ok {
|
|
|
|
e.config = config
|
|
|
|
}
|
2018-06-26 22:24:33 +00:00
|
|
|
desc, err := e.opt.ImageWriter.Commit(ctx, ref, e.config, e.ociTypes)
|
2017-12-08 18:53:17 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 19:56:04 +00:00
|
|
|
defer func() {
|
|
|
|
e.opt.ImageWriter.ContentStore().Delete(context.TODO(), desc.Digest)
|
|
|
|
}()
|
|
|
|
|
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) {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, tagDone(err)
|
2017-10-13 17:08:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
if _, err := e.opt.Images.Create(ctx, img); err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, tagDone(err)
|
2017-10-13 17:08:47 +00:00
|
|
|
}
|
2017-07-13 06:15:12 +00:00
|
|
|
}
|
2017-10-13 17:08:47 +00:00
|
|
|
tagDone(nil)
|
|
|
|
}
|
|
|
|
if e.push {
|
2018-05-03 00:35:07 +00:00
|
|
|
if err := push.Push(ctx, e.opt.SessionManager, e.opt.ImageWriter.ContentStore(), desc.Digest, e.targetName, e.insecure); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-11 00:56:04 +00:00
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
return map[string]string{
|
|
|
|
"containerimage.digest": desc.Digest.String(),
|
|
|
|
}, nil
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|