From 96894f9fee990688bacaa69b241bfcc1ee56a84a Mon Sep 17 00:00:00 2001 From: Sai Diliyaer Date: Wed, 18 Oct 2023 16:30:25 -0400 Subject: [PATCH] Rebase with the latest main --- .../providers/vsphere2/constants/constants.go | 4 +- .../vsphere2/virtualmachine/backup.go | 52 +++++++++---------- .../vsphere2/virtualmachine/backup_test.go | 22 ++++---- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/pkg/vmprovider/providers/vsphere2/constants/constants.go b/pkg/vmprovider/providers/vsphere2/constants/constants.go index 7ab22f9e8..725b1ad0c 100644 --- a/pkg/vmprovider/providers/vsphere2/constants/constants.go +++ b/pkg/vmprovider/providers/vsphere2/constants/constants.go @@ -144,7 +144,7 @@ const ( // BackupVMDiskDataExtraConfigKey is the ExtraConfig key to the VM's disk info // data in JSON, compressed using gzip and base64-encoded. BackupVMDiskDataExtraConfigKey = "vmservice.virtualmachine.diskdata" - // BackupVMInstanceIDExtraConfigKey is the ExtraConfig key to the VM's + // BackupVMCloudInitInstanceIDExtraConfigKey is the ExtraConfig key to the VM's // Cloud-Init instance ID, compressed using gzip and base64-encoded. - BackupVMInstanceIDExtraConfigKey = "vmservice.virtualmachine.instanceid" + BackupVMCloudInitInstanceIDExtraConfigKey = "vmservice.virtualmachine.cloudinit.instanceid" ) diff --git a/pkg/vmprovider/providers/vsphere2/virtualmachine/backup.go b/pkg/vmprovider/providers/vsphere2/virtualmachine/backup.go index 9622b9ccd..bf40f2181 100644 --- a/pkg/vmprovider/providers/vsphere2/virtualmachine/backup.go +++ b/pkg/vmprovider/providers/vsphere2/virtualmachine/backup.go @@ -10,13 +10,13 @@ import ( "sigs.k8s.io/yaml" "github.com/vmware/govmomi/object" + "github.com/vmware/govmomi/vim25/mo" "github.com/vmware/govmomi/vim25/types" vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha2" "github.com/vmware-tanzu/vm-operator/pkg/context" "github.com/vmware-tanzu/vm-operator/pkg/util" "github.com/vmware-tanzu/vm-operator/pkg/vmprovider/providers/vsphere2/constants" - res "github.com/vmware-tanzu/vm-operator/pkg/vmprovider/providers/vsphere2/resources" ) type VMDiskData struct { @@ -35,10 +35,9 @@ func BackupVirtualMachine( vmCtx context.VirtualMachineContextA2, vcVM *object.VirtualMachine, bootstrapData map[string]string) error { - - resVM := res.NewVMFromObject(vcVM) - moVM, err := resVM.GetProperties(vmCtx, []string{"config", "runtime"}) - if err != nil { + var moVM mo.VirtualMachine + if err := vcVM.Properties(vmCtx, vcVM.Reference(), + []string{"config.extraConfig"}, &moVM); err != nil { vmCtx.Logger.Error(err, "Failed to get VM properties for backup") return err } @@ -46,7 +45,7 @@ func BackupVirtualMachine( var ecToUpdate []types.BaseOptionValue - vmKubeDataBackup, err := getVMKubeDataBackup(vmCtx.VM, curEcMap) + vmKubeDataBackup, err := getDesiredVMKubeDataForBackup(vmCtx.VM, curEcMap) if err != nil { vmCtx.Logger.Error(err, "Failed to get VM kube data for backup") return err @@ -60,21 +59,21 @@ func BackupVirtualMachine( }) } - instanceIDBackup, err := getInstanceIDBackup(vmCtx.VM, curEcMap) + instanceIDBackup, err := getDesiredCloudInitInstanceIDForBackup(vmCtx.VM, curEcMap) if err != nil { - vmCtx.Logger.Error(err, "Failed to get VM instance ID for backup") + vmCtx.Logger.Error(err, "Failed to get cloud-init instance ID for backup") return err } if instanceIDBackup == "" { - vmCtx.Logger.V(4).Info("Skipping VM instance ID backup as it exists") + vmCtx.Logger.V(4).Info("Skipping cloud-init instance ID as already stored") } else { ecToUpdate = append(ecToUpdate, &types.OptionValue{ - Key: constants.BackupVMInstanceIDExtraConfigKey, + Key: constants.BackupVMCloudInitInstanceIDExtraConfigKey, Value: instanceIDBackup, }) } - bootstrapDataBackup, err := getBootstrapDataBackup(bootstrapData, curEcMap) + bootstrapDataBackup, err := getDesiredBootstrapDataForBackup(bootstrapData, curEcMap) if err != nil { vmCtx.Logger.Error(err, "Failed to get VM bootstrap data for backup") return err @@ -88,7 +87,7 @@ func BackupVirtualMachine( }) } - diskDataBackup, err := getDiskDataBackup(vmCtx, resVM, curEcMap) + diskDataBackup, err := getDesiredDiskDataForBackup(vmCtx, vcVM, curEcMap) if err != nil { vmCtx.Logger.Error(err, "Failed to get VM disk data for backup") return err @@ -103,7 +102,8 @@ func BackupVirtualMachine( } if len(ecToUpdate) != 0 { - vmCtx.Logger.V(4).Info("Updating VM ExtraConfig with backup data") + vmCtx.Logger.Info("Updating VM ExtraConfig with backup data") + vmCtx.Logger.V(4).Info("", "ExtraConfig", ecToUpdate) if _, err := vcVM.Reconfigure(vmCtx, types.VirtualMachineConfigSpec{ ExtraConfig: ecToUpdate, }); err != nil { @@ -115,17 +115,17 @@ func BackupVirtualMachine( return nil } -func getVMKubeDataBackup( +func getDesiredVMKubeDataForBackup( vm *vmopv1.VirtualMachine, ecMap map[string]string) (string, error) { // If the ExtraConfig already contains the latest VM spec, determined by // 'metadata.generation', return an empty string to skip the backup. if ecKubeData, ok := ecMap[constants.BackupVMKubeDataExtraConfigKey]; ok { - curBackupVM, err := constructVMObj(ecKubeData) + vmFromBackup, err := constructVMObj(ecKubeData) if err != nil { return "", err } - if curBackupVM.ObjectMeta.Generation >= vm.ObjectMeta.Generation { + if vmFromBackup.ObjectMeta.Generation >= vm.ObjectMeta.Generation { return "", nil } } @@ -151,12 +151,12 @@ func constructVMObj(ecKubeData string) (vmopv1.VirtualMachine, error) { return vmObj, err } -func getInstanceIDBackup( +func getDesiredCloudInitInstanceIDForBackup( vm *vmopv1.VirtualMachine, ecMap map[string]string) (string, error) { - // Instance ID should not be changed once persisted in VM's ExtraConfig. - // Return an empty string to skip the backup if it already exists. - if _, ok := ecMap[constants.BackupVMInstanceIDExtraConfigKey]; ok { + // Cloud-Init instance ID should not be changed once persisted in VM's + // ExtraConfig. Return an empty string to skip the backup if it exists. + if _, ok := ecMap[constants.BackupVMCloudInitInstanceIDExtraConfigKey]; ok { return "", nil } @@ -168,7 +168,7 @@ func getInstanceIDBackup( return util.EncodeGzipBase64(instanceID) } -func getBootstrapDataBackup( +func getDesiredBootstrapDataForBackup( bootstrapDataRaw map[string]string, ecMap map[string]string) (string, error) { // No bootstrap data is specified, return an empty string to skip the backup. @@ -193,17 +193,17 @@ func getBootstrapDataBackup( return bootstrapDataBackup, nil } -func getDiskDataBackup( +func getDesiredDiskDataForBackup( ctx goctx.Context, - resVM *res.VirtualMachine, + vcVM *object.VirtualMachine, ecMap map[string]string) (string, error) { - disks, err := resVM.GetVirtualDisks(ctx) + deviceList, err := vcVM.Device(ctx) if err != nil { return "", err } - diskData := []VMDiskData{} - for _, device := range disks { + var diskData []VMDiskData + for _, device := range deviceList.SelectByType((*types.VirtualDisk)(nil)) { if disk, ok := device.(*types.VirtualDisk); ok { vmDiskData := VMDiskData{} if disk.VDiskId != nil { diff --git a/pkg/vmprovider/providers/vsphere2/virtualmachine/backup_test.go b/pkg/vmprovider/providers/vsphere2/virtualmachine/backup_test.go index f25a95f47..d26190ae3 100644 --- a/pkg/vmprovider/providers/vsphere2/virtualmachine/backup_test.go +++ b/pkg/vmprovider/providers/vsphere2/virtualmachine/backup_test.go @@ -148,19 +148,19 @@ func backupTests() { }) }) - Context("VM instance ID data", func() { + Context("VM cloud-init instance ID data", func() { BeforeEach(func() { vmCtx.VM = builder.DummyVirtualMachineA2() }) - When("VM instance ID already exists in ExtraConfig", func() { + When("VM cloud-init instance ID already exists in ExtraConfig", func() { BeforeEach(func() { _, err := vcVM.Reconfigure(vmCtx, types.VirtualMachineConfigSpec{ ExtraConfig: []types.BaseOptionValue{ &types.OptionValue{ - Key: constants.BackupVMInstanceIDExtraConfigKey, + Key: constants.BackupVMCloudInitInstanceIDExtraConfigKey, Value: "ec-instance-id", }, }, @@ -171,13 +171,13 @@ func backupTests() { } }) - It("Should skip backing up the instance ID", func() { + It("Should skip backing up the cloud-init instance ID", func() { Expect(virtualmachine.BackupVirtualMachine(vmCtx, vcVM, nil)).To(Succeed()) - verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMInstanceIDExtraConfigKey, "ec-instance-id") + verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMCloudInitInstanceIDExtraConfigKey, "ec-instance-id") }) }) - When("VM instance ID does not exist in ExtraConfig and is set in annotations", func() { + When("VM cloud-init instance ID does not exist in ExtraConfig and is set in annotations", func() { BeforeEach(func() { vmCtx.VM.Annotations = map[string]string{ @@ -186,22 +186,22 @@ func backupTests() { vmCtx.VM.UID = "vm-uid" }) - It("Should backup the instance ID from annotations", func() { + It("Should backup the cloud-init instance ID from annotations", func() { Expect(virtualmachine.BackupVirtualMachine(vmCtx, vcVM, nil)).To(Succeed()) - verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMInstanceIDExtraConfigKey, "annotation-instance-id") + verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMCloudInitInstanceIDExtraConfigKey, "annotation-instance-id") }) }) - When("VM instance ID does not exist in ExtraConfig and is not set in annotations", func() { + When("VM cloud-init instance ID does not exist in ExtraConfig and is not set in annotations", func() { BeforeEach(func() { vmCtx.VM.Annotations = nil vmCtx.VM.UID = "vm-uid" }) - It("Should backup the instance ID from annotations", func() { + It("Should backup the cloud-init instance ID from annotations", func() { Expect(virtualmachine.BackupVirtualMachine(vmCtx, vcVM, nil)).To(Succeed()) - verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMInstanceIDExtraConfigKey, "vm-uid") + verifyBackupDataInExtraConfig(ctx, vcVM, constants.BackupVMCloudInitInstanceIDExtraConfigKey, "vm-uid") }) }) })