mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-12 18:11:34 +08:00
update service publish rule
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/strategy"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/service"
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/api"
|
||||
@@ -23,6 +25,7 @@ type imlServiceDiff struct {
|
||||
apiDocService api_doc.IAPIDocService `autowired:""`
|
||||
upstreamService upstream.IUpstreamService `autowired:""`
|
||||
releaseService release.IReleaseService `autowired:""`
|
||||
strategyService strategy.IStrategyService `autowired:""`
|
||||
clusterService cluster.IClusterService `autowired:""`
|
||||
}
|
||||
|
||||
@@ -79,6 +82,34 @@ func (m *imlServiceDiff) getBaseInfo(ctx context.Context, serviceId, baseRelease
|
||||
|
||||
return base, nil
|
||||
}
|
||||
|
||||
func (m *imlServiceDiff) latestStrategyCommits(ctx context.Context, serviceId string) ([]*commit.Commit[strategy.StrategyCommit], error) {
|
||||
list, err := m.strategyService.All(ctx, 2, serviceId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get latest strategy failed:%w", err)
|
||||
}
|
||||
|
||||
return utils.SliceToSlice(list, func(s *strategy.Strategy) *commit.Commit[strategy.StrategyCommit] {
|
||||
key := fmt.Sprintf("service-%s", s.Id)
|
||||
return &commit.Commit[strategy.StrategyCommit]{
|
||||
Target: s.Id,
|
||||
Key: key,
|
||||
Data: &strategy.StrategyCommit{
|
||||
Id: s.Id,
|
||||
Name: s.Name,
|
||||
Priority: s.Priority,
|
||||
Filters: s.Filters,
|
||||
Config: s.Config,
|
||||
Driver: s.Driver,
|
||||
IsStop: s.IsStop,
|
||||
Version: s.UpdateAt.Format("20060102150405"),
|
||||
},
|
||||
}
|
||||
}, func(s *strategy.Strategy) bool {
|
||||
return !s.IsDelete
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (m *imlServiceDiff) DiffForLatest(ctx context.Context, serviceId string, baseRelease string) (*service_diff.Diff, bool, error) {
|
||||
serviceInfo, err := m.serviceService.Get(ctx, serviceId)
|
||||
if err != nil {
|
||||
@@ -130,6 +161,11 @@ func (m *imlServiceDiff) DiffForLatest(ctx context.Context, serviceId string, ba
|
||||
return nil, false, fmt.Errorf("upstream not found")
|
||||
}
|
||||
|
||||
strategyCommits, err := m.latestStrategyCommits(ctx, serviceId)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
base, err := m.getBaseInfo(ctx, serviceId, baseRelease)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
@@ -140,6 +176,7 @@ func (m *imlServiceDiff) DiffForLatest(ctx context.Context, serviceId string, ba
|
||||
apiProxyCommits: proxy,
|
||||
apiDocCommits: apiDocCommits,
|
||||
upstreamCommits: upstreamCommits,
|
||||
strategyCommits: strategyCommits,
|
||||
}
|
||||
clusters, err := m.clusterService.List(ctx)
|
||||
if err != nil {
|
||||
@@ -211,6 +248,46 @@ func (m *imlServiceDiff) getReleaseInfo(ctx context.Context, releaseId string) (
|
||||
upstreamCommits: upstreamCommits,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *imlServiceDiff) diffStrategies(base, target []*commit.Commit[strategy.StrategyCommit]) []*service_diff.StrategyDiff {
|
||||
baseStrategy := utils.SliceToMap(base, func(i *commit.Commit[strategy.StrategyCommit]) string {
|
||||
return i.Target
|
||||
})
|
||||
targetStrategy := utils.SliceToMap(target, func(i *commit.Commit[strategy.StrategyCommit]) string {
|
||||
return i.Target
|
||||
})
|
||||
out := make([]*service_diff.StrategyDiff, 0, len(target))
|
||||
for _, tc := range targetStrategy {
|
||||
key := tc.Target
|
||||
t := tc.Data
|
||||
o := &service_diff.StrategyDiff{
|
||||
Strategy: key,
|
||||
Name: t.Name,
|
||||
Priority: t.Priority,
|
||||
Change: service_diff.ChangeTypeNone,
|
||||
Status: 0,
|
||||
}
|
||||
b, hasB := baseStrategy[key]
|
||||
if !hasB {
|
||||
o.Change = service_diff.ChangeTypeNew
|
||||
} else if b.UUID != tc.UUID {
|
||||
o.Change = service_diff.ChangeTypeUpdate
|
||||
}
|
||||
delete(baseStrategy, key)
|
||||
out = append(out, o)
|
||||
}
|
||||
for _, b := range baseStrategy {
|
||||
o := &service_diff.StrategyDiff{
|
||||
Strategy: b.Target,
|
||||
Name: b.Data.Name,
|
||||
Priority: b.Data.Priority,
|
||||
Change: service_diff.ChangeTypeDelete,
|
||||
Status: 0,
|
||||
}
|
||||
out = append(out, o)
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (m *imlServiceDiff) diff(partitions []string, base, target *projectInfo) *service_diff.Diff {
|
||||
out := &service_diff.Diff{
|
||||
Apis: nil,
|
||||
@@ -328,6 +405,7 @@ func (m *imlServiceDiff) diff(partitions []string, base, target *projectInfo) *s
|
||||
}
|
||||
}
|
||||
}
|
||||
out.Strategies = m.diffStrategies(base.strategyCommits, target.strategyCommits)
|
||||
|
||||
return out
|
||||
}
|
||||
@@ -373,5 +451,13 @@ func (m *imlServiceDiff) Out(ctx context.Context, diff *service_diff.Diff) (*Dif
|
||||
}),
|
||||
})
|
||||
}
|
||||
out.Strategies = utils.SliceToSlice(diff.Strategies, func(i *service_diff.StrategyDiff) *StrategyDiffOut {
|
||||
return &StrategyDiffOut{
|
||||
Name: i.Name,
|
||||
Priority: i.Priority,
|
||||
Change: i.Change,
|
||||
Status: i.Status,
|
||||
}
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -4,13 +4,15 @@ import (
|
||||
"github.com/APIParkLab/APIPark/service/api"
|
||||
api_doc "github.com/APIParkLab/APIPark/service/api-doc"
|
||||
"github.com/APIParkLab/APIPark/service/service_diff"
|
||||
"github.com/APIParkLab/APIPark/service/strategy"
|
||||
"github.com/APIParkLab/APIPark/service/universally/commit"
|
||||
"github.com/APIParkLab/APIPark/service/upstream"
|
||||
)
|
||||
|
||||
type DiffOut struct {
|
||||
Routers []*RouterDiffOut `json:"routers"`
|
||||
Upstreams []*UpstreamDiffOut `json:"upstreams"`
|
||||
Routers []*RouterDiffOut `json:"routers"`
|
||||
Upstreams []*UpstreamDiffOut `json:"upstreams"`
|
||||
Strategies []*StrategyDiffOut `json:"strategies"`
|
||||
}
|
||||
|
||||
type RouterDiffOut struct {
|
||||
@@ -30,6 +32,13 @@ type UpstreamDiffOut struct {
|
||||
Addr []string `json:"addr,omitempty"`
|
||||
}
|
||||
|
||||
type StrategyDiffOut struct {
|
||||
Name string `json:"name"`
|
||||
Priority int `json:"priority"`
|
||||
Change service_diff.ChangeType `json:"change,omitempty"`
|
||||
Status service_diff.StatusType `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type projectInfo struct {
|
||||
id string
|
||||
//apis []*api.Info
|
||||
@@ -37,4 +46,5 @@ type projectInfo struct {
|
||||
apiProxyCommits []*commit.Commit[api.Proxy]
|
||||
apiDocCommits []*commit.Commit[api_doc.DocCommit]
|
||||
upstreamCommits []*commit.Commit[upstream.Config]
|
||||
strategyCommits []*commit.Commit[strategy.StrategyCommit]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user