Refactor overlay and btrfs to pass lint

Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan 2017-03-24 17:21:03 -07:00
parent b319ba7c5a
commit 9ffbfccdaf
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
3 changed files with 32 additions and 26 deletions

View File

@ -36,12 +36,15 @@ func init() {
}) })
} }
type Snapshotter struct { type snapshotter struct {
device string // maybe we can resolve it with path? device string // maybe we can resolve it with path?
root string // root provides paths for internal storage. root string // root provides paths for internal storage.
ms *storage.MetaStore ms *storage.MetaStore
} }
// NewSnapshotter returns a Snapshotter using btrfs. Uses the provided
// device and root directory for snapshots and stores the metadata in
// a file in the provided root.
func NewSnapshotter(device, root string) (snapshot.Snapshotter, error) { func NewSnapshotter(device, root string) (snapshot.Snapshotter, error) {
var ( var (
active = filepath.Join(root, "active") active = filepath.Join(root, "active")
@ -61,7 +64,7 @@ func NewSnapshotter(device, root string) (snapshot.Snapshotter, error) {
return nil, err return nil, err
} }
return &Snapshotter{ return &snapshotter{
device: device, device: device,
root: root, root: root,
ms: ms, ms: ms,
@ -73,7 +76,7 @@ func NewSnapshotter(device, root string) (snapshot.Snapshotter, error) {
// //
// Should be used for parent resolution, existence checks and to discern // Should be used for parent resolution, existence checks and to discern
// the kind of snapshot. // the kind of snapshot.
func (b *Snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) { func (b *snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) {
ctx, t, err := b.ms.TransactionContext(ctx, false) ctx, t, err := b.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return snapshot.Info{}, err return snapshot.Info{}, err
@ -83,7 +86,7 @@ func (b *Snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, erro
} }
// Walk the committed snapshots. // Walk the committed snapshots.
func (b *Snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error { func (b *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
ctx, t, err := b.ms.TransactionContext(ctx, false) ctx, t, err := b.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return err return err
@ -92,15 +95,15 @@ func (b *Snapshotter) Walk(ctx context.Context, fn func(context.Context, snapsho
return storage.WalkInfo(ctx, fn) return storage.WalkInfo(ctx, fn)
} }
func (b *Snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) { func (b *snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
return b.makeActive(ctx, key, parent, false) return b.makeActive(ctx, key, parent, false)
} }
func (b *Snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) { func (b *snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
return b.makeActive(ctx, key, parent, true) return b.makeActive(ctx, key, parent, true)
} }
func (b *Snapshotter) makeActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) { func (b *snapshotter) makeActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) {
ctx, t, err := b.ms.TransactionContext(ctx, true) ctx, t, err := b.ms.TransactionContext(ctx, true)
if err != nil { if err != nil {
return nil, err return nil, err
@ -145,7 +148,7 @@ func (b *Snapshotter) makeActive(ctx context.Context, key, parent string, readon
return b.mounts(target) return b.mounts(target)
} }
func (b *Snapshotter) mounts(dir string) ([]containerd.Mount, error) { func (b *snapshotter) mounts(dir string) ([]containerd.Mount, error) {
var options []string var options []string
// get the subvolume id back out for the mount // get the subvolume id back out for the mount
@ -171,7 +174,7 @@ func (b *Snapshotter) mounts(dir string) ([]containerd.Mount, error) {
}, nil }, nil
} }
func (b *Snapshotter) Commit(ctx context.Context, name, key string) (err error) { func (b *snapshotter) Commit(ctx context.Context, name, key string) (err error) {
ctx, t, err := b.ms.TransactionContext(ctx, true) ctx, t, err := b.ms.TransactionContext(ctx, true)
if err != nil { if err != nil {
return err return err
@ -217,7 +220,7 @@ func (b *Snapshotter) Commit(ctx context.Context, name, key string) (err error)
// called on an read-write or readonly transaction. // called on an read-write or readonly transaction.
// //
// This can be used to recover mounts after calling View or Prepare. // This can be used to recover mounts after calling View or Prepare.
func (b *Snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) { func (b *snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) {
ctx, t, err := b.ms.TransactionContext(ctx, false) ctx, t, err := b.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -233,7 +236,7 @@ func (b *Snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Moun
// Remove abandons the transaction identified by key. All resources // Remove abandons the transaction identified by key. All resources
// associated with the key will be removed. // associated with the key will be removed.
func (b *Snapshotter) Remove(ctx context.Context, key string) (err error) { func (b *snapshotter) Remove(ctx context.Context, key string) (err error) {
var ( var (
source, removed string source, removed string
readonly bool readonly bool

View File

@ -25,7 +25,7 @@ func init() {
}) })
} }
type Snapshotter struct { type snapshotter struct {
root string root string
ms *storage.MetaStore ms *storage.MetaStore
} }
@ -37,6 +37,9 @@ type activeSnapshot struct {
readonly bool readonly bool
} }
// NewSnapshotter returns a Snapshotter which uses overlayfs. The overlayfs
// diffs are stored under the provided root. A metadata file is stored under
// the root.
func NewSnapshotter(root string) (snapshot.Snapshotter, error) { func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
if err := os.MkdirAll(root, 0700); err != nil { if err := os.MkdirAll(root, 0700); err != nil {
return nil, err return nil, err
@ -50,7 +53,7 @@ func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
return nil, err return nil, err
} }
return &Snapshotter{ return &snapshotter{
root: root, root: root,
ms: ms, ms: ms,
}, nil }, nil
@ -61,7 +64,7 @@ func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
// //
// Should be used for parent resolution, existence checks and to discern // Should be used for parent resolution, existence checks and to discern
// the kind of snapshot. // the kind of snapshot.
func (o *Snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) { func (o *snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return snapshot.Info{}, err return snapshot.Info{}, err
@ -70,11 +73,11 @@ func (o *Snapshotter) Stat(ctx context.Context, key string) (snapshot.Info, erro
return storage.GetInfo(ctx, key) return storage.GetInfo(ctx, key)
} }
func (o *Snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) { func (o *snapshotter) Prepare(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
return o.createActive(ctx, key, parent, false) return o.createActive(ctx, key, parent, false)
} }
func (o *Snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) { func (o *snapshotter) View(ctx context.Context, key, parent string) ([]containerd.Mount, error) {
return o.createActive(ctx, key, parent, true) return o.createActive(ctx, key, parent, true)
} }
@ -82,7 +85,7 @@ func (o *Snapshotter) View(ctx context.Context, key, parent string) ([]container
// called on an read-write or readonly transaction. // called on an read-write or readonly transaction.
// //
// This can be used to recover mounts after calling View or Prepare. // This can be used to recover mounts after calling View or Prepare.
func (o *Snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) { func (o *snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Mount, error) {
ctx, t, err := o.ms.TransactionContext(ctx, false) ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return nil, err return nil, err
@ -95,7 +98,7 @@ func (o *Snapshotter) Mounts(ctx context.Context, key string) ([]containerd.Moun
return o.mounts(active), nil return o.mounts(active), nil
} }
func (o *Snapshotter) Commit(ctx context.Context, name, key string) error { func (o *snapshotter) Commit(ctx context.Context, name, key string) error {
ctx, t, err := o.ms.TransactionContext(ctx, true) ctx, t, err := o.ms.TransactionContext(ctx, true)
if err != nil { if err != nil {
return err return err
@ -111,7 +114,7 @@ func (o *Snapshotter) Commit(ctx context.Context, name, key string) error {
// Remove abandons the transaction identified by key. All resources // Remove abandons the transaction identified by key. All resources
// associated with the key will be removed. // associated with the key will be removed.
func (o *Snapshotter) Remove(ctx context.Context, key string) (err error) { func (o *snapshotter) Remove(ctx context.Context, key string) (err error) {
ctx, t, err := o.ms.TransactionContext(ctx, true) ctx, t, err := o.ms.TransactionContext(ctx, true)
if err != nil { if err != nil {
return err return err
@ -153,7 +156,7 @@ func (o *Snapshotter) Remove(ctx context.Context, key string) (err error) {
} }
// Walk the committed snapshots. // Walk the committed snapshots.
func (o *Snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error { func (o *snapshotter) Walk(ctx context.Context, fn func(context.Context, snapshot.Info) error) error {
ctx, t, err := o.ms.TransactionContext(ctx, false) ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
return err return err
@ -162,7 +165,7 @@ func (o *Snapshotter) Walk(ctx context.Context, fn func(context.Context, snapsho
return storage.WalkInfo(ctx, fn) return storage.WalkInfo(ctx, fn)
} }
func (o *Snapshotter) createActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) { func (o *snapshotter) createActive(ctx context.Context, key, parent string, readonly bool) ([]containerd.Mount, error) {
var ( var (
path string path string
snapshotDir = filepath.Join(o.root, "snapshots") snapshotDir = filepath.Join(o.root, "snapshots")
@ -225,7 +228,7 @@ func (o *Snapshotter) createActive(ctx context.Context, key, parent string, read
return o.mounts(active), nil return o.mounts(active), nil
} }
func (o *Snapshotter) mounts(active storage.Active) []containerd.Mount { func (o *snapshotter) mounts(active storage.Active) []containerd.Mount {
if len(active.ParentIDs) == 0 { if len(active.ParentIDs) == 0 {
// if we only have one layer/no parents then just return a bind mount as overlay // if we only have one layer/no parents then just return a bind mount as overlay
// will not work // will not work
@ -281,10 +284,10 @@ func (o *Snapshotter) mounts(active storage.Active) []containerd.Mount {
} }
func (o *Snapshotter) upperPath(id string) string { func (o *snapshotter) upperPath(id string) string {
return filepath.Join(o.root, "snapshots", id, "fs") 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") return filepath.Join(o.root, "snapshots", id, "work")
} }

View File

@ -149,7 +149,7 @@ func TestOverlayOverlayMount(t *testing.T) {
} }
func getBasePath(ctx context.Context, sn snapshot.Snapshotter, root, key string) string { func getBasePath(ctx context.Context, sn snapshot.Snapshotter, root, key string) string {
o := sn.(*Snapshotter) o := sn.(*snapshotter)
ctx, t, err := o.ms.TransactionContext(ctx, false) ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
panic(err) panic(err)
@ -165,7 +165,7 @@ func getBasePath(ctx context.Context, sn snapshot.Snapshotter, root, key string)
} }
func getParents(ctx context.Context, sn snapshot.Snapshotter, root, key string) []string { func getParents(ctx context.Context, sn snapshot.Snapshotter, root, key string) []string {
o := sn.(*Snapshotter) o := sn.(*snapshotter)
ctx, t, err := o.ms.TransactionContext(ctx, false) ctx, t, err := o.ms.TransactionContext(ctx, false)
if err != nil { if err != nil {
panic(err) panic(err)