2017-06-09 01:16:19 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-11 08:02:46 +00:00
|
|
|
"encoding/json"
|
2017-06-09 01:16:19 +00:00
|
|
|
"io"
|
2017-07-11 17:12:12 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-06-29 22:31:08 +00:00
|
|
|
"time"
|
2017-06-09 01:16:19 +00:00
|
|
|
|
2018-09-11 08:02:46 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
|
|
|
contentlocal "github.com/containerd/containerd/content/local"
|
2017-06-22 20:15:46 +00:00
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
2018-09-11 08:02:46 +00:00
|
|
|
"github.com/moby/buildkit/client/ociindex"
|
2017-10-03 23:13:35 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2017-07-11 17:12:12 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-09-11 08:02:46 +00:00
|
|
|
sessioncontent "github.com/moby/buildkit/session/content"
|
2017-07-11 17:12:12 +00:00
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"github.com/moby/buildkit/session/grpchijack"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-08-04 19:42:01 +00:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2018-09-11 08:02:46 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-01-05 23:17:35 +00:00
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2017-06-09 01:16:19 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-10-01 11:16:48 +00:00
|
|
|
fstypes "github.com/tonistiigi/fsutil/types"
|
2017-06-14 00:15:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-06-09 01:16:19 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
type SolveOpt struct {
|
2019-04-10 21:43:17 +00:00
|
|
|
Exports []ExportEntry
|
|
|
|
LocalDirs map[string]string
|
|
|
|
SharedKey string
|
|
|
|
Frontend string
|
|
|
|
FrontendAttrs map[string]string
|
|
|
|
CacheExports []CacheOptionsEntry
|
|
|
|
CacheImports []CacheOptionsEntry
|
|
|
|
Session []session.Attachable
|
|
|
|
AllowedEntitlements []entitlements.Entitlement
|
|
|
|
SharedSession *session.Session // TODO: refactor to better session syncing
|
|
|
|
SessionPreInitialized bool // TODO: refactor to better session syncing
|
2017-07-20 22:13:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 16:30:39 +00:00
|
|
|
type ExportEntry struct {
|
|
|
|
Type string
|
|
|
|
Attrs map[string]string
|
2019-07-30 00:50:54 +00:00
|
|
|
Output func(map[string]string) (io.WriteCloser, error) // for ExporterOCI and ExporterDocker
|
|
|
|
OutputDir string // for ExporterLocal
|
2019-01-31 16:30:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 08:02:46 +00:00
|
|
|
type CacheOptionsEntry struct {
|
|
|
|
Type string
|
|
|
|
Attrs map[string]string
|
|
|
|
}
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
// Solve calls Solve on the controller.
|
|
|
|
// def must be nil if (and only if) opt.Frontend is set.
|
2018-05-03 00:35:07 +00:00
|
|
|
func (c *Client) Solve(ctx context.Context, def *llb.Definition, opt SolveOpt, statusChan chan *SolveStatus) (*SolveResponse, error) {
|
2017-07-11 17:12:12 +00:00
|
|
|
defer func() {
|
|
|
|
if statusChan != nil {
|
|
|
|
close(statusChan)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
if opt.Frontend == "" && def == nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.New("invalid empty definition")
|
2017-10-02 04:59:34 +00:00
|
|
|
}
|
|
|
|
if opt.Frontend != "" && def != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, errors.Errorf("invalid definition for frontend %s", opt.Frontend)
|
2017-06-09 01:16:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 16:34:44 +00:00
|
|
|
return c.solve(ctx, def, nil, opt, statusChan)
|
|
|
|
}
|
|
|
|
|
|
|
|
type runGatewayCB func(ref string, s *session.Session) error
|
|
|
|
|
|
|
|
func (c *Client) solve(ctx context.Context, def *llb.Definition, runGateway runGatewayCB, opt SolveOpt, statusChan chan *SolveStatus) (*SolveResponse, error) {
|
|
|
|
if def != nil && runGateway != nil {
|
|
|
|
return nil, errors.New("invalid with def and cb")
|
|
|
|
}
|
|
|
|
|
2017-10-05 20:42:53 +00:00
|
|
|
syncedDirs, err := prepareSyncedDirs(def, opt.LocalDirs)
|
2017-07-20 22:13:00 +00:00
|
|
|
if err != nil {
|
2018-05-03 00:35:07 +00:00
|
|
|
return nil, err
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:13:35 +00:00
|
|
|
ref := identity.NewID()
|
2017-06-14 00:15:55 +00:00
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
2017-06-29 22:31:08 +00:00
|
|
|
statusContext, cancelStatus := context.WithCancel(context.Background())
|
2017-07-11 17:12:12 +00:00
|
|
|
defer cancelStatus()
|
|
|
|
|
2018-01-05 23:17:35 +00:00
|
|
|
if span := opentracing.SpanFromContext(ctx); span != nil {
|
|
|
|
statusContext = opentracing.ContextWithSpan(statusContext, span)
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:43:17 +00:00
|
|
|
s := opt.SharedSession
|
2017-07-11 17:12:12 +00:00
|
|
|
|
2019-04-10 21:43:17 +00:00
|
|
|
if s == nil {
|
|
|
|
if opt.SessionPreInitialized {
|
|
|
|
return nil, errors.Errorf("no session provided for preinitialized option")
|
|
|
|
}
|
|
|
|
s, err = session.NewSession(statusContext, defaultSessionName(), opt.SharedKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create session")
|
|
|
|
}
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:43:17 +00:00
|
|
|
cacheOpt, err := parseCacheOptions(opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-17 22:55:35 +00:00
|
|
|
}
|
2017-10-15 06:49:55 +00:00
|
|
|
|
2019-01-31 16:30:39 +00:00
|
|
|
var ex ExportEntry
|
|
|
|
|
2019-04-10 21:43:17 +00:00
|
|
|
if !opt.SessionPreInitialized {
|
|
|
|
if len(syncedDirs) > 0 {
|
|
|
|
s.Allow(filesync.NewFSSyncProvider(syncedDirs))
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
|
|
|
|
for _, a := range opt.Session {
|
|
|
|
s.Allow(a)
|
2017-08-03 22:24:02 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
|
|
|
|
if len(opt.Exports) > 1 {
|
|
|
|
return nil, errors.New("currently only single Exports can be specified")
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
if len(opt.Exports) == 1 {
|
|
|
|
ex = opt.Exports[0]
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
|
|
|
|
switch ex.Type {
|
|
|
|
case ExporterLocal:
|
|
|
|
if ex.Output != nil {
|
|
|
|
return nil, errors.New("output file writer is not supported by local exporter")
|
|
|
|
}
|
|
|
|
if ex.OutputDir == "" {
|
|
|
|
return nil, errors.New("output directory is required for local exporter")
|
|
|
|
}
|
|
|
|
s.Allow(filesync.NewFSSyncTargetDir(ex.OutputDir))
|
|
|
|
case ExporterOCI, ExporterDocker, ExporterTar:
|
|
|
|
if ex.OutputDir != "" {
|
|
|
|
return nil, errors.Errorf("output directory %s is not supported by %s exporter", ex.OutputDir, ex.Type)
|
|
|
|
}
|
|
|
|
if ex.Output == nil {
|
|
|
|
return nil, errors.Errorf("output file writer is required for %s exporter", ex.Type)
|
|
|
|
}
|
|
|
|
s.Allow(filesync.NewFSSyncTarget(ex.Output))
|
|
|
|
default:
|
|
|
|
if ex.Output != nil {
|
|
|
|
return nil, errors.Errorf("output file writer is not supported by %s exporter", ex.Type)
|
|
|
|
}
|
|
|
|
if ex.OutputDir != "" {
|
|
|
|
return nil, errors.Errorf("output directory %s is not supported by %s exporter", ex.OutputDir, ex.Type)
|
|
|
|
}
|
2018-03-27 04:16:32 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
|
|
|
|
if len(cacheOpt.contentStores) > 0 {
|
|
|
|
s.Allow(sessioncontent.NewAttachable(cacheOpt.contentStores))
|
2017-12-13 00:59:06 +00:00
|
|
|
}
|
2017-08-03 22:24:02 +00:00
|
|
|
|
2019-04-10 21:43:17 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
return s.Run(statusContext, grpchijack.Dialer(c.controlClient()))
|
|
|
|
})
|
2018-09-11 08:02:46 +00:00
|
|
|
}
|
2019-04-10 21:43:17 +00:00
|
|
|
|
2018-09-11 08:02:46 +00:00
|
|
|
for k, v := range cacheOpt.frontendAttrs {
|
|
|
|
opt.FrontendAttrs[k] = v
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:34:44 +00:00
|
|
|
solveCtx, cancelSolve := context.WithCancel(ctx)
|
2018-05-03 00:35:07 +00:00
|
|
|
var res *SolveResponse
|
2017-06-14 00:15:55 +00:00
|
|
|
eg.Go(func() error {
|
2018-07-19 16:34:44 +00:00
|
|
|
ctx := solveCtx
|
|
|
|
defer cancelSolve()
|
|
|
|
|
2017-06-29 22:31:08 +00:00
|
|
|
defer func() { // make sure the Status ends cleanly on build errors
|
|
|
|
go func() {
|
|
|
|
<-time.After(3 * time.Second)
|
|
|
|
cancelStatus()
|
|
|
|
}()
|
2017-07-11 17:12:12 +00:00
|
|
|
logrus.Debugf("stopping session")
|
|
|
|
s.Close()
|
2017-06-29 22:31:08 +00:00
|
|
|
}()
|
2017-10-05 20:42:53 +00:00
|
|
|
var pbd *pb.Definition
|
|
|
|
if def != nil {
|
|
|
|
pbd = def.ToPB()
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
resp, err := c.controlClient().Solve(ctx, &controlapi.SolveRequest{
|
2017-07-10 20:03:38 +00:00
|
|
|
Ref: ref,
|
2017-10-05 20:42:53 +00:00
|
|
|
Definition: pbd,
|
2019-01-31 16:30:39 +00:00
|
|
|
Exporter: ex.Type,
|
|
|
|
ExporterAttrs: ex.Attrs,
|
2017-07-14 18:05:54 +00:00
|
|
|
Session: s.ID(),
|
2017-08-25 20:08:18 +00:00
|
|
|
Frontend: opt.Frontend,
|
|
|
|
FrontendAttrs: opt.FrontendAttrs,
|
2018-09-11 08:02:46 +00:00
|
|
|
Cache: cacheOpt.options,
|
|
|
|
Entitlements: opt.AllowedEntitlements,
|
2017-06-14 00:15:55 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to solve")
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
res = &SolveResponse{
|
|
|
|
ExporterResponse: resp.ExporterResponse,
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
return nil
|
2017-06-09 01:16:19 +00:00
|
|
|
})
|
2017-06-14 00:15:55 +00:00
|
|
|
|
2018-07-19 16:34:44 +00:00
|
|
|
if runGateway != nil {
|
|
|
|
eg.Go(func() error {
|
|
|
|
err := runGateway(ref, s)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the callback failed then the main
|
|
|
|
// `Solve` (called above) should error as
|
|
|
|
// well. However as a fallback we wait up to
|
|
|
|
// 5s for that to happen before failing this
|
|
|
|
// goroutine.
|
|
|
|
select {
|
|
|
|
case <-solveCtx.Done():
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
cancelSolve()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-14 00:15:55 +00:00
|
|
|
eg.Go(func() error {
|
2017-06-29 22:31:08 +00:00
|
|
|
stream, err := c.controlClient().Status(statusContext, &controlapi.StatusRequest{
|
2017-06-14 00:15:55 +00:00
|
|
|
Ref: ref,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get status")
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "failed to receive status")
|
|
|
|
}
|
2017-06-15 23:34:42 +00:00
|
|
|
s := SolveStatus{}
|
|
|
|
for _, v := range resp.Vertexes {
|
|
|
|
s.Vertexes = append(s.Vertexes, &Vertex{
|
|
|
|
Digest: v.Digest,
|
|
|
|
Inputs: v.Inputs,
|
|
|
|
Name: v.Name,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
2017-06-29 22:31:08 +00:00
|
|
|
Error: v.Error,
|
2017-07-06 20:15:54 +00:00
|
|
|
Cached: v.Cached,
|
2017-06-15 23:34:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, v := range resp.Statuses {
|
|
|
|
s.Statuses = append(s.Statuses, &VertexStatus{
|
|
|
|
ID: v.ID,
|
|
|
|
Vertex: v.Vertex,
|
|
|
|
Name: v.Name,
|
|
|
|
Total: v.Total,
|
|
|
|
Current: v.Current,
|
|
|
|
Timestamp: v.Timestamp,
|
|
|
|
Started: v.Started,
|
|
|
|
Completed: v.Completed,
|
|
|
|
})
|
|
|
|
}
|
2017-06-19 05:24:41 +00:00
|
|
|
for _, v := range resp.Logs {
|
|
|
|
s.Logs = append(s.Logs, &VertexLog{
|
|
|
|
Vertex: v.Vertex,
|
|
|
|
Stream: int(v.Stream),
|
|
|
|
Data: v.Msg,
|
|
|
|
Timestamp: v.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
2017-06-15 23:34:42 +00:00
|
|
|
if statusChan != nil {
|
|
|
|
statusChan <- &s
|
|
|
|
}
|
2017-06-14 00:15:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-03 00:35:07 +00:00
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-11 08:02:46 +00:00
|
|
|
// Update index.json of exported cache content store
|
|
|
|
// FIXME(AkihiroSuda): dedupe const definition of cache/remotecache.ExporterResponseManifestDesc = "cache.manifest"
|
|
|
|
if manifestDescJSON := res.ExporterResponse["cache.manifest"]; manifestDescJSON != "" {
|
|
|
|
var manifestDesc ocispec.Descriptor
|
|
|
|
if err = json.Unmarshal([]byte(manifestDescJSON), &manifestDesc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for indexJSONPath, tag := range cacheOpt.indicesToUpdate {
|
|
|
|
if err = ociindex.PutDescToIndexJSONFileLocked(indexJSONPath, manifestDesc, tag); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 00:35:07 +00:00
|
|
|
return res, nil
|
2017-06-09 01:16:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:42:53 +00:00
|
|
|
func prepareSyncedDirs(def *llb.Definition, localDirs map[string]string) ([]filesync.SyncedDir, error) {
|
2017-07-20 22:13:00 +00:00
|
|
|
for _, d := range localDirs {
|
|
|
|
fi, err := os.Stat(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not find %s", d)
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, errors.Errorf("%s not a directory", d)
|
|
|
|
}
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
2019-03-19 02:02:00 +00:00
|
|
|
resetUIDAndGID := func(p string, st *fstypes.Stat) bool {
|
2018-08-28 21:21:20 +00:00
|
|
|
st.Uid = 0
|
|
|
|
st.Gid = 0
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
dirs := make([]filesync.SyncedDir, 0, len(localDirs))
|
2017-10-05 20:42:53 +00:00
|
|
|
if def == nil {
|
2017-08-25 20:08:18 +00:00
|
|
|
for name, d := range localDirs {
|
2018-08-28 21:21:20 +00:00
|
|
|
dirs = append(dirs, filesync.SyncedDir{Name: name, Dir: d, Map: resetUIDAndGID})
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-10-05 20:42:53 +00:00
|
|
|
} else {
|
|
|
|
for _, dt := range def.Def {
|
|
|
|
var op pb.Op
|
|
|
|
if err := (&op).Unmarshal(dt); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse llb proto op")
|
|
|
|
}
|
|
|
|
if src := op.GetSource(); src != nil {
|
|
|
|
if strings.HasPrefix(src.Identifier, "local://") { // TODO: just make a type property
|
|
|
|
name := strings.TrimPrefix(src.Identifier, "local://")
|
|
|
|
d, ok := localDirs[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("local directory %s not enabled", name)
|
|
|
|
}
|
2018-08-28 21:21:20 +00:00
|
|
|
dirs = append(dirs, filesync.SyncedDir{Name: name, Dir: d, Map: resetUIDAndGID}) // TODO: excludes
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 22:13:00 +00:00
|
|
|
return dirs, nil
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
func defaultSessionName() string {
|
|
|
|
wd, err := os.Getwd()
|
2017-07-11 17:12:12 +00:00
|
|
|
if err != nil {
|
2017-07-20 22:13:00 +00:00
|
|
|
return "unknown"
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
2017-07-20 22:13:00 +00:00
|
|
|
return filepath.Base(wd)
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
2018-09-11 08:02:46 +00:00
|
|
|
|
|
|
|
type cacheOptions struct {
|
|
|
|
options controlapi.CacheOptions
|
|
|
|
contentStores map[string]content.Store // key: ID of content store ("local:" + csDir)
|
|
|
|
indicesToUpdate map[string]string // key: index.JSON file name, value: tag
|
|
|
|
frontendAttrs map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCacheOptions(opt SolveOpt) (*cacheOptions, error) {
|
|
|
|
var (
|
|
|
|
cacheExports []*controlapi.CacheOptionsEntry
|
|
|
|
cacheImports []*controlapi.CacheOptionsEntry
|
|
|
|
// legacy API is used for registry caches, because the daemon might not support the new API
|
2019-03-12 18:18:54 +00:00
|
|
|
legacyExportRef string
|
|
|
|
legacyImportRefs []string
|
2018-09-11 08:02:46 +00:00
|
|
|
)
|
|
|
|
contentStores := make(map[string]content.Store)
|
|
|
|
indicesToUpdate := make(map[string]string) // key: index.JSON file name, value: tag
|
|
|
|
frontendAttrs := make(map[string]string)
|
2019-03-12 18:18:54 +00:00
|
|
|
legacyExportAttrs := make(map[string]string)
|
2018-09-11 08:02:46 +00:00
|
|
|
for _, ex := range opt.CacheExports {
|
|
|
|
if ex.Type == "local" {
|
2019-01-31 16:30:39 +00:00
|
|
|
csDir := ex.Attrs["dest"]
|
2018-09-11 08:02:46 +00:00
|
|
|
if csDir == "" {
|
2019-01-31 16:30:39 +00:00
|
|
|
return nil, errors.New("local cache exporter requires dest")
|
2018-09-11 08:02:46 +00:00
|
|
|
}
|
|
|
|
if err := os.MkdirAll(csDir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cs, err := contentlocal.NewStore(csDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
contentStores["local:"+csDir] = cs
|
|
|
|
// TODO(AkihiroSuda): support custom index JSON path and tag
|
|
|
|
indexJSONPath := filepath.Join(csDir, "index.json")
|
|
|
|
indicesToUpdate[indexJSONPath] = "latest"
|
|
|
|
}
|
|
|
|
if ex.Type == "registry" && legacyExportRef == "" {
|
|
|
|
legacyExportRef = ex.Attrs["ref"]
|
|
|
|
for k, v := range ex.Attrs {
|
|
|
|
if k != "ref" {
|
|
|
|
legacyExportAttrs[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cacheExports = append(cacheExports, &controlapi.CacheOptionsEntry{
|
|
|
|
Type: ex.Type,
|
|
|
|
Attrs: ex.Attrs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, im := range opt.CacheImports {
|
|
|
|
attrs := im.Attrs
|
|
|
|
if im.Type == "local" {
|
2019-01-31 16:30:39 +00:00
|
|
|
csDir := im.Attrs["src"]
|
2019-12-12 19:06:14 +00:00
|
|
|
if csDir == "" {
|
|
|
|
return nil, errors.New("local cache importer requires src")
|
|
|
|
}
|
|
|
|
cs, err := contentlocal.NewStore(csDir)
|
2018-09-11 08:02:46 +00:00
|
|
|
if err != nil {
|
2019-12-10 06:55:29 +00:00
|
|
|
logrus.Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
|
|
|
|
continue
|
2018-09-11 08:02:46 +00:00
|
|
|
}
|
2019-12-12 19:06:14 +00:00
|
|
|
// if digest is not specified, load from "latest" tag
|
|
|
|
if attrs["digest"] == "" {
|
|
|
|
idx, err := ociindex.ReadIndexJSONFileLocked(filepath.Join(csDir, "index.json"))
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warning("local cache import at " + csDir + " not found due to err: " + err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, m := range idx.Manifests {
|
|
|
|
if m.Annotations[ocispec.AnnotationRefName] == "latest" {
|
|
|
|
attrs["digest"] = string(m.Digest)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attrs["digest"] == "" {
|
|
|
|
return nil, errors.New("local cache importer requires either explicit digest or \"latest\" tag on index.json")
|
|
|
|
}
|
|
|
|
}
|
2018-09-11 08:02:46 +00:00
|
|
|
contentStores["local:"+csDir] = cs
|
|
|
|
|
|
|
|
}
|
|
|
|
if im.Type == "registry" {
|
|
|
|
legacyImportRef := attrs["ref"]
|
|
|
|
legacyImportRefs = append(legacyImportRefs, legacyImportRef)
|
|
|
|
} else {
|
|
|
|
cacheImports = append(cacheImports, &controlapi.CacheOptionsEntry{
|
|
|
|
Type: im.Type,
|
|
|
|
Attrs: attrs,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if opt.Frontend != "" {
|
|
|
|
// use legacy API for registry importers, because the frontend might not support the new API
|
|
|
|
if len(legacyImportRefs) > 0 {
|
|
|
|
frontendAttrs["cache-from"] = strings.Join(legacyImportRefs, ",")
|
|
|
|
}
|
|
|
|
// use new API for other importers
|
|
|
|
if len(cacheImports) > 0 {
|
|
|
|
s, err := json.Marshal(cacheImports)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
frontendAttrs["cache-imports"] = string(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res := cacheOptions{
|
|
|
|
options: controlapi.CacheOptions{
|
|
|
|
// old API (for registry caches, planned to be removed in early 2019)
|
|
|
|
ExportRefDeprecated: legacyExportRef,
|
|
|
|
ExportAttrsDeprecated: legacyExportAttrs,
|
|
|
|
ImportRefsDeprecated: legacyImportRefs,
|
|
|
|
// new API
|
|
|
|
Exports: cacheExports,
|
|
|
|
Imports: cacheImports,
|
|
|
|
},
|
|
|
|
contentStores: contentStores,
|
|
|
|
indicesToUpdate: indicesToUpdate,
|
|
|
|
frontendAttrs: frontendAttrs,
|
|
|
|
}
|
|
|
|
return &res, nil
|
|
|
|
}
|