Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/v1.5.0' into feat/mongos_autofix/
Browse files Browse the repository at this point in the history
  • Loading branch information
yyhenryyy committed Oct 17, 2024
2 parents 859ac37 + 9a6edc0 commit 3ff55ea
Show file tree
Hide file tree
Showing 1,506 changed files with 96,599 additions and 45,134 deletions.
2 changes: 1 addition & 1 deletion .gtmproject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ github:
repo_name: "blueking-dbm"

# 指定里程碑ID,
milestone_id: "10"
milestone_id: "11"

project:
# 主分支
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/pkg/errors"

"dbm-services/common/go-pubpkg/cmutil"
)

// GenerateConfigVersion godoc
Expand Down Expand Up @@ -124,8 +126,7 @@ func (cf *Config) GenerateConfigVersion(ctx *gin.Context) {
// 还有一种极端情况,多个请求并行generate,但时间是错开不在 1s内,也能generate成功
if resp, err = simpleconfig.GenerateConfigFile(model.DB.Self, r2, r.Method, nil); err != nil {
//logger.Warn("simpleconfig.GenerateConfigFile err: %+v", err)
if util.IsErrorString(err, "Error 1062: Duplicate entry") ||
util.IsErrorString(err, "Error 1213: Deadlock found when trying to get lock") ||
if mysqlErr := cmutil.NewMySQLError(err).Code; mysqlErr == 1062 || mysqlErr == 1213 ||
util.IsErrorString(err, "revision is applied already:") {
// 前面已经判断不存在,现在写入报重复,说明有其它请求 generate version 了。直接读取
logger.Info("level_node has applied versioned, query configs instead of generate")
Expand All @@ -137,6 +138,7 @@ func (cf *Config) GenerateConfigVersion(ctx *gin.Context) {
return
}
} else {
logger.Errorf("level_node has applied versioned, return error: %s", err.Error())
handler.SendResponse(ctx, err, nil)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
// ImportMachParam 资源导入请求参数
type ImportMachParam struct {
// ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels map[string]string `json:"labels"`
Expand Down Expand Up @@ -120,29 +120,13 @@ type ImportHostResp struct {
NotFoundInCCHosts []string `json:"not_found_in_cc_hosts"`
}

func (p ImportMachParam) transParamToBytes() (lableJson, bizJson, rstypes json.RawMessage, err error) {
func (p ImportMachParam) transParamToBytes() (lableJson json.RawMessage, err error) {
// lableJson = []byte("{}")
lableJson, err = json.Marshal(cmutil.CleanStrMap(p.Labels))
if err != nil {
logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error()))
return
}
bizJson = []byte("[]")
if len(p.ForBizs) > 0 {
bizJson, err = json.Marshal(cmutil.IntSliceToStrSlice(p.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
return
}
}
rstypes = []byte("[]")
if len(p.RsTypes) > 0 {
rstypes, err = json.Marshal(cmutil.StringsRemoveEmpty(p.RsTypes))
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
return
}
}
return
}

Expand Down Expand Up @@ -183,7 +167,7 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
}
resp.SearchDiskErrInfo = diskResp.IpFailedLogMap
resp.NotFoundInCCHosts = notFoundHosts
lableJson, bizJson, rstypes, err := param.transParamToBytes()
lableJson, err := param.transParamToBytes()
if err != nil {
return resp, err
}
Expand All @@ -204,7 +188,7 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) {
}
for _, h := range ccHostsInfo {
delete(hostsMap, h.InnerIP)
el := transHostInfoToDbModule(h, h.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson)
el := param.transHostInfoToDbModule(h, h.BkCloudId, lableJson)
el.SetMore(h.InnerIP, diskResp.IpLogContentMap)
// gse agent 1.0的 agent 是用 cloudid:ip
gseAgentId := h.BkAgentId
Expand Down Expand Up @@ -265,16 +249,16 @@ func getCvmMachList(hosts []*cc.Host) []string {
}

// transHostInfoToDbModule 获取的到的主机信息赋值给db model
func transHostInfoToDbModule(h *cc.Host, bkCloudId, bkBizId int, rstp, biz, label []byte) model.TbRpDetail {
func (p ImportMachParam) transHostInfoToDbModule(h *cc.Host, bkCloudId int, label []byte) model.TbRpDetail {
osType := h.BkOsType
if cmutil.IsEmpty(osType) {
osType = bk.OsLinux
}
return model.TbRpDetail{
RsTypes: rstp,
DedicatedBizs: biz,
DedicatedBiz: p.ForBiz,
RsType: p.RsType,
BkCloudID: bkCloudId,
BkBizId: bkBizId,
BkBizId: p.BkBizId,
AssetID: h.AssetID,
BkHostID: h.BKHostId,
IP: h.InnerIP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"gorm.io/gorm"

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/svr/apply"
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/svr/dbmapi"
"dbm-services/common/db-resource/internal/svr/meta"
"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/errno"
Expand All @@ -30,21 +30,24 @@ import (
// MachineResourceGetterInputParam TODO
type MachineResourceGetterInputParam struct {
// 专用业务Ids
ForBizs []int `json:"for_bizs"`
City []string `json:"city"`
SubZones []string `json:"subzones"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsTypes []string `json:"resource_types"`
MountPoint string `json:"mount_point"`
Cpu apply.MeasureRange `json:"cpu"`
Mem apply.MeasureRange `json:"mem"`
Disk apply.MeasureRange `json:"disk"`
DiskType string `json:"disk_type"`
OsType string `json:"os_type"`
StorageSpecs []apply.DiskSpec `json:"storage_spec"`
ForBiz int `json:"for_biz"`
City []string `json:"city"`
SubZoneIds []string `json:"subzone_ids"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsType string `json:"resource_type"`
MountPoint string `json:"mount_point"`
Cpu meta.MeasureRange `json:"cpu"`
Mem meta.MeasureRange `json:"mem"`
Disk meta.MeasureRange `json:"disk"`
DiskType string `json:"disk_type"`
OsType string `json:"os_type"`
StorageSpecs []meta.DiskSpec `json:"storage_spec"`
// 适用于用户没选业务和db类型的情况
SetBizEmpty bool `json:"set_empty_biz"`
SetRsTypeEmpty bool `json:"set_empty_resource_type"`
// true,false,""
GseAgentAlive string `json:"gse_agent_alive"`
Limit int `json:"limit"`
Expand Down Expand Up @@ -129,7 +132,7 @@ func (c *MachineResourceGetterInputParam) matchStorageSpecs(db *gorm.DB) {

func (c *MachineResourceGetterInputParam) getRealCitys() (realCistys []string, err error) {
for _, logicCity := range c.City {
rcitys, err := meta.GetIdcCityByLogicCity(logicCity)
rcitys, err := dbmapi.GetIdcCityByLogicCity(logicCity)
if err != nil {
logger.Error("from %s get real citys failed %s", logicCity, err.Error())
return nil, err
Expand Down Expand Up @@ -172,13 +175,11 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
if len(c.BkCloudIds) > 0 {
db.Where("bk_cloud_id in (?) ", c.BkCloudIds)
}
if len(c.RsTypes) > 0 {
// 如果参数["all"],表示选择没有任何资源类型标签的资源
if c.RsTypes[0] == "all" {
db.Where("JSON_LENGTH(rs_types) <= 0")
} else {
db.Where("(?)", model.JSONQuery("rs_types").JointOrContains(c.RsTypes))
}
if !c.SetRsTypeEmpty {
db.Where("rs_type = ? ", c.RsType)
}
if !c.SetBizEmpty {
db.Where("dedicated_biz = ?", c.ForBiz)
}
c.matchSpec(db)
c.matchStorageSpecs(db)
Expand All @@ -189,18 +190,10 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
}
db.Where(" city in (?) ", realCitys)
}
if len(c.SubZones) > 0 {
db.Where(" sub_zone in (?) ", c.SubZones)
if len(c.SubZoneIds) > 0 {
db.Where(" sub_zone_id in (?) ", c.SubZoneIds)
}

if len(c.ForBizs) > 0 {
// 如果参数[0],表示选择没有任何业务标签的资源
if c.ForBizs[0] == 0 {
db.Where("JSON_LENGTH(dedicated_bizs) <= 0")
} else {
db.Where("(?)", model.JSONQuery("dedicated_bizs").JointOrContains(cmutil.IntSliceToStrSlice(c.ForBizs)))
}
}
if cmutil.IsNotEmpty(c.OsType) {
db.Where("os_type = ?", c.OsType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"dbm-services/common/go-pubpkg/logger"

rf "github.com/gin-gonic/gin"
"github.com/samber/lo"
)

// MachineResourceHandler 主机处理handler
Expand Down Expand Up @@ -86,8 +87,8 @@ func (c *MachineResourceHandler) Delete(r *rf.Context) {
// BatchUpdateMachineInput 批量编辑主机信息请求参数
type BatchUpdateMachineInput struct {
BkHostIds []int `json:"bk_host_ids" binding:"required,dive,gt=0" `
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
RackId string `json:"rack_id"`
SetBizEmpty bool `json:"set_empty_biz"`
SetRsTypeEmpty bool `json:"set_empty_resource_type"`
Expand All @@ -111,31 +112,13 @@ func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) {
}

// update for biz
if len(input.ForBizs) > 0 {
bizJson, err := json.Marshal(cmutil.IntSliceToStrSlice(input.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["dedicated_bizs"] = bizJson
}
if input.SetBizEmpty {
updateMap["dedicated_bizs"] = EmptyArryJson
if input.ForBiz > 0 {
updateMap["dedicated_biz"] = input.ForBiz
}

// update resource type
if len(input.RsTypes) > 0 {
rstypes, err := json.Marshal(input.RsTypes)
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["rs_types"] = rstypes
}
if input.SetRsTypeEmpty {
updateMap["rs_types"] = EmptyArryJson
if lo.IsNotEmpty(input.RsType) {
updateMap["rs_type"] = input.RsType
}

// update disk
Expand All @@ -155,7 +138,7 @@ func (c *MachineResourceHandler) BatchUpdate(r *rf.Context) {
}

// do update
err := model.DB.Self.Table(model.TbRpDetailName()).Select("dedicated_bizs", "rs_types", "storage_device", "rack_id").
err := model.DB.Self.Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type", "storage_device", "rack_id").
Where("bk_host_id in (?)", input.BkHostIds).Updates(updateMap).Error
if err != nil {
c.SendResponse(r, err, requestId, err.Error())
Expand All @@ -175,8 +158,8 @@ type MachineResourceInputParam struct {
type MachineResource struct {
BkHostID int `json:"bk_host_id" binding:"required"`
Labels map[string]string `json:"labels"`
ForBizs []int `json:"for_bizs"`
RsTypes []string `json:"resource_types"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
StorageDevice map[string]bk.DiskDetail `json:"storage_device"`
}

Expand All @@ -199,23 +182,11 @@ func (c *MachineResourceHandler) Update(r *rf.Context) {
}
updateMap["lable"] = l
}
if len(v.ForBizs) > 0 {
bizJson, err := json.Marshal(cmutil.IntSliceToStrSlice(v.ForBizs))
if err != nil {
logger.Error(fmt.Sprintf("conver biz json Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["dedicated_bizs"] = bizJson
if v.ForBiz > 0 {
updateMap["dedicated_biz"] = v.ForBiz
}
if len(v.RsTypes) > 0 {
rstypes, err := json.Marshal(v.RsTypes)
if err != nil {
logger.Error(fmt.Sprintf("conver resource types Failed,Error:%s", err.Error()))
c.SendResponse(r, err, requestId, err.Error())
return
}
updateMap["rs_types"] = rstypes
if lo.IsNotEmpty(v.RsType) {
updateMap["rs_type"] = v.RsType
}
if len(v.StorageDevice) > 0 {
storageJson, err := json.Marshal(v.StorageDevice)
Expand All @@ -226,7 +197,7 @@ func (c *MachineResourceHandler) Update(r *rf.Context) {
}
updateMap["storage_device"] = storageJson
}
err := tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_bizs", "rs_types",
err := tx.Model(&model.TbRpDetail{}).Table(model.TbRpDetailName()).Select("dedicated_biz", "rs_type",
"label").Where("bk_host_id=?", v.BkHostID).Updates(updateMap).Error
if err != nil {
tx.Rollback()
Expand Down
Loading

0 comments on commit 3ff55ea

Please sign in to comment.