2018-07-23 23:27:01 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-02-09 21:20:42 +00:00
|
|
|
cacheconfig "github.com/moby/buildkit/cache/config"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-07-23 23:27:01 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/compression"
|
2018-07-23 23:27:01 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewCacheResultStorage(wc *Controller) solver.CacheResultStorage {
|
|
|
|
return &cacheResultStorage{
|
|
|
|
wc: wc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheResultStorage struct {
|
|
|
|
wc *Controller
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:43:27 +00:00
|
|
|
func (s *cacheResultStorage) Save(res solver.Result, createdAt time.Time) (solver.CacheResult, error) {
|
2018-07-23 23:27:01 +00:00
|
|
|
ref, ok := res.Sys().(*WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return solver.CacheResult{}, errors.Errorf("invalid result: %T", res.Sys())
|
|
|
|
}
|
|
|
|
if ref.ImmutableRef != nil {
|
2021-07-09 00:09:35 +00:00
|
|
|
if !ref.ImmutableRef.HasCachePolicyRetain() {
|
|
|
|
if err := ref.ImmutableRef.SetCachePolicyRetain(); err != nil {
|
2018-07-23 23:27:01 +00:00
|
|
|
return solver.CacheResult{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 16:43:27 +00:00
|
|
|
return solver.CacheResult{ID: ref.ID(), CreatedAt: createdAt}, nil
|
2018-07-23 23:27:01 +00:00
|
|
|
}
|
|
|
|
func (s *cacheResultStorage) Load(ctx context.Context, res solver.CacheResult) (solver.Result, error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
return s.load(ctx, res.ID, false)
|
2018-07-23 23:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cacheResultStorage) getWorkerRef(id string) (Worker, string, error) {
|
|
|
|
workerID, refID, err := parseWorkerRef(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
w, err := s.wc.Get(workerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return w, refID, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (s *cacheResultStorage) load(ctx context.Context, id string, hidden bool) (solver.Result, error) {
|
2018-07-23 23:27:01 +00:00
|
|
|
w, refID, err := s.getWorkerRef(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if refID == "" {
|
|
|
|
return NewWorkerRefResult(nil, w), nil
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
ref, err := w.LoadRef(ctx, refID, hidden)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewWorkerRefResult(ref, w), nil
|
|
|
|
}
|
|
|
|
|
2022-02-01 05:42:15 +00:00
|
|
|
func (s *cacheResultStorage) LoadRemotes(ctx context.Context, res solver.CacheResult, compressionopt *compression.Config, g session.Group) ([]*solver.Remote, error) {
|
2018-07-23 23:27:01 +00:00
|
|
|
w, refID, err := s.getWorkerRef(res.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
ref, err := w.LoadRef(ctx, refID, true)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 02:34:40 +00:00
|
|
|
if ref != nil {
|
|
|
|
defer ref.Release(context.TODO())
|
|
|
|
}
|
2020-11-13 11:09:10 +00:00
|
|
|
wref := WorkerRef{ref, w}
|
2021-10-05 06:43:22 +00:00
|
|
|
all := true // load as many compression blobs as possible
|
|
|
|
if compressionopt == nil {
|
2022-02-01 05:42:15 +00:00
|
|
|
comp := compression.New(compression.Default)
|
|
|
|
compressionopt = &comp
|
2021-10-05 06:43:22 +00:00
|
|
|
all = false
|
|
|
|
}
|
2022-02-09 21:20:42 +00:00
|
|
|
refCfg := cacheconfig.RefConfig{
|
|
|
|
Compression: *compressionopt,
|
|
|
|
}
|
|
|
|
remotes, err := wref.GetRemotes(ctx, false, refCfg, all, g)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil // ignore error. loadRemote is best effort
|
|
|
|
}
|
2021-10-05 06:43:22 +00:00
|
|
|
return remotes, nil
|
2018-07-23 23:27:01 +00:00
|
|
|
}
|
|
|
|
func (s *cacheResultStorage) Exists(id string) bool {
|
2020-05-28 20:46:33 +00:00
|
|
|
ref, err := s.load(context.TODO(), id, true)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ref.Release(context.TODO())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseWorkerRef(id string) (string, string, error) {
|
|
|
|
parts := strings.Split(id, "::")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", errors.Errorf("invalid workerref id: %s", id)
|
|
|
|
}
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|