From 83c78f8f5a7ce2e0734b9ef0f532d6443120517c Mon Sep 17 00:00:00 2001 From: chuntaojun Date: Mon, 4 Mar 2024 23:19:07 +0800 Subject: [PATCH] feat:add plugin register and get func --- access_control/auth/api.go | 124 ++++++++++++----------------- access_control/ratelimit/api.go | 21 +++++ access_control/whitelist/api.go | 21 +++++ apiserver/api.go | 12 ++- cmdb/api.go | 19 +++++ crypto/api.go | 36 +++++++++ observability/discoverevent/api.go | 18 +++++ observability/history/api.go | 17 ++++ observability/statis/api.go | 17 ++++ service/healthcheck/api.go | 18 +++++ store/api.go | 25 +++--- 11 files changed, 237 insertions(+), 91 deletions(-) diff --git a/access_control/auth/api.go b/access_control/auth/api.go index 59ccc2d..9e72447 100644 --- a/access_control/auth/api.go +++ b/access_control/auth/api.go @@ -19,14 +19,29 @@ package auth import ( "context" - "errors" "fmt" - "sync" apisecurity "github.com/polarismesh/specification/source/go/api/v1/security" apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" ) +var ( + _checkerSlots = make(map[string]AuthChecker) +) + +// Register 注册插件 +func RegisterAuthChecker(name string, plugin AuthChecker) { + if _, exist := _checkerSlots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + _checkerSlots[name] = plugin +} + +func GetAuthChecker(name string) (AuthChecker, bool) { + plugin, exist := _checkerSlots[name] + return plugin, exist +} + // AuthChecker 权限管理通用接口定义 type AuthChecker interface { // Initialize 执行初始化动作 @@ -43,6 +58,23 @@ type AuthChecker interface { IsOpenClientAuth() bool } +var ( + _userSlots = make(map[string]UserServer) +) + +// Register 注册插件 +func RegisterUserServer(name string, plugin UserServer) { + if _, exist := _userSlots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + _userSlots[name] = plugin +} + +func GetUserServer(name string) (UserServer, bool) { + plugin, exist := _userSlots[name] + return plugin, exist +} + // UserServer 用户数据管理 server type UserServer interface { // Initialize 初始化 @@ -90,6 +122,23 @@ type GroupOperator interface { ResetGroupToken(ctx context.Context, group *apisecurity.UserGroup) *apiservice.Response } +var ( + _strategySlots = make(map[string]StrategyServer) +) + +// Register 注册插件 +func RegisterStrategyServer(name string, plugin StrategyServer) { + if _, exist := _strategySlots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + _strategySlots[name] = plugin +} + +func GetStrategyServer(name string) (StrategyServer, bool) { + plugin, exist := _strategySlots[name] + return plugin, exist +} + // StrategyServer 策略相关操作 type StrategyServer interface { // Initialize 初始化 @@ -116,13 +165,6 @@ type StrategyServer interface { AfterResourceOperation(afterCtx *AcquireContext) error } -const ( - // DefaultUserMgnPluginName default user server name - DefaultUserMgnPluginName = "defaultUser" - // DefaultStrategyMgnPluginName default strategy server name - DefaultStrategyMgnPluginName = "defaultStrategy" -) - // Config 鉴权能力的相关配置参数 type Config struct { // Name 原AuthServer名称,已废弃 @@ -136,21 +178,6 @@ type Config struct { Strategy *StrategyConfig `yaml:"strategy"` } -func (c *Config) SetDefault() { - if c.User == nil { - c.User = &UserConfig{ - Name: DefaultUserMgnPluginName, - Option: map[string]interface{}{}, - } - } - if c.Strategy == nil { - c.Strategy = &StrategyConfig{ - Name: DefaultStrategyMgnPluginName, - Option: map[string]interface{}{}, - } - } -} - // UserConfig UserOperator的相关配置 type UserConfig struct { // Name UserOperator的名称 @@ -166,52 +193,3 @@ type StrategyConfig struct { // Option StrategyOperator的option Option map[string]interface{} `yaml:"option"` } - -var ( - // userMgnSlots 保存用户管理manager slot - userMgnSlots = map[string]UserServer{} - // strategyMgnSlots 保存策略管理manager slot - strategyMgnSlots = map[string]StrategyServer{} - once sync.Once - userMgn UserServer - strategyMgn StrategyServer - finishInit bool -) - -// RegisterUserServer 注册一个新的 UserServer -func RegisterUserServer(s UserServer) error { - name := s.Name() - if _, ok := userMgnSlots[name]; ok { - return fmt.Errorf("UserServer=[%s] exist", name) - } - - userMgnSlots[name] = s - return nil -} - -// GetUserServer 获取一个 UserServer -func GetUserServer() (UserServer, error) { - if !finishInit { - return nil, errors.New("UserServer has not done Initialize") - } - return userMgn, nil -} - -// RegisterStrategyServer 注册一个新的 StrategyServer -func RegisterStrategyServer(s StrategyServer) error { - name := s.Name() - if _, ok := strategyMgnSlots[name]; ok { - return fmt.Errorf("StrategyServer=[%s] exist", name) - } - - strategyMgnSlots[name] = s - return nil -} - -// GetStrategyServer 获取一个 StrategyServer -func GetStrategyServer() (StrategyServer, error) { - if !finishInit { - return nil, errors.New("StrategyServer has not done Initialize") - } - return strategyMgn, nil -} diff --git a/access_control/ratelimit/api.go b/access_control/ratelimit/api.go index 680dffd..3b2b48e 100644 --- a/access_control/ratelimit/api.go +++ b/access_control/ratelimit/api.go @@ -17,6 +17,27 @@ package ratelimit +import ( + "fmt" +) + +var ( + slots = make(map[string]Ratelimit) +) + +// Register 注册插件 +func Register(name string, plugin Ratelimit) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (Ratelimit, bool) { + plugin, exist := slots[name] + return plugin, exist +} + // RatelimitType rate limit type type RatelimitType int diff --git a/access_control/whitelist/api.go b/access_control/whitelist/api.go index 72ea649..7e1c69a 100644 --- a/access_control/whitelist/api.go +++ b/access_control/whitelist/api.go @@ -17,6 +17,27 @@ package whitelist +import ( + "fmt" +) + +var ( + slots = make(map[string]Whitelist) +) + +// Register 注册插件 +func Register(name string, plugin Whitelist) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (Whitelist, bool) { + server, exist := slots[name] + return server, exist +} + // ConfigEntry 单个插件配置 type ConfigEntry struct { Name string `yaml:"name"` diff --git a/apiserver/api.go b/apiserver/api.go index 9d29bbe..62be35f 100644 --- a/apiserver/api.go +++ b/apiserver/api.go @@ -52,16 +52,20 @@ type Apiserver interface { } var ( - Slots = make(map[string]Apiserver) + slots = make(map[string]Apiserver) ) // Register 注册API服务器 func Register(name string, server Apiserver) error { - if _, exist := Slots[name]; exist { + if _, exist := slots[name]; exist { return fmt.Errorf("apiserver name:%s exist", name) } - Slots[name] = server - + slots[name] = server return nil } + +func Get(name string) (Apiserver, bool) { + server, exist := slots[name] + return server, exist +} diff --git a/cmdb/api.go b/cmdb/api.go index 6aced15..f62df80 100644 --- a/cmdb/api.go +++ b/cmdb/api.go @@ -18,9 +18,28 @@ package plugin import ( + "fmt" + apimodel "github.com/polarismesh/specification/source/go/api/v1/model" ) +var ( + slots = make(map[string]CMDB) +) + +// Register 注册插件 +func Register(name string, plugin CMDB) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (CMDB, bool) { + server, exist := slots[name] + return server, exist +} + // Location cmdb信息,对应内存结构体 type Location struct { Proto *apimodel.Location diff --git a/crypto/api.go b/crypto/api.go index 400d37b..a4b20dc 100644 --- a/crypto/api.go +++ b/crypto/api.go @@ -17,6 +17,25 @@ package crypto +import "fmt" + +var ( + slots = make(map[string]Crypto) +) + +// Register 注册插件 +func RegisterCrypto(name string, plugin Crypto) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func GetCrypto(name string) (Crypto, bool) { + server, exist := slots[name] + return server, exist +} + // ConfigEntry 单个插件配置 type ConfigEntry struct { Name string `yaml:"name"` @@ -39,6 +58,23 @@ type Crypto interface { Decrypt(cryptotext string, key []byte) (string, error) } +var ( + _pwdslots = make(map[string]ParsePassword) +) + +// Register 注册插件 +func RegisterParsePassword(name string, plugin ParsePassword) { + if _, exist := _pwdslots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + _pwdslots[name] = plugin +} + +func GetParsePassword(name string) (ParsePassword, bool) { + server, exist := _pwdslots[name] + return server, exist +} + // ParsePassword Password plug -in type ParsePassword interface { // Name . diff --git a/observability/discoverevent/api.go b/observability/discoverevent/api.go index 9fafcbd..5db83ad 100644 --- a/observability/discoverevent/api.go +++ b/observability/discoverevent/api.go @@ -1,11 +1,29 @@ package discoverevent import ( + "fmt" "time" apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" ) +var ( + slots = make(map[string]DiscoverChannel) +) + +// Register 注册插件 +func Register(name string, plugin DiscoverChannel) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (DiscoverChannel, bool) { + server, exist := slots[name] + return server, exist +} + // ConfigEntry 单个插件配置 type ConfigEntry struct { Name string `yaml:"name"` diff --git a/observability/history/api.go b/observability/history/api.go index c892a0c..9480f66 100644 --- a/observability/history/api.go +++ b/observability/history/api.go @@ -22,6 +22,23 @@ import ( "time" ) +var ( + slots = make(map[string]History) +) + +// Register 注册插件 +func Register(name string, plugin History) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (History, bool) { + server, exist := slots[name] + return server, exist +} + // ConfigEntry 单个插件配置 type ConfigEntry struct { Name string `yaml:"name"` diff --git a/observability/statis/api.go b/observability/statis/api.go index 14d3322..c983806 100644 --- a/observability/statis/api.go +++ b/observability/statis/api.go @@ -23,6 +23,23 @@ import ( "time" ) +var ( + slots = make(map[string]Statis) +) + +// Register 注册插件 +func Register(name string, plugin Statis) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (Statis, bool) { + server, exist := slots[name] + return server, exist +} + // ConfigEntry 单个插件配置 type ConfigEntry struct { Name string `yaml:"name"` diff --git a/service/healthcheck/api.go b/service/healthcheck/api.go index 6c369e1..5346f92 100644 --- a/service/healthcheck/api.go +++ b/service/healthcheck/api.go @@ -19,8 +19,26 @@ package healthcheck import ( "context" + "fmt" ) +var ( + slots = make(map[string]HealthChecker) +) + +// Register 注册插件 +func Register(name string, plugin HealthChecker) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) + } + slots[name] = plugin +} + +func Get(name string) (HealthChecker, bool) { + server, exist := slots[name] + return server, exist +} + // ReportRequest report heartbeat request type ReportRequest struct { QueryRequest diff --git a/store/api.go b/store/api.go index de089f7..73f73de 100644 --- a/store/api.go +++ b/store/api.go @@ -18,30 +18,27 @@ package store import ( - "errors" - "sync" + "fmt" "time" "github.com/polarismesh/polaris-plugin-api/store/model" ) var ( - // StoreSlots store slots - StoreSlots = make(map[string]Store) - - once = &sync.Once{} - config = &Config{} + slots = make(map[string]Store) ) -// RegisterStore 注册一个新的Store -func RegisterStore(s Store) error { - name := s.Name() - if _, ok := StoreSlots[name]; ok { - return errors.New("store name already existed") +// Register 注册插件 +func Register(name string, plugin Store) { + if _, exist := slots[name]; exist { + panic(fmt.Sprintf("existed plugin: name=%v", name)) } + slots[name] = plugin +} - StoreSlots[name] = s - return nil +func Get(name string) (Store, bool) { + server, exist := slots[name] + return server, exist } // Config Store的通用配置