Skip to content

Commit

Permalink
feat: add biz resource limit config
Browse files Browse the repository at this point in the history
  • Loading branch information
AlkaidChan committed Mar 1, 2024
1 parent 9d7646d commit e7b14ba
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
58 changes: 21 additions & 37 deletions bcs-services/bcs-bscp/cmd/api-server/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
package service

import (
"fmt"
"net/http"

"github.com/go-chi/render"
"gopkg.in/yaml.v3"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/cc"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/criteria/errf"
Expand Down Expand Up @@ -69,47 +67,33 @@ func UserInfoHandler(w http.ResponseWriter, r *http.Request) {
render.Render(w, r, rest.OKRender(user))
}

// FeatureFlags map of feature flags
type FeatureFlags map[cc.FeatureFlag]interface{}
// BizFeatureFlags feature flags for business
type BizFeatureFlags struct {
// BizViewFlag 是否开启业务体验
BizView bool `json:"BIZ_VIEW"`
ResourceLimit cc.ResourceLimit `json:"RESOURCE_LIMIT"`
}

// FeatureFlagsHandler 特性开关接口
func FeatureFlagsHandler(w http.ResponseWriter, r *http.Request) {
featureFlags := FeatureFlags{}
featureFlags := BizFeatureFlags{}

biz := r.URL.Query().Get("biz")
for k, v := range cc.ApiServer().FeatureFlags {
switch k {
case cc.BizViewFlag:
bytes, _ := yaml.Marshal(v)
fmt.Println(string(bytes))
bizView := &cc.FeatureBizView{}
if err := yaml.Unmarshal(bytes, bizView); err != nil {
render.Render(w, r, rest.InternalError(fmt.Errorf("invalid feature flag format: %s", k)))
return
}
if enable, exists := bizView.Spec[biz]; exists {
featureFlags[k] = enable
} else {
featureFlags[k] = bizView.Default
}
case cc.ResourceLimitFlag:
bytes, _ := yaml.Marshal(v)
fmt.Println(string(bytes))
resourceLimitConf := &cc.FeatureResourceLimit{}
if err := yaml.Unmarshal(bytes, resourceLimitConf); err != nil {
render.Render(w, r, rest.InternalError(fmt.Errorf("invalid feature flag format: %s", k)))
return
}
resourceLimit := resourceLimitConf.Default
if resource, exists := resourceLimitConf.Spec[biz]; exists {
if resource.MaxConfigItemSize != 0 {
resourceLimit.MaxConfigItemSize = resource.MaxConfigItemSize
}
// TODO: 其他资源限制
}
featureFlags[k] = resourceLimit
// set biz_view feature flag
bizViewConf := cc.ApiServer().FeatureFlags.BizView
featureFlags.BizView = bizViewConf.Default
if enable, ok := bizViewConf.Spec[biz]; ok {
featureFlags.BizView = enable
}
// set biz resource limit
resourceLimitConf := cc.ApiServer().FeatureFlags.ResourceLimit
featureFlags.ResourceLimit = resourceLimitConf.Default

if resource, ok := resourceLimitConf.Spec[biz]; ok {
if resource.MaxConfigItemSize != 0 {
featureFlags.ResourceLimit.MaxConfigItemSize = resource.MaxConfigItemSize
}
// TODO:其他特性开关
// TODO:其他资源限制
}

render.Render(w, r, rest.OKRender(featureFlags))
Expand Down
14 changes: 7 additions & 7 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ type Setting interface {

// ApiServerSetting defines api server used setting options.
type ApiServerSetting struct {
Network Network `yaml:"network"`
Service Service `yaml:"service"`
Log LogOption `yaml:"log"`
Repo Repository `yaml:"repository"`
BKNotice BKNotice `yaml:"bkNotice"`
Esb Esb `yaml:"esb"`
FeatureFlags map[FeatureFlag]interface{} `yaml:"featureFlags"`
Network Network `yaml:"network"`
Service Service `yaml:"service"`
Log LogOption `yaml:"log"`
Repo Repository `yaml:"repository"`
BKNotice BKNotice `yaml:"bkNotice"`
Esb Esb `yaml:"esb"`
FeatureFlags FeatureFlags `yaml:"featureFlags"`
}

// trySetFlagBindIP try set flag bind ip.
Expand Down
16 changes: 7 additions & 9 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ const (
RedisClusterMode = "cluster"
)

// FeatureFlag 枚举类型, 常量使用全部大写表示
type FeatureFlag string

const (
// BizViewFlag 业务白名单
BizViewFlag FeatureFlag = "BIZ_VIEW"
// ResourceLimitFlag 业务资源限制
ResourceLimitFlag FeatureFlag = "RESOURCE_LIMIT"
)
// FeatureFlags 特性配置
type FeatureFlags struct {
// BizView 业务白名单
BizView FeatureBizView `json:"biz_view" yaml:"BIZ_VIEW"`
// ResourceLimit 业务资源限制
ResourceLimit FeatureResourceLimit `json:"resource_limit" yaml:"RESOURCE_LIMIT"`
}

// FeatureBizView 业务白名单
type FeatureBizView struct {
Expand Down

0 comments on commit e7b14ba

Please sign in to comment.