Skip to content

Commit

Permalink
fix: 所有配置文件都支持按绝对路径搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 committed Jun 28, 2024
1 parent bdddfb5 commit 7394999
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,8 @@ func (s *Service) ListAppBoundTmplRevisions(ctx context.Context, req *pbcs.ListA
TemplateSetId: tmplSet.TemplateSetId,
TemplateSetName: tmplSet.TemplateSetName,
}

revisions := tmplSetMap[tmplSet.TemplateSetId]
// 先按照path+name排序好
// 根据path+name排序
sort.SliceStable(revisions, func(i, j int) bool {
iPath := path.Join(revisions[i].Path, revisions[i].Name)
jPath := path.Join(revisions[j].Path, revisions[j].Name)
Expand Down
74 changes: 50 additions & 24 deletions bcs-services/bcs-bscp/cmd/config-server/service/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
pbcontent "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/content"
pbtv "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/core/template-variable"
pbds "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/protocol/data-service"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
)

// CreateConfigItem create config item with option
Expand Down Expand Up @@ -857,21 +858,10 @@ func (s *Service) handleNonTemplateConfig(grpcKit *kit.Kit, bizID, appID, otherA
}

// 模板配置
// nolint:funlen
func (s *Service) handleTemplateConfig(grpcKit *kit.Kit, bizID, appID, otherAppId, releaseId uint32,
templateVars map[string][]*pbtv.TemplateVariableSpec) ([]*pbcs.CompareConfigItemConflictsResp_TemplateConfig, error) {

// 获取该服务模板空间和套餐
tmplSetInfo, err := s.getAllAppTmplSets(grpcKit, bizID, appID)
if err != nil {
logs.Errorf("get all app template sets failed, err: %v, rid: %s", err, grpcKit.Rid)
return nil, err
}

noNamespacePackage := make(map[string]bool)
for _, tmplSet := range tmplSetInfo {
noNamespacePackage[fmt.Sprintf("%d-%d", tmplSet.TemplateSpaceId, tmplSet.TemplateSetId)] = true
}

// 从历史版本/其它服务版本获取发布的套餐以及配置文件信息
rp, err := s.client.DS.ListReleasedAppBoundTmplRevisions(grpcKit.RpcCtx(), &pbds.ListReleasedAppBoundTmplRevisionsReq{
BizId: bizID,
Expand All @@ -885,38 +875,74 @@ func (s *Service) handleTemplateConfig(grpcKit *kit.Kit, bizID, appID, otherAppI
}

rtmplSetMap := make(map[uint32][]*pbatb.ReleasedAppBoundTmplRevision)

templateIds := []uint32{}
for _, d := range rp.Details {
rtmplSetMap[d.TemplateSetId] = append(rtmplSetMap[d.TemplateSetId], d)
templateIds = append(templateIds, d.TemplateId)
releaseTemplateSpaceIds, releaseTemplateSetIds, releaseTemplateIds := []uint32{}, []uint32{}, []uint32{}
for _, v := range rp.GetDetails() {
releaseTemplateSpaceIds = append(releaseTemplateSpaceIds, v.TemplateSpaceId)
releaseTemplateSetIds = append(releaseTemplateSetIds, v.TemplateSetId)
releaseTemplateIds = append(releaseTemplateIds, v.TemplateId)
rtmplSetMap[v.TemplateSetId] = append(rtmplSetMap[v.TemplateSetId], v)
}

tmplr, err := s.client.DS.ListTmplRevisionNamesByTmplIDs(grpcKit.RpcCtx(), &pbds.ListTmplRevisionNamesByTmplIDsReq{
BizId: bizID,
TemplateIds: templateIds,
// 检测历史版本/其它服务版本空间是否存在
_, err = s.client.DS.ListTmplSpacesByIDs(grpcKit.RpcCtx(), &pbds.ListTmplSpacesByIDsReq{
Ids: tools.RemoveDuplicates(releaseTemplateSpaceIds),
})
if err != nil {
return nil, err
}
// 检测历史版本/其它服务版本套餐是否存在
templateSets, err := s.client.DS.ListTemplateSetsByIDs(grpcKit.RpcCtx(), &pbds.ListTemplateSetsByIDsReq{
Ids: tools.RemoveDuplicates(releaseTemplateSetIds),
})
if err != nil {
return nil, err
}
setTemplateIds := []uint32{}
for _, v := range templateSets.GetDetails() {
setTemplateIds = append(setTemplateIds, v.Spec.TemplateIds...)
}

// 从历史版本/其他服务版本导入,存在绑定的模板文件版本标识最新,其实不是最新。
// 需要再次查询是否是最新的
tmplr, _ := s.client.DS.ListTmplRevisionNamesByTmplIDs(grpcKit.RpcCtx(), &pbds.ListTmplRevisionNamesByTmplIDsReq{
BizId: bizID,
TemplateIds: releaseTemplateIds,
})

isLatestMap := make(map[uint32]uint32)
for _, v := range tmplr.GetDetails() {
isLatestMap[v.TemplateId] = v.LatestTemplateRevisionId
}

templateConfigs := make([]*pbcs.CompareConfigItemConflictsResp_TemplateConfig, 0)
// 获取该服务模板空间和套餐
tmplSetInfo, err := s.getAllAppTmplSets(grpcKit, bizID, appID)
if err != nil {
logs.Errorf("get all app template sets failed, err: %v, rid: %s", err, grpcKit.Rid)
return nil, err
}
noNamespacePackage := make(map[string]bool)
for _, tmplSet := range tmplSetInfo {
noNamespacePackage[fmt.Sprintf("%d-%d", tmplSet.TemplateSpaceId, tmplSet.TemplateSetId)] = true
}

templateIds := tools.Difference(tools.RemoveDuplicates(releaseTemplateIds),
tools.RemoveDuplicates(setTemplateIds))
nonExistentTemplateIds := make(map[uint32]bool)
for _, v := range templateIds {
nonExistentTemplateIds[v] = true
}

templateConfigs := make([]*pbcs.CompareConfigItemConflictsResp_TemplateConfig, 0)
for id, revisions := range rtmplSetMap {
templateRevisions := make([]*pbcs.CompareConfigItemConflictsResp_TemplateConfig_TemplateRevisionDetail, 0)
for _, r := range revisions {
if nonExistentTemplateIds[r.TemplateId] {
continue
}
var isLatest bool
if r.IsLatest && isLatestMap[r.TemplateId] == r.TemplateRevisionId {
isLatest = true
} else {
isLatest = false
}

templateRevisions = append(templateRevisions,
&pbcs.CompareConfigItemConflictsResp_TemplateConfig_TemplateRevisionDetail{
TemplateId: r.TemplateId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"context"
"encoding/json"
"fmt"
"path"
"strings"
"time"

Expand Down Expand Up @@ -252,12 +253,13 @@ func (s *Service) ListAppBoundTmplRevisions(ctx context.Context,
for _, f := range fields {
fieldsMap[f] = true
}
fieldsMap["combinedPathName"] = true
newDetails := make([]*pbatb.AppBoundTmplRevision, 0)
for _, detail := range details {
combinedPathName := path.Join(detail.Path, detail.Name)
if (fieldsMap["revision_name"] && strings.Contains(detail.TemplateRevisionName, req.SearchValue)) ||
(fieldsMap["revision_memo"] && strings.Contains(detail.TemplateRevisionMemo, req.SearchValue)) ||
(fieldsMap["name"] && strings.Contains(detail.Name, req.SearchValue)) ||
(fieldsMap["path"] && strings.Contains(detail.Path, req.SearchValue)) ||
(fieldsMap["combinedPathName"] && strings.Contains(combinedPathName, req.SearchValue)) ||
(fieldsMap["creator"] && strings.Contains(detail.Creator, req.SearchValue)) {
newDetails = append(newDetails, detail)
}
Expand Down Expand Up @@ -364,7 +366,7 @@ func (s *Service) ListReleasedAppBoundTmplRevisions(ctx context.Context,
return nil, err
}

details, count, err := s.dao.ReleasedAppTemplate().List(kt, req.BizId, req.AppId, req.ReleaseId, searcher, opt)
details, count, err := s.dao.ReleasedAppTemplate().List(kt, req.BizId, req.AppId, req.ReleaseId, searcher, opt, req.SearchValue)
if err != nil {
logs.Errorf("list released app bound templates revisions failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *Service) GetReleasedAppTmplVariableRefs(ctx context.Context,
kt := kit.FromGrpcContext(ctx)

releasedTmpls, _, err := s.dao.ReleasedAppTemplate().List(kt, req.BizId, req.AppId, req.ReleaseId, nil,
&types.BasePage{All: true})
&types.BasePage{All: true}, "")
if err != nil {
logs.Errorf("list released app templates failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
Expand All @@ -94,7 +94,7 @@ func (s *Service) GetReleasedAppTmplVariableRefs(ctx context.Context,
tmplRevisions = filterSizeForTmplRevisions(tmplRevisions)

releasedCIs, _, err := s.dao.ReleasedCI().List(kt, req.BizId, req.AppId, req.ReleaseId, nil,
&types.BasePage{All: true})
&types.BasePage{All: true}, "")
if err != nil {
logs.Errorf("list released config items failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
Expand Down
26 changes: 23 additions & 3 deletions bcs-services/bcs-bscp/cmd/data-service/service/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func (s *Service) checkTemplateBindings(kt *kit.Kit, bizID, appID uint32,
Spec: &table.AppTemplateBindingSpec{},
}

// 通过bizID和appID找到id
// 通过bizID和appID找到 AppTemplateBinding ID
oldATB, err := s.dao.AppTemplateBinding().GetAppTemplateBindingByAppID(kt, bizID, appID)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errf.Errorf(errf.DBOpFailed,
Expand Down Expand Up @@ -435,6 +435,25 @@ func (s *Service) checkTemplateBindings(kt *kit.Kit, bizID, appID uint32,
appTemplateBinding.ID = oldATB.ID
}

// 校验下模板空间是否存在
if _, err = s.dao.TemplateSpace().ListByIDs(kt, appTemplateBindingSpec.TemplateSpaceIDs); err != nil {
logs.Errorf("list template spaces failed, err: %v, rid: %s", err, kt.Rid)
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("list template spaces failed, err: %v, rid: %s", err, kt.Rid)))
}
// 校验下模板套餐是否存在
if _, err = s.dao.TemplateSet().ListByIDs(kt, appTemplateBindingSpec.TemplateSetIDs); err != nil {
logs.Errorf("list template sets failed, err: %v, rid: %s", err, kt.Rid)
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("list template sets failed, err: %v, rid: %s", err, kt.Rid)))
}
// 校验下模板是否存在
if _, err = s.dao.Template().ListByIDs(kt, appTemplateBindingSpec.TemplateIDs); err != nil {
logs.Errorf("list template failed, err: %v, rid: %s", err, kt.Rid)
return nil, errf.Errorf(errf.DBOpFailed,
i18n.T(kt, fmt.Sprintf("list template failed, err: %v, rid: %s", err, kt.Rid)))
}

return appTemplateBinding, nil
}

Expand Down Expand Up @@ -849,10 +868,11 @@ func (s *Service) ListConfigItems(ctx context.Context, req *pbds.ListConfigItems
for _, f := range fields {
fieldsMap[f] = true
}
fieldsMap["combinedPathName"] = true
cis := make([]*pbci.ConfigItem, 0)
for _, ci := range configItems {
if (fieldsMap["name"] && strings.Contains(ci.Spec.Name, req.SearchValue)) ||
(fieldsMap["path"] && strings.Contains(ci.Spec.Path, req.SearchValue)) ||
combinedPathName := path.Join(ci.Spec.Path, ci.Spec.Name)
if (fieldsMap["combinedPathName"] && strings.Contains(combinedPathName, req.SearchValue)) ||
(fieldsMap["memo"] && strings.Contains(ci.Spec.Memo, req.SearchValue)) ||
(fieldsMap["creator"] && strings.Contains(ci.Revision.Creator, req.SearchValue)) ||
(fieldsMap["reviser"] && strings.Contains(ci.Revision.Reviser, req.SearchValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Service) ListReleasedConfigItems(ctx context.Context,
return nil, err
}

details, count, err := s.dao.ReleasedCI().List(kt, req.BizId, req.AppId, req.ReleaseId, searcher, opt)
details, count, err := s.dao.ReleasedCI().List(kt, req.BizId, req.AppId, req.ReleaseId, searcher, opt, req.SearchValue)
if err != nil {
logs.Errorf("list released app bound templates revisions failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
Expand Down
14 changes: 9 additions & 5 deletions bcs-services/bcs-bscp/cmd/data-service/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *Service) CreateTemplate(ctx context.Context, req *pbds.CreateTemplateRe

// Get all configuration files under a certain package of the service
items, _, err := s.dao.Template().List(kt, req.Attachment.BizId, req.Attachment.TemplateSpaceId,
nil, &types.BasePage{All: true}, nil)
nil, &types.BasePage{All: true}, nil, "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -166,7 +166,8 @@ func (s *Service) ListTemplates(ctx context.Context, req *pbds.ListTemplatesReq)
}
topIds, _ := tools.StrToUint32Slice(req.Ids)
// List templates with options.
details, count, err := s.dao.Template().List(kt, req.BizId, req.TemplateSpaceId, searcher, opt, topIds)
details, count, err := s.dao.Template().List(kt, req.BizId, req.TemplateSpaceId, searcher,
opt, topIds, req.SearchValue)

if err != nil {
logs.Errorf("list templates failed, err: %v, rid: %s", err, kt.Rid)
Expand Down Expand Up @@ -626,7 +627,7 @@ func (s *Service) ListTemplatesNotBound(ctx context.Context, req *pbds.ListTempl

// ListTmplsOfTmplSet list templates of template set.
// 获取到该套餐的template_ids字段,根据这批ID获取对应的详情,做逻辑分页和搜索
func (s *Service) ListTmplsOfTmplSet(ctx context.Context, req *pbds.ListTmplsOfTmplSetReq) ( // nolint
func (s *Service) ListTmplsOfTmplSet(ctx context.Context, req *pbds.ListTmplsOfTmplSetReq) (
*pbds.ListTmplsOfTmplSetResp, error) {
kt := kit.FromGrpcContext(ctx)

Expand Down Expand Up @@ -663,10 +664,13 @@ func (s *Service) ListTmplsOfTmplSet(ctx context.Context, req *pbds.ListTmplsOfT
for _, f := range fields {
fieldsMap[f] = true
}
fieldsMap["combinedPathName"] = true

newDetails := make([]*pbtemplate.Template, 0)
for _, detail := range details {
if (fieldsMap["name"] && strings.Contains(detail.Spec.Name, req.SearchValue)) ||
(fieldsMap["path"] && strings.Contains(detail.Spec.Path, req.SearchValue)) ||
// 拼接path和name
combinedPathName := path.Join(detail.Spec.Path, detail.Spec.Name)
if (fieldsMap["combinedPathName"] && strings.Contains(combinedPathName, req.SearchValue)) ||
(fieldsMap["memo"] && strings.Contains(detail.Spec.Memo, req.SearchValue)) ||
(fieldsMap["creator"] && strings.Contains(detail.Revision.Creator, req.SearchValue)) ||
(fieldsMap["reviser"] && strings.Contains(detail.Revision.Reviser, req.SearchValue)) {
Expand Down
9 changes: 6 additions & 3 deletions bcs-services/bcs-bscp/pkg/dal/dao/release_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/criteria/errf"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/gen"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/table"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/dal/utils"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/kit"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/search"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/types"
Expand All @@ -34,7 +35,7 @@ type ReleasedCI interface {
// GetReleasedLately get released config items lately.
GetReleasedLately(kit *kit.Kit, bizID, appId uint32) ([]*table.ReleasedConfigItem, error)
// List released config items with options.
List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher, opt *types.BasePage) (
List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher, opt *types.BasePage, searchValue string) (
[]*table.ReleasedConfigItem, int64, error)
// ListAll list all released config items in biz.
ListAll(kit *kit.Kit, bizID uint32) ([]*table.ReleasedConfigItem, error)
Expand Down Expand Up @@ -104,8 +105,8 @@ func (dao *releasedCIDao) Get(kit *kit.Kit, bizID, appID, releaseID, configItemI
}

// List released config items with options.
func (dao *releasedCIDao) List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher, opt *types.BasePage) (
[]*table.ReleasedConfigItem, int64, error) {
func (dao *releasedCIDao) List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher,
opt *types.BasePage, searchValue string) ([]*table.ReleasedConfigItem, int64, error) {
m := dao.genQ.ReleasedConfigItem
q := dao.genQ.ReleasedConfigItem.WithContext(kit.Ctx)

Expand All @@ -121,6 +122,8 @@ func (dao *releasedCIDao) List(kit *kit.Kit, bizID, appID, releaseID uint32, s s
}
do = do.Or(exprs[i])
}
do = do.Or(utils.RawCond(`CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,name)
ELSE CONCAT_WS('/', path, name) END LIKE ?`, "%"+searchValue+"%"))
conds = append(conds, do)
}
}
Expand Down
10 changes: 6 additions & 4 deletions bcs-services/bcs-bscp/pkg/dal/dao/released_app_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ReleasedAppTemplate interface {
// Get released app template.
Get(kit *kit.Kit, bizID, appID, releaseID, tmplRevisionID uint32) (*table.ReleasedAppTemplate, error)
// List released app templates with options.
List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher, opt *types.BasePage) (
List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher, opt *types.BasePage, searchValue string) (
[]*table.ReleasedAppTemplate, int64, error)
// GetReleasedLately get released templates lately
GetReleasedLately(kit *kit.Kit, bizID, appID uint32) ([]*table.ReleasedAppTemplate, error)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (dao *releasedAppTemplateDao) Get(kit *kit.Kit, bizID, appID, releaseID,

// List released app templates with options.
func (dao *releasedAppTemplateDao) List(kit *kit.Kit, bizID, appID, releaseID uint32, s search.Searcher,
opt *types.BasePage) (
opt *types.BasePage, searchValue string) (
[]*table.ReleasedAppTemplate, int64, error) {
m := dao.genQ.ReleasedAppTemplate
q := dao.genQ.ReleasedAppTemplate.WithContext(kit.Ctx)
Expand All @@ -119,6 +119,8 @@ func (dao *releasedAppTemplateDao) List(kit *kit.Kit, bizID, appID, releaseID ui
}
do = do.Or(exprs[i])
}
do = do.Or(utils.RawCond(`CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,name)
ELSE CONCAT_WS('/', path, name) END LIKE ?`, "%"+searchValue+"%"))
conds = append(conds, do)
}
}
Expand All @@ -143,8 +145,8 @@ func (dao *releasedAppTemplateDao) GetReleasedLately(kit *kit.Kit, bizID, appId
q := dao.genQ.ReleasedAppTemplate.WithContext(kit.Ctx)
query := q.Where(m.BizID.Eq(bizID), m.AppID.Eq(appId))
subQuery := q.Where(m.BizID.Eq(bizID), m.AppID.Eq(appId)).
Order(m.ReleaseID.Desc(), utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,'name') ELSE "+
"CONCAT_WS('/', path, 'name') END", nil)).
Order(m.ReleaseID.Desc(), utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,`name`) ELSE "+
"CONCAT_WS('/', path, `name`) END", nil)).
Limit(1).
Select(m.ReleaseID)
return query.Where(q.Columns(m.ReleaseID).Eq(subQuery)).Find()
Expand Down
Loading

0 comments on commit 7394999

Please sign in to comment.