Skip to content

Commit

Permalink
changed the snapshotter to allow it to be embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbie Buxton authored and Robbie Buxton committed Jul 31, 2023
1 parent 6eb90a6 commit 49d4fa8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
60 changes: 35 additions & 25 deletions snapshots/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const upperdirKey = "containerd.io/snapshot/overlay.upperdir"
type SnapshotterConfig struct {
asyncRemove bool
upperdirLabel bool
ms *storage.MetaStore
mountOptions []string
}

Expand Down Expand Up @@ -77,7 +78,14 @@ func WithMountOptions(options []string) Opt {
}
}

type snapshotter struct {
func WithMetaStore(ms *storage.MetaStore) Opt {
return func(config *SnapshotterConfig) error {
config.ms = ms
return nil
}
}

type Snapshotter struct {
root string
ms *storage.MetaStore
asyncRemove bool
Expand Down Expand Up @@ -106,9 +114,11 @@ func NewSnapshotter(root string, opts ...Opt) (snapshots.Snapshotter, error) {
if !supportsDType {
return nil, fmt.Errorf("%s does not support d_type. If the backing filesystem is xfs, please reformat with ftype=1 to enable d_type support", root)
}
ms, err := storage.NewMetaStore(filepath.Join(root, "metadata.db"))
if err != nil {
return nil, err
if config.ms == nil {
config.ms, err = storage.NewMetaStore(filepath.Join(root, "metadata.db"))
if err != nil {
return nil, err
}
}

if err := os.Mkdir(filepath.Join(root, "snapshots"), 0700); err != nil && !os.IsExist(err) {
Expand All @@ -130,9 +140,9 @@ func NewSnapshotter(root string, opts ...Opt) (snapshots.Snapshotter, error) {
config.mountOptions = append(config.mountOptions, "index=off")
}

return &snapshotter{
return &Snapshotter{
root: root,
ms: ms,
ms: config.ms,
asyncRemove: config.asyncRemove,
upperdirLabel: config.upperdirLabel,
options: config.mountOptions,
Expand All @@ -157,7 +167,7 @@ func hasOption(options []string, key string, hasValue bool) bool {
//
// Should be used for parent resolution, existence checks and to discern
// the kind of snapshot.
func (o *snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info, err error) {
func (o *Snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info, err error) {
var id string
if err := o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
id, info, _, err = storage.GetInfo(ctx, key)
Expand All @@ -175,7 +185,7 @@ func (o *snapshotter) Stat(ctx context.Context, key string) (info snapshots.Info
return info, nil
}

func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (newInfo snapshots.Info, err error) {
func (o *Snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpaths ...string) (newInfo snapshots.Info, err error) {
err = o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
newInfo, err = storage.UpdateInfo(ctx, info, fieldpaths...)
if err != nil {
Expand Down Expand Up @@ -203,7 +213,7 @@ func (o *snapshotter) Update(ctx context.Context, info snapshots.Info, fieldpath
// "upper") directory and may take some time.
//
// For committed snapshots, the value is returned from the metadata database.
func (o *snapshotter) Usage(ctx context.Context, key string) (_ snapshots.Usage, err error) {
func (o *Snapshotter) Usage(ctx context.Context, key string) (_ snapshots.Usage, err error) {
var (
usage snapshots.Usage
info snapshots.Info
Expand All @@ -228,19 +238,19 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (_ snapshots.Usage,
return usage, nil
}

func (o *snapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
func (o *Snapshotter) Prepare(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
return o.createSnapshot(ctx, snapshots.KindActive, key, parent, opts)
}

func (o *snapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
func (o *Snapshotter) View(ctx context.Context, key, parent string, opts ...snapshots.Opt) ([]mount.Mount, error) {
return o.createSnapshot(ctx, snapshots.KindView, key, parent, opts)
}

// Mounts returns the mounts for the transaction identified by key. Can be
// called on an read-write or readonly transaction.
//
// This can be used to recover mounts after calling View or Prepare.
func (o *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount, err error) {
func (o *Snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount, err error) {
var s storage.Snapshot
if err := o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
s, err = storage.GetSnapshot(ctx, key)
Expand All @@ -254,7 +264,7 @@ func (o *snapshotter) Mounts(ctx context.Context, key string) (_ []mount.Mount,
return o.mounts(s), nil
}

func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
func (o *Snapshotter) Commit(ctx context.Context, name, key string, opts ...snapshots.Opt) error {
return o.ms.WithTransaction(ctx, true, func(ctx context.Context) error {
// grab the existing id
id, _, _, err := storage.GetInfo(ctx, key)
Expand All @@ -277,7 +287,7 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
// Remove abandons the snapshot identified by key. The snapshot will
// immediately become unavailable and unrecoverable. Disk space will
// be freed up on the next call to `Cleanup`.
func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
func (o *Snapshotter) Remove(ctx context.Context, key string) (err error) {
var removals []string
// Remove directories after the transaction is closed, failures must not
// return error since the transaction is committed with the removal
Expand Down Expand Up @@ -308,7 +318,7 @@ func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
}

// Walk the snapshots.
func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error {
func (o *Snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...string) error {
return o.ms.WithTransaction(ctx, false, func(ctx context.Context) error {
if o.upperdirLabel {
return storage.WalkInfo(ctx, func(ctx context.Context, info snapshots.Info) error {
Expand All @@ -328,7 +338,7 @@ func (o *snapshotter) Walk(ctx context.Context, fn snapshots.WalkFunc, fs ...str
}

// Cleanup cleans up disk resources from removed or abandoned snapshots
func (o *snapshotter) Cleanup(ctx context.Context) error {
func (o *Snapshotter) Cleanup(ctx context.Context) error {
cleanup, err := o.cleanupDirectories(ctx)
if err != nil {
return err
Expand All @@ -343,7 +353,7 @@ func (o *snapshotter) Cleanup(ctx context.Context) error {
return nil
}

func (o *snapshotter) cleanupDirectories(ctx context.Context) (_ []string, err error) {
func (o *Snapshotter) cleanupDirectories(ctx context.Context) (_ []string, err error) {
var cleanupDirs []string
// Get a write transaction to ensure no other write transaction can be entered
// while the cleanup is scanning.
Expand All @@ -356,7 +366,7 @@ func (o *snapshotter) cleanupDirectories(ctx context.Context) (_ []string, err e
return cleanupDirs, nil
}

func (o *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, error) {
func (o *Snapshotter) getCleanupDirectories(ctx context.Context) ([]string, error) {
ids, err := storage.IDMap(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -385,7 +395,7 @@ func (o *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, erro
return cleanup, nil
}

func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) {
func (o *Snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ []mount.Mount, err error) {
var (
s storage.Snapshot
td, path string
Expand Down Expand Up @@ -445,7 +455,7 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
return o.mounts(s), nil
}

func (o *snapshotter) prepareDirectory(ctx context.Context, snapshotDir string, kind snapshots.Kind) (string, error) {
func (o *Snapshotter) prepareDirectory(ctx context.Context, snapshotDir string, kind snapshots.Kind) (string, error) {
td, err := os.MkdirTemp(snapshotDir, "new-")
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
Expand All @@ -464,7 +474,7 @@ func (o *snapshotter) prepareDirectory(ctx context.Context, snapshotDir string,
return td, nil
}

func (o *snapshotter) mounts(s storage.Snapshot) []mount.Mount {
func (o *Snapshotter) mounts(s storage.Snapshot) []mount.Mount {
if len(s.ParentIDs) == 0 {
// if we only have one layer/no parents then just return a bind mount as overlay
// will not work
Expand Down Expand Up @@ -520,16 +530,16 @@ func (o *snapshotter) mounts(s storage.Snapshot) []mount.Mount {

}

func (o *snapshotter) upperPath(id string) string {
func (o *Snapshotter) upperPath(id string) string {
return filepath.Join(o.root, "snapshots", id, "fs")
}

func (o *snapshotter) workPath(id string) string {
func (o *Snapshotter) workPath(id string) string {
return filepath.Join(o.root, "snapshots", id, "work")
}

// Close closes the snapshotter
func (o *snapshotter) Close() error {
// Close closes the Snapshotter
func (o *Snapshotter) Close() error {
return o.ms.Close()
}

Expand Down
4 changes: 2 additions & 2 deletions snapshots/overlay/overlay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func testOverlayOverlayMount(t *testing.T, newSnapshotter testsuite.SnapshotterF
}

func getBasePath(ctx context.Context, sn snapshots.Snapshotter, root, key string) string {
o := sn.(*snapshotter)
o := sn.(*Snapshotter)
ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil {
panic(err)
Expand All @@ -202,7 +202,7 @@ func getBasePath(ctx context.Context, sn snapshots.Snapshotter, root, key string
}

func getParents(ctx context.Context, sn snapshots.Snapshotter, root, key string) []string {
o := sn.(*snapshotter)
o := sn.(*Snapshotter)
ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil {
panic(err)
Expand Down

0 comments on commit 49d4fa8

Please sign in to comment.