2018-07-23 23:27:01 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"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 {
|
|
|
|
if !cache.HasCachePolicyRetain(ref.ImmutableRef) {
|
|
|
|
if err := cache.CachePolicyRetain(ref.ImmutableRef); err != nil {
|
|
|
|
return solver.CacheResult{}, err
|
|
|
|
}
|
|
|
|
ref.ImmutableRef.Metadata().Commit()
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2018-09-14 20:35:41 +00:00
|
|
|
return s.load(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
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
func (s *cacheResultStorage) load(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
|
|
|
|
}
|
2018-09-14 20:35:41 +00:00
|
|
|
ref, err := w.LoadRef(refID, hidden)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewWorkerRefResult(ref, w), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cacheResultStorage) LoadRemote(ctx context.Context, res solver.CacheResult) (*solver.Remote, error) {
|
|
|
|
w, refID, err := s.getWorkerRef(res.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-14 20:35:41 +00:00
|
|
|
ref, err := w.LoadRef(refID, true)
|
2018-07-23 23:27:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer ref.Release(context.TODO())
|
|
|
|
remote, err := w.GetRemote(ctx, ref, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil // ignore error. loadRemote is best effort
|
|
|
|
}
|
|
|
|
return remote, nil
|
|
|
|
}
|
|
|
|
func (s *cacheResultStorage) Exists(id string) bool {
|
2018-09-14 20:35:41 +00:00
|
|
|
ref, err := s.load(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
|
|
|
|
}
|