Automatically publish policies and logs during cluster initialization

This commit is contained in:
Liujian
2024-12-09 00:43:05 +08:00
parent bc00f2d577
commit 3dd44c97e9
5 changed files with 102 additions and 21 deletions
+55 -4
View File
@@ -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 -1
View File
@@ -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)
})
}