forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_oci_worker.go
195 lines (174 loc) · 4.91 KB
/
main_oci_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
// +build linux,!no_oci_worker
package main
import (
"os/exec"
"strconv"
ctdsnapshot "github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/native"
"github.com/containerd/containerd/snapshots/overlay"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"github.com/moby/buildkit/worker/runc"
"github.com/opencontainers/runc/libcontainer/system"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func init() {
defaultConf, _ := defaultConf()
enabledValue := func(b *bool) string {
if b == nil {
return "auto"
}
return strconv.FormatBool(*b)
}
if defaultConf.Workers.OCI.Snapshotter == "" {
defaultConf.Workers.OCI.Snapshotter = "auto"
}
flags := []cli.Flag{
cli.StringFlag{
Name: "oci-worker",
Usage: "enable oci workers (true/false/auto)",
Value: enabledValue(defaultConf.Workers.OCI.Enabled),
},
cli.StringSliceFlag{
Name: "oci-worker-labels",
Usage: "user-specific annotation labels (com.example.foo=bar)",
},
cli.StringFlag{
Name: "oci-worker-snapshotter",
Usage: "name of snapshotter (overlayfs or native)",
Value: defaultConf.Workers.OCI.Snapshotter,
},
cli.StringSliceFlag{
Name: "oci-worker-platform",
Usage: "override supported platforms for worker",
},
}
n := "oci-worker-rootless"
u := "enable rootless mode"
if system.RunningInUserNS() {
flags = append(flags, cli.BoolTFlag{
Name: n,
Usage: u,
})
} else {
flags = append(flags, cli.BoolFlag{
Name: n,
Usage: u,
})
}
registerWorkerInitializer(
workerInitializer{
fn: ociWorkerInitializer,
priority: 0,
},
flags...,
)
// TODO: allow multiple oci runtimes
}
func applyOCIFlags(c *cli.Context, cfg *config.Config) error {
if cfg.Workers.OCI.Snapshotter == "" {
cfg.Workers.OCI.Snapshotter = "auto"
}
if c.GlobalIsSet("oci-worker") {
boolOrAuto, err := parseBoolOrAuto(c.GlobalString("oci-worker"))
if err != nil {
return err
}
cfg.Workers.OCI.Enabled = boolOrAuto
}
labels, err := attrMap(c.GlobalStringSlice("oci-worker-labels"))
if err != nil {
return err
}
if cfg.Workers.OCI.Labels == nil {
cfg.Workers.OCI.Labels = make(map[string]string)
}
for k, v := range labels {
cfg.Workers.OCI.Labels[k] = v
}
if c.GlobalIsSet("oci-worker-snapshotter") {
cfg.Workers.OCI.Snapshotter = c.GlobalString("oci-worker-snapshotter")
}
if c.GlobalIsSet("rootless") || c.GlobalBool("rootless") {
cfg.Workers.OCI.Rootless = c.GlobalBool("rootless")
}
if c.GlobalIsSet("oci-worker-rootless") {
cfg.Workers.OCI.Rootless = c.GlobalBool("oci-worker-rootless")
}
if platforms := c.GlobalStringSlice("oci-worker-platform"); len(platforms) != 0 {
cfg.Workers.OCI.Platforms = platforms
}
return nil
}
func ociWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([]worker.Worker, error) {
if err := applyOCIFlags(c, common.config); err != nil {
return nil, err
}
cfg := common.config.Workers.OCI
if (cfg.Enabled == nil && !validOCIBinary()) || (cfg.Enabled != nil && !*cfg.Enabled) {
return nil, nil
}
snFactory, err := snapshotterFactory(common.config.Root, cfg.Snapshotter)
if err != nil {
return nil, err
}
if cfg.Rootless {
logrus.Debugf("running in rootless mode")
}
opt, err := runc.NewWorkerOpt(common.config.Root, snFactory, cfg.Rootless, cfg.Labels)
if err != nil {
return nil, err
}
opt.SessionManager = common.sessionManager
opt.GCPolicy = getGCPolicy(cfg.GCPolicy, common.config.Root)
opt.ResolveOptionsFunc = 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(opt)
if err != nil {
return nil, err
}
return []worker.Worker{w}, nil
}
func snapshotterFactory(commonRoot, name string) (runc.SnapshotterFactory, error) {
if name == "auto" {
if err := overlay.Supported(commonRoot); err == nil {
logrus.Debug("auto snapshotter: using overlayfs")
name = "overlayfs"
} else {
logrus.Debugf("auto snapshotter: using native, because overlayfs is not available for %s: %v", commonRoot, err)
name = "native"
}
}
snFactory := runc.SnapshotterFactory{
Name: name,
}
switch name {
case "native":
snFactory.New = native.NewSnapshotter
case "overlayfs": // not "overlay", for consistency with containerd snapshotter plugin ID.
snFactory.New = func(root string) (ctdsnapshot.Snapshotter, error) {
return overlay.NewSnapshotter(root)
}
default:
return snFactory, errors.Errorf("unknown snapshotter name: %q", name)
}
return snFactory, nil
}
func validOCIBinary() bool {
_, err := exec.LookPath("runc")
_, err1 := exec.LookPath("buildkit-runc")
if err != nil && err1 != nil {
logrus.Warnf("skipping oci worker, as runc does not exist")
return false
}
return true
}