diff --git a/differ/differ.go b/differ/differ.go index 436e33b44..1962989d1 100644 --- a/differ/differ.go +++ b/differ/differ.go @@ -6,7 +6,6 @@ import ( "os" "strings" - "github.com/boltdb/bolt" "github.com/containerd/containerd/archive" "github.com/containerd/containerd/archive/compression" "github.com/containerd/containerd/content" @@ -38,7 +37,7 @@ func init() { if err != nil { return nil, err } - return NewWalkingDiff(metadata.NewContentStore(md.(*bolt.DB), c.(content.Store))) + return NewWalkingDiff(metadata.NewContentStore(md.(*metadata.DB), c.(content.Store))) }, }) } diff --git a/linux/runtime.go b/linux/runtime.go index b9960a2d5..8d54898e8 100644 --- a/linux/runtime.go +++ b/linux/runtime.go @@ -101,7 +101,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { state: ic.State, monitor: monitor.(runtime.TaskMonitor), tasks: runtime.NewTaskList(), - db: m.(*bolt.DB), + db: m.(*metadata.DB), address: ic.Address, events: ic.Events, config: cfg, @@ -127,7 +127,7 @@ type Runtime struct { monitor runtime.TaskMonitor tasks *runtime.TaskList - db *bolt.DB + db *metadata.DB events *events.Exchange config *Config diff --git a/metadata/bolt.go b/metadata/bolt.go index 221dba5a2..ca9ceb939 100644 --- a/metadata/bolt.go +++ b/metadata/bolt.go @@ -19,7 +19,7 @@ func WithTransactionContext(ctx context.Context, tx *bolt.Tx) context.Context { // view gets a bolt db transaction either from the context // or starts a new one with the provided bolt database. -func view(ctx context.Context, db *bolt.DB, fn func(*bolt.Tx) error) error { +func view(ctx context.Context, db *DB, fn func(*bolt.Tx) error) error { tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx) if !ok { return db.View(fn) @@ -29,7 +29,7 @@ func view(ctx context.Context, db *bolt.DB, fn func(*bolt.Tx) error) error { // update gets a writable bolt db transaction either from the context // or starts a new one with the provided bolt database. -func update(ctx context.Context, db *bolt.DB, fn func(*bolt.Tx) error) error { +func update(ctx context.Context, db *DB, fn func(*bolt.Tx) error) error { tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx) if !ok { return db.Update(fn) diff --git a/metadata/content.go b/metadata/content.go index a76e5289a..e93e09292 100644 --- a/metadata/content.go +++ b/metadata/content.go @@ -19,12 +19,12 @@ import ( type contentStore struct { content.Store - db *bolt.DB + db *DB } // NewContentStore returns a namespaced content store using an existing // content store interface. -func NewContentStore(db *bolt.DB, cs content.Store) content.Store { +func NewContentStore(db *DB, cs content.Store) content.Store { return &contentStore{ Store: cs, db: db, @@ -353,7 +353,7 @@ type namespacedWriter struct { content.Writer ref string namespace string - db *bolt.DB + db *DB } func (nw *namespacedWriter) Commit(ctx context.Context, size int64, expected digest.Digest, opts ...content.Opt) error { diff --git a/metadata/db.go b/metadata/db.go new file mode 100644 index 000000000..a6009d83a --- /dev/null +++ b/metadata/db.go @@ -0,0 +1,21 @@ +package metadata + +import "github.com/boltdb/bolt" + +type DB struct { + db *bolt.DB +} + +func NewDB(db *bolt.DB) *DB { + return &DB{ + db: db, + } +} + +func (m *DB) View(fn func(*bolt.Tx) error) error { + return m.db.View(fn) +} + +func (m *DB) Update(fn func(*bolt.Tx) error) error { + return m.db.Update(fn) +} diff --git a/metadata/snapshot.go b/metadata/snapshot.go index 254bc1f0f..e84f2d8ec 100644 --- a/metadata/snapshot.go +++ b/metadata/snapshot.go @@ -19,12 +19,12 @@ import ( type snapshotter struct { snapshot.Snapshotter name string - db *bolt.DB + db *DB } // NewSnapshotter returns a new Snapshotter which namespaces the given snapshot // using the provided name and metadata store. -func NewSnapshotter(db *bolt.DB, name string, sn snapshot.Snapshotter) snapshot.Snapshotter { +func NewSnapshotter(db *DB, name string, sn snapshot.Snapshotter) snapshot.Snapshotter { return &snapshotter{ Snapshotter: sn, name: name, diff --git a/server/server.go b/server/server.go index 7d4ef704c..1719898c4 100644 --- a/server/server.go +++ b/server/server.go @@ -23,6 +23,7 @@ import ( "github.com/containerd/containerd/content/local" "github.com/containerd/containerd/events" "github.com/containerd/containerd/log" + "github.com/containerd/containerd/metadata" "github.com/containerd/containerd/plugin" metrics "github.com/docker/go-metrics" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" @@ -179,7 +180,11 @@ func loadPlugins(config *Config) ([]*plugin.Registration, error) { if err := os.MkdirAll(ic.Root, 0711); err != nil { return nil, err } - return bolt.Open(filepath.Join(ic.Root, "meta.db"), 0644, nil) + db, err := bolt.Open(filepath.Join(ic.Root, "meta.db"), 0644, nil) + if err != nil { + return nil, err + } + return metadata.NewDB(db), nil }, }) diff --git a/services/containers/service.go b/services/containers/service.go index d6aeedcba..4fcb8dfeb 100644 --- a/services/containers/service.go +++ b/services/containers/service.go @@ -28,17 +28,17 @@ func init() { if err != nil { return nil, err } - return NewService(m.(*bolt.DB), ic.Events), nil + return NewService(m.(*metadata.DB), ic.Events), nil }, }) } type Service struct { - db *bolt.DB + db *metadata.DB publisher events.Publisher } -func NewService(db *bolt.DB, publisher events.Publisher) api.ContainersServer { +func NewService(db *metadata.DB, publisher events.Publisher) api.ContainersServer { return &Service{db: db, publisher: publisher} } diff --git a/services/content/service.go b/services/content/service.go index cd2369925..6e01692da 100644 --- a/services/content/service.go +++ b/services/content/service.go @@ -4,7 +4,6 @@ import ( "io" "sync" - "github.com/boltdb/bolt" api "github.com/containerd/containerd/api/services/content/v1" eventsapi "github.com/containerd/containerd/api/services/events/v1" "github.com/containerd/containerd/content" @@ -56,7 +55,9 @@ func NewService(ic *plugin.InitContext) (interface{}, error) { if err != nil { return nil, err } - cs := metadata.NewContentStore(m.(*bolt.DB), c.(content.Store)) + + cs := metadata.NewContentStore(m.(*metadata.DB), c.(content.Store)) + return &Service{ store: cs, publisher: ic.Events, diff --git a/services/images/service.go b/services/images/service.go index 975ba0389..5e2697427 100644 --- a/services/images/service.go +++ b/services/images/service.go @@ -28,17 +28,17 @@ func init() { if err != nil { return nil, err } - return NewService(m.(*bolt.DB), ic.Events), nil + return NewService(m.(*metadata.DB), ic.Events), nil }, }) } type Service struct { - db *bolt.DB + db *metadata.DB publisher events.Publisher } -func NewService(db *bolt.DB, publisher events.Publisher) imagesapi.ImagesServer { +func NewService(db *metadata.DB, publisher events.Publisher) imagesapi.ImagesServer { return &Service{ db: db, publisher: publisher, diff --git a/services/namespaces/service.go b/services/namespaces/service.go index 61e0eeba8..e7f7a15ac 100644 --- a/services/namespaces/service.go +++ b/services/namespaces/service.go @@ -29,19 +29,19 @@ func init() { if err != nil { return nil, err } - return NewService(m.(*bolt.DB), ic.Events), nil + return NewService(m.(*metadata.DB), ic.Events), nil }, }) } type Service struct { - db *bolt.DB + db *metadata.DB publisher events.Publisher } var _ api.NamespacesServer = &Service{} -func NewService(db *bolt.DB, publisher events.Publisher) api.NamespacesServer { +func NewService(db *metadata.DB, publisher events.Publisher) api.NamespacesServer { return &Service{ db: db, publisher: publisher, diff --git a/services/snapshot/service.go b/services/snapshot/service.go index 502958e04..581375f36 100644 --- a/services/snapshot/service.go +++ b/services/snapshot/service.go @@ -3,7 +3,6 @@ package snapshot import ( gocontext "context" - "github.com/boltdb/bolt" eventsapi "github.com/containerd/containerd/api/services/events/v1" snapshotapi "github.com/containerd/containerd/api/services/snapshot/v1" "github.com/containerd/containerd/api/types" @@ -50,7 +49,7 @@ func newService(ic *plugin.InitContext) (interface{}, error) { } snapshotters := make(map[string]snapshot.Snapshotter) for name, sn := range rawSnapshotters { - snapshotters[name] = metadata.NewSnapshotter(md.(*bolt.DB), name, sn.(snapshot.Snapshotter)) + snapshotters[name] = metadata.NewSnapshotter(md.(*metadata.DB), name, sn.(snapshot.Snapshotter)) } if len(snapshotters) == 0 { diff --git a/services/tasks/service.go b/services/tasks/service.go index cc126e73b..84a04dc03 100644 --- a/services/tasks/service.go +++ b/services/tasks/service.go @@ -63,7 +63,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { if err != nil { return nil, err } - cs := metadata.NewContentStore(m.(*bolt.DB), ct.(content.Store)) + cs := metadata.NewContentStore(m.(*metadata.DB), ct.(content.Store)) runtimes := make(map[string]runtime.Runtime) for _, rr := range rt { r := rr.(runtime.Runtime) @@ -71,7 +71,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { } return &Service{ runtimes: runtimes, - db: m.(*bolt.DB), + db: m.(*metadata.DB), store: cs, publisher: ic.Events, }, nil @@ -79,7 +79,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { type Service struct { runtimes map[string]runtime.Runtime - db *bolt.DB + db *metadata.DB store content.Store publisher events.Publisher } diff --git a/windows/runtime.go b/windows/runtime.go index e16c531a1..4c0afb4cc 100644 --- a/windows/runtime.go +++ b/windows/runtime.go @@ -16,6 +16,7 @@ import ( "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/events" "github.com/containerd/containerd/log" + "github.com/containerd/containerd/metadata" "github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/plugin" "github.com/containerd/containerd/runtime" @@ -68,7 +69,7 @@ func New(ic *plugin.InitContext) (interface{}, error) { // TODO(mlaventure): windows needs a stat monitor monitor: nil, tasks: runtime.NewTaskList(), - db: m.(*bolt.DB), + db: m.(*metadata.DB), } // Load our existing containers and kill/delete them. We don't support @@ -89,7 +90,7 @@ type windowsRuntime struct { monitor runtime.TaskMonitor tasks *runtime.TaskList - db *bolt.DB + db *metadata.DB } func (r *windowsRuntime) ID() string {