2018-05-14 14:05:49 -04:00
|
|
|
package buildkit
|
|
|
|
|
|
|
|
import (
|
2018-05-18 01:47:34 -04:00
|
|
|
"net/http"
|
2018-05-14 14:05:49 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content/local"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/containerimage"
|
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/snapshot"
|
|
|
|
containerimageexp "github.com/docker/docker/builder/builder-next/exporter"
|
2018-08-31 21:28:25 -04:00
|
|
|
"github.com/docker/docker/builder/builder-next/imagerefchecker"
|
2018-05-14 14:05:49 -04:00
|
|
|
mobyworker "github.com/docker/docker/builder/builder-next/worker"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2018-09-04 22:12:44 -04:00
|
|
|
units "github.com/docker/go-units"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2019-02-11 21:37:22 -05:00
|
|
|
"github.com/moby/buildkit/cache/remotecache"
|
2018-07-11 18:05:57 -04:00
|
|
|
registryremotecache "github.com/moby/buildkit/cache/remotecache/registry"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/moby/buildkit/client"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/exporter"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
2018-07-11 18:05:57 -04:00
|
|
|
dockerfile "github.com/moby/buildkit/frontend/dockerfile/builder"
|
2018-05-18 21:25:16 -04:00
|
|
|
"github.com/moby/buildkit/frontend/gateway"
|
2018-07-11 18:05:57 -04:00
|
|
|
"github.com/moby/buildkit/frontend/gateway/forwarder"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/snapshot/blobmapping"
|
2018-09-21 16:43:26 -04:00
|
|
|
"github.com/moby/buildkit/solver/bboltcachestorage"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-18 01:47:34 -04:00
|
|
|
func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
|
2018-05-14 14:05:49 -04:00
|
|
|
if err := os.MkdirAll(opt.Root, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dist := opt.Dist
|
|
|
|
root := opt.Root
|
|
|
|
|
|
|
|
var driver graphdriver.Driver
|
|
|
|
if ls, ok := dist.LayerStore.(interface {
|
|
|
|
Driver() graphdriver.Driver
|
|
|
|
}); ok {
|
|
|
|
driver = ls.Driver()
|
|
|
|
} else {
|
|
|
|
return nil, errors.Errorf("could not access graphdriver")
|
|
|
|
}
|
|
|
|
|
|
|
|
sbase, err := snapshot.NewSnapshotter(snapshot.Opt{
|
|
|
|
GraphDriver: driver,
|
|
|
|
LayerStore: dist.LayerStore,
|
|
|
|
Root: root,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store, err := local.NewStore(filepath.Join(root, "content"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
store = &contentStoreNoLabels{store}
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata.db"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshotter := blobmapping.NewSnapshotter(blobmapping.Opt{
|
|
|
|
Content: store,
|
|
|
|
Snapshotter: sbase,
|
|
|
|
MetadataStore: md,
|
|
|
|
})
|
|
|
|
|
2018-08-31 21:28:25 -04:00
|
|
|
layerGetter, ok := sbase.(imagerefchecker.LayerGetter)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("snapshotter does not implement layergetter")
|
|
|
|
}
|
|
|
|
|
|
|
|
refChecker := imagerefchecker.New(imagerefchecker.Opt{
|
|
|
|
ImageStore: dist.ImageStore,
|
|
|
|
LayerGetter: layerGetter,
|
|
|
|
})
|
|
|
|
|
2018-05-14 14:05:49 -04:00
|
|
|
cm, err := cache.NewManager(cache.ManagerOpt{
|
2018-08-31 21:28:25 -04:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
MetadataStore: md,
|
|
|
|
PruneRefChecker: refChecker,
|
2018-05-14 14:05:49 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := containerimage.NewSource(containerimage.SourceOpt{
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
CacheAccessor: cm,
|
|
|
|
ContentStore: store,
|
|
|
|
DownloadManager: dist.DownloadManager,
|
|
|
|
MetadataStore: dist.V2MetadataService,
|
|
|
|
ImageStore: dist.ImageStore,
|
|
|
|
ReferenceStore: dist.ReferenceStore,
|
2018-09-14 19:07:16 -04:00
|
|
|
ResolverOpt: opt.ResolverOpt,
|
2018-05-14 14:05:49 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:12:55 -05:00
|
|
|
exec, err := newExecutor(root, opt.DefaultCgroupParent, opt.NetworkController, opt.Rootless)
|
2018-05-14 14:05:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
differ, ok := sbase.(containerimageexp.Differ)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("snapshotter doesn't support differ")
|
|
|
|
}
|
|
|
|
|
|
|
|
exp, err := containerimageexp.New(containerimageexp.Opt{
|
|
|
|
ImageStore: dist.ImageStore,
|
|
|
|
ReferenceStore: dist.ReferenceStore,
|
|
|
|
Differ: differ,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:43:26 -04:00
|
|
|
cacheStorage, err := bboltcachestorage.NewStore(filepath.Join(opt.Root, "cache.db"))
|
2018-05-14 14:05:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:12:44 -04:00
|
|
|
gcPolicy, err := getGCPolicy(opt.BuilderConfig, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get builder GC policy")
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
wopt := mobyworker.Opt{
|
2018-05-14 14:05:49 -04:00
|
|
|
ID: "moby",
|
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
MetadataStore: md,
|
|
|
|
ContentStore: store,
|
|
|
|
CacheManager: cm,
|
2018-09-04 22:12:44 -04:00
|
|
|
GCPolicy: gcPolicy,
|
2018-05-14 14:05:49 -04:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
Executor: exec,
|
|
|
|
ImageSource: src,
|
|
|
|
DownloadManager: dist.DownloadManager,
|
|
|
|
V2MetadataService: dist.V2MetadataService,
|
|
|
|
Exporters: map[string]exporter.Exporter{
|
|
|
|
"moby": exp,
|
|
|
|
},
|
2018-05-18 01:47:34 -04:00
|
|
|
Transport: rt,
|
2018-05-14 14:05:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wc := &worker.Controller{}
|
|
|
|
w, err := mobyworker.NewWorker(wopt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wc.Add(w)
|
|
|
|
|
2018-07-11 18:05:57 -04:00
|
|
|
frontends := map[string]frontend.Frontend{
|
|
|
|
"dockerfile.v0": forwarder.NewGatewayForwarder(wc, dockerfile.Build),
|
|
|
|
"gateway.v0": gateway.NewGatewayFrontend(wc),
|
|
|
|
}
|
2018-05-14 14:05:49 -04:00
|
|
|
|
|
|
|
return control.NewController(control.Opt{
|
2019-02-11 21:37:22 -05:00
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
WorkerController: wc,
|
|
|
|
Frontends: frontends,
|
|
|
|
CacheKeyStorage: cacheStorage,
|
|
|
|
ResolveCacheImporterFuncs: map[string]remotecache.ResolveCacheImporterFunc{
|
|
|
|
"registry": registryremotecache.ResolveCacheImporterFunc(opt.SessionManager, opt.ResolverOpt),
|
|
|
|
},
|
2018-07-11 18:05:57 -04:00
|
|
|
// TODO: set ResolveCacheExporterFunc for exporting cache
|
2018-05-14 14:05:49 -04:00
|
|
|
})
|
|
|
|
}
|
2018-09-04 22:12:44 -04:00
|
|
|
|
|
|
|
func getGCPolicy(conf config.BuilderConfig, root string) ([]client.PruneInfo, error) {
|
|
|
|
var gcPolicy []client.PruneInfo
|
|
|
|
if conf.GC.Enabled {
|
|
|
|
var (
|
|
|
|
defaultKeepStorage int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if conf.GC.DefaultKeepStorage != "" {
|
|
|
|
defaultKeepStorage, err = units.RAMInBytes(conf.GC.DefaultKeepStorage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not parse '%s' as Builder.GC.DefaultKeepStorage config", conf.GC.DefaultKeepStorage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.GC.Policy == nil {
|
|
|
|
gcPolicy = mobyworker.DefaultGCPolicy(root, defaultKeepStorage)
|
|
|
|
} else {
|
|
|
|
gcPolicy = make([]client.PruneInfo, len(conf.GC.Policy))
|
|
|
|
for i, p := range conf.GC.Policy {
|
|
|
|
b, err := units.RAMInBytes(p.KeepStorage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if b == 0 {
|
|
|
|
b = defaultKeepStorage
|
|
|
|
}
|
|
|
|
gcPolicy[i], err = toBuildkitPruneInfo(types.BuildCachePruneOptions{
|
|
|
|
All: p.All,
|
|
|
|
KeepStorage: b,
|
|
|
|
Filters: p.Filter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gcPolicy, nil
|
|
|
|
}
|