185 lines
5.5 KiB
Go
185 lines
5.5 KiB
Go
// +build standalone
|
|
|
|
package control
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/boltdb/bolt"
|
|
"github.com/containerd/containerd/content"
|
|
"github.com/containerd/containerd/content/local"
|
|
"github.com/containerd/containerd/diff/walking"
|
|
"github.com/containerd/containerd/metadata"
|
|
"github.com/containerd/containerd/mount"
|
|
"github.com/containerd/containerd/namespaces"
|
|
ctdsnapshot "github.com/containerd/containerd/snapshot"
|
|
"github.com/containerd/containerd/snapshot/overlay"
|
|
"github.com/moby/buildkit/worker/runcworker"
|
|
digest "github.com/opencontainers/go-digest"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func NewStandalone(root string) (*Controller, error) {
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
return nil, errors.Wrapf(err, "failed to create %s", root)
|
|
}
|
|
|
|
// TODO: take lock to make sure there are no duplicates
|
|
|
|
pd, err := newStandalonePullDeps(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
opt, err := defaultControllerOpts(root, *pd)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
w, err := runcworker.New(filepath.Join(root, "runc"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
opt.Worker = w
|
|
|
|
return NewController(*opt)
|
|
}
|
|
|
|
func newStandalonePullDeps(root string) (*pullDeps, error) {
|
|
s, err := overlay.NewSnapshotter(filepath.Join(root, "snapshots"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c, err := local.NewStore(filepath.Join(root, "content"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
db, err := bolt.Open(filepath.Join(root, "containerdmeta.db"), 0644, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mdb := metadata.NewDB(db, c, map[string]ctdsnapshot.Snapshotter{
|
|
"overlay": s,
|
|
})
|
|
if err := mdb.Init(context.TODO()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c = &nsContent{mdb.ContentStore()}
|
|
|
|
df, err := walking.NewWalkingDiff(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// TODO: call mdb.GarbageCollect . maybe just inject it into nsSnapshotter.Remove and csContent.Delete
|
|
|
|
return &pullDeps{
|
|
Snapshotter: &nsSnapshotter{mdb.Snapshotter("overlay")},
|
|
ContentStore: c,
|
|
Applier: df,
|
|
Differ: df,
|
|
}, nil
|
|
}
|
|
|
|
// this should be supported by containerd. currently packages are unusable without wrapping
|
|
const dummyNs = "buildkit"
|
|
|
|
type nsContent struct {
|
|
content.Store
|
|
}
|
|
|
|
func (c *nsContent) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Info(ctx, dgst)
|
|
}
|
|
|
|
func (c *nsContent) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Update(ctx, info, fieldpaths...)
|
|
}
|
|
|
|
func (c *nsContent) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Walk(ctx, fn, filters...)
|
|
}
|
|
|
|
func (c *nsContent) Delete(ctx context.Context, dgst digest.Digest) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Delete(ctx, dgst)
|
|
}
|
|
|
|
func (c *nsContent) Status(ctx context.Context, ref string) (content.Status, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Status(ctx, ref)
|
|
}
|
|
|
|
func (c *nsContent) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.ListStatuses(ctx, filters...)
|
|
}
|
|
|
|
func (c *nsContent) Abort(ctx context.Context, ref string) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Abort(ctx, ref)
|
|
}
|
|
|
|
func (c *nsContent) ReaderAt(ctx context.Context, dgst digest.Digest) (content.ReaderAt, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.ReaderAt(ctx, dgst)
|
|
}
|
|
|
|
func (c *nsContent) Writer(ctx context.Context, ref string, size int64, expected digest.Digest) (content.Writer, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return c.Store.Writer(ctx, ref, size, expected)
|
|
}
|
|
|
|
type nsSnapshotter struct {
|
|
ctdsnapshot.Snapshotter
|
|
}
|
|
|
|
func (s *nsSnapshotter) Stat(ctx context.Context, key string) (ctdsnapshot.Info, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Stat(ctx, key)
|
|
}
|
|
|
|
func (s *nsSnapshotter) Update(ctx context.Context, info ctdsnapshot.Info, fieldpaths ...string) (ctdsnapshot.Info, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Update(ctx, info, fieldpaths...)
|
|
}
|
|
|
|
func (s *nsSnapshotter) Usage(ctx context.Context, key string) (ctdsnapshot.Usage, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Usage(ctx, key)
|
|
}
|
|
func (s *nsSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Mounts(ctx, key)
|
|
}
|
|
func (s *nsSnapshotter) Prepare(ctx context.Context, key, parent string, opts ...ctdsnapshot.Opt) ([]mount.Mount, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Prepare(ctx, key, parent, opts...)
|
|
}
|
|
func (s *nsSnapshotter) View(ctx context.Context, key, parent string, opts ...ctdsnapshot.Opt) ([]mount.Mount, error) {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.View(ctx, key, parent, opts...)
|
|
}
|
|
func (s *nsSnapshotter) Commit(ctx context.Context, name, key string, opts ...ctdsnapshot.Opt) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Commit(ctx, name, key, opts...)
|
|
}
|
|
func (s *nsSnapshotter) Remove(ctx context.Context, key string) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Remove(ctx, key)
|
|
}
|
|
func (s *nsSnapshotter) Walk(ctx context.Context, fn func(context.Context, ctdsnapshot.Info) error) error {
|
|
ctx = namespaces.WithNamespace(ctx, dummyNs)
|
|
return s.Snapshotter.Walk(ctx, fn)
|
|
}
|