Skip to content

Commit

Permalink
Automatically publish policies and logs during cluster initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Dec 8, 2024
1 parent 8ed2c84 commit 22455e2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 21 deletions.
59 changes: 55 additions & 4 deletions module/log/iml.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ 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,
})
if err != nil {
return err
}
info, err := i.service.GetLogSource(ctx, driver)
info, err := i.service.GetLogSource(txCtx, driver)
if err != nil {
return err
}
Expand All @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion module/log/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
})
}
33 changes: 33 additions & 0 deletions module/strategy/iml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
3 changes: 3 additions & 0 deletions module/strategy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
21 changes: 5 additions & 16 deletions service/log/iml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 22455e2

Please sign in to comment.