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"
|
2018-08-07 07:59:47 +00:00
|
|
|
"strings"
|
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/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-09-07 20:45:59 +00:00
|
|
|
"github.com/moby/buildkit/util/resolver"
|
2018-05-08 05:54:16 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-10 20:03:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-07-13 01:02:36 +00:00
|
|
|
keyImageName = "name"
|
|
|
|
keyPush = "push"
|
|
|
|
keyInsecure = "registry.insecure"
|
|
|
|
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
|
2018-09-07 20:45:59 +00:00
|
|
|
ResolverOpt resolver.ResolveOptionsFunc
|
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
|
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:
|
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-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
|
2018-07-13 18:28:36 +00:00
|
|
|
meta map[string][]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-07-10 18:05:22 +00:00
|
|
|
func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source) (map[string]string, error) {
|
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
|
|
|
desc, err := e.opt.ImageWriter.Commit(ctx, src, 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)
|
|
|
|
}()
|
|
|
|
|
2018-08-31 09:54:41 +00:00
|
|
|
resp := make(map[string]string)
|
|
|
|
|
2018-08-31 16:34:49 +00:00
|
|
|
if n, ok := src.Metadata["image.name"]; e.targetName == "*" && ok {
|
2018-08-15 15:11:37 +00:00
|
|
|
e.targetName = string(n)
|
|
|
|
}
|
|
|
|
|
2017-10-13 17:08:47 +00:00
|
|
|
if e.targetName != "" {
|
2018-08-07 07:59:47 +00:00
|
|
|
targetNames := strings.Split(e.targetName, ",")
|
|
|
|
for _, targetName := range targetNames {
|
|
|
|
if e.opt.Images != nil {
|
|
|
|
tagDone := oneOffProgress(ctx, "naming to "+targetName)
|
|
|
|
img := images.Image{
|
|
|
|
Name: targetName,
|
|
|
|
Target: *desc,
|
|
|
|
CreatedAt: time.Now(),
|
2017-10-13 17:08:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 07:59:47 +00:00
|
|
|
if _, err := e.opt.Images.Update(ctx, img); err != nil {
|
|
|
|
if !errdefs.IsNotFound(err) {
|
|
|
|
return nil, tagDone(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := e.opt.Images.Create(ctx, img); err != nil {
|
|
|
|
return nil, tagDone(err)
|
|
|
|
}
|
2017-10-13 17:08:47 +00:00
|
|
|
}
|
2018-08-07 07:59:47 +00:00
|
|
|
tagDone(nil)
|
2017-07-13 06:15:12 +00:00
|
|
|
}
|
2018-08-07 07:59:47 +00:00
|
|
|
if e.push {
|
2018-09-07 20:45:59 +00:00
|
|
|
if err := push.Push(ctx, e.opt.SessionManager, e.opt.ImageWriter.ContentStore(), desc.Digest, targetName, e.insecure, e.opt.ResolverOpt); err != nil {
|
2018-08-07 07:59:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
}
|
2017-07-11 00:56:04 +00:00
|
|
|
}
|
2018-08-31 09:54:41 +00:00
|
|
|
resp["image.name"] = e.targetName
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 09:54:41 +00:00
|
|
|
resp["containerimage.digest"] = desc.Digest.String()
|
|
|
|
return resp, nil
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|