libnetwork/datastore: remove DataStore interface

It only had a single implementation, so let's remove the interface.

While changing, also renaming;

- datastore.DataStore -> datastore.Store
- datastore.NewDataStore -> datastore.New
- datastore.NewDataStoreFromConfig -> datastore.FromConfig

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-21 14:24:18 +02:00
parent d5b1e43b8f
commit 704a19b25d
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
13 changed files with 48 additions and 67 deletions

View file

@ -91,7 +91,7 @@ type Controller struct {
ipamRegistry drvregistry.IPAMs
sandboxes sandboxTable
cfg *config.Config
store datastore.DataStore
store *datastore.Store
extKeyListener net.Listener
watchCh chan *Endpoint
unWatchCh chan *Endpoint

View file

@ -13,10 +13,10 @@ type kvMap map[string]KVObject
type cache struct {
sync.Mutex
kmm map[string]kvMap
ds *datastore
ds *Store
}
func newCache(ds *datastore) *cache {
func newCache(ds *Store) *cache {
return &cache{kmm: make(map[string]kvMap), ds: ds}
}

View file

@ -12,42 +12,20 @@ import (
"github.com/docker/docker/libnetwork/types"
)
// DataStore exported
type DataStore interface {
// GetObject gets data from datastore and unmarshals to the specified object
GetObject(key string, o KVObject) error
// PutObjectAtomic provides an atomic add and update operation for a Record
PutObjectAtomic(kvObject KVObject) error
// DeleteObjectAtomic performs an atomic delete operation
DeleteObjectAtomic(kvObject KVObject) error
// List returns of a list of KVObjects belonging to the parent
// key. The caller must pass a KVObject of the same type as
// the objects that need to be listed
List(string, KVObject) ([]KVObject, error)
// Map returns a Map of KVObjects
Map(key string, kvObject KVObject) (map[string]KVObject, error)
// Scope returns the scope of the store
Scope() string
// KVStore returns access to the KV Store
KVStore() store.Store
// Close closes the data store
Close()
}
// ErrKeyModified is raised for an atomic update when the update is working on a stale state
var (
ErrKeyModified = store.ErrKeyModified
ErrKeyNotFound = store.ErrKeyNotFound
)
type datastore struct {
type Store struct {
mu sync.Mutex
scope string
store store.Store
cache *cache
}
// KVObject is Key/Value interface used by objects to be part of the DataStore
// KVObject is Key/Value interface used by objects to be part of the Store.
type KVObject interface {
// Key method lets an object provide the Key to be used in KV Store
Key() []string
@ -61,7 +39,7 @@ type KVObject interface {
Index() uint64
// SetIndex method allows the datastore to store the latest DB Index into the object
SetIndex(uint64)
// True if the object exists in the datastore, false if it hasn't been stored yet.
// Exists returns true if the object exists in the datastore, false if it hasn't been stored yet.
// When SetIndex() is called, the object has been stored.
Exists() bool
// DataScope indicates the storage scope of the KV object
@ -166,7 +144,7 @@ func ParseKey(key string) ([]string, error) {
}
// newClient used to connect to KV Store
func newClient(kv string, addr string, config *store.Config) (DataStore, error) {
func newClient(kv string, addr string, config *store.Config) (*Store, error) {
if config == nil {
config = &store.Config{}
}
@ -192,14 +170,14 @@ func newClient(kv string, addr string, config *store.Config) (DataStore, error)
return nil, err
}
ds := &datastore{scope: LocalScope, store: s}
ds := &Store{scope: LocalScope, store: s}
ds.cache = newCache(ds)
return ds, nil
}
// NewDataStore creates a new instance of LibKV data store
func NewDataStore(cfg ScopeCfg) (DataStore, error) {
// New creates a new Store instance.
func New(cfg ScopeCfg) (*Store, error) {
if cfg.Client.Provider == "" || cfg.Client.Address == "" {
cfg = DefaultScope("")
}
@ -207,8 +185,8 @@ func NewDataStore(cfg ScopeCfg) (DataStore, error) {
return newClient(cfg.Client.Provider, cfg.Client.Address, cfg.Client.Config)
}
// NewDataStoreFromConfig creates a new instance of LibKV data store starting from the datastore config data
func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, error) {
// FromConfig creates a new instance of LibKV data store starting from the datastore config data.
func FromConfig(dsc discoverapi.DatastoreConfigData) (*Store, error) {
var (
ok bool
sCfgP *store.Config
@ -219,15 +197,13 @@ func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, err
return nil, fmt.Errorf("cannot parse store configuration: %v", dsc.Config)
}
scopeCfg := ScopeCfg{
ds, err := New(ScopeCfg{
Client: ScopeClientCfg{
Address: dsc.Address,
Provider: dsc.Provider,
Config: sCfgP,
},
}
ds, err := NewDataStore(scopeCfg)
})
if err != nil {
return nil, fmt.Errorf("failed to construct datastore client from datastore configuration %v: %v", dsc, err)
}
@ -235,20 +211,23 @@ func NewDataStoreFromConfig(dsc discoverapi.DatastoreConfigData) (DataStore, err
return ds, err
}
func (ds *datastore) Close() {
// Close closes the data store.
func (ds *Store) Close() {
ds.store.Close()
}
func (ds *datastore) Scope() string {
// Scope returns the scope of the store.
func (ds *Store) Scope() string {
return ds.scope
}
func (ds *datastore) KVStore() store.Store {
// KVStore returns access to the KV Store.
func (ds *Store) KVStore() store.Store {
return ds.store
}
// PutObjectAtomic adds a new Record based on an object into the datastore
func (ds *datastore) PutObjectAtomic(kvObject KVObject) error {
// PutObjectAtomic provides an atomic add and update operation for a Record.
func (ds *Store) PutObjectAtomic(kvObject KVObject) error {
var (
previous *store.KVPair
pair *store.KVPair
@ -297,8 +276,8 @@ add_cache:
return nil
}
// GetObject returns a record matching the key
func (ds *datastore) GetObject(key string, o KVObject) error {
// GetObject gets data from the store and unmarshals to the specified object.
func (ds *Store) GetObject(key string, o KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()
@ -321,7 +300,7 @@ func (ds *datastore) GetObject(key string, o KVObject) error {
return nil
}
func (ds *datastore) ensureParent(parent string) error {
func (ds *Store) ensureParent(parent string) error {
exists, err := ds.store.Exists(parent)
if err != nil {
return err
@ -332,7 +311,9 @@ func (ds *datastore) ensureParent(parent string) error {
return ds.store.Put(parent, []byte{})
}
func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
// List returns of a list of KVObjects belonging to the parent key. The caller
// must pass a KVObject of the same type as the objects that need to be listed.
func (ds *Store) List(key string, kvObject KVObject) ([]KVObject, error) {
ds.mu.Lock()
defer ds.mu.Unlock()
@ -351,7 +332,7 @@ func (ds *datastore) List(key string, kvObject KVObject) ([]KVObject, error) {
return kvol, nil
}
func (ds *datastore) iterateKVPairsFromStore(key string, kvObject KVObject, callback func(string, KVObject)) error {
func (ds *Store) iterateKVPairsFromStore(key string, kvObject KVObject, callback func(string, KVObject)) error {
// Bail out right away if the kvObject does not implement KVConstructor
ctor, ok := kvObject.(KVConstructor)
if !ok {
@ -387,7 +368,8 @@ func (ds *datastore) iterateKVPairsFromStore(key string, kvObject KVObject, call
return nil
}
func (ds *datastore) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
// Map returns a Map of KVObjects.
func (ds *Store) Map(key string, kvObject KVObject) (map[string]KVObject, error) {
ds.mu.Lock()
defer ds.mu.Unlock()
@ -403,8 +385,8 @@ func (ds *datastore) Map(key string, kvObject KVObject) (map[string]KVObject, er
return kvol, nil
}
// DeleteObjectAtomic performs atomic delete on a record
func (ds *datastore) DeleteObjectAtomic(kvObject KVObject) error {
// DeleteObjectAtomic performs atomic delete on a record.
func (ds *Store) DeleteObjectAtomic(kvObject KVObject) error {
ds.mu.Lock()
defer ds.mu.Unlock()

View file

@ -11,9 +11,9 @@ import (
var dummyKey = "dummy"
// NewCustomDataStore can be used by other Tests in order to use custom datastore
func NewTestDataStore() DataStore {
return &datastore{scope: LocalScope, store: NewMockStore()}
// NewTestDataStore can be used by other Tests in order to use custom datastore
func NewTestDataStore() *Store {
return &Store{scope: LocalScope, store: NewMockStore()}
}
func TestKey(t *testing.T) {
@ -36,13 +36,12 @@ func TestParseKey(t *testing.T) {
}
func TestInvalidDataStore(t *testing.T) {
config := ScopeCfg{
_, err := New(ScopeCfg{
Client: ScopeClientCfg{
Provider: "invalid",
Address: "localhost:8500",
},
}
_, err := NewDataStore(config)
})
if err == nil {
t.Fatal("Invalid Datastore connection configuration must result in a failure")
}

View file

@ -153,7 +153,7 @@ type driver struct {
isolationChain1V6 *iptables.ChainInfo
isolationChain2V6 *iptables.ChainInfo
networks map[string]*bridgeNetwork
store datastore.DataStore
store *datastore.Store
nlh *netlink.Handle
configNetwork sync.Mutex
portAllocator *portallocator.PortAllocator // Overridable for tests.

View file

@ -30,7 +30,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
d.store, err = datastore.NewDataStoreFromConfig(dsc)
d.store, err = datastore.FromConfig(dsc)
if err != nil {
return types.InternalErrorf("bridge driver failed to initialize data store: %v", err)
}

View file

@ -40,7 +40,7 @@ type driver struct {
networks networkTable
sync.Once
sync.Mutex
store datastore.DataStore
store *datastore.Store
}
type endpoint struct {

View file

@ -50,7 +50,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
d.store, err = datastore.NewDataStoreFromConfig(dsc)
d.store, err = datastore.FromConfig(dsc)
if err != nil {
return types.InternalErrorf("ipvlan driver failed to initialize data store: %v", err)
}

View file

@ -34,7 +34,7 @@ type driver struct {
networks networkTable
sync.Once
sync.Mutex
store datastore.DataStore
store *datastore.Store
}
type endpoint struct {

View file

@ -49,7 +49,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
d.store, err = datastore.NewDataStoreFromConfig(dsc)
d.store, err = datastore.FromConfig(dsc)
if err != nil {
return types.InternalErrorf("macvlan driver failed to initialize data store: %v", err)
}

View file

@ -101,7 +101,7 @@ type hnsNetwork struct {
type driver struct {
name string
networks map[string]*hnsNetwork
store datastore.DataStore
store *datastore.Store
sync.Mutex
}

View file

@ -27,7 +27,7 @@ func (d *driver) initStore(option map[string]interface{}) error {
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}
d.store, err = datastore.NewDataStoreFromConfig(dsc)
d.store, err = datastore.FromConfig(dsc)
if err != nil {
return types.InternalErrorf("windows driver failed to initialize data store: %v", err)
}

View file

@ -24,7 +24,7 @@ func (c *Controller) initStores() error {
return nil
}
var err error
c.store, err = datastore.NewDataStore(c.cfg.Scope)
c.store, err = datastore.New(c.cfg.Scope)
if err != nil {
return err
}
@ -39,7 +39,7 @@ func (c *Controller) closeStores() {
}
}
func (c *Controller) getStore() datastore.DataStore {
func (c *Controller) getStore() *datastore.Store {
c.mu.Lock()
defer c.mu.Unlock()