Skip to content

Commit

Permalink
Fix using localID in non-UUID format as vmRef value (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
haijianyang authored Jul 20, 2023
1 parent f93b69c commit e296203
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
17 changes: 6 additions & 11 deletions controllers/elfmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ func (r *ElfMachineReconciler) reconcileDelete(ctx *context.MachineContext) (rec
return reconcile.Result{}, nil
}

if vm.LocalID != nil && len(*vm.LocalID) > 0 {
ctx.ElfMachine.SetVM(*vm.LocalID)
} else {
ctx.ElfMachine.SetVM(*vm.ID)
}
ctx.ElfMachine.SetVM(util.GetVMRef(vm))
}

err := r.reconcileDeleteVM(ctx)
Expand Down Expand Up @@ -517,7 +513,7 @@ func (r *ElfMachineReconciler) reconcileVM(ctx *context.MachineContext) (*models
return nil, err
}

ctx.ElfMachine.SetVM(*vm.ID)
ctx.ElfMachine.SetVM(util.GetVMRef(vm))
} else {
ctx.Logger.Error(err, "failed to create VM",
"vmRef", ctx.ElfMachine.Status.VMRef, "taskRef", ctx.ElfMachine.Status.TaskRef)
Expand Down Expand Up @@ -559,16 +555,15 @@ func (r *ElfMachineReconciler) reconcileVM(ctx *context.MachineContext) (*models
ctx.Logger.V(1).Info(fmt.Sprintf("Updated VM hostServerName from %s to %s", hostName, *vm.Host.Name))
}

vmLocalID := service.GetTowerString(vm.LocalID)
// Before the ELF VM is created, Tower sets a "placeholder-{UUID}" format string to localId, such as "placeholder-7d8b6df1-c623-4750-a771-3ba6b46995fa".
// After the ELF VM is created, Tower sets the VM ID in UUID format to localId.
if !machineutil.IsUUID(vmLocalID) {
vmRef := util.GetVMRef(vm)
// If vmRef is in UUID format, it means that the ELF VM created.
if !machineutil.IsUUID(vmRef) {
return vm, nil
}

// When ELF VM created, set UUID to VMRef
if !machineutil.IsUUID(ctx.ElfMachine.Status.VMRef) {
ctx.ElfMachine.SetVM(vmLocalID)
ctx.ElfMachine.SetVM(vmRef)
}

// The VM was moved to the recycle bin. Treat the VM as deleted, and will not reconganize it even if it's moved back from the recycle bin.
Expand Down
3 changes: 2 additions & 1 deletion controllers/elfmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ var _ = Describe("ElfMachineReconciler", func() {
It("should recover from lost task", func() {
vm := fake.NewTowerVM()
vm.Name = &elfMachine.Name
vm.LocalID = pointer.String("placeholder-%s" + *vm.LocalID)
ctrlContext := newCtrlContexts(elfCluster, cluster, elfMachine, machine, secret, md)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)

Expand Down Expand Up @@ -1876,7 +1877,7 @@ var _ = Describe("ElfMachineReconciler", func() {

It("should delete the VM that in creating status and have not been saved to ElfMachine", func() {
vm := fake.NewTowerVM()
vm.LocalID = nil
vm.LocalID = pointer.String("placeholder-%s" + *vm.LocalID)
ctrlContext := newCtrlContexts(elfCluster, cluster, elfMachine, machine, secret, md)
fake.InitOwnerReferences(ctrlContext, elfCluster, cluster, elfMachine, machine)
mockVMService.EXPECT().GetByName(elfMachine.Name).Return(vm, nil)
Expand Down
42 changes: 42 additions & 0 deletions pkg/util/tower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package util

import (
"github.com/smartxworks/cloudtower-go-sdk/v2/models"

"github.com/smartxworks/cluster-api-provider-elf/pkg/service"
machineutil "github.com/smartxworks/cluster-api-provider-elf/pkg/util/machine"
)

// GetVMRef returns the ID or localID of the VM.
// If the localID is in UUID format, return the localID, otherwise return the ID.
//
// Before the ELF VM is created, Tower sets a "placeholder-{UUID}" format string to localID, such as "placeholder-7d8b6df1-c623-4750-a771-3ba6b46995fa".
// After the ELF VM is created, Tower sets the VM ID in UUID format to localID.
func GetVMRef(vm *models.VM) string {
if vm == nil {
return ""
}

vmLocalID := service.GetTowerString(vm.LocalID)
if machineutil.IsUUID(vmLocalID) {
return vmLocalID
}

return *vm.ID
}

0 comments on commit e296203

Please sign in to comment.