Skip to content

Commit

Permalink
Merge pull request kubevirt#11065 from fossedihelm/vmclone_fix_misali…
Browse files Browse the repository at this point in the history
…gnment_source_vm

fix(vmclone): generate vm patches from vmsnapshotcontent vm
  • Loading branch information
kubevirt-bot authored Jan 27, 2024
2 parents 25e9df2 + 08a3699 commit d8d58c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/virt-controller/watch/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ func (ctrl *VMCloneController) syncSourceVMTargetVM(source *k6tv1.VirtualMachine
}

if vmClone.Status.RestoreName == nil {
syncInfo = ctrl.createRestoreFromVm(vmClone, source, snapshot.Name, syncInfo)
vm, err := ctrl.getVmFromSnapshot(snapshot)
if err != nil {
return addErrorToSyncInfo(syncInfo, fmt.Errorf("cannot get VM manifest from snapshot: %v", err))
}

syncInfo = ctrl.createRestoreFromVm(vmClone, vm, snapshot.Name, syncInfo)
return syncInfo
}

Expand Down
54 changes: 54 additions & 0 deletions pkg/virt-controller/watch/clone/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ var _ = Describe("Clone", func() {
Expect(err).ShouldNot(HaveOccurred())
}

addSnapshotContent := func(snapshotContent *snapshotv1alpha1.VirtualMachineSnapshotContent) {
err := snapshotContentInformer.GetStore().Add(snapshotContent)
Expect(err).ShouldNot(HaveOccurred())
}

addRestore := func(restore *snapshotv1alpha1.VirtualMachineRestore) {
err := restoreInformer.GetStore().Add(restore)
Expect(err).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -375,6 +380,7 @@ var _ = Describe("Clone", func() {

It("when snapshot is ready - should update status and create restore", func() {
snapshot := createVirtualMachineSnapshot(sourceVM)
snapshotContent := createVirtualMachineSnapshotContent(sourceVM)
snapshot.Status.ReadyToUse = pointer.Bool(true)

vmClone.Status.SnapshotName = pointer.String(snapshot.Name)
Expand All @@ -383,6 +389,7 @@ var _ = Describe("Clone", func() {
addVM(sourceVM)
addClone(vmClone)
addSnapshot(snapshot)
addSnapshotContent(snapshotContent)

expectCloneUpdate(clonev1alpha1.RestoreInProgress)
expectRestoreCreate(snapshot.Name, vmClone)
Expand Down Expand Up @@ -594,6 +601,7 @@ var _ = Describe("Clone", func() {
When("target VM already exists", func() {
It("should fire an event", func() {
snapshot := createVirtualMachineSnapshot(sourceVM)
snapshotContent := createVirtualMachineSnapshotContent(sourceVM)
snapshot.Status.ReadyToUse = pointer.Bool(true)

restore := createVirtualMachineRestore(sourceVM, snapshot.Name)
Expand All @@ -605,6 +613,7 @@ var _ = Describe("Clone", func() {
addVM(sourceVM)
addClone(vmClone)
addSnapshot(snapshot)
addSnapshotContent(snapshotContent)
addRestore(restore)
expectRestoreCreationFailure(snapshot.Name, vmClone, restore.Name)
expectCloneUpdate(clonev1alpha1.RestoreInProgress)
Expand Down Expand Up @@ -672,6 +681,8 @@ var _ = Describe("Clone", func() {

addVM(sourceVM)
addSnapshot(snapshot)
content := createVirtualMachineSnapshotContent(sourceVM)
addSnapshotContent(content)

// update to restore name is expected, although phase remains the same
expectCloneUpdate(clonev1alpha1.RestoreInProgress)
Expand Down Expand Up @@ -841,6 +852,7 @@ var _ = Describe("Clone", func() {
It("should delete smbios serial if serial is not provided", func() {
addClone(vmClone)
expectSMbiosSerial(emptySerial)
expectSnapshotContentGet(sourceVM)

controller.Execute()
})
Expand Down Expand Up @@ -941,6 +953,29 @@ var _ = Describe("Clone", func() {
controller.Execute()
})

It("should generate patches from the vmsnapshotcontent, instead of the current VM", func() {
if sourceVM.Annotations == nil {
sourceVM.Annotations = make(map[string]string)
}
// add annotation to create dis-alignment between vm and vmsnapshotcontent
sourceVM.Annotations["new_annotation_matching_filter"] = "ok"
vmClone.Spec.AnnotationFilters = []string{"somekey/*"}
addVM(sourceVM)
addClone(vmClone)

client.Fake.PrependReactor("create", restoreResource, func(action testing.Action) (handled bool, ret runtime.Object, err error) {
create, ok := action.(testing.CreateAction)
Expect(ok).To(BeTrue())

restore := create.GetObject().(*snapshotv1alpha1.VirtualMachineRestore)
Expect(restore.Spec.VirtualMachineSnapshotName).To(Equal(snapshotName))
Expect(restore.Spec.Patches).ToNot(ContainElement(`{"op": "remove", "path": "/metadata/annotations/new_annotation_matching_filter"}`))

return true, create.GetObject(), nil
})

controller.Execute()
})
})

Context("Firmware UUID", func() {
Expand Down Expand Up @@ -1005,6 +1040,25 @@ func createVirtualMachineSnapshot(vm *virtv1.VirtualMachine) *snapshotv1alpha1.V
}
}

func createVirtualMachineSnapshotContent(vm *virtv1.VirtualMachine) *snapshotv1alpha1.VirtualMachineSnapshotContent {
return &snapshotv1alpha1.VirtualMachineSnapshotContent{
ObjectMeta: metav1.ObjectMeta{
Name: "vmsnapshot-content-snapshot-UID",
Namespace: vm.Namespace,
UID: "vmsnapshot-UID",
},
Spec: snapshotv1alpha1.VirtualMachineSnapshotContentSpec{
Source: snapshotv1alpha1.SourceSpec{
VirtualMachine: &snapshotv1alpha1.VirtualMachine{
ObjectMeta: vm.ObjectMeta,
Spec: vm.Spec,
Status: vm.Status,
},
},
},
}
}

func createVirtualMachineRestore(vm *virtv1.VirtualMachine, snapshotName string) *snapshotv1alpha1.VirtualMachineRestore {
return &snapshotv1alpha1.VirtualMachineRestore{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit d8d58c1

Please sign in to comment.