Skip to content

Commit

Permalink
Fix adoption of orphan DataVolumes
Browse files Browse the repository at this point in the history
Signed-off-by: Zvi Cahana <zvic@il.ibm.com>
  • Loading branch information
zcahana committed May 5, 2021
1 parent 1b2accc commit beb92a0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/controller/controller_ref_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func (m *VirtualMachineControllerRefManager) AdoptDataVolume(dataVolume *cdiv1.D
`{"metadata":{"ownerReferences":[{"apiVersion":"%s","kind":"%s","name":"%s","uid":"%s","controller":true,"blockOwnerDeletion":true}],"uid":"%s"}}`,
m.controllerKind.GroupVersion(), m.controllerKind.Kind,
m.Controller.GetName(), m.Controller.GetUID(), dataVolume.UID)
return m.virtualMachineControl.PatchVirtualMachine(dataVolume.Namespace, dataVolume.Name, []byte(addControllerPatch))
return m.virtualMachineControl.PatchDataVolume(dataVolume.Namespace, dataVolume.Name, []byte(addControllerPatch))
}

// ReleaseDataVolume sends a patch to free the dataVolume from the control of the controller.
Expand Down
43 changes: 43 additions & 0 deletions pkg/virt-controller/watch/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,49 @@ var _ = Describe("VirtualMachine", func() {
controller.Execute()
})

It("should detect that a DataVolume already exists and adopt it", func() {
vm, _ := DefaultVirtualMachine(false)
vm.Spec.Template.Spec.Volumes = append(vm.Spec.Template.Spec.Volumes, v1.Volume{
Name: "test1",
VolumeSource: v1.VolumeSource{
DataVolume: &v1.DataVolumeSource{
Name: "dv1",
},
},
})

vm.Spec.DataVolumeTemplates = append(vm.Spec.DataVolumeTemplates, v1.DataVolumeTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: "dv1",
Namespace: vm.Namespace,
},
})

addVirtualMachine(vm)

dv := createDataVolumeManifest(&vm.Spec.DataVolumeTemplates[0], vm)
dv.Status.Phase = cdiv1.Succeeded

orphanDV := dv.DeepCopy()
orphanDV.ObjectMeta.OwnerReferences = nil
dataVolumeInformer.GetStore().Add(orphanDV)

cdiClient.Fake.PrependReactor("patch", "datavolumes", func(action testing.Action) (handled bool, obj runtime.Object, err error) {
patch, ok := action.(testing.PatchAction)
Expect(ok).To(BeTrue())
Expect(patch.GetName()).To(Equal(dv.Name))
Expect(patch.GetNamespace()).To(Equal(dv.Namespace))
Expect(string(patch.GetPatch())).To(ContainSubstring(string(vm.UID)))
Expect(string(patch.GetPatch())).To(ContainSubstring("ownerReferences"))
return true, dv, nil
})

vmInterface.EXPECT().Get(vm.ObjectMeta.Name, gomock.Any()).Return(vm, nil)
vmInterface.EXPECT().UpdateStatus(gomock.Any()).Return(vm, nil)

controller.Execute()
})

It("should detect that it has nothing to do beside updating the status", func() {
vm, vmi := DefaultVirtualMachine(true)

Expand Down

0 comments on commit beb92a0

Please sign in to comment.