Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NewWorkerOpt function so it accepts a pre-created containerd client #4365

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,13 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
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))

client, err := ctd.New(cfg.Address, ctd.WithTimeout(60*time.Second), ctd.WithDefaultNamespace(cfg.Namespace))
if err != nil {
return nil, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", cfg.Address)
}

opt, err := containerd.NewWorkerOpt(common.config.Root, client, snapshotter, cfg.Rootless, cfg.Labels, dns, nc, common.config.Workers.Containerd.ApparmorProfile, common.config.Workers.Containerd.SELinux, parallelismSem, common.traceSocket, runtime)
if err != nil {
return nil, err
}
Expand Down
20 changes: 5 additions & 15 deletions worker/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,7 @@ import (
type RuntimeInfo = containerdexecutor.RuntimeInfo

// NewWorkerOpt creates a WorkerOpt.
func NewWorkerOpt(root string, address, snapshotterName, ns string, rootless bool, labels map[string]string, dns *oci.DNSConfig, nopt netproviders.Opt, apparmorProfile string, selinux bool, parallelismSem *semaphore.Weighted, traceSocket string, runtime *RuntimeInfo, opts ...containerd.ClientOpt) (base.WorkerOpt, error) {
opts = append(opts, containerd.WithDefaultNamespace(ns))

client, err := containerd.New(address, opts...)
if err != nil {
return base.WorkerOpt{}, errors.Wrapf(err, "failed to connect client to %q . make sure containerd is running", address)
}
return newContainerd(root, client, snapshotterName, ns, rootless, labels, dns, nopt, apparmorProfile, selinux, parallelismSem, traceSocket, runtime)
}

func newContainerd(root string, client *containerd.Client, snapshotterName, ns 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) {
func NewWorkerOpt(root string, client *containerd.Client, snapshotterName 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) {
if strings.Contains(snapshotterName, "/") {
return base.WorkerOpt{}, errors.Errorf("bad snapshotter name: %q", snapshotterName)
}
Expand Down Expand Up @@ -80,13 +70,13 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns s
if apparmorProfile != "" {
xlabels[wlabel.ApparmorProfile] = apparmorProfile
}
xlabels[wlabel.ContainerdNamespace] = ns
xlabels[wlabel.ContainerdNamespace] = client.DefaultNamespace()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like containerd guarantees that DefaultNamespace() returns a non-zero value. In that case we need to validate it in this function before using.

xlabels[wlabel.ContainerdUUID] = serverInfo.UUID
for k, v := range labels {
xlabels[k] = v
}

lm := leaseutil.WithNamespace(client.LeasesService(), ns)
lm := leaseutil.WithNamespace(client.LeasesService(), client.DefaultNamespace())

gc := func(ctx context.Context) (gc.Stats, error) {
l, err := lm.Create(ctx)
Expand All @@ -96,7 +86,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns s
return nil, lm.Delete(ctx, leases.Lease{ID: l.ID}, leases.SynchronousDelete)
}

cs := containerdsnapshot.NewContentStore(client.ContentStore(), ns)
cs := containerdsnapshot.NewContentStore(client.ContentStore(), client.DefaultNamespace())

resp, err := client.IntrospectionService().Plugins(context.TODO(), []string{"type==io.containerd.runtime.v1", "type==io.containerd.runtime.v2"})
if err != nil {
Expand All @@ -117,7 +107,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName, ns s
}
}

snap := containerdsnapshot.NewSnapshotter(snapshotterName, client.SnapshotService(snapshotterName), ns, nil)
snap := containerdsnapshot.NewSnapshotter(snapshotterName, client.SnapshotService(snapshotterName), client.DefaultNamespace(), nil)

if err := cache.MigrateV2(
context.TODO(),
Expand Down
5 changes: 4 additions & 1 deletion worker/containerd/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"testing"

"github.com/containerd/containerd"
"github.com/moby/buildkit/util/network/netproviders"
"github.com/moby/buildkit/util/testutil/integration"
"github.com/moby/buildkit/util/testutil/workers"
Expand All @@ -31,8 +32,10 @@ func TestContainerdWorkerIntegration(t *testing.T) {

func newWorkerOpt(t *testing.T, addr string) base.WorkerOpt {
tmpdir := t.TempDir()
client, err := containerd.New(addr, containerd.WithDefaultNamespace("buildkit-test"))
require.NoError(t, err)
rootless := false
workerOpt, err := NewWorkerOpt(tmpdir, addr, "overlayfs", "buildkit-test", rootless, nil, nil, netproviders.Opt{Mode: "host"}, "", false, nil, "", nil)
workerOpt, err := NewWorkerOpt(tmpdir, client, "overlayfs", rootless, nil, nil, netproviders.Opt{Mode: "host"}, "", false, nil, "", nil)
require.NoError(t, err)
return workerOpt
}
Expand Down