Skip to content

Commit

Permalink
Rebase with the latest main
Browse files Browse the repository at this point in the history
  • Loading branch information
dilyar85 committed Oct 18, 2023
1 parent 6e1d3f9 commit 96894f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions pkg/vmprovider/providers/vsphere2/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
52 changes: 26 additions & 26 deletions pkg/vmprovider/providers/vsphere2/virtualmachine/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -35,18 +35,17 @@ 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
}
curEcMap := util.ExtraConfigToMap(moVM.Config.ExtraConfig)

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions pkg/vmprovider/providers/vsphere2/virtualmachine/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
Expand All @@ -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{
Expand All @@ -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")
})
})
})
Expand Down

0 comments on commit 96894f9

Please sign in to comment.