Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bscp支持应用配置总数及模版套餐下模版数总数限制-- story=118111438 #3318

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (s *Service) CreateAppTemplateBinding(ctx context.Context, req *pbcs.Create
grpcKit := kit.FromGrpcContext(ctx)

// validate input param
templateSetIDs, templateIDs, err := parseBindings(req.Bindings)
templateSetIDs, _, err := parseBindings(req.Bindings)
if err != nil {
logs.Errorf("create app template binding failed, parse bindings err: %v, rid: %s", err, grpcKit.Rid)
return nil, err
Expand All @@ -49,11 +49,6 @@ func (s *Service) CreateAppTemplateBinding(ctx context.Context, req *pbcs.Create
return nil, fmt.Errorf("repeated template set ids: %v, id must be unique", repeatedTmplSetIDs)
}

if len(templateIDs) > 500 {
return nil, fmt.Errorf("the length of template ids is %d, it must be within the range of [1,500]",
len(templateIDs))
}

res := []*meta.ResourceAttribute{
{Basic: meta.Basic{Type: meta.Biz, Action: meta.FindBusinessResource}, BizID: req.BizId},
{Basic: meta.Basic{Type: meta.App, Action: meta.Update, ResourceID: req.AppId}, BizID: req.BizId},
Expand Down Expand Up @@ -122,7 +117,7 @@ func (s *Service) UpdateAppTemplateBinding(ctx context.Context, req *pbcs.Update
grpcKit := kit.FromGrpcContext(ctx)

// validate input param
templateSetIDs, templateIDs, err := parseBindings(req.Bindings)
templateSetIDs, _, err := parseBindings(req.Bindings)
if err != nil {
logs.Errorf("update app template binding failed, parse bindings err: %v, rid: %s", err, grpcKit.Rid)
return nil, err
Expand All @@ -133,11 +128,6 @@ func (s *Service) UpdateAppTemplateBinding(ctx context.Context, req *pbcs.Update
return nil, fmt.Errorf("repeated template set ids: %v, id must be unique", repeatedTmplSetIDs)
}

if len(templateIDs) > 500 {
return nil, fmt.Errorf("the length of template ids is %d, it must be within the range of [1,500]",
len(templateIDs))
}

res := []*meta.ResourceAttribute{
{Basic: meta.Basic{Type: meta.Biz, Action: meta.FindBusinessResource}, BizID: req.BizId},
{Basic: meta.Basic{Type: meta.App, Action: meta.Update, ResourceID: req.AppId}, BizID: req.BizId},
Expand Down Expand Up @@ -559,10 +549,6 @@ func (s *Service) UpdateAppBoundTmplRevisions(ctx context.Context, req *pbcs.Upd
if len(repeatedTmplRevisionIDs) > 0 {
return nil, fmt.Errorf("repeated template ids: %v, id must be unique", repeatedTmplRevisionIDs)
}
if len(templateIDs) > 500 {
return nil, fmt.Errorf("the length of template ids is %d, it must be within the range of [1,500]",
len(templateIDs))
}

res := []*meta.ResourceAttribute{
{Basic: meta.Basic{Type: meta.Biz, Action: meta.FindBusinessResource}, BizID: req.BizId},
Expand Down
7 changes: 7 additions & 0 deletions bcs-services/bcs-bscp/cmd/data-service/etc/data_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ sharding:
qps: 500
burst: 500

# configLimit is config related limit
configLimit:
fireyun marked this conversation as resolved.
Show resolved Hide resolved
# appConfigCnt is the max limit of config item for an app for user to create, default is 2000
appConfigCnt:
# tmplSetTmplCnt is the max limit of templates for a template set for user to create, default is 2000
tmplSetTmplCnt:

# defines log's related configuration
log:
# log storage directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,28 @@ func (s *Service) CreateAppTemplateBinding(ctx context.Context, req *pbds.Create
return nil, err
}

id, err := s.dao.AppTemplateBinding().Create(kt, appTemplateBinding)
tx := s.dao.GenQuery().Begin()

id, err := s.dao.AppTemplateBinding().CreateWithTx(kt, tx, appTemplateBinding)
if err != nil {
logs.Errorf("create app template binding failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

// validate config items count.
if err = s.dao.ConfigItem().ValidateAppCINumber(kt, tx, req.Attachment.BizId, req.Attachment.AppId); err != nil {
logs.Errorf("validate config items count failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

if err = tx.Commit(); err != nil {
logs.Errorf("commit transaction failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
}

Expand Down Expand Up @@ -103,11 +122,27 @@ func (s *Service) UpdateAppTemplateBinding(ctx context.Context, req *pbds.Update
return nil, err
}

if err := s.dao.AppTemplateBinding().Update(kt, appTemplateBinding); err != nil {
tx := s.dao.GenQuery().Begin()

if err := s.dao.AppTemplateBinding().UpdateWithTx(kt, tx, appTemplateBinding); err != nil {
logs.Errorf("update app template binding failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
}

// validate config items count.
if err := s.dao.ConfigItem().ValidateAppCINumber(kt, tx, req.Attachment.BizId, req.Attachment.AppId); err != nil {
logs.Errorf("validate config items count failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

if err := tx.Commit(); err != nil {
logs.Errorf("commit transaction failed, err: %v, rid: %s", err, kt.Rid)
return nil, err
}

return new(pbbase.EmptyResp), nil
}

Expand Down Expand Up @@ -513,6 +548,12 @@ func (s *Service) CascadeUpdateATB(kt *kit.Kit, tx *gen.QueryTx, atb *table.AppT
return err
}

// validate config items count.
if err := s.dao.ConfigItem().ValidateAppCINumber(kt, tx, atb.Attachment.BizID, atb.Attachment.AppID); err != nil {
logs.Errorf("validate config items count failed, err: %v, rid: %s", err, kt.Rid)
return err
}

return nil
}

Expand Down
22 changes: 22 additions & 0 deletions bcs-services/bcs-bscp/cmd/data-service/service/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ func (s *Service) CreateTemplate(ctx context.Context, req *pbds.CreateTemplateRe
return nil, err
}

// validate template set's templates count.
for _, tmplSetID := range req.TemplateSetIds {
if err = s.dao.TemplateSet().ValidateTmplNumber(kt, tx, req.Attachment.BizId, tmplSetID); err != nil {
logs.Errorf("validate template set's templates count failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}
}

// 2. create template revision
spec := req.TrSpec.TemplateRevisionSpec()
// if no revision name is specified, generate it by system
Expand Down Expand Up @@ -437,6 +448,17 @@ func (s *Service) AddTmplsToTmplSets(ctx context.Context, req *pbds.AddTmplsToTm
return nil, err
}

// validate template set's templates count.
for _, tmplSetID := range req.TemplateSetIds {
if err := s.dao.TemplateSet().ValidateTmplNumber(kt, tx, req.BizId, tmplSetID); err != nil {
logs.Errorf("validate template set's templates count failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}
}

// 2. update app template bindings if necessary
atbs, err := s.dao.TemplateBindingRelation().
ListTemplateSetsBoundATBs(kt, req.BizId, req.TemplateSetIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ func (s *Service) UpdateTemplateSet(ctx context.Context, req *pbds.UpdateTemplat
return nil, err
}

// validate template set's templates count.
if err = s.dao.TemplateSet().ValidateTmplNumber(kt, tx, req.Attachment.BizId, req.Id); err != nil {
logs.Errorf("validate template set's templates count failed, err: %v, rid: %s", err, kt.Rid)
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
}
return nil, err
}

// 2. update app template bindings if necessary
// delete invisible template set from correspond app template bindings
if len(invisibleATBs) > 0 {
Expand Down
16 changes: 11 additions & 5 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ type DataServiceSetting struct {
Service Service `yaml:"service"`
Log LogOption `yaml:"log"`

Credential Credential `yaml:"credential"`
Sharding Sharding `yaml:"sharding"`
Esb Esb `yaml:"esb"`
Repo Repository `yaml:"repository"`
Vault Vault `yaml:"vault"`
Credential Credential `yaml:"credential"`
Sharding Sharding `yaml:"sharding"`
Esb Esb `yaml:"esb"`
Repo Repository `yaml:"repository"`
Vault Vault `yaml:"vault"`
ConfigLimit ConfigLimit `yaml:"configLimit"`
}

// trySetFlagBindIP try set flag bind ip.
Expand All @@ -304,6 +305,7 @@ func (s *DataServiceSetting) trySetDefault() {
s.Sharding.trySetDefault()
s.Repo.trySetDefault()
s.Vault.getConfigFromEnv()
s.ConfigLimit.trySetDefault()
}

// Validate DataServiceSetting option.
Expand Down Expand Up @@ -333,6 +335,10 @@ func (s DataServiceSetting) Validate() error {
return err
}

if err := s.ConfigLimit.validate(); err != nil {
return err
}

return nil
}

Expand Down
39 changes: 39 additions & 0 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,45 @@ func (v *Vault) getConfigFromEnv() {
}
}

// ConfigLimit is config related limit
type ConfigLimit struct {
// AppConfigCnt is the max limit of config item for an app for user to create
AppConfigCnt int `yaml:"appConfigCnt"`
// TmplSetTmplCnt is the max limit of templates for a template set for user to create
TmplSetTmplCnt int `yaml:"tmplSetTmplCnt"`
}

// validate if the config limit is valid or not.
func (c ConfigLimit) validate() error {
if c.AppConfigCnt < 0 {
return fmt.Errorf("invalid configLimit.appConfigCnt value %d, should >= 1", c.AppConfigCnt)
}

if c.TmplSetTmplCnt < 0 {
return fmt.Errorf("invalid configLimit.tmplSetTmplCnt value %d, should >= 1", c.TmplSetTmplCnt)
}

return nil
}

const (
// DefaultAppConfigCnt is default app's config count
DefaultAppConfigCnt = 2000
// DefaultTmplSetTmplCnt is default template set's template count
DefaultTmplSetTmplCnt = 2000
)

// trySetDefault try set the default value of config limit
func (c *ConfigLimit) trySetDefault() {
if c.AppConfigCnt == 0 {
c.AppConfigCnt = DefaultAppConfigCnt
}

if c.TmplSetTmplCnt == 0 {
c.TmplSetTmplCnt = DefaultTmplSetTmplCnt
}
}

// BKNotice defines all the bk notice related runtime.
type BKNotice struct {
Enable bool `yaml:"enable"`
Expand Down
30 changes: 11 additions & 19 deletions bcs-services/bcs-bscp/pkg/dal/dao/app_template_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

// AppTemplateBinding supplies all the app template binding related operations.
type AppTemplateBinding interface {
// Create one app template binding instance.
Create(kit *kit.Kit, atb *table.AppTemplateBinding) (uint32, error)
// CreateWithTx create one app template binding instance with transaction.
CreateWithTx(kit *kit.Kit, tx *gen.QueryTx, atb *table.AppTemplateBinding) (uint32, error)
// Update one app template binding's info.
Update(kit *kit.Kit, atb *table.AppTemplateBinding) error
// UpdateWithTx Update one app template binding's info with transaction.
Expand Down Expand Up @@ -126,8 +126,9 @@ func (dao *appTemplateBindingDao) GetAppTemplateBindingByAppID(kit *kit.Kit, biz
Where(m.BizID.Eq(bizID), m.AppID.Eq(appID)).Take()
}

// Create one app template binding instance.
func (dao *appTemplateBindingDao) Create(kit *kit.Kit, g *table.AppTemplateBinding) (uint32, error) {
// CreateWithTx create one app template binding instance with transaction.
func (dao *appTemplateBindingDao) CreateWithTx(kit *kit.Kit, tx *gen.QueryTx, g *table.AppTemplateBinding) (
uint32, error) {
if err := g.ValidateCreate(); err != nil {
return 0, err
}
Expand All @@ -142,22 +143,13 @@ func (dao *appTemplateBindingDao) Create(kit *kit.Kit, g *table.AppTemplateBindi
}
g.ID = id

ad := dao.auditDao.DecoratorV2(kit, g.Attachment.BizID).PrepareCreate(g)

// 多个使用事务处理
createTx := func(tx *gen.Query) error {
q := tx.AppTemplateBinding.WithContext(kit.Ctx)
if err = q.Create(g); err != nil {
return err
}

if err = ad.Do(tx); err != nil {
return err
}

return nil
q := tx.AppTemplateBinding.WithContext(kit.Ctx)
if err = q.Create(g); err != nil {
return 0, err
}
if err = dao.genQ.Transaction(createTx); err != nil {

ad := dao.auditDao.DecoratorV2(kit, g.Attachment.BizID).PrepareCreate(g)
if err = ad.Do(tx.Query); err != nil {
return 0, err
}

Expand Down
24 changes: 22 additions & 2 deletions bcs-services/bcs-bscp/pkg/dal/dao/config_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"gorm.io/gen/field"
"gorm.io/gorm"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc"
"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"
Expand Down Expand Up @@ -454,15 +455,34 @@ func (dao *configItemDao) validateAttachmentAppExist(kit *kit.Kit, am *table.Con
}

// ValidateAppCINumber verify whether the current number of app config items has reached the maximum.
// the number is the total count of template and non-template config items
func (dao *configItemDao) ValidateAppCINumber(kt *kit.Kit, tx *gen.QueryTx, bizID, appID uint32) error {
// get non-template config count
m := tx.ConfigItem
count, err := m.WithContext(kt.Ctx).Where(m.BizID.Eq(bizID), m.AppID.Eq(appID)).Count()
if err != nil {
return fmt.Errorf("count app %d's config items failed, err: %v", appID, err)
}

if err := table.ValidateAppCINumber(count); err != nil {
return err
// get template config count
tm := tx.AppTemplateBinding
tcount := 0
var atb *table.AppTemplateBinding
atb, err = tm.WithContext(kt.Ctx).Where(tm.BizID.Eq(bizID), tm.AppID.Eq(appID)).Take()
if err != nil {
// if not found, means the count should be 0
if !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("get app %d's template binding failed, err: %v", appID, err)
}
} else {
tcount = len(atb.Spec.TemplateRevisionIDs)
}

total := int(count) + tcount
if total > cc.DataService().ConfigLimit.AppConfigCnt {
return errf.New(errf.InvalidParameter,
fmt.Sprintf("the total number of app %d's config items(including template and non-template)"+
"exceeded the limit %d", appID, cc.DataService().ConfigLimit.AppConfigCnt))
}

return nil
Expand Down
Loading
Loading