Skip to content

Commit

Permalink
feat: bscp支持应用配置总数及模版套餐下模版数总数限制-- story=118111438 (#3318)
Browse files Browse the repository at this point in the history
* feat: bscp设置app文件配置(模版+非模版)总数限制

* feat: bscp支持模版套餐下模版数总数限制

* feat: update config

* chore: adjust code

* chore: adjust code
  • Loading branch information
fireyun authored Jul 3, 2024
1 parent 7e2bfab commit 580740d
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 67 deletions.
30 changes: 26 additions & 4 deletions bcs-services/bcs-bscp/cmd/api-server/etc/api_server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,34 @@ repository:
# the password to decrypt the certificate.
password:

# 特性配置
featureFlags:
# 业务展示白名单
BIZ_VIEW:
enabled: false
list:
- "xx"
- "xx"
# 全局默认配置(优先级低于业务级配置),默认为true(展示)
default:
# 业务级配置,默认为空
spec:
"2":
# 业务资源限制
RESOURCE_LIMIT:
# 全局默认配置(优先级低于业务级配置)
default:
# 配置文件大小上限,单位为MB,默认为100MB
maxFileSize:
# 单个app下允许创建的配置数(模版+非模版),默认为2000
appConfigCnt:
# 单个模版套餐下允许创建的模版数,默认为2000
tmplSetTmplCnt:
# 业务级配置,默认为空
spec:
"2":
# 配置文件大小上限,单位为MB
maxFileSize:
# 单个app下允许创建的配置数(模版+非模版)
appConfigCnt:
# 单个模版套餐下允许创建的模版数
tmplSetTmplCnt:

# defines service related settings.
service:
Expand Down
12 changes: 9 additions & 3 deletions bcs-services/bcs-bscp/cmd/api-server/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func FeatureFlagsHandler(w http.ResponseWriter, r *http.Request) {
biz := r.URL.Query().Get("biz")
// set biz_view feature flag
bizViewConf := cc.ApiServer().FeatureFlags.BizView
featureFlags.BizView = bizViewConf.Default
if enable, ok := bizViewConf.Spec[biz]; ok {
featureFlags.BizView = enable
featureFlags.BizView = *bizViewConf.Default
if enable, ok := bizViewConf.Spec[biz]; ok && enable != nil {
featureFlags.BizView = *enable
}
// set biz resource limit
resourceLimitConf := cc.ApiServer().FeatureFlags.ResourceLimit
Expand All @@ -94,6 +94,12 @@ func FeatureFlagsHandler(w http.ResponseWriter, r *http.Request) {
if resource.MaxFileSize != 0 {
featureFlags.ResourceLimit.MaxFileSize = resource.MaxFileSize
}
if resource.AppConfigCnt != 0 {
featureFlags.ResourceLimit.AppConfigCnt = resource.AppConfigCnt
}
if resource.TmplSetTmplCnt != 0 {
featureFlags.ResourceLimit.TmplSetTmplCnt = resource.TmplSetTmplCnt
}
// NOCC:golint/todo(忽略)
// nolint TODO:其他资源限制
}
Expand Down
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 @@ -558,10 +548,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
29 changes: 29 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,35 @@ sharding:
qps: 500
burst: 500

# 特性配置
featureFlags:
# 业务展示白名单
BIZ_VIEW:
# 全局默认配置(优先级低于业务级配置),默认为true(展示)
default:
# 业务级配置,默认为空
spec:
"2":
# 业务资源限制
RESOURCE_LIMIT:
# 全局默认配置(优先级低于业务级配置)
default:
# 配置文件大小上限,单位为MB,默认为100MB
maxFileSize:
# 单个app下允许创建的配置数(模版+非模版),默认为2000
appConfigCnt:
# 单个模版套餐下允许创建的模版数,默认为2000
tmplSetTmplCnt:
# 业务级配置,默认为空
spec:
"2":
# 配置文件大小上限,单位为MB
maxFileSize:
# 单个app下允许创建的配置数(模版+非模版)
appConfigCnt:
# 单个模版套餐下允许创建的模版数
tmplSetTmplCnt:

# defines log's related configuration
log:
# log storage directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,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 @@ -104,11 +123,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 @@ -516,6 +551,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 @@ -438,6 +449,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
21 changes: 16 additions & 5 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (s *ApiServerSetting) trySetDefault() {
s.Service.trySetDefault()
s.Log.trySetDefault()
s.Repo.trySetDefault()
s.FeatureFlags.trySetDefault()
}

// Validate ApiServerSetting option.
Expand All @@ -115,6 +116,10 @@ func (s ApiServerSetting) Validate() error {
return err
}

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

return nil
}

Expand Down Expand Up @@ -279,11 +284,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"`
FeatureFlags FeatureFlags `yaml:"featureFlags"`
}

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

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

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

return nil
}

Expand Down
Loading

0 comments on commit 580740d

Please sign in to comment.