From 3470e83ac272d45e414e85fc6818c2ec47bf04a2 Mon Sep 17 00:00:00 2001 From: yuanruji Date: Thu, 16 Jan 2025 11:38:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(dbm-services):=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E8=B7=A8=E4=BA=A4=E6=8D=A2=E6=9C=BA=E5=AD=97?= =?UTF-8?q?=E6=AE=B5,net=5Fdevice=5Fid=20=E5=B7=B2=E7=BB=8F=E4=BB=8Ecc?= =?UTF-8?q?=E5=88=A0=E9=99=A4=20#8965?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/controller/controller.go | 10 ++++++ .../internal/controller/manage/rs_import.go | 2 +- .../db-resource/internal/svr/apply/apply.go | 2 +- .../internal/svr/apply/apply_match.go | 2 +- .../common/db-resource/internal/svr/bk/cc.go | 2 ++ .../db-resource/internal/svr/task/task.go | 34 +++++++++++++++++++ .../common/db-resource/internal/util/util.go | 19 +++++++++++ .../bk-dbm/charts/db-resource/Chart.yaml | 2 +- 8 files changed, 69 insertions(+), 4 deletions(-) diff --git a/dbm-services/common/db-resource/internal/controller/controller.go b/dbm-services/common/db-resource/internal/controller/controller.go index 954dc697fe..387ee2cf1f 100644 --- a/dbm-services/common/db-resource/internal/controller/controller.go +++ b/dbm-services/common/db-resource/internal/controller/controller.go @@ -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) } } @@ -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") +} diff --git a/dbm-services/common/db-resource/internal/controller/manage/rs_import.go b/dbm-services/common/db-resource/internal/controller/manage/rs_import.go index 38b8451244..9775d69f63 100644 --- a/dbm-services/common/db-resource/internal/controller/manage/rs_import.go +++ b/dbm-services/common/db-resource/internal/controller/manage/rs_import.go @@ -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, diff --git a/dbm-services/common/db-resource/internal/svr/apply/apply.go b/dbm-services/common/db-resource/internal/svr/apply/apply.go index 3d9729ab91..40d0ba2d9e 100644 --- a/dbm-services/common/db-resource/internal/svr/apply/apply.go +++ b/dbm-services/common/db-resource/internal/svr/apply/apply.go @@ -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 ") } } diff --git a/dbm-services/common/db-resource/internal/svr/apply/apply_match.go b/dbm-services/common/db-resource/internal/svr/apply/apply_match.go index 8cdb33deed..5c80a42027 100644 --- a/dbm-services/common/db-resource/internal/svr/apply/apply_match.go +++ b/dbm-services/common/db-resource/internal/svr/apply/apply_match.go @@ -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) { diff --git a/dbm-services/common/db-resource/internal/svr/bk/cc.go b/dbm-services/common/db-resource/internal/svr/bk/cc.go index ed192d28aa..336d9d94e5 100644 --- a/dbm-services/common/db-resource/internal/svr/bk/cc.go +++ b/dbm-services/common/db-resource/internal/svr/bk/cc.go @@ -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", diff --git a/dbm-services/common/db-resource/internal/svr/task/task.go b/dbm-services/common/db-resource/internal/svr/task/task.go index 525fb47ead..e9325320a4 100644 --- a/dbm-services/common/db-resource/internal/svr/task/task.go +++ b/dbm-services/common/db-resource/internal/svr/task/task.go @@ -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 +} diff --git a/dbm-services/common/db-resource/internal/util/util.go b/dbm-services/common/db-resource/internal/util/util.go index dba5b035fa..66a9d1e632 100644 --- a/dbm-services/common/db-resource/internal/util/util.go +++ b/dbm-services/common/db-resource/internal/util/util.go @@ -12,8 +12,12 @@ package util import ( + "fmt" + "hash/crc32" "regexp" "strings" + + "github.com/samber/lo" ) // CleanOsName clean os name @@ -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), ",") +} diff --git a/helm-charts/bk-dbm/charts/db-resource/Chart.yaml b/helm-charts/bk-dbm/charts/db-resource/Chart.yaml index 13ed40cab3..1e50c51036 100644 --- a/helm-charts/bk-dbm/charts/db-resource/Chart.yaml +++ b/helm-charts/bk-dbm/charts/db-resource/Chart.yaml @@ -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