mirror of
https://github.com/tailscale/tailscale.git
synced 2025-06-07 16:28:37 +00:00
ipn/store: remove a layer of indirection for registering stores (#15986)
Registering a new store is cheap, it just adds a map entry. No need to lazy-init it with sync.Once and an intermediate slice holding init functions. Updates #cleanup Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
This commit is contained in:
parent
5a8b99e977
commit
0bab16448e
@ -12,10 +12,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerAvailableExternalStores = append(registerAvailableExternalStores, registerAWSStore)
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerAWSStore() {
|
|
||||||
Register("arn:", func(logf logger.Logf, arg string) (ipn.StateStore, error) {
|
Register("arn:", func(logf logger.Logf, arg string) (ipn.StateStore, error) {
|
||||||
ssmARN, opts, err := awsstore.ParseARNAndOpts(arg)
|
ssmARN, opts, err := awsstore.ParseARNAndOpts(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -14,10 +14,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registerAvailableExternalStores = append(registerAvailableExternalStores, registerKubeStore)
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerKubeStore() {
|
|
||||||
Register("kube:", func(logf logger.Logf, path string) (ipn.StateStore, error) {
|
Register("kube:", func(logf logger.Logf, path string) (ipn.StateStore, error) {
|
||||||
secretName := strings.TrimPrefix(path, "kube:")
|
secretName := strings.TrimPrefix(path, "kube:")
|
||||||
return kubestore.New(logf, secretName)
|
return kubestore.New(logf, secretName)
|
||||||
|
@ -26,16 +26,8 @@ import (
|
|||||||
// The arg is of the form "prefix:rest", where prefix was previously registered with Register.
|
// The arg is of the form "prefix:rest", where prefix was previously registered with Register.
|
||||||
type Provider func(logf logger.Logf, arg string) (ipn.StateStore, error)
|
type Provider func(logf logger.Logf, arg string) (ipn.StateStore, error)
|
||||||
|
|
||||||
var regOnce sync.Once
|
func init() {
|
||||||
|
|
||||||
var registerAvailableExternalStores []func()
|
|
||||||
|
|
||||||
func registerDefaultStores() {
|
|
||||||
Register("mem:", mem.New)
|
Register("mem:", mem.New)
|
||||||
|
|
||||||
for _, f := range registerAvailableExternalStores {
|
|
||||||
f()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var knownStores map[string]Provider
|
var knownStores map[string]Provider
|
||||||
@ -55,7 +47,6 @@ var knownStores map[string]Provider
|
|||||||
// the suffix is a Kubernetes secret name
|
// the suffix is a Kubernetes secret name
|
||||||
// - In all other cases, the path is treated as a filepath.
|
// - In all other cases, the path is treated as a filepath.
|
||||||
func New(logf logger.Logf, path string) (ipn.StateStore, error) {
|
func New(logf logger.Logf, path string) (ipn.StateStore, error) {
|
||||||
regOnce.Do(registerDefaultStores)
|
|
||||||
for prefix, sf := range knownStores {
|
for prefix, sf := range knownStores {
|
||||||
if strings.HasPrefix(path, prefix) {
|
if strings.HasPrefix(path, prefix) {
|
||||||
// We can't strip the prefix here as some NewStoreFunc (like arn:)
|
// We can't strip the prefix here as some NewStoreFunc (like arn:)
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"maps"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -14,10 +15,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNewStore(t *testing.T) {
|
func TestNewStore(t *testing.T) {
|
||||||
regOnce.Do(registerDefaultStores)
|
oldKnownStores := maps.Clone(knownStores)
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
knownStores = map[string]Provider{}
|
knownStores = oldKnownStores
|
||||||
registerDefaultStores()
|
|
||||||
})
|
})
|
||||||
knownStores = map[string]Provider{}
|
knownStores = map[string]Provider{}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user