Merge pull request #8945 from pdtpartners/feature/make-overlay-snapshotter-embeddable

Add WithMetaStore to overlay snapshotter to allow bringing your own
This commit is contained in:
Derek McGowan 2023-08-21 15:47:15 -07:00 committed by GitHub
commit fb786b2f17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,7 @@ const upperdirKey = "containerd.io/snapshot/overlay.upperdir"
type SnapshotterConfig struct {
asyncRemove bool
upperdirLabel bool
ms MetaStore
mountOptions []string
}
@ -77,9 +78,24 @@ func WithMountOptions(options []string) Opt {
}
}
type MetaStore interface {
TransactionContext(ctx context.Context, writable bool) (context.Context, storage.Transactor, error)
WithTransaction(ctx context.Context, writable bool, fn storage.TransactionCallback) error
Close() error
}
// WithMetaStore allows the MetaStore to be created outside the snapshotter
// and passed in.
func WithMetaStore(ms MetaStore) Opt {
return func(config *SnapshotterConfig) error {
config.ms = ms
return nil
}
}
type snapshotter struct {
root string
ms *storage.MetaStore
ms MetaStore
asyncRemove bool
upperdirLabel bool
options []string
@ -106,9 +122,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) {
@ -132,7 +150,7 @@ func NewSnapshotter(root string, opts ...Opt) (snapshots.Snapshotter, error) {
return &snapshotter{
root: root,
ms: ms,
ms: config.ms,
asyncRemove: config.asyncRemove,
upperdirLabel: config.upperdirLabel,
options: config.mountOptions,