fix vcenter update task to avoid re-adding update records for previous updates
This commit is contained in:
@@ -115,18 +115,23 @@ func (c *CronTask) RunVcenterPoll(ctx context.Context, logger *slog.Logger) erro
|
||||
// UpdateVmInventory will compare database against current vcenter values, and create update record if not matching
|
||||
func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcenter, ctx context.Context, dbVm queries.Inventory) error {
|
||||
var (
|
||||
err error
|
||||
numVcpus int32
|
||||
numRam int32
|
||||
srmPlaceholder string
|
||||
updateType string
|
||||
rpName string
|
||||
err error
|
||||
numVcpus int32
|
||||
numRam int32
|
||||
srmPlaceholder string
|
||||
updateType string
|
||||
rpName string
|
||||
existingUpdateFound bool
|
||||
)
|
||||
|
||||
// TODO - how to prevent creating a new record every polling cycle?
|
||||
|
||||
params := queries.CreateUpdateParams{
|
||||
InventoryId: sql.NullInt64{Int64: dbVm.Iid, Valid: dbVm.Iid > 0},
|
||||
}
|
||||
srmPlaceholder = "FALSE" // default value
|
||||
updateType = "unknown" // default value
|
||||
srmPlaceholder = "FALSE" // default value
|
||||
updateType = "unknown" // default value
|
||||
existingUpdateFound = false // default value
|
||||
numRam = vmObj.Config.Hardware.MemoryMB
|
||||
numVcpus = vmObj.Config.Hardware.NumCPU
|
||||
|
||||
@@ -143,7 +148,7 @@ func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcent
|
||||
// Determine if the VM is a normal VM or an SRM placeholder
|
||||
if vmObj.Config.ManagedBy != nil && vmObj.Config.ManagedBy.ExtensionKey == "com.vmware.vcDr" {
|
||||
if vmObj.Config.ManagedBy.Type == "placeholderVm" {
|
||||
//c.Logger.Debug("VM is a placeholder")
|
||||
c.Logger.Debug("VM is a placeholder")
|
||||
srmPlaceholder = "TRUE"
|
||||
} else {
|
||||
//c.Logger.Debug("VM is managed by SRM but not a placeholder", "details", vmObj.Config.ManagedBy)
|
||||
@@ -151,6 +156,7 @@ func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcent
|
||||
}
|
||||
|
||||
if srmPlaceholder != dbVm.SrmPlaceholder {
|
||||
c.Logger.Debug("VM has changed placeholder type", "db_value", dbVm.SrmPlaceholder, "current_Value", srmPlaceholder)
|
||||
params.PlaceholderChange = sql.NullString{String: srmPlaceholder, Valid: srmPlaceholder != ""}
|
||||
if updateType == "unknown" {
|
||||
updateType = "srm"
|
||||
@@ -162,6 +168,7 @@ func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcent
|
||||
c.Logger.Error("Unable to determine resource pool name", "error", err)
|
||||
}
|
||||
if rpName != dbVm.ResourcePool.String {
|
||||
c.Logger.Debug("VM has changed resource pool", "db_value", dbVm.ResourcePool.String, "current_Value", rpName)
|
||||
params.NewResourcePool = sql.NullString{String: rpName, Valid: rpName != ""}
|
||||
if updateType == "unknown" {
|
||||
updateType = "move"
|
||||
@@ -171,18 +178,64 @@ func (c *CronTask) UpdateVmInventory(vmObj *mo.VirtualMachine, vc *vcenter.Vcent
|
||||
// TODO - should we bother to check if disk space has changed?
|
||||
|
||||
if updateType != "unknown" {
|
||||
c.Logger.Debug("Detected change in VM, inserting update record into database", "update_type", updateType)
|
||||
params.UpdateType = updateType
|
||||
|
||||
result, err := c.Database.Queries().CreateUpdate(ctx, params)
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed creating database record", "error", err)
|
||||
return err
|
||||
// TODO query updates table to see if there is already an update of this type and the new value
|
||||
|
||||
checkParams := queries.GetVmUpdatesParams{
|
||||
InventoryId: sql.NullInt64{Int64: dbVm.Iid, Valid: dbVm.Iid > 0},
|
||||
UpdateType: updateType,
|
||||
}
|
||||
|
||||
existingUpdates, err := c.Database.Queries().GetVmUpdates(ctx, checkParams)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.Logger.Debug("No update records found")
|
||||
} else {
|
||||
c.Logger.Error("Unbale to query database for vm update records", "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, u := range existingUpdates {
|
||||
// check if we already recorded this same update
|
||||
if u.UpdateType == updateType {
|
||||
switch u.UpdateType {
|
||||
case "srm":
|
||||
if u.PlaceholderChange.String == srmPlaceholder {
|
||||
c.Logger.Debug("SRM update already exists for vm", "update_value", u.PlaceholderChange.String, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
||||
existingUpdateFound = true
|
||||
}
|
||||
case "move":
|
||||
if u.NewResourcePool.String == rpName {
|
||||
c.Logger.Debug("Resource pool update already exists for vm", "update_value", u.NewResourcePool.String, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
||||
existingUpdateFound = true
|
||||
}
|
||||
case "reconfigure":
|
||||
if u.NewRam.Int64 == int64(numRam) || u.NewVcpus.Int64 == int64(numVcpus) {
|
||||
c.Logger.Debug("RAM/vCPU update already exists for vm", "update_ram", u.NewRam.Int64, "update_vcpu", u.NewVcpus.Int64, "inventory_id", u.InventoryId.Int64, "vm_name", u.Name.String)
|
||||
existingUpdateFound = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !existingUpdateFound {
|
||||
params.UpdateType = updateType
|
||||
updateTime := time.Now().Unix()
|
||||
params.UpdateTime = sql.NullInt64{Int64: updateTime, Valid: updateTime > 0}
|
||||
c.Logger.Debug("Detected new change in VM, inserting update record into database", "update_type", updateType, "params", params)
|
||||
|
||||
result, err := c.Database.Queries().CreateUpdate(ctx, params)
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed creating database record", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
c.Logger.Debug("created database record", "insert_result", result)
|
||||
// add sleep to slow down mass VM additions
|
||||
utils.SleepWithContext(ctx, (10 * time.Millisecond))
|
||||
}
|
||||
|
||||
c.Logger.Debug("created database record", "insert_result", result)
|
||||
// add sleep to slow down mass VM additions
|
||||
utils.SleepWithContext(ctx, (10 * time.Millisecond))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user