Skip to content

Commit 22455e2

Browse files
committed
Automatically publish policies and logs during cluster initialization
1 parent 8ed2c84 commit 22455e2

File tree

5 files changed

+102
-21
lines changed

5 files changed

+102
-21
lines changed

module/log/iml.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
8585
cfg = &tmp
8686
}
8787
return i.transaction.Transaction(ctx, func(txCtx context.Context) error {
88-
err := i.service.UpdateLogSource(ctx, driver, &log.Save{
88+
err := i.service.UpdateLogSource(txCtx, driver, &log.Save{
8989
ID: input.ID,
9090
Cluster: &input.Cluster,
9191
Config: cfg,
9292
})
9393
if err != nil {
9494
return err
9595
}
96-
info, err := i.service.GetLogSource(ctx, driver)
96+
info, err := i.service.GetLogSource(txCtx, driver)
9797
if err != nil {
9898
return err
9999
}
@@ -102,10 +102,11 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
102102
return err
103103
}
104104

105-
client, err := i.clusterService.GatewayClient(ctx, input.Cluster)
105+
client, err := i.clusterService.GatewayClient(txCtx, input.Cluster)
106106
if err != nil {
107107
return err
108108
}
109+
defer client.Close(txCtx)
109110
dynamicClient, err := client.Dynamic(driver)
110111
if err != nil {
111112
return err
@@ -118,7 +119,7 @@ func (i *imlLogModule) Save(ctx context.Context, driver string, input *log_dto.S
118119
for k, v := range c {
119120
attr[k] = v
120121
}
121-
err = dynamicClient.Online(ctx, &gateway.DynamicRelease{
122+
err = dynamicClient.Online(txCtx, &gateway.DynamicRelease{
122123
BasicItem: &gateway.BasicItem{
123124
ID: driver,
124125
Description: "collect access log",
@@ -159,3 +160,53 @@ func (i *imlLogModule) Get(ctx context.Context, driver string) (*log_dto.LogSour
159160
UpdateAt: auto.TimeLabel(info.UpdateAt),
160161
}, nil
161162
}
163+
164+
func (i *imlLogModule) initGateway(ctx context.Context, clusterId string, clientDriver gateway.IClientDriver) error {
165+
drivers := log_driver.Drivers()
166+
if len(drivers) < 1 {
167+
return nil
168+
}
169+
170+
for _, driver := range drivers {
171+
factory, has := log_driver.GetFactory(driver)
172+
if !has {
173+
continue
174+
}
175+
info, err := i.service.GetLogSource(ctx, driver)
176+
if err != nil {
177+
continue
178+
}
179+
d, c, err := factory.Create(info.Config)
180+
if err != nil {
181+
continue
182+
}
183+
184+
dynamicClient, err := clientDriver.Dynamic(driver)
185+
if err != nil {
186+
continue
187+
}
188+
attr := make(map[string]interface{})
189+
attr["driver"] = driver
190+
attr["formatter"] = logFormatter
191+
attr["labels"] = labels
192+
attr["method"] = "POST"
193+
for k, v := range c {
194+
attr[k] = v
195+
}
196+
err = dynamicClient.Online(ctx, &gateway.DynamicRelease{
197+
BasicItem: &gateway.BasicItem{
198+
ID: driver,
199+
Description: "collect access log",
200+
Version: time.Now().Format("20060102150405"),
201+
Resource: gateway.ProfessionOutput,
202+
},
203+
Attr: attr,
204+
})
205+
if err != nil {
206+
continue
207+
}
208+
log_driver.SetDriver(driver, d)
209+
}
210+
211+
return nil
212+
}

module/log/module.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/eolinker/go-common/autowire"
88

9+
"github.com/APIParkLab/APIPark/gateway"
910
log_dto "github.com/APIParkLab/APIPark/module/log/dto"
1011
)
1112

@@ -15,7 +16,11 @@ type ILogModule interface {
1516
}
1617

1718
func init() {
19+
logModule := new(imlLogModule)
20+
1821
autowire.Auto[ILogModule](func() reflect.Value {
19-
return reflect.ValueOf(new(imlLogModule))
22+
23+
gateway.RegisterInitHandleFunc(logModule.initGateway)
24+
return reflect.ValueOf(logModule)
2025
})
2126
}

module/strategy/iml.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,36 @@ func (i *imlStrategyModule) Delete(ctx context.Context, id string) error {
359359
}
360360
return i.strategyService.SortDelete(ctx, id)
361361
}
362+
363+
func (i *imlStrategyModule) initGateway(ctx context.Context, clusterId string, clientDriver gateway.IClientDriver) error {
364+
commits, err := i.strategyService.ListLatestStrategyCommit(ctx, strategy_dto.ScopeGlobal, "")
365+
if err != nil {
366+
return err
367+
}
368+
publishStrategies := make([]*eosc.Base[gateway.StrategyRelease], 0, len(commits))
369+
for _, c := range commits {
370+
l := c.Data
371+
if l.IsDelete {
372+
err = i.strategyService.Delete(ctx, l.Id)
373+
if err != nil {
374+
return err
375+
}
376+
}
377+
d, has := strategy_driver.GetDriver(l.Driver)
378+
if !has {
379+
continue
380+
}
381+
publishStrategies = append(publishStrategies, d.ToRelease(strategy_dto.ToStrategy(&strategy.Strategy{
382+
Id: l.Id,
383+
Name: l.Name,
384+
Priority: l.Priority,
385+
Filters: l.Filters,
386+
Config: l.Config,
387+
Driver: l.Driver,
388+
IsStop: l.IsStop,
389+
IsDelete: l.IsDelete,
390+
}), nil, 5000))
391+
}
392+
393+
return clientDriver.Strategy().Online(ctx, publishStrategies...)
394+
}

module/strategy/module.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"reflect"
66
"time"
77

8+
"github.com/APIParkLab/APIPark/gateway"
9+
810
"github.com/eolinker/go-common/autowire"
911

1012
_ "github.com/APIParkLab/APIPark/module/strategy/driver/data-masking"
@@ -32,6 +34,7 @@ type IStrategyModule interface {
3234
func init() {
3335
strategyModule := new(imlStrategyModule)
3436
autowire.Auto[IStrategyModule](func() reflect.Value {
37+
gateway.RegisterInitHandleFunc(strategyModule.initGateway)
3538
return reflect.ValueOf(strategyModule)
3639
})
3740
}

service/log/iml.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,7 @@ type imlLogService struct {
2525
}
2626

2727
func (i *imlLogService) OnComplete() {
28-
drivers := log_driver.Drivers()
29-
for _, d := range drivers {
30-
factory, has := log_driver.GetFactory(d)
31-
if !has {
32-
continue
33-
}
34-
s, err := i.GetLogSource(context.Background(), d)
35-
if err != nil {
36-
continue
37-
}
38-
driver, _, err := factory.Create(s.Config)
39-
if err != nil {
40-
continue
41-
}
42-
log_driver.SetDriver(d, driver)
4328

44-
}
4529
}
4630

4731
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
8064
s.UpdateAt = time.Now()
8165
}
8266

67+
err = i.store.Save(ctx, s)
68+
if err != nil {
69+
return err
70+
}
71+
8372
return nil
8473
}
8574

0 commit comments

Comments
 (0)