Skip to content

Commit

Permalink
[CONTROLLER/RECORDER] supports updating vif vm
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengya committed May 9, 2024
1 parent b0115a5 commit 8801422
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/controller/recorder/cache/diff_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model"
"github.com/deepflowio/deepflow/server/controller/common"
"github.com/deepflowio/deepflow/server/controller/db/mysql"
. "github.com/deepflowio/deepflow/server/controller/recorder/common"
)
Expand Down Expand Up @@ -348,6 +349,10 @@ func (b *DiffBaseDataSet) addVInterface(dbItem *mysql.VInterface, seq int, toolD
if dbItem.NetworkID != 0 {
networkLcuuid, _ = toolDataSet.GetNetworkLcuuidByID(dbItem.NetworkID)
}
var deviceLcuuid string
if dbItem.DeviceType == common.VIF_DEVICE_TYPE_VM {
deviceLcuuid, _ = toolDataSet.GetVMLcuuidByID(dbItem.DeviceID)
}
b.VInterfaces[dbItem.Lcuuid] = &VInterface{
DiffBase: DiffBase{
Sequence: seq,
Expand All @@ -356,6 +361,7 @@ func (b *DiffBaseDataSet) addVInterface(dbItem *mysql.VInterface, seq int, toolD
Name: dbItem.Name,
Type: dbItem.Type,
TapMac: dbItem.TapMac,
DeviceLcuuid: deviceLcuuid,
NetworkLcuuid: networkLcuuid,
RegionLcuuid: dbItem.Region,
SubDomainLcuuid: dbItem.SubDomain,
Expand Down Expand Up @@ -1179,6 +1185,7 @@ type VInterface struct {
Name string `json:"name"`
Type int `json:"type"`
TapMac string `json:"tap_mac"`
DeviceLcuuid string `json:"device_lcuuid"`
NetworkLcuuid string `json:"network_lcuuid"`
RegionLcuuid string `json:"region_lcuuid"`
SubDomainLcuuid string `json:"sub_domain_lcuuid"`
Expand All @@ -1188,6 +1195,7 @@ func (v *VInterface) Update(cloudItem *cloudmodel.VInterface) {
v.Name = cloudItem.Name
v.Type = cloudItem.Type
v.TapMac = cloudItem.TapMac
v.DeviceLcuuid = cloudItem.DeviceLcuuid
v.NetworkLcuuid = cloudItem.NetworkLcuuid
v.RegionLcuuid = cloudItem.RegionLcuuid
log.Info(updateDiffBase(RESOURCE_TYPE_VINTERFACE_EN, v))
Expand Down
21 changes: 21 additions & 0 deletions server/controller/recorder/cache/tool_data_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ToolDataSet struct {
hostLcuuidToID map[string]int

vmLcuuidToID map[string]int
vmIDToLcuuid map[int]string

vpcLcuuidToID map[string]int
vpcIDToLcuuid map[int]string
Expand Down Expand Up @@ -121,6 +122,7 @@ func NewToolDataSet() ToolDataSet {
hostLcuuidToID: make(map[string]int),

vmLcuuidToID: make(map[string]int),
vmIDToLcuuid: make(map[int]string),

vpcLcuuidToID: make(map[string]int),
vpcIDToLcuuid: make(map[int]string),
Expand Down Expand Up @@ -268,6 +270,7 @@ func (t *ToolDataSet) updateHost(cloudItem *cloudmodel.Host) {

func (t *ToolDataSet) addVM(item *mysql.VM) {
t.vmLcuuidToID[item.Lcuuid] = item.ID
t.vmIDToLcuuid[item.ID] = item.Lcuuid
t.vmIDToInfo[item.ID] = &vmInfo{
Name: item.Name,
VPCID: item.VPCID,
Expand Down Expand Up @@ -312,6 +315,7 @@ func (t *ToolDataSet) updateVM(cloudItem *cloudmodel.VM) {

func (t *ToolDataSet) deleteVM(lcuuid string) {
id, _ := t.GetVMIDByLcuuid(lcuuid)
delete(t.vmIDToLcuuid, id)
delete(t.vmIDToIPNetworkIDMap, id)
delete(t.vmLcuuidToID, lcuuid)
delete(t.vmIDToInfo, id)
Expand Down Expand Up @@ -1116,6 +1120,23 @@ func (t *ToolDataSet) GetVPCLcuuidByID(id int) (string, bool) {
}
}

func (t *ToolDataSet) GetVMLcuuidByID(id int) (string, bool) {
lcuuid, exists := t.vmIDToLcuuid[id]
if exists {
return lcuuid, true
}
log.Warning(cacheLcuuidByIDNotFound(RESOURCE_TYPE_VM_EN, id))
var vm mysql.VM
result := mysql.Db.Where("lcuuid = ?", id).Find(&vm)
if result.RowsAffected == 1 {
t.addVM(&vm)
return vm.Lcuuid, true
} else {
log.Error(dbResourceByIDNotFound(RESOURCE_TYPE_VM_EN, id))
return lcuuid, false
}
}

func (t *ToolDataSet) GetNetworkIDByLcuuid(lcuuid string) (int, bool) {
if lcuuid == PUBLIC_NETWORK_LCUUID {
return t.publicNetworkID, true
Expand Down
19 changes: 19 additions & 0 deletions server/controller/recorder/updater/vinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

cloudmodel "github.com/deepflowio/deepflow/server/controller/cloud/model"
ctrlrcommon "github.com/deepflowio/deepflow/server/controller/common"
"github.com/deepflowio/deepflow/server/controller/db/mysql"
"github.com/deepflowio/deepflow/server/controller/recorder/cache"
"github.com/deepflowio/deepflow/server/controller/recorder/common"
Expand Down Expand Up @@ -118,6 +119,24 @@ func (i *VInterface) generateUpdateInfo(diffBase *cache.VInterface, cloudItem *c
updateInfo["subnetid"] = networkID
}
}
if cloudItem.DeviceType == ctrlrcommon.VIF_DEVICE_TYPE_VM {
if diffBase.DeviceLcuuid != cloudItem.DeviceLcuuid {
vmID, exists := i.cache.ToolDataSet.GetVMIDByLcuuid(cloudItem.DeviceLcuuid)
if !exists {
if i.domainToolDataSet != nil {
vmID, exists = i.domainToolDataSet.GetVMIDByLcuuid(cloudItem.DeviceLcuuid)
}
if !exists {
log.Errorf(resourceAForResourceBNotFound(
common.RESOURCE_TYPE_VM_EN, cloudItem.DeviceLcuuid,
common.RESOURCE_TYPE_VINTERFACE_EN, cloudItem.Lcuuid,
))
return nil, false
}
}
updateInfo["deviceid"] = vmID
}
}
if diffBase.Name != cloudItem.Name {
updateInfo["name"] = cloudItem.Name
}
Expand Down

0 comments on commit 8801422

Please sign in to comment.