From 4b777eaa6bf935e00be34a179f6c8270d84276c0 Mon Sep 17 00:00:00 2001 From: ymakedaq <996156275@qq.com> Date: Tue, 31 Oct 2023 17:29:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(dbm-services):=20fix=20=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E4=BA=91=E5=8C=BA=E5=9F=9Fbug=20close=20#158?= =?UTF-8?q?8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/controller/manage/import.go | 52 +++++++++++++------ .../db-resource/internal/svr/bk/disk.go | 7 ++- dbm-services/common/go-pubpkg/cc.v3/schema.go | 1 + .../bk-dbm/charts/db-resource/Chart.yaml | 2 +- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/dbm-services/common/db-resource/internal/controller/manage/import.go b/dbm-services/common/db-resource/internal/controller/manage/import.go index ab3a904bd5..ea75bc483b 100644 --- a/dbm-services/common/db-resource/internal/controller/manage/import.go +++ b/dbm-services/common/db-resource/internal/controller/manage/import.go @@ -31,7 +31,6 @@ import ( // ImportMachParam TODO type ImportMachParam struct { - BkCloudId int `json:"bk_cloud_id"` // ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用 ForBizs []int `json:"for_bizs"` RsTypes []string `json:"resource_types"` @@ -43,8 +42,9 @@ type ImportMachParam struct { // HostBase TODO type HostBase struct { - Ip string `json:"ip" binding:"required,ip"` - HostId int `json:"host_id" binding:"required"` + Ip string `json:"ip" binding:"required,ip"` + HostId int `json:"host_id" binding:"required"` + BkCloudId int `json:"bk_cloud_id"` } func (p ImportMachParam) getOperationInfo(requestId string, hostIds json.RawMessage, @@ -72,6 +72,16 @@ func (p ImportMachParam) getIps() (ips []string) { return } +func (p ImportMachParam) getIpsByCloudId() (ipMap map[int][]string) { + ipMap = make(map[int][]string) + for _, v := range p.Hosts { + if !cmutil.IsEmpty(v.Ip) { + ipMap[v.BkCloudId] = append(ipMap[v.BkCloudId], v.Ip) + } + } + return +} + func (p ImportMachParam) getHostIds() (hostIds []int) { for _, v := range p.Hosts { if v.HostId > 0 { @@ -83,17 +93,20 @@ func (p ImportMachParam) getHostIds() (hostIds []int) { func (p *ImportMachParam) existCheck() (err error) { var alreadyExistRs []model.TbRpDetail - err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_cloud_id = ? and ip in (?)", p.BkCloudId, p.getIps()). - Scan(&alreadyExistRs).Error - if err != nil { - return errno.ErrDBQuery.Add(err.Error()) - } - if len(alreadyExistRs) > 0 { - errMsg := "already exist:\n " - for _, r := range alreadyExistRs { - errMsg += fmt.Sprintf(" bk_cloud_id:%d,ip:%s \n", r.BkCloudID, r.IP) + ipmap := p.getIpsByCloudId() + for cloudId, ips := range ipmap { + err = model.DB.Self.Table(model.TbRpDetailName()).Where("bk_cloud_id = ? and ip in (?)", cloudId, ips). + Scan(&alreadyExistRs).Error + if err != nil { + return errno.ErrDBQuery.Add(err.Error()) + } + if len(alreadyExistRs) > 0 { + errMsg := "already exist:\n " + for _, r := range alreadyExistRs { + errMsg += fmt.Sprintf(" bk_cloud_id:%d,ip:%s \n", r.BkCloudID, r.IP) + } + return fmt.Errorf(errMsg) } - return fmt.Errorf(errMsg) } return nil } @@ -184,7 +197,14 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) { ccHostsInfo, notFoundHosts, derr = bk.BatchQueryHostsInfo(param.BkBizId, targetHosts) }() // get disk information in batch - diskResp, err = bk.GetDiskInfo(targetHosts, param.BkCloudId, param.BkBizId) + var ipList []bk.IPList + for _, host := range param.Hosts { + ipList = append(ipList, bk.IPList{ + IP: host.Ip, + BkCloudID: host.BkCloudId, + }) + } + diskResp, err = bk.GetDiskInfo(ipList, param.BkBizId) if err != nil { logger.Error("query host cc info failed %s", err.Error()) return resp, err @@ -217,12 +237,12 @@ func Doimport(param ImportMachParam) (resp *ImportHostResp, err error) { logger.Info("more info %v", ccHostsInfo) for _, h := range ccHostsInfo { delete(hostsMap, h.InnerIP) - el := transHostInfoToDbModule(h, param.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson) + el := transHostInfoToDbModule(h, h.BkCloudId, param.BkBizId, rstypes, bizJson, lableJson) el.SetMore(h.InnerIP, diskResp.IpLogContentMap) // gse agent 1.0的 agent 是用 cloudid:ip gseAgentId := h.BkAgentId if cmutil.IsEmpty(gseAgentId) { - gseAgentId = fmt.Sprintf("%d:%s", param.BkCloudId, h.InnerIP) + gseAgentId = fmt.Sprintf("%d:%s", h.BkCloudId, h.InnerIP) } gseAgentIds = append(gseAgentIds, gseAgentId) el.BkAgentId = gseAgentId diff --git a/dbm-services/common/db-resource/internal/svr/bk/disk.go b/dbm-services/common/db-resource/internal/svr/bk/disk.go index 46d81ec651..f91787cf59 100644 --- a/dbm-services/common/db-resource/internal/svr/bk/disk.go +++ b/dbm-services/common/db-resource/internal/svr/bk/disk.go @@ -117,8 +117,7 @@ func getIpList(hosts []string, bk_cloud_id int) []IPList { } // GetDiskInfo TODO -func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp, err error) { - iplist := getIpList(hosts, bk_cloud_id) +func GetDiskInfo(hosts []IPList, bk_biz_id int) (resp GetDiskResp, err error) { jober := JobV3{ Client: EsbClient, } @@ -129,7 +128,7 @@ func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp, ScriptLanguage: 1, AccountAlias: "root", TargetServer: TargetServer{ - IPList: iplist, + IPList: hosts, }, }, ) @@ -174,7 +173,7 @@ func GetDiskInfo(hosts []string, bk_cloud_id, bk_biz_id int) (resp GetDiskResp, BKBizId: bk_biz_id, JobInstanceID: job.JobInstanceID, StepInstanceID: job.StepInstanceID, - IPList: iplist, + IPList: hosts, }) resp.IpLogContentMap = make(map[string]*ShellResCollection) for _, d := range ipLogs.ScriptTaskLogs { diff --git a/dbm-services/common/go-pubpkg/cc.v3/schema.go b/dbm-services/common/go-pubpkg/cc.v3/schema.go index 1c7eaaa61d..0c6c83d3bf 100644 --- a/dbm-services/common/go-pubpkg/cc.v3/schema.go +++ b/dbm-services/common/go-pubpkg/cc.v3/schema.go @@ -73,6 +73,7 @@ type Host struct { BkCpu int `json:"bk_cpu,omitempty"` BkMem int `json:"bk_mem,omitempty"` BkDisk int `json:"bk_disk"` + BkCloudId int `json:"bk_cloud_id"` BkAgentId string `json:"bk_agent_id,omitempty"` SvrTypeName string `json:"svr_type_name"` BKBSInfos []*CMDBInfo `json:"bk_bs_info"` diff --git a/helm-charts/bk-dbm/charts/db-resource/Chart.yaml b/helm-charts/bk-dbm/charts/db-resource/Chart.yaml index 079f7d5f3c..328a08ece2 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.47 +appVersion: 0.0.1-alpha.48 description: A Helm chart for Kubernetes name: db-resource type: application