Skip to content

Commit

Permalink
feat: bcs-monitor rest 泛型优化
Browse files Browse the repository at this point in the history
  • Loading branch information
LidolLxf committed Feb 26, 2025
1 parent 1a3c846 commit eca9639
Show file tree
Hide file tree
Showing 23 changed files with 508 additions and 471 deletions.
18 changes: 9 additions & 9 deletions bcs-services/bcs-monitor/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Tencent/bk-bcs/bcs-services/bcs-monitor

go 1.23
go 1.23.0

require (
github.com/Tencent/bk-bcs/bcs-common v0.0.0-20241225073847-71cfc38de294
Expand Down Expand Up @@ -43,7 +43,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/mongo/otelmongo v0.44.0
go.opentelemetry.io/otel/metric v1.18.0
go.uber.org/automaxprocs v1.5.1
golang.org/x/sync v0.10.0
golang.org/x/sync v0.11.0
google.golang.org/grpc v1.62.1
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -68,7 +68,7 @@ require (
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/frankban/quicktest v1.14.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/ggicci/owl v0.8.2 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/golang-migrate/migrate/v4 v4.17.0 // indirect
Expand Down Expand Up @@ -231,13 +231,13 @@ require (
go.opentelemetry.io/otel/trace v1.18.0
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/term v0.29.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/api v0.150.0 // indirect
Expand Down Expand Up @@ -265,7 +265,7 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/go-playground/validator/v10 v10.25.0
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
Expand Down
37 changes: 24 additions & 13 deletions bcs-services/bcs-monitor/pkg/api/logrule/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"

"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/operator"
"github.com/pkg/errors"

bklog "github.com/Tencent/bk-bcs/bcs-services/bcs-monitor/pkg/component/bk_log"
"github.com/Tencent/bk-bcs/bcs-services/bcs-monitor/pkg/config"
Expand All @@ -38,15 +40,15 @@ const (

// ListLogCollectorsReq list log collectors request
type ListLogCollectorsReq struct {
ProjectId string `in:"path=projectId;required"`
ClusterId string `in:"path=clusterId;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
}

// GetLogRuleReq get log rule request
type GetLogRuleReq struct {
ProjectId string `json:"-" in:"path=projectId;required"`
ClusterId string `json:"-" in:"path=clusterId;required"`
ID string `in:"path=id;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
ID string `json:"id" in:"path=id" validate:"required"`
}

// GetLogRuleResp log rule resp
Expand Down Expand Up @@ -117,16 +119,25 @@ func (l GetLogRuleRespSortByStatus) Swap(i, j int) { l[i], l[j] = l[j], l[i] }

// CreateLogRuleReq req
type CreateLogRuleReq struct {
ProjectId string `json:"-" in:"path=projectId;required"`
ClusterId string `json:"-" in:"path=clusterId;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
DisplayName string `json:"display_name" form:"display_name"`
Name string `json:"name" form:"name" binding:"required" validate:"max=30,min=5,regexp=^[A-Za-z0-9_]+$"`
Name string `json:"name" form:"name" binding:"required" validate:"required,max=30,min=5"`
RuleName string `json:"-" form:"-"`
Description string `json:"description"`
Rule bklog.LogRule `json:"rule"`
FromRule string `json:"from_rule"`
}

// Validate CreateLogRuleReq validate
func (req *CreateLogRuleReq) Validate() error {
nameRegexp := regexp.MustCompile(`^[A-Za-z0-9_]+$`)
if !nameRegexp.MatchString(req.Name) {
return errors.New("name is invalid: " + req.Name)
}
return nil
}

// toEntity convert to entity.LogRule
func (req *CreateLogRuleReq) toEntity(c *rest.Context) *entity.LogRule {
if req.DisplayName == "" {
Expand Down Expand Up @@ -173,9 +184,9 @@ func (req *CreateLogRuleReq) toBKLog(c *rest.Context) *bklog.CreateBCSCollectorR

// UpdateLogRuleReq req
type UpdateLogRuleReq struct {
ProjectId string `json:"-" in:"path=projectId;required"`
ClusterId string `json:"-" in:"path=clusterId;required"`
ID string `json:"-" in:"path=id;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
ID string `json:"id" in:"path=id" validate:"required"`
DisplayName string `json:"display_name" form:"display_name"`
Description string `json:"description"`
Rule bklog.LogRule `json:"rule"`
Expand Down Expand Up @@ -374,7 +385,7 @@ func getBkLogDeleteMessage(isFileDeleted, isSTDDeleted bool, originMessage strin
}

// getContainerQueryLogLinks get container query log links
func getContainerQueryLogLinks(containerIDs []string, projectCode, clusterID string) map[string]Entrypoint {
func getContainerQueryLogLinks(containerIDs []string, projectCode, clusterID string) *map[string]Entrypoint {
result := make(map[string]Entrypoint, 0)
type addition struct {
Field string `json:"field"`
Expand All @@ -395,7 +406,7 @@ func getContainerQueryLogLinks(containerIDs []string, projectCode, clusterID str
FileLogURL: fmt.Sprintf("%s/#/retrieve/?%s", config.G.BKLog.Entrypoint, query.Encode()),
}
}
return result
return &result
}

// getRuleIDByNames get rule id by names
Expand Down
61 changes: 33 additions & 28 deletions bcs-services/bcs-monitor/pkg/api/logrule/logrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (

// GetEntrypointsReq params
type GetEntrypointsReq struct {
ProjectId string `json:"-" in:"path=projectId;required"`
ClusterId string `json:"-" in:"path=clusterId;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
ContainerIDs []string `json:"container_ids" form:"container_ids"`
}

Expand All @@ -51,7 +51,7 @@ type Entrypoint struct {
// @Produce json
// @Success 200 {object} map[string]Entrypoint
// @Router /log_collector/entrypoints [post]
func GetEntrypoints(c context.Context, req GetEntrypointsReq) (map[string]Entrypoint, error) {
func GetEntrypoints(c context.Context, req *GetEntrypointsReq) (*map[string]Entrypoint, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand All @@ -65,7 +65,7 @@ func GetEntrypoints(c context.Context, req GetEntrypointsReq) (map[string]Entryp
// @Produce json
// @Success 200 {array} GetLogRuleResp
// @Router /log_collector/rules [get]
func ListLogCollectors(c context.Context, req ListLogCollectorsReq) ([]*GetLogRuleResp, error) {
func ListLogCollectors(c context.Context, req *ListLogCollectorsReq) (*[]*GetLogRuleResp, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -104,7 +104,8 @@ func ListLogCollectors(c context.Context, req ListLogCollectorsReq) ([]*GetLogRu
}

sort.Sort(GetLogRuleRespSortByName(result))
return result, nil

return &result, nil
}

// GetLogRule 获取日志采集规则详情
Expand All @@ -113,7 +114,7 @@ func ListLogCollectors(c context.Context, req ListLogCollectorsReq) ([]*GetLogRu
// @Produce json
// @Success 200 object GetLogRuleResp
// @Router /log_collector/rules/:id [get]
func GetLogRule(c context.Context, req GetLogRuleReq) (*GetLogRuleResp, error) {
func GetLogRule(c context.Context, req *GetLogRuleReq) (*GetLogRuleResp, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -156,10 +157,14 @@ func GetLogRule(c context.Context, req GetLogRuleReq) (*GetLogRuleResp, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules [post]
func CreateLogRule(c context.Context, req CreateLogRuleReq) (string, error) {
func CreateLogRule(c context.Context, req *CreateLogRuleReq) (*string, error) {
err := req.Validate()
if err != nil {
return nil, err
}
rctx, err := rest.GetRestContext(c)
if err != nil {
return "", err
return nil, err
}
req.RuleName = fmt.Sprintf("%s_%s", req.Name, rand.String(5))

Expand All @@ -172,20 +177,20 @@ func CreateLogRule(c context.Context, req CreateLogRuleReq) (string, error) {
})
count, _, err := store.ListLogRules(c, cond, &utils.ListOption{})
if err != nil {
return "", err
return nil, err
}
if count > 0 {
return "", errors.Errorf("%s is exist", req.Name)
return nil, errors.Errorf("%s is exist", req.Name)
}

id, err := store.CreateLogRule(c, req.toEntity(rctx))
if err != nil {
return "", err
return nil, err
}

// 创建 bklog 规则耗时比较长,异步调用
go createBKLog(req.toBKLog(rctx))
return id, nil
return &id, nil
}

// UpdateLogRule 更新日志采集规则
Expand All @@ -194,35 +199,35 @@ func CreateLogRule(c context.Context, req CreateLogRuleReq) (string, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules/:id [put]
func UpdateLogRule(c context.Context, req UpdateLogRuleReq) (string, error) {
func UpdateLogRule(c context.Context, req *UpdateLogRuleReq) (*string, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return "", err
return nil, err
}
id := req.ID
if isBcsLogConfigID(id) || isBKLogID(id) {
return "", fmt.Errorf("id is invalid")
return nil, fmt.Errorf("id is invalid")
}

// check rule is exist
store := storage.GlobalStorage
rule, err := store.GetLogRule(c, id)
if err != nil {
return "", err
return nil, err
}

if rule.RuleID == 0 {
return "", errors.Errorf("invalid rule id")
return nil, errors.Errorf("invalid rule id")
}

err = store.UpdateLogRule(c, id, req.toEntity(rctx.Username, rctx.ProjectCode, rule.Name))
if err != nil {
return "", err
return nil, err
}

// 更新 bklog 规则耗时比较长,异步调用
go updateBKLog(rule.ID.Hex(), rule.RuleID, req.toBKLog(rctx, rule.RuleName))
return id, nil
return &id, nil
}

// DeleteLogRule 删除日志采集规则
Expand All @@ -231,7 +236,7 @@ func UpdateLogRule(c context.Context, req UpdateLogRuleReq) (string, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules/:id [delete]
func DeleteLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
func DeleteLogRule(c context.Context, req *GetLogRuleReq) (*any, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -271,7 +276,7 @@ func DeleteLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules/:id/retry [post]
func RetryLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
func RetryLogRule(c context.Context, req *GetLogRuleReq) (*any, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -345,7 +350,7 @@ func RetryLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules/:id/enable [post]
func EnableLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
func EnableLogRule(c context.Context, req *GetLogRuleReq) (*any, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -395,7 +400,7 @@ func EnableLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
// @Produce json
// @Success 200
// @Router /log_collector/rules/:id/disable [post]
func DisableLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
func DisableLogRule(c context.Context, req *GetLogRuleReq) (*any, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down Expand Up @@ -446,7 +451,7 @@ func DisableLogRule(c context.Context, req GetLogRuleReq) (interface{}, error) {
// @Success 200
// @Router /log_collector/storages/cluster_groups [get]
func GetStorageClusters(
c context.Context, req bklog.GetStorageClustersReq) ([]bklog.GetStorageClustersRespData, error) {
c context.Context, req *bklog.GetStorageClustersReq) (*[]bklog.GetStorageClustersRespData, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand All @@ -465,13 +470,13 @@ func GetStorageClusters(
}
}

return data, nil
return &data, nil
}

// SwitchStorageReq 切换 ES 存储集群请求
type SwitchStorageReq struct {
ProjectId string `json:"-" in:"path=projectId;required"`
ClusterId string `json:"-" in:"path=clusterId;required"`
ProjectId string `json:"projectId" in:"path=projectId" validate:"required"`
ClusterId string `json:"clusterId" in:"path=clusterId" validate:"required"`
StorageClusterID int `json:"storage_cluster_id"`
}

Expand All @@ -481,7 +486,7 @@ type SwitchStorageReq struct {
// @Produce json
// @Success 200
// @Router /log_collector/storages/switch_storage [post]
func SwitchStorage(c context.Context, req SwitchStorageReq) (interface{}, error) {
func SwitchStorage(c context.Context, req *SwitchStorageReq) (*any, error) {
rctx, err := rest.GetRestContext(c)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit eca9639

Please sign in to comment.