2017-12-28 07:07:13 +00:00
|
|
|
package containerd
|
2017-12-28 06:29:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-28 07:07:13 +00:00
|
|
|
"time"
|
2017-12-28 06:29:50 +00:00
|
|
|
|
2017-12-28 07:07:13 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2017-12-28 06:29:50 +00:00
|
|
|
"github.com/containerd/containerd/mount"
|
|
|
|
"github.com/containerd/containerd/namespaces"
|
|
|
|
ctdsnapshot "github.com/containerd/containerd/snapshots"
|
2019-03-20 06:40:24 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-12-28 07:07:13 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/snapshot/blobmapping"
|
2017-12-28 06:29:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-28 09:26:34 +00:00
|
|
|
func NewSnapshotter(name string, snapshotter ctdsnapshot.Snapshotter, store content.Store, mdstore *metadata.Store, ns string, gc func(context.Context) error, idmap *idtools.IdentityMapping) snapshot.Snapshotter {
|
2017-12-28 07:07:13 +00:00
|
|
|
return blobmapping.NewSnapshotter(blobmapping.Opt{
|
|
|
|
Content: store,
|
2019-03-28 09:26:34 +00:00
|
|
|
Snapshotter: snapshot.FromContainerdSnapshotter(name, &nsSnapshotter{ns, snapshotter, gc}, idmap),
|
2017-12-28 07:07:13 +00:00
|
|
|
MetadataStore: mdstore,
|
|
|
|
})
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nsSnapshotter struct {
|
|
|
|
ns string
|
|
|
|
ctdsnapshot.Snapshotter
|
|
|
|
gc garbageCollectFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *nsSnapshotter) Stat(ctx context.Context, key string) (ctdsnapshot.Info, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
2018-04-30 17:40:52 +00:00
|
|
|
info, err := s.Snapshotter.Stat(ctx, key)
|
|
|
|
if err == nil {
|
|
|
|
if _, ok := info.Labels["labels.containerd.io/gc.root"]; !ok {
|
|
|
|
if err := addRootLabel()(&info); err != nil {
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
return s.Update(ctx, info, "labels.containerd.io/gc.root")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info, err
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *nsSnapshotter) Update(ctx context.Context, info ctdsnapshot.Info, fieldpaths ...string) (ctdsnapshot.Info, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
|
|
|
return s.Snapshotter.Update(ctx, info, fieldpaths...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *nsSnapshotter) Usage(ctx context.Context, key string) (ctdsnapshot.Usage, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
|
|
|
return s.Snapshotter.Usage(ctx, key)
|
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
|
|
|
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, s.ns)
|
2017-12-28 07:07:13 +00:00
|
|
|
return s.Snapshotter.Prepare(ctx, key, parent, addRootLabel(opts...))
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) View(ctx context.Context, key, parent string, opts ...ctdsnapshot.Opt) ([]mount.Mount, error) {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
2017-12-28 07:07:13 +00:00
|
|
|
return s.Snapshotter.View(ctx, key, parent, addRootLabel(opts...))
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Commit(ctx context.Context, name, key string, opts ...ctdsnapshot.Opt) error {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
2017-12-28 07:07:13 +00:00
|
|
|
return s.Snapshotter.Commit(ctx, name, key, addRootLabel(opts...))
|
2017-12-28 06:29:50 +00:00
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Remove(ctx context.Context, key string) error {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
|
|
|
if _, err := s.Update(ctx, ctdsnapshot.Info{
|
|
|
|
Name: key,
|
|
|
|
}, "labels.containerd.io/gc.root"); err != nil {
|
|
|
|
return err
|
|
|
|
} // calling snapshotter.Remove here causes a race in containerd
|
|
|
|
if s.gc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.gc(ctx)
|
|
|
|
}
|
|
|
|
func (s *nsSnapshotter) Walk(ctx context.Context, fn func(context.Context, ctdsnapshot.Info) error) error {
|
|
|
|
ctx = namespaces.WithNamespace(ctx, s.ns)
|
|
|
|
return s.Snapshotter.Walk(ctx, fn)
|
|
|
|
}
|
2017-12-28 07:07:13 +00:00
|
|
|
|
|
|
|
func addRootLabel(opts ...ctdsnapshot.Opt) ctdsnapshot.Opt {
|
|
|
|
return func(info *ctdsnapshot.Info) error {
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if info.Labels == nil {
|
|
|
|
info.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
info.Labels["containerd.io/gc.root"] = time.Now().UTC().Format(time.RFC3339Nano)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|