mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-15 22:53:43 +00:00
virtcontainers: store: Add an internal backend interface
All Store backends will have to implement that simple interface. Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
parent
6b87ecfc1b
commit
d22cdf2dd9
50
virtcontainers/store/backend.go
Normal file
50
virtcontainers/store/backend.go
Normal file
@ -0,0 +1,50 @@
|
||||
// Copyright (c) 2019 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type backendType string
|
||||
|
||||
const (
|
||||
filesystemBackend backendType = "filesystem"
|
||||
)
|
||||
|
||||
const (
|
||||
filesystemScheme string = "file"
|
||||
)
|
||||
|
||||
func schemeToBackendType(scheme string) (backendType, error) {
|
||||
switch scheme {
|
||||
case filesystemScheme:
|
||||
return filesystemBackend, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Unsupported scheme %s", scheme)
|
||||
}
|
||||
|
||||
func newBackend(scheme string) (backend, error) {
|
||||
t, err := schemeToBackendType(scheme)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch t {
|
||||
case filesystemBackend:
|
||||
return &filesystem{}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Unsupported scheme %s", scheme)
|
||||
}
|
||||
|
||||
type backend interface {
|
||||
new(ctx context.Context, path string, host string) error
|
||||
load(item Item, data interface{}) error
|
||||
store(item Item, data interface{}) error
|
||||
}
|
70
virtcontainers/store/filesystem.go
Normal file
70
virtcontainers/store/filesystem.go
Normal file
@ -0,0 +1,70 @@
|
||||
// Copyright (c) 2019 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type filesystem struct {
|
||||
ctx context.Context
|
||||
|
||||
path string
|
||||
}
|
||||
|
||||
// Logger returns a logrus logger appropriate for logging Store filesystem messages
|
||||
func (f *filesystem) logger() *logrus.Entry {
|
||||
return storeLog.WithFields(logrus.Fields{
|
||||
"subsystem": "store",
|
||||
"backend": "filesystem",
|
||||
"path": f.path,
|
||||
})
|
||||
}
|
||||
|
||||
func (f *filesystem) trace(name string) (opentracing.Span, context.Context) {
|
||||
if f.ctx == nil {
|
||||
f.logger().WithField("type", "bug").Error("trace called before context set")
|
||||
f.ctx = context.Background()
|
||||
}
|
||||
|
||||
span, ctx := opentracing.StartSpanFromContext(f.ctx, name)
|
||||
|
||||
span.SetTag("subsystem", "store")
|
||||
span.SetTag("type", "filesystem")
|
||||
span.SetTag("path", f.path)
|
||||
|
||||
return span, ctx
|
||||
}
|
||||
|
||||
func (f *filesystem) new(ctx context.Context, path string, host string) error {
|
||||
f.ctx = ctx
|
||||
f.path = path
|
||||
|
||||
f.logger().Infof("New filesystem store backend for %s", path)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *filesystem) load(item Item, data interface{}) error {
|
||||
span, _ := f.trace("load")
|
||||
defer span.Finish()
|
||||
|
||||
span.SetTag("item", item)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *filesystem) store(item Item, data interface{}) error {
|
||||
span, _ := f.trace("store")
|
||||
defer span.Finish()
|
||||
|
||||
span.SetTag("item", item)
|
||||
|
||||
return nil
|
||||
}
|
@ -86,6 +86,8 @@ type Store struct {
|
||||
scheme string
|
||||
path string
|
||||
host string
|
||||
|
||||
backend backend
|
||||
}
|
||||
|
||||
type manager struct {
|
||||
@ -152,6 +154,18 @@ func New(ctx context.Context, storeURL string) (*Store, error) {
|
||||
host: u.Host,
|
||||
}
|
||||
|
||||
backend, err := newBackend(s.scheme)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.backend = backend
|
||||
|
||||
// Create new backend
|
||||
if err := s.backend.new(ctx, s.path, s.host); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := stores.addStore(s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -193,7 +207,7 @@ func (s *Store) Load(item Item, data interface{}) error {
|
||||
s.RLock()
|
||||
defer s.RUnlock()
|
||||
|
||||
return nil
|
||||
return s.backend.load(item, data)
|
||||
}
|
||||
|
||||
// Store stores a virtcontainers item into a Store.
|
||||
@ -206,5 +220,5 @@ func (s *Store) Store(item Item, data interface{}) error {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
|
||||
return nil
|
||||
return s.backend.store(item, data)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user