mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-04 10:13:53 +08:00
Automatically publish policies and logs during cluster initialization
This commit is contained in:
+55
-4
@@ -85,7 +85,7 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
|
||||
cfg = &tmp
|
||||
}
|
||||
return i.transaction.Transaction(ctx, func(txCtx context.Context) error {
|
||||
err := i.service.UpdateLogSource(ctx, driver, &log.Save{
|
||||
err := i.service.UpdateLogSource(txCtx, driver, &log.Save{
|
||||
ID: input.ID,
|
||||
Cluster: &input.Cluster,
|
||||
Config: cfg,
|
||||
@@ -93,7 +93,7 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := i.service.GetLogSource(ctx, driver)
|
||||
info, err := i.service.GetLogSource(txCtx, driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -102,10 +102,11 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
|
||||
return err
|
||||
}
|
||||
|
||||
client, err := i.clusterService.GatewayClient(ctx, input.Cluster)
|
||||
client, err := i.clusterService.GatewayClient(txCtx, input.Cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer client.Close(txCtx)
|
||||
dynamicClient, err := client.Dynamic(driver)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -118,7 +119,7 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
|
||||
for k, v := range c {
|
||||
attr[k] = v
|
||||
}
|
||||
err = dynamicClient.Online(ctx, &gateway.DynamicRelease{
|
||||
err = dynamicClient.Online(txCtx, &gateway.DynamicRelease{
|
||||
BasicItem: &gateway.BasicItem{
|
||||
ID: driver,
|
||||
Description: "collect access log",
|
||||
@@ -159,3 +160,53 @@ func (i *imlLogModule) Get(ctx context.Context, driver string) (*log_dto.LogSour
|
||||
UpdateAt: auto.TimeLabel(info.UpdateAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (i *imlLogModule) initGateway(ctx context.Context, clusterId string, clientDriver gateway.IClientDriver) error {
|
||||
drivers := log_driver.Drivers()
|
||||
if len(drivers) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, driver := range drivers {
|
||||
factory, has := log_driver.GetFactory(driver)
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
info, err := i.service.GetLogSource(ctx, driver)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
d, c, err := factory.Create(info.Config)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
dynamicClient, err := clientDriver.Dynamic(driver)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
attr := make(map[string]interface{})
|
||||
attr["driver"] = driver
|
||||
attr["formatter"] = logFormatter
|
||||
attr["labels"] = labels
|
||||
attr["method"] = "POST"
|
||||
for k, v := range c {
|
||||
attr[k] = v
|
||||
}
|
||||
err = dynamicClient.Online(ctx, &gateway.DynamicRelease{
|
||||
BasicItem: &gateway.BasicItem{
|
||||
ID: driver,
|
||||
Description: "collect access log",
|
||||
Version: time.Now().Format("20060102150405"),
|
||||
Resource: gateway.ProfessionOutput,
|
||||
},
|
||||
Attr: attr,
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
log_driver.SetDriver(driver, d)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway"
|
||||
log_dto "github.com/APIParkLab/APIPark/module/log/dto"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,11 @@ type ILogModule interface {
|
||||
}
|
||||
|
||||
func init() {
|
||||
logModule := new(imlLogModule)
|
||||
|
||||
autowire.Auto[ILogModule](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlLogModule))
|
||||
|
||||
gateway.RegisterInitHandleFunc(logModule.initGateway)
|
||||
return reflect.ValueOf(logModule)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -359,3 +359,36 @@ func (i *imlStrategyModule) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
return i.strategyService.SortDelete(ctx, id)
|
||||
}
|
||||
|
||||
func (i *imlStrategyModule) initGateway(ctx context.Context, clusterId string, clientDriver gateway.IClientDriver) error {
|
||||
commits, err := i.strategyService.ListLatestStrategyCommit(ctx, strategy_dto.ScopeGlobal, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
publishStrategies := make([]*eosc.Base[gateway.StrategyRelease], 0, len(commits))
|
||||
for _, c := range commits {
|
||||
l := c.Data
|
||||
if l.IsDelete {
|
||||
err = i.strategyService.Delete(ctx, l.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
d, has := strategy_driver.GetDriver(l.Driver)
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
publishStrategies = append(publishStrategies, d.ToRelease(strategy_dto.ToStrategy(&strategy.Strategy{
|
||||
Id: l.Id,
|
||||
Name: l.Name,
|
||||
Priority: l.Priority,
|
||||
Filters: l.Filters,
|
||||
Config: l.Config,
|
||||
Driver: l.Driver,
|
||||
IsStop: l.IsStop,
|
||||
IsDelete: l.IsDelete,
|
||||
}), nil, 5000))
|
||||
}
|
||||
|
||||
return clientDriver.Strategy().Online(ctx, publishStrategies...)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/APIParkLab/APIPark/gateway"
|
||||
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
|
||||
_ "github.com/APIParkLab/APIPark/module/strategy/driver/data-masking"
|
||||
@@ -32,6 +34,7 @@ type IStrategyModule interface {
|
||||
func init() {
|
||||
strategyModule := new(imlStrategyModule)
|
||||
autowire.Auto[IStrategyModule](func() reflect.Value {
|
||||
gateway.RegisterInitHandleFunc(strategyModule.initGateway)
|
||||
return reflect.ValueOf(strategyModule)
|
||||
})
|
||||
}
|
||||
|
||||
+5
-16
@@ -25,23 +25,7 @@ type imlLogService struct {
|
||||
}
|
||||
|
||||
func (i *imlLogService) OnComplete() {
|
||||
drivers := log_driver.Drivers()
|
||||
for _, d := range drivers {
|
||||
factory, has := log_driver.GetFactory(d)
|
||||
if !has {
|
||||
continue
|
||||
}
|
||||
s, err := i.GetLogSource(context.Background(), d)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
driver, _, err := factory.Create(s.Config)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
log_driver.SetDriver(d, driver)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (i *imlLogService) UpdateLogSource(ctx context.Context, driver string, input *Save) error {
|
||||
@@ -80,6 +64,11 @@ func (i *imlLogService) UpdateLogSource(ctx context.Context, driver string, inpu
|
||||
s.UpdateAt = time.Now()
|
||||
}
|
||||
|
||||
err = i.store.Save(ctx, s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user