Skip to content

Commit

Permalink
fix: 修复配置模版文件没有按照预期效果排序 (#3311)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 authored Jun 27, 2024
1 parent 19156f5 commit edab065
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,6 @@ func (s *Service) getPBSForCascade(kt *kit.Kit, tx *gen.QueryTx, bindings []*tab
}
}
}
if e := s.validateTmplForATBWithTx(kt, tx, pbs.TemplateIDs); e != nil {
logs.Errorf("validate template for app template binding failed, err: %v, rid: %s", e, kt.Rid)
return nil, e
}

// get all latest revisions of latest templates
latestTmplRevisions, err := s.dao.TemplateRevision().ListByTemplateIDsWithTx(kt, tx, kt.BizID,
Expand Down Expand Up @@ -631,44 +627,6 @@ func (s *Service) getPBSForCascade(kt *kit.Kit, tx *gen.QueryTx, bindings []*tab
return pbs, nil
}

// validateTmplForATBWithTx validate template with transaction to avoid same templates are bound to one app
func (s *Service) validateTmplForATBWithTx(kt *kit.Kit, tx *gen.QueryTx, tmplIDs []uint32) error {
if len(tmplIDs) == 0 {
return nil
}

if repeated := tools.SliceRepeatedElements(tmplIDs); len(repeated) > 0 {
// get template details
tmpls, err := s.dao.Template().ListByIDsWithTx(kt, tx, repeated)
if err != nil {
logs.Errorf("list template by ids failed, err: %v, rid: %s", err, kt.Rid)
return err
}
type tmplT struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
}
details := make([]tmplT, len(tmpls))
for idx, t := range tmpls {
details[idx] = tmplT{
ID: t.ID,
Name: t.Spec.Name,
Path: t.Spec.Path,
}
}
detailsJs, err := json.Marshal(details)
if err != nil {
logs.Errorf("marshal template details failed, err: %v, rid: %s", err, kt.Rid)
return err
}
return fmt.Errorf("same template id in %v can't be bound to the same app, template details: %s",
repeated, detailsJs)
}

return nil
}

// genFinalATB generate the final app template binding.
func (s *Service) genFinalATB(kt *kit.Kit, atb *table.AppTemplateBinding) error {
pbs := parseBindings(atb.Spec.Bindings)
Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/data-service/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,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
10 changes: 5 additions & 5 deletions bcs-services/bcs-bscp/pkg/dal/dao/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ func (dao *templateDao) List(kit *kit.Kit, bizID, templateSpaceID uint32, s sear
d := q.Where(m.BizID.Eq(bizID), m.TemplateSpaceID.Eq(templateSpaceID)).Where(conds...)
if len(topIds) != 0 {
d = d.Order(utils.NewCustomExpr("CASE WHEN id IN (?) THEN 0 ELSE 1 END,"+
"CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,'name') ELSE CONCAT_WS('/', path, 'name') END",
"CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,`name`) ELSE CONCAT_WS('/', path, `name`) END",
[]interface{}{topIds}))
} else {
d = d.Order(utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,'name') ELSE "+
"CONCAT_WS('/', path, 'name') END", nil))
d = d.Order(utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,`name`) ELSE "+
"CONCAT_WS('/', path, `name`) END", nil))
}

if opt.All {
Expand Down Expand Up @@ -347,8 +347,8 @@ func (dao *templateDao) ListByIDs(kit *kit.Kit, ids []uint32) ([]*table.Template
m := dao.genQ.Template
q := dao.genQ.Template.WithContext(kit.Ctx)
result, err := q.Where(m.ID.In(ids...)).
Order(utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,'name') "+
"ELSE CONCAT_WS('/', path, 'name') END", nil)).Find()
Order(utils.NewCustomExpr("CASE WHEN RIGHT(path, 1) = '/' THEN CONCAT(path,`name`) "+
"ELSE CONCAT_WS('/', path, `name`) END", nil)).Find()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit edab065

Please sign in to comment.