Skip to content

Commit

Permalink
stage save
Browse files Browse the repository at this point in the history
  • Loading branch information
ymakedaq committed Oct 25, 2024
1 parent 8d96e3c commit fa19025
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ func (c *ApplyHandler) ApplyBase(r *gin.Context, mode string) {
}()
}
defer func() {
apply.RollBackAllInstanceUnused(pickers)
if err != nil {
apply.RollBackAllInstanceUnused(pickers)
}
}()
pickers, err = apply.CycleApply(param)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ import (
// ImportMachParam 资源导入请求参数
type ImportMachParam struct {
// ForBizs 业务标签,表示这个资源将来给ForBizs这个业务使用
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels map[string]string `json:"labels"`
ForBiz int `json:"for_biz"`
RsType string `json:"resource_type"`
BkBizId int `json:"bk_biz_id" binding:"number"`
Hosts []HostBase `json:"hosts" binding:"gt=0,dive,required"`
Labels []string `json:"labels"`
apply.ActionInfo
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func (p *ImportMachParam) existCheck() (err error) {
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("%s", errMsg)
}
}
return nil
Expand Down Expand Up @@ -121,8 +121,7 @@ type ImportHostResp struct {
}

func (p ImportMachParam) transParamToBytes() (lableJson json.RawMessage, err error) {
// lableJson = []byte("{}")
lableJson, err = json.Marshal(cmutil.CleanStrMap(p.Labels))
lableJson, err = json.Marshal(p.Labels)
if err != nil {
logger.Error(fmt.Sprintf("ConverLableToJsonStr Failed,Error:%s", err.Error()))
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type MachineResourceGetterInputParam struct {
City []string `json:"city"`
SubZoneIds []string `json:"subzone_ids"`
DeviceClass []string `json:"device_class"`
Labels map[string]string `json:"labels"`
Labels []string `json:"labels"`
Hosts []string `json:"hosts"`
BkCloudIds []int `json:"bk_cloud_ids"`
RsType string `json:"resource_type"`
Expand Down Expand Up @@ -193,7 +193,9 @@ func (c *MachineResourceGetterInputParam) queryBs(db *gorm.DB) (err error) {
if len(c.SubZoneIds) > 0 {
db.Where(" sub_zone_id in (?) ", c.SubZoneIds)
}

if len(c.Labels) > 0 {
db.Where(model.JSONQuery("labels").JointOrContains(c.Labels))
}
if cmutil.IsNotEmpty(c.OsType) {
db.Where("os_type = ?", c.OsType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type SpecInfo struct {
GroupMark string `json:"group_mark" binding:"required" `
DeviceClass []string `json:"device_class"`
Spec meta.Spec `json:"spec"`
Labels []string `json:"labels"`
StorageSpecs []meta.DiskSpec `json:"storage_spec"`
}

Expand Down Expand Up @@ -93,6 +94,7 @@ func (m MachineResourceHandler) SpecSum(r *gin.Context) {
s.MatchStorage(db)
s.MatchSpec(db)
s.MatchLocationSpec(db)
s.MatchLables(db)
if err := db.Scan(&count).Error; err != nil {
logger.Error("query pre check count failed %s", err.Error())
m.SendResponse(r, errno.ErrDBQuery.AddErr(err), err.Error(), requestId)
Expand Down
61 changes: 39 additions & 22 deletions dbm-services/common/db-resource/internal/model/TbRpDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,48 @@ type TbRpDetail struct {
StorageDevice json.RawMessage `gorm:"column:storage_device;type:json;comment:'磁盘设备'" json:"storage_device"`
TotalStorageCap int `gorm:"column:total_storage_cap;type:int(11);comment:'磁盘总容量'" json:"total_storage_cap"`
Storages map[string]bk.DiskDetail `gorm:"-" json:"-"`
// 操作系统类型 Liunx,Windows
/*Linux(1) Windows(2) AIX(3) Unix(4) Solaris(5) FreeBSD(7)*/
OsType string `gorm:"column:os_type;type:varchar(32);not null;comment:'操作系统类型'" json:"os_type"` // 操作系统类型 Liunx,Windows
OsBit string `gorm:"column:os_bit;type:varchar(32);not null;comment:'操作系统位数'" json:"os_bit"`
OsVerion string `gorm:"column:os_version;type:varchar(64);not null;comment:'操作系统版本'" json:"os_version"` // 操作系统版本
OsName string `gorm:"column:os_name;type:varchar(64);not null;comment:'操作系统名称'" json:"os_name"` // 操作系统名称
Raid string `gorm:"column:raid;type:varchar(20);not null" json:"raid"` // 磁盘Raid
CityID string `gorm:"column:city_id;type:varchar(64);not null" json:"city_id"` // 实际城市ID
City string `gorm:"column:city;type:varchar(128);not null" json:"city"` // 实际城市名称
SubZone string `gorm:"column:sub_zone;type:varchar(32);not null" json:"sub_zone"` // 园区, 例如光明 cc_device_szone
SubZoneID string `gorm:"column:sub_zone_id;type:varchar(64);not null" json:"sub_zone_id"` // 园区ID cc_device_szone_id
RackID string `gorm:"column:rack_id;type:varchar(64);not null" json:"rack_id"` // 存放机架ID,判断是否是同机架
NetDeviceID string `gorm:"column:net_device_id;type:varchar(128)" json:"net_device_id"` // 网络设备ID, 判断是同交换机
Label json.RawMessage `gorm:"column:label;type:json" json:"label"` // 标签
LabelMap map[string]string `gorm:"-" json:"-"`
IsInit int `gorm:"column:is_init;type:int(11);comment:'是否初始化过'" json:"-"` // 是否初始化过
IsIdle int `gorm:"column:is_idle;type:int(11);comment:'是否空闲检查过'" json:"-"` // 是否空闲检查过
Status string `gorm:"column:status;type:varchar(20);not null" json:"status"` // Unused: 未使用 Used: 已经售卖被使用: Preselected:预占用
BkAgentId string `gorm:"index:idx_bk_agent_id;column:bk_agent_id;type:varchar(64);not null" json:"bk_agent_id"`
OsType string `gorm:"column:os_type;type:varchar(32);not null;comment:'操作系统类型'" json:"os_type"`
OsBit string `gorm:"column:os_bit;type:varchar(32);not null;comment:'操作系统位数'" json:"os_bit"`
// 操作系统版本
OsVerion string `gorm:"column:os_version;type:varchar(64);not null;comment:'操作系统版本'" json:"os_version"`
// 操作系统名称
OsName string `gorm:"column:os_name;type:varchar(64);not null;comment:'操作系统名称'" json:"os_name"`
// 磁盘Raid
Raid string `gorm:"column:raid;type:varchar(20);not null" json:"raid"`
// 实际城市ID
CityID string `gorm:"column:city_id;type:varchar(64);not null" json:"city_id"`
// 实际城市名称
City string `gorm:"column:city;type:varchar(128);not null" json:"city"`
// 园区, 例如光明 cc_device_szone
SubZone string `gorm:"column:sub_zone;type:varchar(32);not null" json:"sub_zone"`
// 园区ID cc_device_szone_id
SubZoneID string `gorm:"column:sub_zone_id;type:varchar(64);not null" json:"sub_zone_id"`
// 存放机架ID,判断是否是同机架
RackID string `gorm:"column:rack_id;type:varchar(64);not null" json:"rack_id"`
// 网络设备ID, 判断是同交换机
NetDeviceID string `gorm:"column:net_device_id;type:varchar(128)" json:"net_device_id"`
// 标签
Label json.RawMessage `gorm:"column:label;type:json" json:"label"`
LabelMap map[string]string `gorm:"-" json:"-"`
// 是否初始化过
IsInit int `gorm:"column:is_init;type:int(11);comment:'是否初始化过'" json:"-"`
// 是否空闲检查过
IsIdle int `gorm:"column:is_idle;type:int(11);comment:'是否空闲检查过'" json:"-"`
// Unused: 未使用 Used: 已经售卖被使用: Preselected:预占用
Status string `gorm:"column:status;type:varchar(20);not null" json:"status"`
BkAgentId string `gorm:"index:idx_bk_agent_id;column:bk_agent_id;type:varchar(64);not null" json:"bk_agent_id"`
// gse Agent当前运行状态码, -1:未知 0:初始安装 1:启动中 2:运行中 3:有损状态 4:繁忙状态 5:升级中 6:停止中 7:解除安装
AgentStatusCode int `gorm:"column:gse_agent_status_code;type:int(11);not null" json:"gse_agent_status_code"`
// agent status 最后一次更新时间
AgentStatusUpdateTime time.Time `gorm:"column:agent_status_update_time;type:timestamp;default:1970-01-01 08:00:01" json:"agent_status_update_time"`
ConsumeTime time.Time `gorm:"column:consume_time;type:timestamp;default:1970-01-01 08:00:01" json:"consume_time"` // 消费时间
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"update_time"` // 最后修改时间
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"create_time"` // 创建时间
// 消费时间
ConsumeTime time.Time `gorm:"column:consume_time;type:timestamp;default:1970-01-01 08:00:01" json:"consume_time"`
// 最后修改时间
UpdateTime time.Time `gorm:"column:update_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"update_time"`
// 创建时间
CreateTime time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP()" json:"create_time"`
// foreiginKey:关联表的结构字段 references:当前表的结构字段
// SubStorages []TbRpStorageItem `gorm:"foreignKey:BkHostID;references:BkHostID"`
}
Expand All @@ -109,12 +126,12 @@ func ConvertOsTypeToHuman(osType string) string {
}
}

// TableName TODO
// TableName table name
func (TbRpDetail) TableName() string {
return TbRpDetailName()
}

// TbRpDetailName TODO
// TbRpDetailName tbrp detail table name
func TbRpDetailName() string {
return "tb_rp_detail"
}
Expand Down
35 changes: 27 additions & 8 deletions dbm-services/common/db-resource/internal/svr/apply/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ type RequestInputParam struct {
ActionInfo
}

// BuildMessage build apply message
func (param RequestInputParam) BuildMessage() (msg string) {
var count int
groupMap := make(map[string]int)
groupCountMap := make(map[string]int)
for _, d := range param.Details {
groupMap[d.Affinity]++
groupCountMap[d.Affinity] += d.Count
count += d.Count
}
msg = fmt.Sprintf("此次申请分%d组申请%d个机器\n", len(param.Details), count)
for affinity, count := range groupMap {
msg += fmt.Sprintf("按照亲和性%s申请的资源分组%d,总共包含机器数量%d\n", affinity, count, groupCountMap[affinity])
}
return msg
}

// SortDetails 优先去匹配有明确需求的参数
func (param RequestInputParam) SortDetails() ([]ObjectDetail, error) {
if len(param.Details) == 1 {
Expand Down Expand Up @@ -186,22 +203,24 @@ const (
NONE = "NONE"
)

// ObjectDetail 资源申请对象详情
// 反亲和性 目前只有一种选项,当campus是空的时候,则此值生效
// SAME_SUBZONE_CROSS_SWTICH: 同城同subzone跨交换机跨机架、
// SAME_SUBZONE: 同城同subzone
// CROS_SUBZONE:同城跨subzone
// NONE: 无需亲和性处理
// ObjectDetail TODO
type ObjectDetail struct {
BkCloudId int `json:"bk_cloud_id"`
GroupMark string `json:"group_mark" binding:"required" ` // 资源组标记
Labels map[string]string `json:"labels"` // 标签
BkCloudId int `json:"bk_cloud_id"`
GroupMark string `json:"group_mark" binding:"required" ` // 资源组标记
Labels []string `json:"labels"` // 标签
// 通过机型规格 或者 资源规格描述来匹配资源
// 这两个条件是 || 关系
DeviceClass []string `json:"device_class"` // 机器类型 "IT5.8XLARGE128" "SA3.2XLARGE32"
Spec meta.Spec `json:"spec"` // 规格描述
StorageSpecs []meta.DiskSpec `json:"storage_spec"`
LocationSpec meta.LocationSpec `json:"location_spec"` // 地域区间
// 反亲和性 目前只有一种选项,当campus是空的时候,则此值生效
// SAME_SUBZONE_CROSS_SWTICH: 同城同subzone跨交换机跨机架、
// SAME_SUBZONE: 同城同subzone
// CROS_SUBZONE:同城跨subzone
// NONE: 无需亲和性处理

Affinity string `json:"affinity"`
// Windows,Linux
OsType string `json:"os_type"`
Expand Down
9 changes: 4 additions & 5 deletions dbm-services/common/db-resource/internal/svr/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,11 @@ func (o *SearchContext) PickInstance() (picker *PickerObject, err error) {
// MatchLables match lables
func (o *SearchContext) MatchLables(db *gorm.DB) {
if len(o.Labels) > 0 {
for key, v := range o.Labels {
db.Where(" ( json_contains(label,json_object(?,?) )", key, v)
}
return
db.Where(model.JSONQuery("labels").JointOrContains(o.Labels))
} else {
// 如果请求没有标签, 只能匹配没有标签的资源
db.Where(" JSON_TYPE(label) = 'NULL' OR JSON_LENGTH(label) < 1 ")
}
db.Where(" JSON_TYPE(label) = 'NULL' OR JSON_LENGTH(label) <= 1 ")
}

func matchNoMountPointStorage(spec []meta.DiskSpec, sinc map[string]bk.DiskDetail) bool {
Expand Down

0 comments on commit fa19025

Please sign in to comment.