2017-05-25 23:25:44 +00:00
|
|
|
package snapshot
|
|
|
|
|
2017-05-26 17:20:41 +00:00
|
|
|
import (
|
2017-12-28 07:07:13 +00:00
|
|
|
"context"
|
2019-08-15 23:14:02 +00:00
|
|
|
"os"
|
2018-04-16 22:23:10 +00:00
|
|
|
"sync"
|
2019-08-15 23:14:02 +00:00
|
|
|
"sync/atomic"
|
2017-12-28 07:07:13 +00:00
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
"github.com/containerd/containerd/mount"
|
2017-12-05 07:34:25 +00:00
|
|
|
"github.com/containerd/containerd/snapshots"
|
2019-03-20 06:40:24 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-05-26 17:20:41 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
type Mountable interface {
|
|
|
|
// ID() string
|
2019-08-15 22:42:03 +00:00
|
|
|
Mount() ([]mount.Mount, func() error, error)
|
2019-03-20 06:40:24 +00:00
|
|
|
IdentityMapping() *idtools.IdentityMapping
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
// Snapshotter defines interface that any snapshot implementation should satisfy
|
|
|
|
type Snapshotter interface {
|
2019-03-28 09:26:34 +00:00
|
|
|
Name() string
|
2018-04-16 22:23:10 +00:00
|
|
|
Mounts(ctx context.Context, key string) (Mountable, error)
|
|
|
|
Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error
|
|
|
|
View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (Mountable, error)
|
|
|
|
|
|
|
|
Stat(ctx context.Context, key string) (snapshots.Info, error)
|
|
|
|
Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (snapshots.Info, error)
|
|
|
|
Usage(ctx context.Context, key string) (snapshots.Usage, error)
|
|
|
|
Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error
|
|
|
|
Remove(ctx context.Context, key string) error
|
2020-02-25 02:06:30 +00:00
|
|
|
Walk(ctx context.Context, fn snapshots.WalkFunc, filters ...string) error
|
2018-04-16 22:23:10 +00:00
|
|
|
Close() error
|
2019-03-20 06:40:24 +00:00
|
|
|
IdentityMapping() *idtools.IdentityMapping
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
func FromContainerdSnapshotter(name string, s snapshots.Snapshotter, idmap *idtools.IdentityMapping) Snapshotter {
|
2019-03-28 09:26:34 +00:00
|
|
|
return &fromContainerd{name: name, Snapshotter: s, idmap: idmap}
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fromContainerd struct {
|
2019-03-28 09:26:34 +00:00
|
|
|
name string
|
2018-04-16 22:23:10 +00:00
|
|
|
snapshots.Snapshotter
|
2019-03-20 06:40:24 +00:00
|
|
|
idmap *idtools.IdentityMapping
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 09:26:34 +00:00
|
|
|
func (s *fromContainerd) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
func (s *fromContainerd) Mounts(ctx context.Context, key string) (Mountable, error) {
|
|
|
|
mounts, err := s.Snapshotter.Mounts(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-15 23:14:02 +00:00
|
|
|
return &staticMountable{mounts: mounts, idmap: s.idmap, id: key}, nil
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
func (s *fromContainerd) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) error {
|
|
|
|
_, err := s.Snapshotter.Prepare(ctx, key, parent, opts...)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (s *fromContainerd) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) (Mountable, error) {
|
|
|
|
mounts, err := s.Snapshotter.View(ctx, key, parent, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-15 23:14:02 +00:00
|
|
|
return &staticMountable{mounts: mounts, idmap: s.idmap, id: key}, nil
|
2019-03-20 06:40:24 +00:00
|
|
|
}
|
|
|
|
func (s *fromContainerd) IdentityMapping() *idtools.IdentityMapping {
|
|
|
|
return s.idmap
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type staticMountable struct {
|
2019-08-15 23:14:02 +00:00
|
|
|
count int32
|
|
|
|
id string
|
2018-04-16 22:23:10 +00:00
|
|
|
mounts []mount.Mount
|
2019-03-20 06:40:24 +00:00
|
|
|
idmap *idtools.IdentityMapping
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 23:14:02 +00:00
|
|
|
func (cm *staticMountable) Mount() ([]mount.Mount, func() error, error) {
|
|
|
|
atomic.AddInt32(&cm.count, 1)
|
|
|
|
return cm.mounts, func() error {
|
|
|
|
if atomic.AddInt32(&cm.count, -1) < 0 {
|
|
|
|
if v := os.Getenv("BUILDKIT_DEBUG_PANIC_ON_ERROR"); v == "1" {
|
|
|
|
panic("release of released mount " + cm.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}, nil
|
2018-04-16 22:23:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 06:40:24 +00:00
|
|
|
func (cm *staticMountable) IdentityMapping() *idtools.IdentityMapping {
|
|
|
|
return cm.idmap
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
// NewContainerdSnapshotter converts snapshotter to containerd snapshotter
|
|
|
|
func NewContainerdSnapshotter(s Snapshotter) (snapshots.Snapshotter, func() error) {
|
|
|
|
cs := &containerdSnapshotter{Snapshotter: s}
|
|
|
|
return cs, cs.release
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerdSnapshotter struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
releasers []func() error
|
|
|
|
Snapshotter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *containerdSnapshotter) release() error {
|
|
|
|
cs.mu.Lock()
|
|
|
|
defer cs.mu.Unlock()
|
|
|
|
var err error
|
|
|
|
for _, f := range cs.releasers {
|
2018-12-10 22:36:13 +00:00
|
|
|
if err1 := f(); err1 != nil && err == nil {
|
2018-04-16 22:23:10 +00:00
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *containerdSnapshotter) returnMounts(mf Mountable) ([]mount.Mount, error) {
|
2019-08-15 22:42:03 +00:00
|
|
|
mounts, release, err := mf.Mount()
|
2018-04-16 22:23:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cs.mu.Lock()
|
2019-08-15 22:42:03 +00:00
|
|
|
cs.releasers = append(cs.releasers, release)
|
2018-04-16 22:23:10 +00:00
|
|
|
cs.mu.Unlock()
|
|
|
|
return mounts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *containerdSnapshotter) Mounts(ctx context.Context, key string) ([]mount.Mount, error) {
|
|
|
|
mf, err := cs.Snapshotter.Mounts(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cs.returnMounts(mf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *containerdSnapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
|
|
|
if err := cs.Snapshotter.Prepare(ctx, key, parent, opts...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cs.Mounts(ctx, key)
|
|
|
|
}
|
|
|
|
func (cs *containerdSnapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
|
|
|
|
mf, err := cs.Snapshotter.View(ctx, key, parent, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cs.returnMounts(mf)
|
|
|
|
}
|