Skip to content

ESB 切换 APIGW #10252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
update tb_rp_detail
set gse_agent_status_code = 1
WHERE gse_agent_status_code = 2;
5 changes: 4 additions & 1 deletion dbm-services/common/db-resource/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ type Config struct {
RunMode string `yaml:"runMode"`
ListenAddress string `yaml:"listenAddress"`
// dbmeta: http://bk-dbm
DbMeta string `json:"dbmeta"`
DbMeta string `yaml:"dbmeta"`
BkCmdbApiUrl string `yaml:"bkCmdbApiUrl"`
BkJobApiUrl string `yaml:"bkJobApiUrl"`
BkNodeManApiUrl string `yaml:"bkNodeManApiUrl"`
Db Db `yaml:"db"`
CmdbDb Db `yaml:"cmdb_db" mapstructure:"cmdb_db"`
LoggerConfig LoggerConfig `yaml:"loggerConfig"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ func Doimport(param ImportMachParam, requestId string) (resp *ImportHostResp, er
var ccHostsInfo []*cc.Host
var derr error
var diskResp bk.GetDiskResp
var notFoundHosts, gseAgentIds []string
var notFoundHosts []string
var bkHostIds []int
var elems []model.TbRpDetail
resp = &ImportHostResp{}
targetHosts := lo.Uniq(param.getIps())
Expand Down Expand Up @@ -197,13 +198,7 @@ func Doimport(param ImportMachParam, requestId string) (resp *ImportHostResp, er
delete(hostsMap, h.InnerIP)
el := param.transHostInfoToDbModule(h, h.BkCloudId, lableJson)
el.SetMore(h.InnerIP, diskResp.IpLogContentMap)
// gse agent 1.0的 agent 是用 cloudid:ip
gseAgentId := h.BkAgentId
if lo.IsEmpty(gseAgentId) {
gseAgentId = fmt.Sprintf("%d:%s", h.BkCloudId, h.InnerIP)
}
gseAgentIds = append(gseAgentIds, gseAgentId)
el.BkAgentId = gseAgentId
bkHostIds = append(bkHostIds, h.BKHostId)
if v, ok := cvmInfoMap[h.InnerIP]; ok {
el.DramCap = v.Memory * 1000
}
Expand All @@ -213,7 +208,7 @@ func Doimport(param ImportMachParam, requestId string) (resp *ImportHostResp, er
logger.Error("failed to save resource: %s", err.Error())
return resp, err
}
task.SyncRsGseAgentStatusChan <- gseAgentIds
task.SyncRsGseAgentStatusChan <- bkHostIds
return resp, err
}

Expand Down Expand Up @@ -267,36 +262,37 @@ func buildTbRpItem(h *cc.Host, forBizId, bkbizId, bkCloudId int, rsType string,
osType = bk.OsLinux
}
return model.TbRpDetail{
DedicatedBiz: forBizId,
RsType: dealEmptyRs(rsType),
BkCloudID: bkCloudId,
BkBizId: bkbizId,
AssetID: h.AssetID,
BkHostID: h.BKHostId,
IP: h.InnerIP,
Labels: label,
DeviceClass: h.DeviceClass,
DramCap: h.BkMem,
CPUNum: h.BkCpu,
City: h.IdcCityName,
CityID: h.IdcCityId,
SubZone: h.SZone,
SubZoneID: h.SZoneID,
RackID: cleanStr(h.Equipment),
SvrTypeName: h.SvrTypeName,
Status: model.Unused,
NetDeviceID: util.TransInnerSwitchIpAsNetDeviceId(h.InnerSwitchIp),
StorageDevice: []byte("{}"),
TotalStorageCap: h.BkDisk,
BkAgentId: h.BkAgentId,
AgentStatusCode: 2,
OsType: model.ConvertOsTypeToHuman(osType),
OsBit: h.BkOsBit,
OsVerion: h.BkOsVersion,
OsName: util.CleanOsName(h.OSName),
Operator: operator,
UpdateTime: time.Now(),
CreateTime: time.Now(),
DedicatedBiz: forBizId,
RsType: dealEmptyRs(rsType),
BkCloudID: bkCloudId,
BkBizId: bkbizId,
AssetID: h.AssetID,
BkHostID: h.BKHostId,
IP: h.InnerIP,
Labels: label,
DeviceClass: h.DeviceClass,
DramCap: h.BkMem,
CPUNum: h.BkCpu,
City: h.IdcCityName,
CityID: h.IdcCityId,
SubZone: h.SZone,
SubZoneID: h.SZoneID,
RackID: cleanStr(h.Equipment),
SvrTypeName: h.SvrTypeName,
Status: model.Unused,
NetDeviceID: util.TransInnerSwitchIpAsNetDeviceId(h.InnerSwitchIp),
StorageDevice: []byte("{}"),
TotalStorageCap: h.BkDisk,
BkAgentId: h.BkAgentId,
AgentStatusCode: bk.GseAlive,
AgentStatusUpdateTime: time.Now(),
OsType: model.ConvertOsTypeToHuman(osType),
OsBit: h.BkOsBit,
OsVerion: h.BkOsVersion,
OsName: util.CleanOsName(h.OSName),
Operator: operator,
UpdateTime: time.Now(),
CreateTime: time.Now(),
}
}

Expand Down Expand Up @@ -345,7 +341,7 @@ func (c *MachineResourceHandler) ImportMachineWithDiffInfo(r *rf.Context) {
hostsGroupbyBkBizId[v.BkBizId] = append(hostsGroupbyBkBizId[v.BkBizId], v)
}
var elems []model.TbRpDetail
var gseAgentIds []string
var bkHostIds []int
for bkBizId, hostList := range hostsGroupbyBkBizId {
var ccHostsInfo []*cc.Host
var diskResp bk.GetDiskResp
Expand Down Expand Up @@ -397,13 +393,7 @@ func (c *MachineResourceHandler) ImportMachineWithDiffInfo(r *rf.Context) {
el := buildTbRpItem(h, hostInfo.ForBiz, hostInfo.BkBizId, hostInfo.BkCloudId, hostInfo.RsType, jsonRaw,
input.Operator)
el.SetMore(h.InnerIP, diskResp.IpLogContentMap)
// gse agent 1.0的 agent 是用 cloudid:ip
gseAgentId := h.BkAgentId
if lo.IsEmpty(gseAgentId) {
gseAgentId = fmt.Sprintf("%d:%s", h.BkCloudId, h.InnerIP)
}
gseAgentIds = append(gseAgentIds, gseAgentId)
el.BkAgentId = gseAgentId
bkHostIds = append(bkHostIds, h.BKHostId)
if v, ok := cvmInfoMap[h.InnerIP]; ok {
el.DramCap = v.Memory * 1000
}
Expand All @@ -413,7 +403,7 @@ func (c *MachineResourceHandler) ImportMachineWithDiffInfo(r *rf.Context) {
if err := model.DB.Self.Table(model.TbRpDetailName()).Create(elems).Error; err != nil {
c.SendResponse(r, err, err)
}
task.SyncRsGseAgentStatusChan <- gseAgentIds
task.SyncRsGseAgentStatusChan <- bkHostIds
c.SendResponse(r, nil, "success")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
}
switch strings.TrimSpace(strings.ToLower(c.GseAgentAlive)) {
case "true":
db.Where("gse_agent_status_code = ? ", bk.GSE_AGENT_OK)
db.Where("gse_agent_status_code = ? ", bk.GseAlive)
case "false":
db.Where("gse_agent_status_code != ? ", bk.GSE_AGENT_OK)
db.Where("gse_agent_status_code != ? ", bk.GseAlive)
}
if len(c.BkCloudIds) > 0 {
db.Where("bk_cloud_id in (?) ", c.BkCloudIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m MachineResourceHandler) SpecSum(r *gin.Context) {
}

db := model.DB.Self.Table(model.TbRpDetailName()).Select("count(*)")
db.Where("gse_agent_status_code = ? ", bk.GSE_AGENT_OK)
db.Where("gse_agent_status_code = ? ", bk.GseAlive)
db.Where(" bk_cloud_id = ? and status = ? ", input.BkCloudId, model.Unused)
// 如果没有指定资源类型,表示只能选择无资源类型标签的资源
// 没有资源类型标签的资源可以被所有其他类型使用
Expand Down
6 changes: 3 additions & 3 deletions dbm-services/common/db-resource/internal/svr/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ func (o *SearchContext) pickBase(db *gorm.DB) {
// 如果指定了特殊资源,就只查询这些资源
if len(o.SpecialHostIds) > 0 {
db.Where("bk_host_id in (?) and status = ? and gse_agent_status_code = ? ", o.SpecialHostIds, model.Unused,
bk.GSE_AGENT_OK)
bk.GseAlive)
return
}
db.Where("bk_cloud_id = ? and status = ? and gse_agent_status_code = ? ", o.BkCloudId, model.Unused, bk.GSE_AGENT_OK)
db.Where("bk_cloud_id = ? and status = ? and gse_agent_status_code = ? ", o.BkCloudId, model.Unused, bk.GseAlive)

o.MatchIntetionBkBiz(db)
o.MatchRsType(db)
Expand Down Expand Up @@ -357,7 +357,7 @@ func (o *SearchContext) predictResourceNoMatchReason() (reason string) {
name: "base",
fn: func(db *gorm.DB) {
db.Where("bk_cloud_id = ? and status = ? and gse_agent_status_code = ? ",
o.BkCloudId, model.Unused, bk.GSE_AGENT_OK)
o.BkCloudId, model.Unused, bk.GseAlive)
},
desc: fmt.Sprintf("在匹配云区域%d,gse_agent 状态为ok的资源的时候没有匹配到资源", o.BkCloudId),
},
Expand Down
18 changes: 9 additions & 9 deletions dbm-services/common/db-resource/internal/svr/bk/bk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
package bk

const (
// GSE_AGENT_OK 运行中
// Agent当前运行状态码, -1:未知 0:初始安装 1:启动中 2:运行中 3:有损状态 4:繁忙状态 5:升级中 6:停止中 7:解除安装
GSE_AGENT_OK = 2
// GSE_AGENT_INSTALLING TODO
GSE_AGENT_INSTALLING = 0
// GSE_AGENT_STARTING TODO
GSE_AGENT_STARTING = 1
// GSE_AGENT_STATUS_UNKNOWN TODO
GSE_AGENT_STATUS_UNKNOWN = -1
// GseNotAlive gse agent not alive
GseNotAlive = 0
// GseAlive TODO
// GseOK gse agent ok
GseAlive = 1
// GseTERMINATED gse agent terminated
GseTERMINATED = 2
// GseNotInstalled not installed gse agent
GseNotInstalled = 3
)

const (
Expand Down
55 changes: 22 additions & 33 deletions dbm-services/common/db-resource/internal/svr/bk/cc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
package bk

import (
"os"
"sync"
"strconv"
"time"

"dbm-services/common/db-resource/internal/config"
Expand All @@ -21,26 +20,34 @@ import (
"dbm-services/common/go-pubpkg/logger"
)

// EsbClient TODO
var EsbClient *cc.Client
// BkCmdbClient bk cmdb client
var BkCmdbClient *cc.Client

// GseClient TODO
var GseClient *cc.Client
// BkJobClient bk job client
var BkJobClient *cc.Client

// BkNodeManClient bk node man client
var BkNodeManClient *cc.Client

// CCModuleFields TODO
var CCModuleFields []string

// init TODO
func init() {
var err error
EsbClient, err = NewClient()
BkCmdbClient, err = NewClient(config.AppConfig.BkCmdbApiUrl)
if err != nil {
logger.Fatal("init cmdb client failed %s", err.Error())
return
}
GseClient, err = NewGseClient()
BkJobClient, err = NewClient(config.AppConfig.BkJobApiUrl)
if err != nil {
logger.Fatal("init bk job client failed %s", err.Error())
return
}
BkNodeManClient, err = NewClient(config.AppConfig.BkNodeManApiUrl)
if err != nil {
logger.Fatal("init gse client failed %s", err.Error())
logger.Fatal("init bk node man client failed %s", err.Error())
return
}
CCModuleFields = []string{
Expand Down Expand Up @@ -68,33 +75,15 @@ func init() {
}
}

// NewGseClient TODO
func NewGseClient() (*cc.Client, error) {
var apiserver string
apiserver = config.AppConfig.BkSecretConfig.GseBaseUrl
if cmutil.IsNotEmpty(os.Getenv("GSE_APIGW_DOMAIN")) {
apiserver = os.Getenv("GSE_APIGW_DOMAIN")
}
logger.Info("gse api url %s", apiserver)
return cc.NewClient(apiserver, cc.Secret{
BKAppCode: config.AppConfig.BkSecretConfig.BkAppCode,
BKAppSecret: config.AppConfig.BkSecretConfig.BKAppSecret,
BKUsername: config.AppConfig.BkSecretConfig.BkUserName,
})
}

var once sync.Once
var cli *cc.Client
var clierr error

// NewClient TODO
func NewClient() (*cc.Client, error) {
once.Do(func() {
cli, clierr = cc.NewClient(config.AppConfig.BkSecretConfig.BkBaseUrl, cc.Secret{
BKAppCode: config.AppConfig.BkSecretConfig.BkAppCode,
BKAppSecret: config.AppConfig.BkSecretConfig.BKAppSecret,
BKUsername: config.AppConfig.BkSecretConfig.BkUserName,
})
func NewClient(apiurl string) (*cc.Client, error) {
cli, clierr = cc.NewClient(apiurl, cc.Secret{
BKAppCode: config.AppConfig.BkSecretConfig.BkAppCode,
BKAppSecret: config.AppConfig.BkSecretConfig.BKAppSecret,
BKUsername: config.AppConfig.BkSecretConfig.BkUserName,
})
return cli, clierr
}
Expand All @@ -103,7 +92,7 @@ func NewClient() (*cc.Client, error) {
func BatchQueryHostsInfo(bizId int, allhosts []string) (ccHosts []*cc.Host, nofoundHosts []string, err error) {
for _, hosts := range cmutil.SplitGroup(allhosts, 200) {
err = cmutil.Retry(cmutil.RetryConfig{Times: 3, DelayTime: 1 * time.Second}, func() error {
data, resp, errx := cc.NewListBizHosts(EsbClient).QueryListBizHosts(&cc.ListBizHostsParam{
data, resp, errx := cc.NewListBizHostsGw(BkCmdbClient, strconv.Itoa(bizId)).QueryListBizHosts(&cc.ListBizHostsParam{
BkBizId: bizId,
Fileds: CCModuleFields,
Page: cc.BKPage{
Expand Down
8 changes: 5 additions & 3 deletions dbm-services/common/db-resource/internal/svr/bk/cc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bk_test

import (
"fmt"
"os"
"testing"

Expand All @@ -9,16 +10,17 @@ import (
)

func TestReserverCC(t *testing.T) {
client, err := cc.NewClient(os.Getenv("BK_COMPONENT_API_URL"), cc.Secret{
client, err := cc.NewClient(os.Getenv("BK_CMDB_API_URL"), cc.Secret{
BKAppCode: os.Getenv("BK_APP_CODE"),
BKAppSecret: os.Getenv("BK_APP_SECRET"),
BKUsername: os.Getenv("BK_USERNAME"),
})
fmt.Println(os.Getenv("BK_CMDB_API_URL"))
if err != nil {
t.Fatalf("new client failed %s", err.Error())
return
}
listBizHosts := cc.NewListBizHosts(client)
listBizHosts := cc.NewListBizHostsGw(client, "100443")

resp, _, err := listBizHosts.QueryListBizHosts(&cc.ListBizHostsParam{
BkBizId: 100443,
Expand All @@ -44,7 +46,7 @@ func TestReserverCC(t *testing.T) {
},
Page: cc.BKPage{
Start: 100,
Limit: 100,
Limit: 2,
},
})
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion dbm-services/common/db-resource/internal/svr/bk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func GetWindowsDiskInfo(hosts []IPList, bk_biz_id int) (ipFailedLogMap map[strin
func getDiskInfoBase(hosts []IPList, bk_biz_id int, param *FastExecuteScriptParam) (ipFailedLogMap map[string]string,
ipLogs BatchGetJobInstanceIpLogRpData, err error) {
jober := JobV3{
Client: EsbClient,
Client: BkJobClient,
}
job, err := jober.ExecuteJob(param)
if err != nil {
Expand All @@ -253,6 +253,7 @@ func getDiskInfoBase(hosts []IPList, bk_biz_id int, param *FastExecuteScriptPara
var jobStatus GetJobInstanceStatusRpData
for i := 0; i < 100; i++ {
jobStatus, err = jober.GetJobStatus(&GetJobInstanceStatusParam{
BkScopeType: "biz",
BKBizId: bk_biz_id,
JobInstanceID: job.JobInstanceID,
})
Expand All @@ -272,6 +273,7 @@ func getDiskInfoBase(hosts []IPList, bk_biz_id int, param *FastExecuteScriptPara
}
// 再查询一遍状态
jobStatus, err = jober.GetJobStatus(&GetJobInstanceStatusParam{
BkScopeType: "biz",
BKBizId: bk_biz_id,
JobInstanceID: job.JobInstanceID,
})
Expand Down
Loading