2017-05-27 06:12:13 +00:00
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-05-27 06:12:13 +00:00
|
|
|
"sync"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2019-02-23 12:56:04 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Source interface {
|
|
|
|
ID() string
|
2020-05-28 20:46:33 +00:00
|
|
|
Resolve(ctx context.Context, id Identifier, sm *session.Manager, vtx solver.Vertex) (SourceInstance, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 19:07:42 +00:00
|
|
|
type SourceInstance interface {
|
2021-09-17 23:39:19 +00:00
|
|
|
CacheKey(ctx context.Context, g session.Group, index int) (string, string, solver.CacheOpts, bool, error)
|
2020-06-30 01:06:02 +00:00
|
|
|
Snapshot(ctx context.Context, g session.Group) (cache.ImmutableRef, error)
|
2017-07-06 19:07:42 +00:00
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
type Manager struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
sources map[string]Source
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager() (*Manager, error) {
|
|
|
|
return &Manager{
|
|
|
|
sources: make(map[string]Source),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *Manager) Register(src Source) {
|
|
|
|
sm.mu.Lock()
|
|
|
|
sm.sources[src.ID()] = src
|
|
|
|
sm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (sm *Manager) Resolve(ctx context.Context, id Identifier, sessM *session.Manager, vtx solver.Vertex) (SourceInstance, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
sm.mu.Lock()
|
|
|
|
src, ok := sm.sources[id.ID()]
|
|
|
|
sm.mu.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
2017-11-11 02:10:10 +00:00
|
|
|
return nil, errors.Errorf("no handler for %s", id.ID())
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
return src.Resolve(ctx, id, sessM, vtx)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|