Skip to content

Commit

Permalink
Merge remote-tracking branch 'github-bk-bcs/master'
Browse files Browse the repository at this point in the history
* github-bk-bcs/master:
  feat: bcs-task mysql store add WithDebug option (#3592)
  feat: bscp支持配置gorm日志级别 (#3489)
  feat: 1.配置示例 示例切换客户端密钥记忆功能;2.配置文件名/配置项名称placeholder区分 (#3590)
  fix: 分组列表展示优化--bug=131907617  (#3589)
  • Loading branch information
wenxinlee2015 committed Oct 25, 2024
2 parents 8f4d22d + f38a1c1 commit 80564e8
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 112 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/bcs-webconsole
bcs-monitor:
Expand All @@ -38,7 +38,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/bcs-monitor
bcs-bscp:
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m --out-format=colored-line-number
working-directory: bcs-services/bcs-project-manager
bcs-user-manager:
Expand All @@ -139,7 +139,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
args: --timeout=30m
args: --timeout=30m --out-format=colored-line-number
working-directory: bcs-services/bcs-user-manager
bcs-cluster-resources:
name: bcs-cluster-resources
Expand All @@ -158,7 +158,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/cluster-resources

Expand All @@ -179,7 +179,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-common/common/task
- name: Test
Expand Down
4 changes: 2 additions & 2 deletions bcs-common/common/task/stores/iface/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type ListOption struct {
CurrentStep string
Status string
Creator string
StartGte *time.Time // StartGte start time greater or equal to
StartLte *time.Time // StartLte start time less or equal to
CreatedGte *time.Time // CreatedGte create time greater or equal to
CreatedLte *time.Time // CreatedLte create time less or equal to
Sort map[string]int // Sort map for sort list results
Offset int64 // Offset offset for list results
Limit int64 // Limit limit for list results
Expand Down
3 changes: 2 additions & 1 deletion bcs-common/common/task/stores/mysql/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func toTask(task *TaskRecord, steps []*StepRecord) *types.Task {
End: task.End,
ExecutionTime: task.ExecutionTime,
MaxExecutionSeconds: task.MaxExecutionSeconds,
Creator: task.Creator,
CreatedAt: task.CreatedAt,
LastUpdate: task.UpdatedAt,
Creator: task.Creator,
Updater: task.Updater,
}

Expand Down
58 changes: 22 additions & 36 deletions bcs-common/common/task/stores/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package mysql

import (
"context"
"net/url"
"strconv"

"gorm.io/driver/mysql"
"gorm.io/gorm"
Expand All @@ -27,19 +25,30 @@ import (
)

type mysqlStore struct {
dsn string
showDebug bool
db *gorm.DB
dsn string
debug bool
db *gorm.DB
}

type option func(*mysqlStore)

// WithDebug 是否显示sql语句
func WithDebug(debug bool) option {
return func(s *mysqlStore) {
s.debug = debug
}
}

// New init mysql iface.Store
func New(dsn string) (iface.Store, error) {
store := &mysqlStore{dsn: dsn, showDebug: false}
store.initDsn(dsn)
func New(dsn string, opts ...option) (iface.Store, error) {
store := &mysqlStore{dsn: dsn, debug: false}
for _, opt := range opts {
opt(store)
}

// 是否显示sql语句
level := logger.Warn
if store.showDebug {
if store.debug {
level = logger.Info
}

Expand All @@ -54,29 +63,6 @@ func New(dsn string) (iface.Store, error) {
return store, nil
}

// initDsn 解析debug参数是否开启sql显示, 任意异常都原样不动
func (s *mysqlStore) initDsn(raw string) {
u, err := url.Parse(raw)
if err != nil {
return
}
query := u.Query()

// 是否开启debug
debugStr := query.Get("debug")
if debugStr != "" {
debug, err := strconv.ParseBool(debugStr)
if err != nil {
return
}
s.showDebug = debug
query.Del("debug")
u.RawQuery = query.Encode()
}

s.dsn = u.String()
}

// EnsureTable implement istore EnsureTable interface
func (s *mysqlStore) EnsureTable(ctx context.Context, dst ...any) error {
// 没有自定义数据, 使用默认表结构
Expand Down Expand Up @@ -120,11 +106,11 @@ func (s *mysqlStore) ListTask(ctx context.Context, opt *iface.ListOption) (*ifac
})

// mysql store 使用创建时间过滤
if opt.StartGte != nil {
tx = tx.Where("created_at >= ?", opt.StartGte)
if opt.CreatedGte != nil {
tx = tx.Where("created_at >= ?", opt.CreatedGte)
}
if opt.StartLte != nil {
tx = tx.Where("created_at <= ?", opt.StartLte)
if opt.CreatedLte != nil {
tx = tx.Where("created_at <= ?", opt.CreatedLte)
}

// 只使用id排序
Expand Down
1 change: 1 addition & 0 deletions bcs-common/common/task/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Task struct {
Updater string `json:"updater"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
CreatedAt time.Time `json:"createdAt"`
LastUpdate time.Time `json:"lastUpdate"`
}

Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/cache-service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (cs *cacheService) prepare(opt *options.Option) error {
cs.bds = bds

// initial DAO set
set, err := dao.NewDaoSet(cc.CacheService().Sharding, cc.CacheService().Credential)
set, err := dao.NewDaoSet(cc.CacheService().Sharding, cc.CacheService().Credential, cc.CacheService().Gorm)
if err != nil {
return fmt.Errorf("initial dao set failed, err: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion bcs-services/bcs-bscp/cmd/data-service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (ds *dataService) prepare(opt *options.Option) error {
}

// initial DAO set
set, err := dao.NewDaoSet(cc.DataService().Sharding, cc.DataService().Credential)
set, err := dao.NewDaoSet(cc.DataService().Sharding, cc.DataService().Credential, cc.DataService().Gorm)
if err != nil {
return fmt.Errorf("initial dao set failed, err: %v", err)
}
Expand Down
5 changes: 5 additions & 0 deletions bcs-services/bcs-bscp/etc/bcs-bscp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ redisCluster:
password:
db: 1

# gorm related settings
gorm:
# 日志级别,支持silent、error、warn、info四种级别(不区分大小写),默认为info
logLevel:

downstream:
bounceIntervalHour: 48

Expand Down
16 changes: 14 additions & 2 deletions bcs-services/bcs-bscp/pkg/cc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type CacheServiceSetting struct {
Credential Credential `yaml:"credential"`
Sharding Sharding `yaml:"sharding"`
RedisCluster RedisCluster `yaml:"redisCluster"`
Gorm Gorm `yaml:"gorm"`
}

// trySetFlagBindIP try set flag bind ip.
Expand All @@ -215,6 +216,7 @@ func (s *CacheServiceSetting) trySetDefault() {
s.Log.trySetDefault()
s.Sharding.trySetDefault()
s.RedisCluster.trySetDefault()
s.Gorm.trySetDefault()
}

// Validate CacheServiceSetting option.
Expand All @@ -236,6 +238,10 @@ func (s CacheServiceSetting) Validate() error {
return err
}

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

return nil
}

Expand Down Expand Up @@ -300,6 +306,7 @@ type DataServiceSetting struct {
Repo Repository `yaml:"repository"`
Vault Vault `yaml:"vault"`
FeatureFlags FeatureFlags `yaml:"featureFlags"`
Gorm Gorm `yaml:"gorm"`
}

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

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

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

return nil
}

Expand Down Expand Up @@ -535,15 +547,15 @@ func (s *VaultServerSetting) trySetFlagPort(port, grpcPort int) error {
return s.Network.trySetFlagPort(port, grpcPort)
}

// trySetDefault set the CacheServiceSetting default value if user not configured.
// trySetDefault set the VaultServerSetting default value if user not configured.
func (s *VaultServerSetting) trySetDefault() {
s.Network.trySetDefault()
s.Service.trySetDefault()
s.Log.trySetDefault()
s.Sharding.trySetDefault()
}

// Validate CacheServiceSetting option.
// Validate VaultServerSetting option.
func (s VaultServerSetting) Validate() error {

if err := s.Network.validate(); err != nil {
Expand Down
56 changes: 56 additions & 0 deletions bcs-services/bcs-bscp/pkg/cc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

etcd3 "go.etcd.io/etcd/client/v3"
"gorm.io/gorm/logger"

"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/logs"
"github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/tools"
Expand Down Expand Up @@ -1388,3 +1389,58 @@ func (g GSE) validate() error {
}
return nil
}

// Gorm defines the grom related settings.
type Gorm struct {
LogLevel GormLogLevel `yaml:"logLevel"`
}

// GormLogLevel is gorm log level type
type GormLogLevel string

const (
// GormLogSilent used for gorm log level
GormLogSilent GormLogLevel = "silent"
// GormLogError used for gorm log level
GormLogError GormLogLevel = "error"
// GormLogWarn used for gorm log level
GormLogWarn GormLogLevel = "warn"
// GormLogInfo used for gorm log level
GormLogInfo GormLogLevel = "info"
)

// GetLogLevel get log level for gorm.
func (g Gorm) GetLogLevel() logger.LogLevel {
switch strings.ToLower(string(g.LogLevel)) {
case string(GormLogSilent):
return logger.Silent
case string(GormLogError):
return logger.Error
case string(GormLogWarn):
return logger.Warn
case string(GormLogInfo):
return logger.Info
default:
return logger.Info
}
}

// validate if the gorm is valid or not.
func (g Gorm) validate() error {
switch strings.ToLower(string(g.LogLevel)) {
case string(GormLogSilent):
case string(GormLogError):
case string(GormLogWarn):
case string(GormLogInfo):
default:
return fmt.Errorf("unsopported log level: %s", g.LogLevel)
}
return nil
}

// trySetDefault try set the default value of gorm
func (g *Gorm) trySetDefault() {
if g.LogLevel == "" {
g.LogLevel = GormLogInfo
}
}
4 changes: 2 additions & 2 deletions bcs-services/bcs-bscp/pkg/dal/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Set interface {
}

// NewDaoSet create the DAO set instance.
func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential) (Set, error) {
func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential, gormSetting cc.Gorm) (Set, error) {

// opt cc.Database
sd, err := sharding.InitSharding(&opt)
Expand All @@ -82,7 +82,7 @@ func NewDaoSet(opt cc.Sharding, credentialSetting cc.Credential) (Set, error) {
}

adminDB, err := gorm.Open(mysql.Open(sharding.URI(opt.AdminDatabase)),
&gorm.Config{Logger: logger.Default.LogMode(logger.Info)})
&gorm.Config{Logger: logger.Default.LogMode(gormSetting.GetLogLevel())})
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion bcs-services/bcs-bscp/ui/src/api/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const getServiceGroupList = (biz_id: string, app_id: number) =>
* @param biz_id 空间ID
* @returns
*/
export const getSpaceGroupList = (biz_id: string) => http.get(`/config/biz/${biz_id}/groups`).then((res) => res.data);
export const getSpaceGroupList = (biz_id: string, topId?: number) =>
http.get(`/config/biz/${biz_id}/groups`, { params: { top_ids: topId } }).then((res) => res.data);

/**
* 新增分组
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:input-search="false"
:clearable="false"
:loading="loading"
:search-placeholder="$t('配置项名称')"
:search-placeholder="basicInfo?.serviceType.value === 'file' ? $t('配置文件名') : $t('配置项名称')"
:no-data-text="$t('暂无可用配置')"
:no-match-text="$t('搜索结果为空')"
@change="handleConfigChange">
Expand Down
Loading

0 comments on commit 80564e8

Please sign in to comment.