mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 15:26:40 +00:00
Move StoreKind under own StoreConfig configuration and rename to Engine (#1219)
* Move StoreKind under own StoreConfig configuration parameter * Rename StoreKind option to Engine * Rename StoreKind internal methods and types to Engine * Add template engine value test --------- Co-authored-by: Maycon Santos <mlsmaycon@gmail.com>
This commit is contained in:
@@ -126,7 +126,7 @@ var (
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store, err := server.NewStore(config.StoreKind, config.Datadir, appMetrics)
|
||||
store, err := server.NewStore(config.StoreConfig.Engine, config.Datadir, appMetrics)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed creating Store: %s: %v", config.Datadir, err)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ type Config struct {
|
||||
|
||||
PKCEAuthorizationFlow *PKCEAuthorizationFlow
|
||||
|
||||
StoreKind StoreKind
|
||||
StoreConfig StoreConfig
|
||||
}
|
||||
|
||||
// GetAuthAudiences returns the audience from the http config and device authorization flow config
|
||||
@@ -138,6 +138,11 @@ type ProviderConfig struct {
|
||||
RedirectURLs []string
|
||||
}
|
||||
|
||||
// StoreConfig contains Store configuration
|
||||
type StoreConfig struct {
|
||||
Engine StoreEngine
|
||||
}
|
||||
|
||||
// validateURL validates input http url
|
||||
func validateURL(httpURL string) bool {
|
||||
_, err := url.ParseRequestURI(httpURL)
|
||||
|
||||
@@ -615,7 +615,7 @@ func (s *FileStore) Close() error {
|
||||
return s.persist(s.storeFile)
|
||||
}
|
||||
|
||||
// GetStoreKind returns FileStoreKind
|
||||
func (s *FileStore) GetStoreKind() StoreKind {
|
||||
return FileStoreKind
|
||||
// GetStoreEngine returns FileStoreEngine
|
||||
func (s *FileStore) GetStoreEngine() StoreEngine {
|
||||
return FileStoreEngine
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ type properties map[string]interface{}
|
||||
// DataSource metric data source
|
||||
type DataSource interface {
|
||||
GetAllAccounts() []*server.Account
|
||||
GetStoreKind() server.StoreKind
|
||||
GetStoreEngine() server.StoreEngine
|
||||
}
|
||||
|
||||
// ConnManager peer connection manager that holds state for current active connections
|
||||
@@ -296,7 +296,7 @@ func (w *Worker) generateProperties() properties {
|
||||
metricsProperties["max_active_peer_version"] = maxActivePeerVersion
|
||||
metricsProperties["ui_clients"] = uiClient
|
||||
metricsProperties["idp_manager"] = w.idpManager
|
||||
metricsProperties["store_kind"] = w.dataSource.GetStoreKind()
|
||||
metricsProperties["store_engine"] = w.dataSource.GetStoreEngine()
|
||||
|
||||
for protocol, count := range rulesProtocol {
|
||||
metricsProperties["rules_protocol_"+protocol] = count
|
||||
|
||||
@@ -151,9 +151,9 @@ func (mockDatasource) GetAllAccounts() []*server.Account {
|
||||
}
|
||||
}
|
||||
|
||||
// GetStoreKind returns FileStoreKind
|
||||
func (mockDatasource) GetStoreKind() server.StoreKind {
|
||||
return server.FileStoreKind
|
||||
// GetStoreEngine returns FileStoreEngine
|
||||
func (mockDatasource) GetStoreEngine() server.StoreEngine {
|
||||
return server.FileStoreEngine
|
||||
}
|
||||
|
||||
// TestGenerateProperties tests and validate the properties generation by using the mockDatasource for the Worker.generateProperties
|
||||
@@ -242,7 +242,7 @@ func TestGenerateProperties(t *testing.T) {
|
||||
t.Errorf("expected 2 user_peers, got %d", properties["user_peers"])
|
||||
}
|
||||
|
||||
if properties["store_kind"] != server.FileStoreKind {
|
||||
t.Errorf("expected JsonFile, got %s", properties["store_kind"])
|
||||
if properties["store_engine"] != server.FileStoreEngine {
|
||||
t.Errorf("expected JsonFile, got %s", properties["store_engine"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,7 +451,7 @@ func (s *SqliteStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStoreKind returns SqliteStoreKind
|
||||
func (s *SqliteStore) GetStoreKind() StoreKind {
|
||||
return SqliteStoreKind
|
||||
// GetStoreEngine returns SqliteStoreEngine
|
||||
func (s *SqliteStore) GetStoreEngine() StoreEngine {
|
||||
return SqliteStoreEngine
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/netbirdio/netbird/management/server/telemetry"
|
||||
@@ -31,42 +32,43 @@ type Store interface {
|
||||
SaveUserLastLogin(accountID, userID string, lastLogin time.Time) error
|
||||
// Close should close the store persisting all unsaved data.
|
||||
Close() error
|
||||
// GetStoreKind should return StoreKind of the current store implementation.
|
||||
// GetStoreEngine should return StoreEngine of the current store implementation.
|
||||
// This is also a method of metrics.DataSource interface.
|
||||
GetStoreKind() StoreKind
|
||||
GetStoreEngine() StoreEngine
|
||||
}
|
||||
|
||||
type StoreKind string
|
||||
type StoreEngine string
|
||||
|
||||
const (
|
||||
FileStoreKind StoreKind = "JsonFile"
|
||||
SqliteStoreKind StoreKind = "Sqlite"
|
||||
FileStoreEngine StoreEngine = "jsonfile"
|
||||
SqliteStoreEngine StoreEngine = "sqlite"
|
||||
)
|
||||
|
||||
func GetStoreKindFromEnv() StoreKind {
|
||||
kind, ok := os.LookupEnv("NETBIRD_STORE_KIND")
|
||||
func getStoreEngineFromEnv() StoreEngine {
|
||||
// NETBIRD_STORE_ENGINE supposed to be used in tests. Otherwise rely on the config file.
|
||||
kind, ok := os.LookupEnv("NETBIRD_STORE_ENGINE")
|
||||
if !ok {
|
||||
return FileStoreKind
|
||||
return FileStoreEngine
|
||||
}
|
||||
|
||||
value := StoreKind(kind)
|
||||
value := StoreEngine(strings.ToLower(kind))
|
||||
|
||||
if value == FileStoreKind || value == SqliteStoreKind {
|
||||
if value == FileStoreEngine || value == SqliteStoreEngine {
|
||||
return value
|
||||
}
|
||||
|
||||
return FileStoreKind
|
||||
return FileStoreEngine
|
||||
}
|
||||
|
||||
func NewStore(kind StoreKind, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
||||
func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
||||
if kind == "" {
|
||||
// fallback to env. Normally this only should be used from tests
|
||||
kind = GetStoreKindFromEnv()
|
||||
kind = getStoreEngineFromEnv()
|
||||
}
|
||||
switch kind {
|
||||
case FileStoreKind:
|
||||
case FileStoreEngine:
|
||||
return NewFileStore(dataDir, metrics)
|
||||
case SqliteStoreKind:
|
||||
case SqliteStoreEngine:
|
||||
return NewSqliteStore(dataDir, metrics)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported kind of store %s", kind)
|
||||
@@ -79,15 +81,14 @@ func NewStoreFromJson(dataDir string, metrics telemetry.AppMetrics) (Store, erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
kind := GetStoreKindFromEnv()
|
||||
kind := getStoreEngineFromEnv()
|
||||
|
||||
switch kind {
|
||||
case FileStoreKind:
|
||||
case FileStoreEngine:
|
||||
return fstore, nil
|
||||
case SqliteStoreKind:
|
||||
case SqliteStoreEngine:
|
||||
return NewSqliteStoreFromFileStore(fstore, dataDir, metrics)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported kind of store %s", kind)
|
||||
return nil, fmt.Errorf("unsupported store engine %s", kind)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user