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"
|
2019-03-14 21:32:21 -04:00
|
|
|
"github.com/containerd/containerd/platforms"
|
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"
|
2019-03-14 17:27:18 -04:00
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/localinlinecache"
|
2018-05-14 14:05:49 -04:00
|
|
|
"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"
|
2019-02-28 01:35:26 -05:00
|
|
|
inlineremotecache "github.com/moby/buildkit/cache/remotecache/inline"
|
2019-04-23 21:43:39 -04:00
|
|
|
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
|
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/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"
|
2019-03-14 21:32:21 -04:00
|
|
|
"github.com/moby/buildkit/util/binfmt_misc"
|
2019-04-03 00:55:22 -04:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/worker"
|
2019-03-14 21:32:21 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-18 01:47:34 -04:00
|
|
|
func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
|
2019-06-10 19:28:01 -04:00
|
|
|
if err := os.MkdirAll(opt.Root, 0711); err != nil {
|
2018-05-14 14:05:49 -04:00
|
|
|
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{
|
2019-06-10 19:28:01 -04:00
|
|
|
GraphDriver: driver,
|
|
|
|
LayerStore: dist.LayerStore,
|
|
|
|
Root: root,
|
|
|
|
IdentityMapping: opt.IdentityMapping,
|
2018-05-14 14:05:49 -04:00
|
|
|
})
|
|
|
|
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{
|
|
|
|
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-06-05 21:36:33 -04:00
|
|
|
dns := getDNSConfig(opt.DNSConfig)
|
|
|
|
|
|
|
|
exec, err := newExecutor(root, opt.DefaultCgroupParent, opt.NetworkController, dns, opt.Rootless, opt.IdentityMapping)
|
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")
|
|
|
|
}
|
|
|
|
|
2019-03-11 22:04:08 -04:00
|
|
|
layers, ok := sbase.(mobyworker.LayerAccess)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("snapshotter doesn't support differ")
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:32:21 -04:00
|
|
|
p, err := parsePlatforms(binfmt_misc.SupportedPlatforms())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
wopt := mobyworker.Opt{
|
2018-05-14 14:05:49 -04:00
|
|
|
ID: "moby",
|
|
|
|
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,
|
2019-02-28 01:35:26 -05:00
|
|
|
Exporter: exp,
|
|
|
|
Transport: rt,
|
2019-03-11 22:04:08 -04:00
|
|
|
Layers: layers,
|
2019-03-14 21:32:21 -04:00
|
|
|
Platforms: p,
|
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{
|
2019-03-14 17:27:18 -04:00
|
|
|
"registry": localinlinecache.ResolveCacheImporterFunc(opt.SessionManager, opt.ResolverOpt, dist.ReferenceStore, dist.ImageStore),
|
2019-04-23 21:43:39 -04:00
|
|
|
"local": localremotecache.ResolveCacheImporterFunc(opt.SessionManager),
|
2019-02-11 21:37:22 -05:00
|
|
|
},
|
2019-02-28 01:35:26 -05:00
|
|
|
ResolveCacheExporterFuncs: map[string]remotecache.ResolveCacheExporterFunc{
|
|
|
|
"inline": inlineremotecache.ResolveCacheExporterFunc(),
|
|
|
|
},
|
2019-04-03 00:55:22 -04:00
|
|
|
Entitlements: []string{
|
|
|
|
string(entitlements.EntitlementNetworkHost),
|
|
|
|
// string(entitlements.EntitlementSecurityInsecure),
|
|
|
|
},
|
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
|
|
|
|
}
|
2019-03-14 21:32:21 -04:00
|
|
|
|
|
|
|
func parsePlatforms(platformsStr []string) ([]specs.Platform, error) {
|
|
|
|
out := make([]specs.Platform, 0, len(platformsStr))
|
|
|
|
for _, s := range platformsStr {
|
|
|
|
p, err := platforms.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, platforms.Normalize(p))
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|