Skip to content

Commit

Permalink
feat(dbm-services): 删除判断跨交换机字段,net_device_id 已经从cc删除 #8965
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Jan 17, 2025
1 parent 151ca49 commit 3470e83
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
10 changes: 10 additions & 0 deletions dbm-services/common/db-resource/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (c *BackStageHandler) RegisterRouter(engine *gin.Engine) {
r.POST("/cc/module/check", c.RunModuleCheck)
r.POST("/cc/async", c.RunAsyncCmdb)
r.POST("/cc/sync/os/info", c.SyncOsInfo)
r.POST("/cc/sync/netdevice", c.FlushNetDeviceInfo)
}
}

Expand Down Expand Up @@ -106,3 +107,12 @@ func (c BackStageHandler) SyncOsInfo(r *gin.Context) {
}
c.SendResponse(r, nil, "async success")
}

// FlushNetDeviceInfo flush net device info
func (c BackStageHandler) FlushNetDeviceInfo(r *gin.Context) {
err := task.FlushNetDeviceInfo()
if err != nil {
logger.Error("flush failed %v", err)
}
c.SendResponse(r, nil, "success")
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (p ImportMachParam) transHostInfoToDbModule(h *cc.Host, bkCloudId int, labe
RackID: strings.TrimSpace(h.Equipment),
SvrTypeName: h.SvrTypeName,
Status: model.Unused,
NetDeviceID: strings.TrimSpace(h.LinkNetdeviceId),
NetDeviceID: util.TransInnerSwitchIpAsNetDeviceId(h.InnerSwitchIp),
StorageDevice: []byte("{}"),
TotalStorageCap: h.BkDisk,
BkAgentId: h.BkAgentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func (o *SearchContext) MatchLabels(db *gorm.DB) {
db.Where(model.JSONQuery("labels").JointOrContains(o.Labels))
} else {
// 如果请求没有标签, 只能匹配没有标签的资源
db.Where(" JSON_TYPE(labels) is null OR JSON_LENGTH(labels) < 1 ")
db.Where(" JSON_TYPE(labels) = 'NULL' or JSON_TYPE(labels) is null OR JSON_LENGTH(labels) < 1 ")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (o *SearchContext) setResourcePriority(ins model.TbRpDetail, ele *Item) {
// 如果请求的磁盘为空,尽量匹配没有磁盘的机器
// 请求参数需要几块盘,如果机器盘数量预制相等,则优先级更高
if len(o.StorageSpecs) == len(ins.Storages) {
ele.Priority += PriorityP0
ele.Priority += PriorityP1
}
// 如果请求参数包含规格,如果机器机型匹配,则高优先级
if len(o.DeviceClass) > 0 && lo.Contains(o.DeviceClass, ins.DeviceClass) {
Expand Down
2 changes: 2 additions & 0 deletions dbm-services/common/db-resource/internal/svr/bk/cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func init() {
"rack_id",
"svr_type_name",
"net_device_id",
"bk_inner_switch_ip",
"bk_outer_switch_ip",
"bk_os_type",
"bk_os_bit",
"bk_os_version",
Expand Down
34 changes: 34 additions & 0 deletions dbm-services/common/db-resource/internal/svr/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,37 @@ func SyncOsNameInfo() (err error) {
}
return nil
}

// FlushNetDeviceInfo TODO
func FlushNetDeviceInfo() (err error) {
var rsList []model.TbRpDetail
err = model.DB.Self.Table(model.TbRpDetailName()).Find(&rsList).Error
if err != nil {
if err == sql.ErrNoRows {
return nil
}
logger.Error("query total_storage_cap less than 0,err %w ", err)
return err
}
bizHostMap := make(map[int][]string)
for _, rs := range rsList {
bizHostMap[rs.BkBizId] = append(bizHostMap[rs.BkBizId], rs.IP)
}
for bizId, hosts := range bizHostMap {
ccInfos, _, err := bk.BatchQueryHostsInfo(bizId, hosts)
if err != nil {
logger.Warn("query machine hardinfo from cmdb failed %s", err.Error())
continue
}
for _, ccInfo := range ccInfos {
err = model.DB.Self.Table(model.TbRpDetailName()).Where("ip = ? and bk_biz_id = ? ", ccInfo.InnerIP, bizId).
Updates(map[string]interface{}{
"net_device_id": util.TransInnerSwitchIpAsNetDeviceId(ccInfo.InnerSwitchIp),
}).Error
if err != nil {
logger.Warn("request cmdb api failed %s", err.Error())
}
}
}
return nil
}
19 changes: 19 additions & 0 deletions dbm-services/common/db-resource/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
package util

import (
"fmt"
"hash/crc32"
"regexp"
"strings"

"github.com/samber/lo"
)

// CleanOsName clean os name
Expand All @@ -29,3 +33,18 @@ func CleanOsName(osName string) string {
}
return osName
}

// TransInnerSwitchIpAsNetDeviceId trans inner switch ip as net_device_id
func TransInnerSwitchIpAsNetDeviceId(inner_switch_ips string) string {
if lo.IsEmpty(inner_switch_ips) {
return ""
}
sep := regexp.MustCompile(";|,")
ips := sep.Split(inner_switch_ips, -1)
crc32q := crc32.MakeTable(0xD5828281)
fakeNetDeviceIdList := []string{}
for _, ip := range ips {
fakeNetDeviceIdList = append(fakeNetDeviceIdList, fmt.Sprintf("%08x", crc32.Checksum([]byte(ip), crc32q)))
}
return strings.Join(lo.Uniq(fakeNetDeviceIdList), ",")
}
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-resource/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.115
appVersion: 0.0.1-alpha.116
description: A Helm chart for Kubernetes
name: db-resource
type: application
Expand Down

0 comments on commit 3470e83

Please sign in to comment.