Skip to content

Commit

Permalink
feat(dbm-services): 资源池匹配支持筛选os版本 TencentBlueKing#8333
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq authored and iSecloud committed Dec 6, 2024
1 parent b04c4e4 commit 77499c2
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 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 @@ -76,6 +76,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)
}
}

Expand All @@ -96,3 +97,12 @@ func (c BackStageHandler) RunAsyncCmdb(r *gin.Context) {
}
c.SendResponse(r, nil, "async success")
}

// SyncOsInfo sync os info
func (c BackStageHandler) SyncOsInfo(r *gin.Context) {
err := task.SyncOsNameInfo()
if err != nil {
logger.Error("SyncOsNameInfo failed %v", err)
}
c.SendResponse(r, nil, "async success")
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/svr/task"
"dbm-services/common/db-resource/internal/svr/yunti"
"dbm-services/common/db-resource/internal/util"
"dbm-services/common/go-pubpkg/cc.v3"
"dbm-services/common/go-pubpkg/errno"
"dbm-services/common/go-pubpkg/logger"
Expand Down Expand Up @@ -377,7 +378,7 @@ func (p ImportMachParam) transHostInfoToDbModule(h *cc.Host, bkCloudId int, labe
OsType: model.ConvertOsTypeToHuman(osType),
OsBit: h.BkOsBit,
OsVerion: h.BkOsVersion,
OsName: strings.TrimSpace(strings.ToLower(strings.ReplaceAll(h.OSName, " ", ""))),
OsName: util.CleanOsName(h.OSName),
UpdateTime: time.Now(),
CreateTime: time.Now(),
}
Expand Down
37 changes: 21 additions & 16 deletions dbm-services/common/db-resource/internal/svr/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// SearchContext TODO
Expand Down Expand Up @@ -298,23 +297,29 @@ func (o *SearchContext) MatchOsType(db *gorm.DB) {
db.Where("os_type = ? ", osType)
}

// MatchOsName TODO
// MatchOsName match os name os_name = "tlinux-1.2"
func (o *SearchContext) MatchOsName(db *gorm.DB) {
// match os name like Windows Server 2012
if len(o.ObjectDetail.OsNames) > 0 {
conditions := []clause.Expression{}
for _, osname := range o.ObjectDetail.OsNames {
conditions = append(conditions, clause.Like{
Column: "os_name",
Value: "%" + strings.TrimSpace(strings.ToLower(osname)) + "%",
})
}
if len(conditions) == 1 {
db.Clauses(clause.AndConditions{Exprs: conditions})
} else {
// 有多个条件,使用or,才会被用()包括起来所有的or条件
db.Clauses(clause.OrConditions{Exprs: conditions})
}
// conditions := []clause.Expression{}
// for _, osname := range o.ObjectDetail.OsNames {
// conditions = append(conditions, clause.Like{
// Column: "os_name",
// Value: "%" + strings.TrimSpace(strings.ToLower(osname)) + "%",
// })
// }
// if len(conditions) == 1 {
// db.Clauses(clause.AndConditions{Exprs: conditions})
// } else {
// // 有多个条件,使用or,才会被用()包括起来所有的or条件
// db.Clauses(clause.OrConditions{Exprs: conditions})
// }
if len(o.ObjectDetail.OsNames) == 0 {
return
}
if o.ObjectDetail.ExcludeOsName {
db.Where("os_name not in (?)", o.ObjectDetail.OsNames)
} else {
db.Where("os_name in (?)", o.ObjectDetail.OsNames)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ type ObjectDetail struct {

Affinity string `json:"affinity"`
// Windows,Linux
OsType string `json:"os_type"`
OsNames []string `json:"os_names"`
Count int `json:"count" binding:"required,min=1"` // 申请数量
OsType string `json:"os_type"`
OsNames []string `json:"os_names"`
ExcludeOsName bool `json:"exclude_os_name"`
Count int `json:"count" binding:"required,min=1"` // 申请数量
}

// Hosts bk hosts
Expand Down
37 changes: 37 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 @@ -19,6 +19,7 @@ import (

"dbm-services/common/db-resource/internal/model"
"dbm-services/common/db-resource/internal/svr/bk"
"dbm-services/common/db-resource/internal/util"
"dbm-services/common/go-pubpkg/cc.v3"
"dbm-services/common/go-pubpkg/cmutil"
"dbm-services/common/go-pubpkg/logger"
Expand Down Expand Up @@ -215,3 +216,39 @@ func AsyncResourceHardInfo() (err error) {
}
return nil
}

// SyncOsNameInfo sync os name info
func SyncOsNameInfo() (err error) {
logger.Info("start async from cmdb ...")
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{}{
"os_name": util.CleanOsName(ccInfo.OSName),
"os_version": ccInfo.BkOsVersion,
}).Error
if err != nil {
logger.Warn("request cmdb api failed %s", err.Error())
}
}
}
return nil
}
31 changes: 31 additions & 0 deletions dbm-services/common/db-resource/internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
* Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at https://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

// Package util TODO
package util

import (
"regexp"
"strings"
)

// CleanOsName clean os name
func CleanOsName(osName string) string {
tr := regexp.MustCompile(`(?i)^tencent`)
if tr.MatchString(strings.TrimSpace(osName)) {
r := regexp.MustCompile(`\d+(.\d)+`)
return "tliunx-" + r.FindString(osName)
}
wr := regexp.MustCompile(`(?i)Windows\s*Server\s*\d\d\d\d`)
if wr.MatchString(osName) {
return strings.TrimSpace(strings.ReplaceAll(wr.FindString(osName), " ", ""))
}
return osName
}

0 comments on commit 77499c2

Please sign in to comment.