-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
main_containerd_worker.go
378 lines (341 loc) · 11.7 KB
/
main_containerd_worker.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//go:build linux || windows || freebsd
// +build linux windows freebsd
package main
import (
"context"
"os"
"runtime"
"strconv"
"strings"
"time"
ctd "github.com/containerd/containerd"
"github.com/containerd/containerd/defaults"
runtimeoptions "github.com/containerd/containerd/pkg/runtimeoptions/v1"
"github.com/containerd/containerd/pkg/userns"
"github.com/containerd/containerd/plugin"
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/network/cniprovider"
"github.com/moby/buildkit/util/network/netproviders"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"github.com/moby/buildkit/worker/containerd"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/sync/semaphore"
)
const (
defaultContainerdNamespace = "buildkit"
)
func init() {
defaultConf, _ := defaultConf()
enabledValue := func(b *bool) string {
if b == nil {
return "auto"
}
return strconv.FormatBool(*b)
}
if defaultConf.Workers.Containerd.Address == "" {
defaultConf.Workers.Containerd.Address = defaultContainerdAddress
}
if defaultConf.Workers.Containerd.Namespace == "" {
defaultConf.Workers.Containerd.Namespace = defaultContainerdNamespace
}
if defaultConf.Workers.Containerd.Runtime.Name == "" {
if runtime.GOOS == "freebsd" {
// TODO: this can be removed once containerd/containerd#8964 is included
defaultConf.Workers.Containerd.Runtime.Name = "wtf.sbk.runj.v1"
} else {
defaultConf.Workers.Containerd.Runtime.Name = defaults.DefaultRuntime
}
}
flags := []cli.Flag{
cli.StringFlag{
Name: "containerd-worker",
Usage: "enable containerd workers (true/false/auto)",
Value: enabledValue(defaultConf.Workers.Containerd.Enabled),
},
cli.StringFlag{
Name: "containerd-worker-addr",
Usage: "containerd socket",
Value: defaultConf.Workers.Containerd.Address,
},
cli.StringSliceFlag{
Name: "containerd-worker-labels",
Usage: "user-specific annotation labels (com.example.foo=bar)",
},
// TODO: containerd-worker-platform should be replaced by ability
// to set these from containerd configuration
cli.StringSliceFlag{
Name: "containerd-worker-platform",
Usage: "override supported platforms for worker",
Hidden: true,
},
cli.StringFlag{
Name: "containerd-worker-namespace",
Usage: "override containerd namespace",
Value: defaultConf.Workers.Containerd.Namespace,
Hidden: true,
},
cli.StringFlag{
Name: "containerd-worker-runtime",
Usage: "override containerd runtime",
Value: defaultConf.Workers.Containerd.Runtime.Name,
Hidden: true,
},
cli.StringFlag{
Name: "containerd-worker-net",
Usage: "worker network type (auto, cni or host)",
Value: defaultConf.Workers.Containerd.NetworkConfig.Mode,
},
cli.StringFlag{
Name: "containerd-cni-config-path",
Usage: "path of cni config file",
Value: defaultConf.Workers.Containerd.NetworkConfig.CNIConfigPath,
},
cli.StringFlag{
Name: "containerd-cni-binary-dir",
Usage: "path of cni binary files",
Value: defaultConf.Workers.Containerd.NetworkConfig.CNIBinaryPath,
},
cli.IntFlag{
Name: "containerd-cni-pool-size",
Usage: "size of cni network namespace pool",
Value: defaultConf.Workers.Containerd.NetworkConfig.CNIPoolSize,
},
cli.StringFlag{
Name: "containerd-worker-snapshotter",
Usage: "snapshotter name to use",
Value: ctd.DefaultSnapshotter,
},
cli.StringFlag{
Name: "containerd-worker-apparmor-profile",
Usage: "set the name of the apparmor profile applied to containers",
},
cli.BoolFlag{
Name: "containerd-worker-selinux",
Usage: "apply SELinux labels",
},
}
n := "containerd-worker-rootless"
u := "enable rootless mode"
if userns.RunningInUserNS() {
flags = append(flags, cli.BoolTFlag{
Name: n,
Usage: u,
})
} else {
flags = append(flags, cli.BoolFlag{
Name: n,
Usage: u,
})
}
if defaultConf.Workers.Containerd.GC == nil || *defaultConf.Workers.Containerd.GC {
flags = append(flags, cli.BoolTFlag{
Name: "containerd-worker-gc",
Usage: "Enable automatic garbage collection on worker",
})
} else {
flags = append(flags, cli.BoolFlag{
Name: "containerd-worker-gc",
Usage: "Enable automatic garbage collection on worker",
})
}
flags = append(flags, cli.Int64Flag{
Name: "containerd-worker-gc-keepstorage",
Usage: "Amount of storage GC keep locally (MB)",
Value: func() int64 {
keep := defaultConf.Workers.Containerd.GCKeepStorage.AsBytes(defaultConf.Root)
if keep == 0 {
keep = config.DetectDefaultGCCap().AsBytes(defaultConf.Root)
}
return keep / 1e6
}(),
Hidden: len(defaultConf.Workers.Containerd.GCPolicy) != 0,
})
registerWorkerInitializer(
workerInitializer{
fn: containerdWorkerInitializer,
// 1 is less preferred than 0 (runcCtor)
priority: 1,
},
flags...,
)
// TODO(AkihiroSuda): allow using multiple snapshotters. should be useful for some applications that does not work with the default overlay snapshotter. e.g. mysql (docker/for-linux#72)",
}
func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
if cfg.Workers.Containerd.Address == "" {
cfg.Workers.Containerd.Address = defaultContainerdAddress
}
if c.GlobalIsSet("containerd-worker") {
boolOrAuto, err := parseBoolOrAuto(c.GlobalString("containerd-worker"))
if err != nil {
return err
}
cfg.Workers.Containerd.Enabled = boolOrAuto
}
if c.GlobalIsSet("rootless") || c.GlobalBool("rootless") {
cfg.Workers.Containerd.Rootless = c.GlobalBool("rootless")
}
if c.GlobalIsSet("containerd-worker-rootless") {
if !userns.RunningInUserNS() || os.Geteuid() > 0 {
return errors.New("rootless mode requires to be executed as the mapped root in a user namespace; you may use RootlessKit for setting up the namespace")
}
cfg.Workers.Containerd.Rootless = c.GlobalBool("containerd-worker-rootless")
}
labels, err := attrMap(c.GlobalStringSlice("containerd-worker-labels"))
if err != nil {
return err
}
if cfg.Workers.Containerd.Labels == nil {
cfg.Workers.Containerd.Labels = make(map[string]string)
}
for k, v := range labels {
cfg.Workers.Containerd.Labels[k] = v
}
if c.GlobalIsSet("containerd-worker-addr") {
cfg.Workers.Containerd.Address = c.GlobalString("containerd-worker-addr")
}
if platforms := c.GlobalStringSlice("containerd-worker-platform"); len(platforms) != 0 {
cfg.Workers.Containerd.Platforms = platforms
}
if c.GlobalIsSet("containerd-worker-namespace") || cfg.Workers.Containerd.Namespace == "" {
cfg.Workers.Containerd.Namespace = c.GlobalString("containerd-worker-namespace")
}
if c.GlobalIsSet("containerd-worker-runtime") || cfg.Workers.Containerd.Runtime.Name == "" {
cfg.Workers.Containerd.Runtime = config.ContainerdRuntime{
Name: c.GlobalString("containerd-worker-runtime"),
}
}
if c.GlobalIsSet("containerd-worker-gc") {
v := c.GlobalBool("containerd-worker-gc")
cfg.Workers.Containerd.GC = &v
}
if c.GlobalIsSet("containerd-worker-gc-keepstorage") {
cfg.Workers.Containerd.GCKeepStorage = config.DiskSpace{Bytes: c.GlobalInt64("containerd-worker-gc-keepstorage") * 1e6}
}
if c.GlobalIsSet("containerd-worker-net") {
cfg.Workers.Containerd.NetworkConfig.Mode = c.GlobalString("containerd-worker-net")
}
if c.GlobalIsSet("containerd-cni-config-path") {
cfg.Workers.Containerd.NetworkConfig.CNIConfigPath = c.GlobalString("containerd-cni-config-path")
}
if c.GlobalIsSet("containerd-cni-pool-size") {
cfg.Workers.Containerd.NetworkConfig.CNIPoolSize = c.GlobalInt("containerd-cni-pool-size")
}
if c.GlobalIsSet("containerd-cni-binary-dir") {
cfg.Workers.Containerd.NetworkConfig.CNIBinaryPath = c.GlobalString("containerd-cni-binary-dir")
}
if c.GlobalIsSet("containerd-worker-snapshotter") {
cfg.Workers.Containerd.Snapshotter = c.GlobalString("containerd-worker-snapshotter")
}
if c.GlobalIsSet("containerd-worker-apparmor-profile") {
cfg.Workers.Containerd.ApparmorProfile = c.GlobalString("containerd-worker-apparmor-profile")
}
if c.GlobalIsSet("containerd-worker-selinux") {
cfg.Workers.Containerd.SELinux = c.GlobalBool("containerd-worker-selinux")
}
return nil
}
func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error) {
if err := applyContainerdFlags(c, common.config); err != nil {
return nil, err
}
cfg := common.config.Workers.Containerd
if (cfg.Enabled == nil && !validContainerdSocket(cfg)) || (cfg.Enabled != nil && !*cfg.Enabled) {
return nil, nil
}
if cfg.Rootless {
bklog.L.Debugf("running in rootless mode")
if common.config.Workers.Containerd.NetworkConfig.Mode == "auto" {
common.config.Workers.Containerd.NetworkConfig.Mode = "host"
}
}
dns := getDNSConfig(common.config.DNS)
nc := netproviders.Opt{
Mode: common.config.Workers.Containerd.NetworkConfig.Mode,
CNI: cniprovider.Opt{
Root: common.config.Root,
ConfigPath: common.config.Workers.Containerd.CNIConfigPath,
BinaryDir: common.config.Workers.Containerd.CNIBinaryPath,
PoolSize: common.config.Workers.Containerd.CNIPoolSize,
},
}
var parallelismSem *semaphore.Weighted
if cfg.MaxParallelism > 0 {
parallelismSem = semaphore.NewWeighted(int64(cfg.MaxParallelism))
}
snapshotter := ctd.DefaultSnapshotter
if cfg.Snapshotter != "" {
snapshotter = cfg.Snapshotter
}
var runtime *containerd.RuntimeInfo
if cfg.Runtime.Name != "" {
opts := getRuntimeOptionsType(cfg.Runtime.Name)
t, err := toml.TreeFromMap(cfg.Runtime.Options)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse runtime options config")
}
err = t.Unmarshal(opts)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse runtime options config")
}
runtime = &containerd.RuntimeInfo{
Name: cfg.Runtime.Name,
Options: opts,
}
}
opt, err := containerd.NewWorkerOpt(common.config.Root, cfg.Address, snapshotter, cfg.Namespace, cfg.Rootless, cfg.Labels, dns, nc, common.config.Workers.Containerd.ApparmorProfile, common.config.Workers.Containerd.SELinux, parallelismSem, common.traceSocket, runtime, ctd.WithTimeout(60*time.Second))
if err != nil {
return nil, err
}
opt.GCPolicy = getGCPolicy(cfg.GCConfig, common.config.Root)
opt.BuildkitVersion = getBuildkitVersion()
opt.RegistryHosts = resolverFunc(common.config)
if platformsStr := cfg.Platforms; len(platformsStr) != 0 {
platforms, err := parsePlatforms(platformsStr)
if err != nil {
return nil, errors.Wrap(err, "invalid platforms")
}
opt.Platforms = platforms
}
w, err := base.NewWorker(context.TODO(), opt)
if err != nil {
return nil, err
}
return []worker.Worker{w}, nil
}
func validContainerdSocket(cfg config.ContainerdConfig) bool {
socket := cfg.Address
if strings.HasPrefix(socket, "tcp://") {
// FIXME(AkihiroSuda): prohibit tcp?
return true
}
socketPath := strings.TrimPrefix(socket, "unix://")
if _, err := os.Stat(socketPath); errors.Is(err, os.ErrNotExist) {
// FIXME(AkihiroSuda): add more conditions
bklog.L.Warnf("skipping containerd worker, as %q does not exist", socketPath)
return false
}
c, err := ctd.New(socketPath, ctd.WithDefaultNamespace(cfg.Namespace))
if err != nil {
bklog.L.Warnf("skipping containerd worker, as failed to connect client to %q: %v", socketPath, err)
return false
}
if _, err := c.Server(context.Background()); err != nil {
bklog.L.Warnf("skipping containerd worker, as failed to call introspection API on %q: %v", socketPath, err)
return false
}
return true
}
// getRuntimeOptionsType gets empty runtime options by the runtime type name.
func getRuntimeOptionsType(t string) interface{} {
switch t {
case plugin.RuntimeRuncV2:
return &runcoptions.Options{}
default:
return &runtimeoptions.Options{}
}
}