2018-05-14 18:05:49 +00:00
|
|
|
package buildkit
|
|
|
|
|
|
|
|
import (
|
2018-05-18 05:47:34 +00:00
|
|
|
"net/http"
|
2018-05-14 18:05:49 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/content/local"
|
2019-03-15 01:32:21 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2018-09-05 02:12:44 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/containerimage"
|
2019-03-14 21:27:18 +00:00
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/localinlinecache"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/docker/docker/builder/builder-next/adapters/snapshot"
|
|
|
|
containerimageexp "github.com/docker/docker/builder/builder-next/exporter"
|
2018-09-01 01:28:25 +00:00
|
|
|
"github.com/docker/docker/builder/builder-next/imagerefchecker"
|
2018-05-14 18:05:49 +00:00
|
|
|
mobyworker "github.com/docker/docker/builder/builder-next/worker"
|
2018-09-05 02:12:44 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2018-09-05 02:12:44 +00:00
|
|
|
units "github.com/docker/go-units"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2019-02-12 02:37:22 +00:00
|
|
|
"github.com/moby/buildkit/cache/remotecache"
|
2019-02-28 06:35:26 +00:00
|
|
|
inlineremotecache "github.com/moby/buildkit/cache/remotecache/inline"
|
2019-04-24 01:43:39 +00:00
|
|
|
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
|
2018-09-05 02:12:44 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/moby/buildkit/control"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
2018-07-11 22:05:57 +00:00
|
|
|
dockerfile "github.com/moby/buildkit/frontend/dockerfile/builder"
|
2018-05-19 01:25:16 +00:00
|
|
|
"github.com/moby/buildkit/frontend/gateway"
|
2018-07-11 22:05:57 +00:00
|
|
|
"github.com/moby/buildkit/frontend/gateway/forwarder"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/moby/buildkit/snapshot/blobmapping"
|
2018-09-21 20:43:26 +00:00
|
|
|
"github.com/moby/buildkit/solver/bboltcachestorage"
|
2019-03-15 01:32:21 +00:00
|
|
|
"github.com/moby/buildkit/util/binfmt_misc"
|
2019-04-03 04:55:22 +00:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
2019-03-15 01:32:21 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-05-14 18:05:49 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-05-18 05:47:34 +00:00
|
|
|
func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
|
2019-06-10 23:28:01 +00:00
|
|
|
if err := os.MkdirAll(opt.Root, 0711); err != nil {
|
2018-05-14 18:05:49 +00: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 23:28:01 +00:00
|
|
|
GraphDriver: driver,
|
|
|
|
LayerStore: dist.LayerStore,
|
|
|
|
Root: root,
|
|
|
|
IdentityMapping: opt.IdentityMapping,
|
2018-05-14 18:05:49 +00: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-09-01 01:28:25 +00: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 18:05:49 +00:00
|
|
|
cm, err := cache.NewManager(cache.ManagerOpt{
|
2018-09-01 01:28:25 +00:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
MetadataStore: md,
|
|
|
|
PruneRefChecker: refChecker,
|
2018-05-14 18:05:49 +00: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 23:07:16 +00:00
|
|
|
ResolverOpt: opt.ResolverOpt,
|
2018-05-14 18:05:49 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-06 01:36:33 +00:00
|
|
|
dns := getDNSConfig(opt.DNSConfig)
|
|
|
|
|
|
|
|
exec, err := newExecutor(root, opt.DefaultCgroupParent, opt.NetworkController, dns, opt.Rootless, opt.IdentityMapping)
|
2018-05-14 18:05:49 +00: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 20:43:26 +00:00
|
|
|
cacheStorage, err := bboltcachestorage.NewStore(filepath.Join(opt.Root, "cache.db"))
|
2018-05-14 18:05:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-05 02:12:44 +00:00
|
|
|
gcPolicy, err := getGCPolicy(opt.BuilderConfig, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not get builder GC policy")
|
|
|
|
}
|
|
|
|
|
2019-03-12 02:04:08 +00:00
|
|
|
layers, ok := sbase.(mobyworker.LayerAccess)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("snapshotter doesn't support differ")
|
|
|
|
}
|
|
|
|
|
2019-03-15 01:32:21 +00:00
|
|
|
p, err := parsePlatforms(binfmt_misc.SupportedPlatforms())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:53:14 +00:00
|
|
|
wopt := mobyworker.Opt{
|
2018-05-14 18:05:49 +00:00
|
|
|
ID: "moby",
|
|
|
|
MetadataStore: md,
|
|
|
|
ContentStore: store,
|
|
|
|
CacheManager: cm,
|
2018-09-05 02:12:44 +00:00
|
|
|
GCPolicy: gcPolicy,
|
2018-05-14 18:05:49 +00:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
Executor: exec,
|
|
|
|
ImageSource: src,
|
|
|
|
DownloadManager: dist.DownloadManager,
|
|
|
|
V2MetadataService: dist.V2MetadataService,
|
2019-02-28 06:35:26 +00:00
|
|
|
Exporter: exp,
|
|
|
|
Transport: rt,
|
2019-03-12 02:04:08 +00:00
|
|
|
Layers: layers,
|
2019-03-15 01:32:21 +00:00
|
|
|
Platforms: p,
|
2018-05-14 18:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wc := &worker.Controller{}
|
|
|
|
w, err := mobyworker.NewWorker(wopt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wc.Add(w)
|
|
|
|
|
2018-07-11 22:05:57 +00:00
|
|
|
frontends := map[string]frontend.Frontend{
|
|
|
|
"dockerfile.v0": forwarder.NewGatewayForwarder(wc, dockerfile.Build),
|
|
|
|
"gateway.v0": gateway.NewGatewayFrontend(wc),
|
|
|
|
}
|
2018-05-14 18:05:49 +00:00
|
|
|
|
|
|
|
return control.NewController(control.Opt{
|
2019-02-12 02:37:22 +00:00
|
|
|
SessionManager: opt.SessionManager,
|
|
|
|
WorkerController: wc,
|
|
|
|
Frontends: frontends,
|
|
|
|
CacheKeyStorage: cacheStorage,
|
|
|
|
ResolveCacheImporterFuncs: map[string]remotecache.ResolveCacheImporterFunc{
|
2019-10-03 00:16:29 +00:00
|
|
|
"registry": localinlinecache.ResolveCacheImporterFunc(opt.SessionManager, opt.ResolverOpt, store, dist.ReferenceStore, dist.ImageStore),
|
2019-04-24 01:43:39 +00:00
|
|
|
"local": localremotecache.ResolveCacheImporterFunc(opt.SessionManager),
|
2019-02-12 02:37:22 +00:00
|
|
|
},
|
2019-02-28 06:35:26 +00:00
|
|
|
ResolveCacheExporterFuncs: map[string]remotecache.ResolveCacheExporterFunc{
|
|
|
|
"inline": inlineremotecache.ResolveCacheExporterFunc(),
|
|
|
|
},
|
2019-04-26 06:04:34 +00:00
|
|
|
Entitlements: getEntitlements(opt.BuilderConfig),
|
2018-05-14 18:05:49 +00:00
|
|
|
})
|
|
|
|
}
|
2018-09-05 02:12:44 +00: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-15 01:32:21 +00: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
|
|
|
|
}
|
2019-04-26 06:04:34 +00:00
|
|
|
|
|
|
|
func getEntitlements(conf config.BuilderConfig) []string {
|
|
|
|
var ents []string
|
|
|
|
// Incase of no config settings, NetworkHost should be enabled & SecurityInsecure must be disabled.
|
|
|
|
if conf.Entitlements.NetworkHost == nil || *conf.Entitlements.NetworkHost {
|
|
|
|
ents = append(ents, string(entitlements.EntitlementNetworkHost))
|
|
|
|
}
|
|
|
|
if conf.Entitlements.SecurityInsecure != nil && *conf.Entitlements.SecurityInsecure {
|
|
|
|
ents = append(ents, string(entitlements.EntitlementSecurityInsecure))
|
|
|
|
}
|
|
|
|
return ents
|
|
|
|
}
|