Skip to content

Commit 4490066

Browse files
authored
Merge pull request moby#5086 from Benehiko/containerd-worker-parameters
Refactor containerd `NewWorkerOpt` & containerdexecutor `New` parameters
2 parents 966302e + c3925da commit 4490066

File tree

4 files changed

+114
-57
lines changed

4 files changed

+114
-57
lines changed

cmd/buildkitd/main_containerd_worker.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,25 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
321321
Options: opts,
322322
}
323323
}
324-
opt, err := containerd.NewWorkerOpt(common.config.Root, cfg.Address, snapshotter, cfg.Namespace, cfg.DefaultCgroupParent, 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))
324+
325+
workerOpts := containerd.WorkerOptions{
326+
Root: common.config.Root,
327+
Address: cfg.Address,
328+
SnapshotterName: snapshotter,
329+
Namespace: cfg.Namespace,
330+
CgroupParent: cfg.DefaultCgroupParent,
331+
Rootless: cfg.Rootless,
332+
Labels: cfg.Labels,
333+
DNS: dns,
334+
NetworkOpt: nc,
335+
ApparmorProfile: common.config.Workers.Containerd.ApparmorProfile,
336+
Selinux: common.config.Workers.Containerd.SELinux,
337+
ParallelismSem: parallelismSem,
338+
TraceSocket: common.traceSocket,
339+
Runtime: runtime,
340+
}
341+
342+
opt, err := containerd.NewWorkerOpt(workerOpts, ctd.WithTimeout(60*time.Second))
325343
if err != nil {
326344
return nil, err
327345
}

executor/containerdexecutor/executor.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,37 @@ type RuntimeInfo struct {
6060
Options any
6161
}
6262

63+
type ExecutorOptions struct {
64+
Client *containerd.Client
65+
Root string
66+
CgroupParent string
67+
NetworkProviders map[pb.NetMode]network.Provider
68+
DNSConfig *oci.DNSConfig
69+
ApparmorProfile string
70+
Selinux bool
71+
TraceSocket string
72+
Rootless bool
73+
Runtime *RuntimeInfo
74+
}
75+
6376
// New creates a new executor backed by connection to containerd API
64-
func New(client *containerd.Client, root, cgroup string, networkProviders map[pb.NetMode]network.Provider, dnsConfig *oci.DNSConfig, apparmorProfile string, selinux bool, traceSocket string, rootless bool, runtime *RuntimeInfo) executor.Executor {
77+
func New(executorOpts ExecutorOptions) executor.Executor {
6578
// clean up old hosts/resolv.conf file. ignore errors
66-
os.RemoveAll(filepath.Join(root, "hosts"))
67-
os.RemoveAll(filepath.Join(root, "resolv.conf"))
79+
os.RemoveAll(filepath.Join(executorOpts.Root, "hosts"))
80+
os.RemoveAll(filepath.Join(executorOpts.Root, "resolv.conf"))
6881

6982
return &containerdExecutor{
70-
client: client,
71-
root: root,
72-
networkProviders: networkProviders,
73-
cgroupParent: cgroup,
74-
dnsConfig: dnsConfig,
83+
client: executorOpts.Client,
84+
root: executorOpts.Root,
85+
networkProviders: executorOpts.NetworkProviders,
86+
cgroupParent: executorOpts.CgroupParent,
87+
dnsConfig: executorOpts.DNSConfig,
7588
running: make(map[string]*containerState),
76-
apparmorProfile: apparmorProfile,
77-
selinux: selinux,
78-
traceSocket: traceSocket,
79-
rootless: rootless,
80-
runtime: runtime,
89+
apparmorProfile: executorOpts.ApparmorProfile,
90+
selinux: executorOpts.Selinux,
91+
traceSocket: executorOpts.TraceSocket,
92+
rootless: executorOpts.Rootless,
93+
runtime: executorOpts.Runtime,
8194
}
8295
}
8396

worker/containerd/containerd.go

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,31 @@ import (
3030

3131
type RuntimeInfo = containerdexecutor.RuntimeInfo
3232

33+
type WorkerOptions struct {
34+
Root string
35+
Address string
36+
SnapshotterName string
37+
Namespace string
38+
CgroupParent string
39+
Rootless bool
40+
Labels map[string]string
41+
DNS *oci.DNSConfig
42+
NetworkOpt netproviders.Opt
43+
ApparmorProfile string
44+
Selinux bool
45+
ParallelismSem *semaphore.Weighted
46+
TraceSocket string
47+
Runtime *RuntimeInfo
48+
}
49+
3350
// NewWorkerOpt creates a WorkerOpt.
3451
func NewWorkerOpt(
35-
root string,
36-
address, snapshotterName, ns, cgroupParent string,
37-
rootless bool,
38-
labels map[string]string,
39-
dns *oci.DNSConfig,
40-
nopt netproviders.Opt,
41-
apparmorProfile string,
42-
selinux bool,
43-
parallelismSem *semaphore.Weighted,
44-
traceSocket string,
45-
runtime *RuntimeInfo,
52+
workerOpts WorkerOptions,
4653
opts ...containerd.ClientOpt,
4754
) (base.WorkerOpt, error) {
48-
opts = append(opts, containerd.WithDefaultNamespace(ns))
55+
opts = append(opts, containerd.WithDefaultNamespace(workerOpts.Namespace))
56+
57+
address := workerOpts.Address
4958

5059
if goRuntime.GOOS == "windows" {
5160
// TODO(profnandaa): once the upstream PR[1] is merged and
@@ -58,29 +67,17 @@ func NewWorkerOpt(
5867
return base.WorkerOpt{}, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", address)
5968
}
6069
return newContainerd(
61-
root,
6270
client,
63-
snapshotterName,
64-
ns,
65-
cgroupParent,
66-
rootless,
67-
labels,
68-
dns,
69-
nopt,
70-
apparmorProfile,
71-
selinux,
72-
parallelismSem,
73-
traceSocket,
74-
runtime,
71+
workerOpts,
7572
)
7673
}
7774

78-
func newContainerd(root string, client *containerd.Client, snapshotterName, ns, cgroupParent string, rootless bool, labels map[string]string, dns *oci.DNSConfig, nopt netproviders.Opt, apparmorProfile string, selinux bool, parallelismSem *semaphore.Weighted, traceSocket string, runtime *RuntimeInfo) (base.WorkerOpt, error) {
79-
if strings.Contains(snapshotterName, "/") {
80-
return base.WorkerOpt{}, errors.Errorf("bad snapshotter name: %q", snapshotterName)
75+
func newContainerd(client *containerd.Client, workerOpts WorkerOptions) (base.WorkerOpt, error) {
76+
if strings.Contains(workerOpts.SnapshotterName, "/") {
77+
return base.WorkerOpt{}, errors.Errorf("bad snapshotter name: %q", workerOpts.SnapshotterName)
8178
}
82-
name := "containerd-" + snapshotterName
83-
root = filepath.Join(root, name)
79+
name := "containerd-" + workerOpts.SnapshotterName
80+
root := filepath.Join(workerOpts.Root, name)
8481
if err := os.MkdirAll(root, 0700); err != nil {
8582
return base.WorkerOpt{}, errors.Wrapf(err, "failed to create %s", root)
8683
}
@@ -97,7 +94,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
9794
return base.WorkerOpt{}, err
9895
}
9996

100-
np, npResolvedMode, err := netproviders.Providers(nopt)
97+
np, npResolvedMode, err := netproviders.Providers(workerOpts.NetworkOpt)
10198
if err != nil {
10299
return base.WorkerOpt{}, err
103100
}
@@ -108,21 +105,21 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
108105
}
109106
xlabels := map[string]string{
110107
wlabel.Executor: "containerd",
111-
wlabel.Snapshotter: snapshotterName,
108+
wlabel.Snapshotter: workerOpts.SnapshotterName,
112109
wlabel.Hostname: hostname,
113110
wlabel.Network: npResolvedMode,
114-
wlabel.SELinuxEnabled: strconv.FormatBool(selinux),
111+
wlabel.SELinuxEnabled: strconv.FormatBool(workerOpts.Selinux),
115112
}
116-
if apparmorProfile != "" {
117-
xlabels[wlabel.ApparmorProfile] = apparmorProfile
113+
if workerOpts.ApparmorProfile != "" {
114+
xlabels[wlabel.ApparmorProfile] = workerOpts.ApparmorProfile
118115
}
119-
xlabels[wlabel.ContainerdNamespace] = ns
116+
xlabels[wlabel.ContainerdNamespace] = workerOpts.Namespace
120117
xlabels[wlabel.ContainerdUUID] = serverInfo.UUID
121-
for k, v := range labels {
118+
for k, v := range workerOpts.Labels {
122119
xlabels[k] = v
123120
}
124121

125-
lm := leaseutil.WithNamespace(client.LeasesService(), ns)
122+
lm := leaseutil.WithNamespace(client.LeasesService(), workerOpts.Namespace)
126123

127124
gc := func(ctx context.Context) (gc.Stats, error) {
128125
l, err := lm.Create(ctx)
@@ -132,7 +129,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
132129
return nil, lm.Delete(ctx, leases.Lease{ID: l.ID}, leases.SynchronousDelete)
133130
}
134131

135-
cs := containerdsnapshot.NewContentStore(client.ContentStore(), ns)
132+
cs := containerdsnapshot.NewContentStore(client.ContentStore(), workerOpts.Namespace)
136133

137134
resp, err := client.IntrospectionService().Plugins(context.TODO(), []string{"type==io.containerd.runtime.v1", "type==io.containerd.runtime.v2"})
138135
if err != nil {
@@ -154,7 +151,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
154151
}
155152
}
156153

157-
snap := containerdsnapshot.NewSnapshotter(snapshotterName, client.SnapshotService(snapshotterName), ns, nil)
154+
snap := containerdsnapshot.NewSnapshotter(workerOpts.SnapshotterName, client.SnapshotService(workerOpts.SnapshotterName), workerOpts.Namespace, nil)
158155

159156
if err := cache.MigrateV2(
160157
context.TODO(),
@@ -172,12 +169,25 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
172169
return base.WorkerOpt{}, err
173170
}
174171

172+
executorOpts := containerdexecutor.ExecutorOptions{
173+
Client: client,
174+
Root: root,
175+
CgroupParent: workerOpts.CgroupParent,
176+
ApparmorProfile: workerOpts.ApparmorProfile,
177+
DNSConfig: workerOpts.DNS,
178+
Selinux: workerOpts.Selinux,
179+
TraceSocket: workerOpts.TraceSocket,
180+
Rootless: workerOpts.Rootless,
181+
Runtime: workerOpts.Runtime,
182+
NetworkProviders: np,
183+
}
184+
175185
opt := base.WorkerOpt{
176186
ID: id,
177187
Labels: xlabels,
178188
MetadataStore: md,
179189
NetworkProviders: np,
180-
Executor: containerdexecutor.New(client, root, cgroupParent, np, dns, apparmorProfile, selinux, traceSocket, rootless, runtime),
190+
Executor: containerdexecutor.New(executorOpts),
181191
Snapshotter: snap,
182192
ContentStore: cs,
183193
Applier: winlayers.NewFileSystemApplierWithWindows(cs, df),
@@ -186,7 +196,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns,
186196
Platforms: platformSpecs,
187197
LeaseManager: lm,
188198
GarbageCollect: gc,
189-
ParallelismSem: parallelismSem,
199+
ParallelismSem: workerOpts.ParallelismSem,
190200
MountPoolRoot: filepath.Join(root, "cachemounts"),
191201
}
192202
return opt, nil

worker/containerd/containerd_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,23 @@ func TestContainerdWorkerIntegration(t *testing.T) {
3131
func newWorkerOpt(t *testing.T, addr string) base.WorkerOpt {
3232
tmpdir := t.TempDir()
3333
rootless := false
34-
workerOpt, err := NewWorkerOpt(tmpdir, addr, "overlayfs", "buildkit-test", "", rootless, nil, nil, netproviders.Opt{Mode: "host"}, "", false, nil, "", nil)
34+
options := WorkerOptions{
35+
Root: tmpdir,
36+
Address: addr,
37+
SnapshotterName: "overlayfs",
38+
Namespace: "buildkit-test",
39+
CgroupParent: "",
40+
Rootless: rootless,
41+
Labels: nil,
42+
DNS: nil,
43+
NetworkOpt: netproviders.Opt{Mode: "host"},
44+
ApparmorProfile: "",
45+
Selinux: false,
46+
ParallelismSem: nil,
47+
TraceSocket: "",
48+
Runtime: nil,
49+
}
50+
workerOpt, err := NewWorkerOpt(options)
3551
require.NoError(t, err)
3652
return workerOpt
3753
}

0 commit comments

Comments
 (0)