From e296203310315305a26e318ce1e4cdc26eee8e4a Mon Sep 17 00:00:00 2001 From: "Weber.Yang" Date: Thu, 20 Jul 2023 14:31:44 +0800 Subject: [PATCH] Fix using localID in non-UUID format as vmRef value (#129) --- controllers/elfmachine_controller.go | 17 ++++----- controllers/elfmachine_controller_test.go | 3 +- pkg/util/tower.go | 42 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 pkg/util/tower.go diff --git a/controllers/elfmachine_controller.go b/controllers/elfmachine_controller.go index ff683f81..918beecd 100644 --- a/controllers/elfmachine_controller.go +++ b/controllers/elfmachine_controller.go @@ -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) @@ -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) @@ -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. diff --git a/controllers/elfmachine_controller_test.go b/controllers/elfmachine_controller_test.go index a3c9b6d9..f108ff14 100644 --- a/controllers/elfmachine_controller_test.go +++ b/controllers/elfmachine_controller_test.go @@ -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) @@ -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) diff --git a/pkg/util/tower.go b/pkg/util/tower.go new file mode 100644 index 00000000..33f86f5a --- /dev/null +++ b/pkg/util/tower.go @@ -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 +}