/
workeropt.go
137 lines (120 loc) · 3.97 KB
/
workeropt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package client
import (
"context"
"fmt"
"path/filepath"
"time"
"github.com/boltdb/bolt"
"github.com/containerd/containerd/content/local"
"github.com/containerd/containerd/diff/apply"
"github.com/containerd/containerd/diff/walking"
ctdmetadata "github.com/containerd/containerd/metadata"
"github.com/containerd/containerd/platforms"
ctdsnapshot "github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/native"
"github.com/containerd/containerd/snapshots/overlay"
"github.com/genuinetools/img/types"
"github.com/moby/buildkit/cache/metadata"
"github.com/moby/buildkit/executor"
"github.com/moby/buildkit/executor/runcexecutor"
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
"github.com/moby/buildkit/util/network"
"github.com/moby/buildkit/util/resolver"
"github.com/moby/buildkit/util/throttle"
"github.com/moby/buildkit/worker/base"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/sirupsen/logrus"
)
// createWorkerOpt creates a base.WorkerOpt to be used for a new worker.
func (c *Client) createWorkerOpt(withExecutor bool) (opt base.WorkerOpt, err error) {
sm, err := c.getSessionManager()
if err != nil {
return opt, err
}
// Create the metadata store.
md, err := metadata.NewStore(filepath.Join(c.root, "metadata.db"))
if err != nil {
return opt, err
}
snapshotRoot := filepath.Join(c.root, "snapshots")
unprivileged := system.GetParentNSeuid() != 0
// Create the snapshotter.
var (
s ctdsnapshot.Snapshotter
)
switch c.backend {
case types.NativeBackend:
s, err = native.NewSnapshotter(snapshotRoot)
case types.OverlayFSBackend:
// On some distros such as Ubuntu overlayfs can be mounted without privileges
s, err = overlay.NewSnapshotter(snapshotRoot)
default:
// "auto" backend needs to be already resolved on Client instantiation
return opt, fmt.Errorf("%s is not a valid snapshots backend", c.backend)
}
if err != nil {
return opt, fmt.Errorf("creating %s snapshotter failed: %v", c.backend, err)
}
var exe executor.Executor
if withExecutor {
exeOpt := runcexecutor.Opt{
Root: filepath.Join(c.root, "executor"),
Rootless: unprivileged,
}
exe, err = runcexecutor.New(exeOpt, network.Default())
if err != nil {
return opt, err
}
}
// Create the content store locally.
contentStore, err := local.NewStore(filepath.Join(c.root, "content"))
if err != nil {
return opt, err
}
// Open the bolt database for metadata.
db, err := bolt.Open(filepath.Join(c.root, "containerdmeta.db"), 0644, nil)
if err != nil {
return opt, err
}
// Create the new database for metadata.
mdb := ctdmetadata.NewDB(db, contentStore, map[string]ctdsnapshot.Snapshotter{
c.backend: s,
})
if err := mdb.Init(context.TODO()); err != nil {
return opt, err
}
// Create the image store.
imageStore := ctdmetadata.NewImageStore(mdb)
// Create the garbage collector.
throttledGC := throttle.Throttle(time.Second, func() {
if _, err := mdb.GarbageCollect(context.TODO()); err != nil {
logrus.Errorf("GC error: %+v", err)
}
})
gc := func(ctx context.Context) error {
throttledGC()
return nil
}
contentStore = containerdsnapshot.NewContentStore(mdb.ContentStore(), "buildkit", gc)
id, err := base.ID(c.root)
if err != nil {
return opt, err
}
xlabels := base.Labels("oci", c.backend)
opt = base.WorkerOpt{
ID: id,
Labels: xlabels,
SessionManager: sm,
MetadataStore: md,
Executor: exe,
Snapshotter: containerdsnapshot.NewSnapshotter(mdb.Snapshotter(c.backend), contentStore, md, "buildkit", gc),
ContentStore: contentStore,
Applier: apply.NewFileSystemApplier(contentStore),
Differ: walking.NewWalkingDiff(contentStore),
ImageStore: imageStore,
Platforms: []specs.Platform{platforms.Normalize(platforms.DefaultSpec())},
ResolveOptionsFunc: resolver.NewResolveOptionsFunc(nil),
}
return opt, err
}