Skip to content

Commit

Permalink
Add option to specify containerd runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Marat Radchenko <[email protected]>
  • Loading branch information
slonopotamus committed Sep 24, 2023
1 parent bbe48e7 commit a96327d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ type ContainerdConfig struct {
Labels map[string]string `toml:"labels"`
Platforms []string `toml:"platforms"`
Namespace string `toml:"namespace"`
Runtime string `toml:"runtime"`
GCConfig
NetworkConfig
Snapshotter string `toml:"snapshotter"`
Expand Down
2 changes: 2 additions & 0 deletions cmd/buildkitd/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ foo="bar"
[worker.containerd]
namespace="non-default"
runtime="exotic"
platforms=["linux/amd64"]
address="containerd.sock"
[[worker.containerd.gcpolicy]]
Expand Down Expand Up @@ -103,6 +104,7 @@ searchDomains=["example.com"]

require.Equal(t, 0, len(cfg.Workers.OCI.GCPolicy))
require.Equal(t, "non-default", cfg.Workers.Containerd.Namespace)
require.Equal(t, "exotic", cfg.Workers.Containerd.Runtime)
require.Equal(t, 3, len(cfg.Workers.Containerd.GCPolicy))

require.Nil(t, cfg.Workers.Containerd.GC)
Expand Down
22 changes: 21 additions & 1 deletion cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package main
import (
"context"
"os"
"runtime"
"strconv"
"strings"
"time"

ctd "github.com/containerd/containerd"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/pkg/userns"
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/util/bklog"
Expand Down Expand Up @@ -46,6 +48,14 @@ func init() {
defaultConf.Workers.Containerd.Namespace = defaultContainerdNamespace
}

if defaultConf.Workers.Containerd.Runtime == "" {
if runtime.GOOS == "freebsd" {
defaultConf.Workers.Containerd.Runtime = "wtf.sbk.runj.v1"
} else {
defaultConf.Workers.Containerd.Runtime = defaults.DefaultRuntime
}
}

flags := []cli.Flag{
cli.StringFlag{
Name: "containerd-worker",
Expand Down Expand Up @@ -74,6 +84,12 @@ func init() {
Value: defaultConf.Workers.Containerd.Namespace,
Hidden: true,
},
cli.StringFlag{
Name: "containerd-worker-runtime",
Usage: "override containerd runtime",
Value: defaultConf.Workers.Containerd.Runtime,
Hidden: true,
},
cli.StringFlag{
Name: "containerd-worker-net",
Usage: "worker network type (auto, cni or host)",
Expand Down Expand Up @@ -202,6 +218,10 @@ func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
cfg.Workers.Containerd.Namespace = c.GlobalString("containerd-worker-namespace")
}

if c.GlobalIsSet("containerd-worker-runtime") || cfg.Workers.Containerd.Runtime == "" {
cfg.Workers.Containerd.Runtime = c.GlobalString("containerd-worker-runtime")
}

if c.GlobalIsSet("containerd-worker-gc") {
v := c.GlobalBool("containerd-worker-gc")
cfg.Workers.Containerd.GC = &v
Expand Down Expand Up @@ -275,7 +295,7 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
if cfg.Snapshotter != "" {
snapshotter = cfg.Snapshotter
}
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, ctd.WithTimeout(60*time.Second))
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, ctd.WithTimeout(60*time.Second), ctd.WithDefaultRuntime(cfg.Runtime))
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions docs/buildkitd.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ insecure-entitlements = [ "network.host", "security.insecure" ]
enabled = true
platforms = [ "linux/amd64", "linux/arm64" ]
namespace = "buildkit"
runtime = "runc"
gc = true
# gckeepstorage sets storage limit for default gc profile, in MB.
gckeepstorage = 9000
Expand Down
4 changes: 0 additions & 4 deletions worker/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"

Expand All @@ -30,9 +29,6 @@ import (
// 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, opts ...containerd.ClientOpt) (base.WorkerOpt, error) {
opts = append(opts, containerd.WithDefaultNamespace(ns))
if runtime.GOOS == "freebsd" {
opts = append(opts, containerd.WithDefaultRuntime("wtf.sbk.runj.v1"))
}

client, err := containerd.New(address, opts...)
if err != nil {
Expand Down

0 comments on commit a96327d

Please sign in to comment.