2018-05-14 14:05:49 -04:00
|
|
|
package buildkit
|
|
|
|
|
|
|
|
import (
|
2019-10-22 02:15:56 -04:00
|
|
|
"context"
|
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-10-09 20:20:17 -04:00
|
|
|
ctdmetadata "github.com/containerd/containerd/metadata"
|
|
|
|
"github.com/containerd/containerd/snapshots"
|
2018-09-04 22:12:44 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2019-09-24 13:53:39 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
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"
|
2019-10-09 20:20:17 -04:00
|
|
|
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
|
2018-09-21 16:43:26 -04:00
|
|
|
"github.com/moby/buildkit/solver/bboltcachestorage"
|
2020-11-12 21:14:57 -05:00
|
|
|
"github.com/moby/buildkit/util/archutil"
|
2019-04-03 00:55:22 -04:00
|
|
|
"github.com/moby/buildkit/util/entitlements"
|
2019-10-09 20:20:17 -04:00
|
|
|
"github.com/moby/buildkit/util/leaseutil"
|
2018-05-14 14:05:49 -04:00
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
"github.com/pkg/errors"
|
2019-10-09 20:20:17 -04:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2018-05-14 14:05:49 -04:00
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
store, err := local.NewStore(filepath.Join(root, "content"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-09 20:20:17 -04:00
|
|
|
|
|
|
|
db, err := bolt.Open(filepath.Join(root, "containerdmeta.db"), 0644, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb := ctdmetadata.NewDB(db, store, map[string]snapshots.Snapshotter{})
|
|
|
|
|
|
|
|
store = containerdsnapshot.NewContentStore(mdb.ContentStore(), "buildkit")
|
2018-05-14 14:05:49 -04:00
|
|
|
|
2019-10-10 18:13:11 -04:00
|
|
|
lm := leaseutil.WithNamespace(ctdmetadata.NewLeaseManager(mdb), "buildkit")
|
|
|
|
|
|
|
|
snapshotter, lm, err := snapshot.NewSnapshotter(snapshot.Opt{
|
|
|
|
GraphDriver: driver,
|
|
|
|
LayerStore: dist.LayerStore,
|
|
|
|
Root: root,
|
|
|
|
IdentityMapping: opt.IdentityMapping,
|
|
|
|
}, lm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-15 01:28:06 -04:00
|
|
|
if err := cache.MigrateV2(context.Background(), filepath.Join(root, "metadata.db"), filepath.Join(root, "metadata_v2.db"), store, snapshotter, lm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
md, err := metadata.NewStore(filepath.Join(root, "metadata_v2.db"))
|
2018-05-14 14:05:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:20:17 -04:00
|
|
|
layerGetter, ok := snapshotter.(imagerefchecker.LayerGetter)
|
2018-08-31 21:28:25 -04:00
|
|
|
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,
|
2019-10-10 18:13:11 -04:00
|
|
|
LeaseManager: lm,
|
2019-10-09 20:20:17 -04:00
|
|
|
ContentStore: store,
|
2021-02-12 23:20:36 -05:00
|
|
|
GarbageCollect: mdb.GarbageCollect,
|
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,
|
2020-04-13 23:31:26 -04:00
|
|
|
RegistryHosts: opt.RegistryHosts,
|
2019-10-09 20:20:17 -04:00
|
|
|
LayerStore: dist.LayerStore,
|
2021-02-12 23:20:36 -05:00
|
|
|
LeaseManager: lm,
|
|
|
|
GarbageCollect: mdb.GarbageCollect,
|
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)
|
|
|
|
|
2020-10-09 13:20:48 -04:00
|
|
|
exec, err := newExecutor(root, opt.DefaultCgroupParent, opt.NetworkController, dns, opt.Rootless, opt.IdentityMapping, opt.ApparmorProfile)
|
2018-05-14 14:05:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-09 20:20:17 -04:00
|
|
|
differ, ok := snapshotter.(containerimageexp.Differ)
|
2018-05-14 14:05:49 -04:00
|
|
|
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-10-09 20:20:17 -04:00
|
|
|
layers, ok := snapshotter.(mobyworker.LayerAccess)
|
2019-03-11 22:04:08 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("snapshotter doesn't support differ")
|
|
|
|
}
|
|
|
|
|
2019-10-22 02:15:56 -04:00
|
|
|
leases, err := lm.List(context.TODO(), "labels.\"buildkit/lease.temporary\"")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, l := range leases {
|
|
|
|
lm.Delete(context.TODO(), l)
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:53:14 -04:00
|
|
|
wopt := mobyworker.Opt{
|
2018-05-14 14:05:49 -04:00
|
|
|
ID: "moby",
|
|
|
|
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,
|
2022-03-18 11:01:18 -04:00
|
|
|
Platforms: archutil.SupportedPlatforms(true),
|
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{
|
2020-04-13 23:31:26 -04:00
|
|
|
"registry": localinlinecache.ResolveCacheImporterFunc(opt.SessionManager, opt.RegistryHosts, store, 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-26 02:04:34 -04:00
|
|
|
Entitlements: getEntitlements(opt.BuilderConfig),
|
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,
|
2019-09-24 13:53:39 -04:00
|
|
|
Filters: filters.Args(p.Filter),
|
2018-09-04 22:12:44 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gcPolicy, nil
|
|
|
|
}
|
2019-03-14 21:32:21 -04:00
|
|
|
|
2019-04-26 02:04:34 -04: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
|
|
|
|
}
|