Merge pull request #605 from fuweid/feature_add_namespace_in_config

cmd,worker: support customize namespace for containerd worker
docker-18.09
Akihiro Suda 2018-09-11 11:56:24 +09:00 committed by GitHub
commit 907411c7e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 10 deletions

View File

@ -63,6 +63,7 @@ type ContainerdConfig struct {
Labels map[string]string `toml:"labels"`
Platforms []string `toml:"platforms"`
GCPolicy []GCPolicy `toml:"gcpolicy"`
Namespace string `toml:"namespace"`
}
type GCPolicy struct {

View File

@ -29,6 +29,7 @@ foo="bar"
"aa.bb.cc"="baz"
[worker.containerd]
namespace="non-default"
platforms=["linux/amd64"]
address="containerd.sock"
[[worker.containerd.gcpolicy]]
@ -72,6 +73,7 @@ http=true
require.Equal(t, "containerd.sock", cfg.Workers.Containerd.Address)
require.Equal(t, 0, len(cfg.Workers.OCI.GCPolicy))
require.Equal(t, "non-default", cfg.Workers.Containerd.Namespace)
require.Equal(t, 2, len(cfg.Workers.Containerd.GCPolicy))
require.Equal(t, true, cfg.Workers.Containerd.GCPolicy[0].All)

View File

@ -18,7 +18,10 @@ import (
"github.com/urfave/cli"
)
const defaultContainerdAddress = "/run/containerd/containerd.sock"
const (
defaultContainerdAddress = "/run/containerd/containerd.sock"
defaultContainerdNamespace = "buildkit"
)
func init() {
defaultConf, _ := defaultConf()
@ -34,6 +37,10 @@ func init() {
defaultConf.Workers.Containerd.Address = defaultContainerdAddress
}
if defaultConf.Workers.Containerd.Namespace == "" {
defaultConf.Workers.Containerd.Namespace = defaultContainerdNamespace
}
registerWorkerInitializer(
workerInitializer{
fn: containerdWorkerInitializer,
@ -61,6 +68,12 @@ func init() {
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,
},
)
// 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)",
}
@ -105,6 +118,10 @@ func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
cfg.Workers.Containerd.Platforms = platforms
}
if c.GlobalIsSet("containerd-worker-namespace") || cfg.Workers.Containerd.Namespace == "" {
cfg.Workers.Containerd.Namespace = c.GlobalString("containerd-worker-namespace")
}
return nil
}
@ -119,7 +136,7 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
return nil, nil
}
opt, err := containerd.NewWorkerOpt(common.config.Root, cfg.Address, ctd.DefaultSnapshotter, cfg.Labels, ctd.WithTimeout(60*time.Second))
opt, err := containerd.NewWorkerOpt(common.config.Root, cfg.Address, ctd.DefaultSnapshotter, cfg.Namespace, cfg.Labels, ctd.WithTimeout(60*time.Second))
if err != nil {
return nil, err
}

View File

@ -27,17 +27,16 @@ import (
// NewWorkerOpt creates a WorkerOpt.
// But it does not set the following fields:
// - SessionManager
func NewWorkerOpt(root string, address, snapshotterName string, labels map[string]string, opts ...containerd.ClientOpt) (base.WorkerOpt, error) {
// TODO: take lock to make sure there are no duplicates
opts = append([]containerd.ClientOpt{containerd.WithDefaultNamespace("buildkit")}, opts...)
func NewWorkerOpt(root string, address, snapshotterName, ns string, labels map[string]string, 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, labels)
return newContainerd(root, client, snapshotterName, ns, labels)
}
func newContainerd(root string, client *containerd.Client, snapshotterName string, labels map[string]string) (base.WorkerOpt, error) {
func newContainerd(root string, client *containerd.Client, snapshotterName, ns string, labels map[string]string) (base.WorkerOpt, error) {
if strings.Contains(snapshotterName, "/") {
return base.WorkerOpt{}, errors.Errorf("bad snapshotter name: %q", snapshotterName)
}
@ -66,7 +65,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName strin
// TODO: how to avoid this?
ctx := context.TODO()
snapshotter := client.SnapshotService(snapshotterName)
ctx = namespaces.WithNamespace(ctx, "buildkit")
ctx = namespaces.WithNamespace(ctx, ns)
key := identity.NewID()
if _, err := snapshotter.Prepare(ctx, key, "", snapshots.WithLabels(map[string]string{
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
@ -83,7 +82,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName strin
return nil
}
cs := containerdsnapshot.NewContentStore(client.ContentStore(), "buildkit", gc)
cs := containerdsnapshot.NewContentStore(client.ContentStore(), ns, gc)
resp, err := client.IntrospectionService().Plugins(context.TODO(), &introspection.PluginsRequest{Filters: []string{"type==io.containerd.runtime.v1"}})
if err != nil {
@ -109,7 +108,7 @@ func newContainerd(root string, client *containerd.Client, snapshotterName strin
Labels: xlabels,
MetadataStore: md,
Executor: containerdexecutor.New(client, root, "", network.Default()),
Snapshotter: containerdsnapshot.NewSnapshotter(client.SnapshotService(snapshotterName), cs, md, "buildkit", gc),
Snapshotter: containerdsnapshot.NewSnapshotter(client.SnapshotService(snapshotterName), cs, md, ns, gc),
ContentStore: cs,
Applier: winlayers.NewFileSystemApplierWithWindows(cs, df),
Differ: winlayers.NewWalkingDiffWithWindows(cs, df),