Skip to content

Commit

Permalink
client-go, VM: Add context to the Delete() method
Browse files Browse the repository at this point in the history
Currently, the `Delete()` method is not receiving a context,
thus is unstoppable from the outside.
Internally it uses `context.Background()` when calling
the underlying REST client.

Add the `context` parameter to the VM's Delete() method.
Add the `context.Background()` to all calls to VM's
`Delete()` method.

Signed-off-by: Orel Misan <[email protected]>
  • Loading branch information
orelmisan committed Feb 14, 2023
1 parent d6f1927 commit fd41937
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/virt-controller/watch/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (c *PoolController) scaleIn(pool *poolv1.VirtualMachinePool, vms []*virtv1.
vm := deleteList[idx]

foreGround := metav1.DeletePropagationForeground
err := c.clientset.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{PropagationPolicy: &foreGround})
err := c.clientset.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{PropagationPolicy: &foreGround})
if err != nil {
c.expectations.DeletionObserved(poolKey, controller.VirtualMachineKey(vm))
c.recorder.Eventf(pool, k8score.EventTypeWarning, FailedDeleteVirtualMachineReason, "Error deleting virtual machine %s: %v", vm.ObjectMeta.Name, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-controller/watch/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ var _ = Describe("Pool", func() {
})

// Check if only 10 are deleted
vmInterface.EXPECT().Delete(gomock.Any(), gomock.Any()).Times(10).Return(nil)
vmInterface.EXPECT().Delete(context.Background(), gomock.Any(), gomock.Any()).Times(10).Return(nil)

controller.Execute()

Expand Down Expand Up @@ -618,7 +618,7 @@ var _ = Describe("Pool", func() {
})

// Check if only 5 are deleted
vmInterface.EXPECT().Delete(gomock.Any(), gomock.Any()).Times(5).Return(nil)
vmInterface.EXPECT().Delete(context.Background(), gomock.Any(), gomock.Any()).Times(5).Return(nil)

controller.Execute()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1514,14 +1514,14 @@ func (_mr *_MockVirtualMachineInterfaceRecorder) Update(arg0, arg1 interface{})
return _mr.mock.ctrl.RecordCall(_mr.mock, "Update", arg0, arg1)
}

func (_m *MockVirtualMachineInterface) Delete(name string, options *v12.DeleteOptions) error {
ret := _m.ctrl.Call(_m, "Delete", name, options)
func (_m *MockVirtualMachineInterface) Delete(ctx context.Context, name string, options *v12.DeleteOptions) error {
ret := _m.ctrl.Call(_m, "Delete", ctx, name, options)
ret0, _ := ret[0].(error)
return ret0
}

func (_mr *_MockVirtualMachineInterfaceRecorder) Delete(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Delete", arg0, arg1)
func (_mr *_MockVirtualMachineInterfaceRecorder) Delete(arg0, arg1, arg2 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Delete", arg0, arg1, arg2)
}

func (_m *MockVirtualMachineInterface) Patch(name string, pt types.PatchType, data []byte, patchOptions *v12.PatchOptions, subresources ...string) (*v120.VirtualMachine, error) {
Expand Down
2 changes: 1 addition & 1 deletion staging/src/kubevirt.io/client-go/kubecli/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ type VirtualMachineInterface interface {
List(ctx context.Context, opts *metav1.ListOptions) (*v1.VirtualMachineList, error)
Create(ctx context.Context, vm *v1.VirtualMachine) (*v1.VirtualMachine, error)
Update(ctx context.Context, vm *v1.VirtualMachine) (*v1.VirtualMachine, error)
Delete(name string, options *metav1.DeleteOptions) error
Delete(ctx context.Context, name string, options *metav1.DeleteOptions) error
Patch(name string, pt types.PatchType, data []byte, patchOptions *metav1.PatchOptions, subresources ...string) (result *v1.VirtualMachine, err error)
UpdateStatus(*v1.VirtualMachine) (*v1.VirtualMachine, error)
PatchStatus(name string, pt types.PatchType, data []byte, patchOptions *metav1.PatchOptions) (result *v1.VirtualMachine, err error)
Expand Down
4 changes: 2 additions & 2 deletions staging/src/kubevirt.io/client-go/kubecli/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func (v *vm) Update(ctx context.Context, vm *v1.VirtualMachine) (*v1.VirtualMach
}

// Delete the defined VirtualMachine in the cluster in defined namespace
func (v *vm) Delete(name string, options *k8smetav1.DeleteOptions) error {
func (v *vm) Delete(ctx context.Context, name string, options *k8smetav1.DeleteOptions) error {
err := v.restClient.Delete().
Resource(v.resource).
Namespace(v.namespace).
Name(name).
Body(options).
Do(context.Background()).
Do(ctx).
Error()

return err
Expand Down
2 changes: 1 addition & 1 deletion staging/src/kubevirt.io/client-go/kubecli/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ var _ = Describe("Kubevirt VirtualMachine Client", func() {
ghttp.VerifyRequest("DELETE", path.Join(proxyPath, vmPath)),
ghttp.RespondWithJSONEncoded(http.StatusOK, nil),
))
err = client.VirtualMachine(k8sv1.NamespaceDefault).Delete("testvm", &k8smetav1.DeleteOptions{})
err = client.VirtualMachine(k8sv1.NamespaceDefault).Delete(context.Background(), "testvm", &k8smetav1.DeleteOptions{})

Expect(server.ReceivedRequests()).To(HaveLen(1))
Expect(err).ToNot(HaveOccurred())
Expand Down
4 changes: 2 additions & 2 deletions tests/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ var _ = Describe("[Serial][sig-compute]VirtualMachineClone Tests", Serial, decor
// of snapshot tests scope.

By("Deleting VM")
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(sourceVM.Name, &v1.DeleteOptions{})
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(context.Background(), sourceVM.Name, &v1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

By("Creating a clone with a snapshot source")
Expand Down Expand Up @@ -547,7 +547,7 @@ var _ = Describe("[Serial][sig-compute]VirtualMachineClone Tests", Serial, decor
snapshot = waitSnapshotReady(snapshot)

By("Deleting VM")
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(sourceVM.Name, &v1.DeleteOptions{})
err = virtClient.VirtualMachine(sourceVM.Namespace).Delete(context.Background(), sourceVM.Name, &v1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

By("Creating a clone and expecting error")
Expand Down
2 changes: 1 addition & 1 deletion tests/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var _ = Describe("[sig-compute]Dry-Run requests", decorators.SigCompute, func()
DryRun: []string{metav1.DryRunAll},
PropagationPolicy: &deletePolicy,
}
err = virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &opts)
err = virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &opts)
Expect(err).ToNot(HaveOccurred())

By("Check that no Virtual Machine was actually deleted")
Expand Down
2 changes: 1 addition & 1 deletion tests/instancetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ var _ = Describe("[crit:medium][vendor:[email protected]][level:component][sig-c
By("Stopping and removing VM")
vm = tests.StopVirtualMachine(vm)

err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{})
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

// Wait until ControllerRevision is deleted
Expand Down
2 changes: 1 addition & 1 deletion tests/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var _ = Describe("[sig-compute]oc/kubectl integration", decorators.SigCompute, f
})

AfterEach(func() {
virtCli.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{})
virtCli.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
})

DescribeTable("should verify set of columns for", func(verb, resource string, expectedHeader []string) {
Expand Down
2 changes: 1 addition & 1 deletion tests/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,7 @@ spec:
}

if vm != nil && vm.DeletionTimestamp == nil {
err = virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{})
err = virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
}
vm = nil
Expand Down
4 changes: 2 additions & 2 deletions tests/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ var _ = Describe("[sig-compute]VirtualMachinePool", decorators.SigCompute, func(

By("deleting a VM")
foreGround := metav1.DeletePropagationForeground
Expect(virtClient.VirtualMachine(newPool.ObjectMeta.Namespace).Delete(name, &k8smetav1.DeleteOptions{PropagationPolicy: &foreGround})).To(Succeed())
Expect(virtClient.VirtualMachine(newPool.ObjectMeta.Namespace).Delete(context.Background(), name, &k8smetav1.DeleteOptions{PropagationPolicy: &foreGround})).To(Succeed())

By("Waiting for deleted VM to be replaced")
Eventually(func() error {
Expand Down Expand Up @@ -338,7 +338,7 @@ var _ = Describe("[sig-compute]VirtualMachinePool", decorators.SigCompute, func(
name := vms.Items[1].Name

By("deleting a VM")
Expect(virtClient.VirtualMachine(newPool.ObjectMeta.Namespace).Delete(name, &k8smetav1.DeleteOptions{})).To(Succeed())
Expect(virtClient.VirtualMachine(newPool.ObjectMeta.Namespace).Delete(context.Background(), name, &k8smetav1.DeleteOptions{})).To(Succeed())

By("Waiting for deleted VM to be replaced")
Eventually(func() error {
Expand Down
6 changes: 3 additions & 3 deletions tests/storage/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ var _ = SIGDescribe("DataVolume Integration", func() {
Expect(err).ToNot(HaveOccurred())
Expect(domXml).ToNot(ContainSubstring("discard='unmap'"))
vm = tests.StopVirtualMachine(vm)
Expect(virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{})).To(Succeed())
Expect(virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})).To(Succeed())
})

DescribeTable("[test_id:3191]should be successfully started and stopped multiple times", func(isHTTP bool) {
Expand Down Expand Up @@ -737,7 +737,7 @@ var _ = SIGDescribe("DataVolume Integration", func() {
// Delete the VM with orphan Propagation
orphanPolicy := metav1.DeletePropagationOrphan
Expect(virtClient.VirtualMachine(vm.Namespace).
Delete(vm.Name, &metav1.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
Delete(context.Background(), vm.Name, &metav1.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())

// Wait for the owner reference to disappear
Eventually(ThisDVWith(vm.Namespace, vm.Spec.DataVolumeTemplates[0].Name), 100).Should(Not(BeOwned()))
Expand Down Expand Up @@ -799,7 +799,7 @@ var _ = SIGDescribe("DataVolume Integration", func() {
}

if createdVirtualMachine != nil {
err := virtClient.VirtualMachine(createdVirtualMachine.Namespace).Delete(createdVirtualMachine.Name, &metav1.DeleteOptions{})
err := virtClient.VirtualMachine(createdVirtualMachine.Namespace).Delete(context.Background(), createdVirtualMachine.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
}
})
Expand Down
4 changes: 2 additions & 2 deletions tests/storage/hotplug.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ var _ = SIGDescribe("Hotplug", func() {
}

deleteVirtualMachine := func(vm *v1.VirtualMachine) error {
return virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{})
return virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
}

getAddVolumeOptions := func(volumeName string, bus v1.DiskBus, volumeSource *v1.HotplugVolumeSource, dryRun bool, cache v1.DriverCache) *v1.AddVolumeOptions {
Expand Down Expand Up @@ -1091,7 +1091,7 @@ var _ = SIGDescribe("Hotplug", func() {

AfterEach(func() {
if vm != nil {
err := virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{})
err := virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
vm = nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/memorydump.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var _ = SIGDescribe("Memory dump", func() {

deleteVirtualMachine := func(vm *v1.VirtualMachine) {
waitDeleted(func() error {
return virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{})
return virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
})
vm = nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/storage/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ var _ = SIGDescribe("VirtualMachineRestore Tests", func() {
}

deleteVM := func(vm *v1.VirtualMachine) {
err := virtClient.VirtualMachine(vm.Namespace).Delete(vm.Name, &metav1.DeleteOptions{})
err := virtClient.VirtualMachine(vm.Namespace).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
if errors.IsNotFound(err) {
err = nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("[Serial][sig-compute]Templates", Serial, decorators.SigCompute
return func() {
if vm, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, &metav1.GetOptions{}); err == nil && vm.ObjectMeta.DeletionTimestamp == nil {
By("Deleting the VirtualMachine")
ExpectWithOffset(1, virtClient.VirtualMachine(util.NamespaceTestDefault).Delete(vmName, &metav1.DeleteOptions{})).To(Succeed(), "failed to delete VirtualMachine %q: %v", vmName, err)
ExpectWithOffset(1, virtClient.VirtualMachine(util.NamespaceTestDefault).Delete(context.Background(), vmName, &metav1.DeleteOptions{})).To(Succeed(), "failed to delete VirtualMachine %q: %v", vmName, err)
EventuallyWithOffset(1, func() bool {
obj, err := virtClient.VirtualMachine(util.NamespaceTestDefault).Get(context.Background(), vmName, &metav1.GetOptions{})
return k8serrors.IsNotFound(err) || obj.ObjectMeta.DeletionTimestamp != nil
Expand Down
8 changes: 4 additions & 4 deletions tests/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
defer libstorage.DeleteDataVolume(&dv)
newVM := createVirtualMachine(true, template)
// Delete it
Expect(virtClient.VirtualMachine(newVM.Namespace).Delete(newVM.Name, &k8smetav1.DeleteOptions{})).To(Succeed())
Expect(virtClient.VirtualMachine(newVM.Namespace).Delete(context.Background(), newVM.Name, &k8smetav1.DeleteOptions{})).To(Succeed())
// Wait until VMI is gone
Eventually(func() int {
vmis, err := virtClient.VirtualMachineInstance(newVM.Namespace).List(context.Background(), &k8smetav1.ListOptions{})
Expand All @@ -457,7 +457,7 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com
orphanPolicy := k8smetav1.DeletePropagationOrphan
By("Deleting VM")
Expect(virtClient.VirtualMachine(newVM.Namespace).
Delete(newVM.Name, &k8smetav1.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
Delete(context.Background(), newVM.Name, &k8smetav1.DeleteOptions{PropagationPolicy: &orphanPolicy})).To(Succeed())
// Wait until the virtual machine is deleted
By("Waiting for VM to delete")
Eventually(func() bool {
Expand Down Expand Up @@ -2381,7 +2381,7 @@ status:
Expect(err).ToNot(HaveOccurred())
}

err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{})
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

By("Ensure the vm has disappeared")
Expand Down Expand Up @@ -2412,7 +2412,7 @@ status:
g.Expect(controller.HasFinalizer(vm, v1.VirtualMachineControllerFinalizer)).To(BeTrue())
}, 2*time.Minute, 1*time.Second)

err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{})
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(context.Background(), vm.Name, &metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())

Eventually(func(g Gomega) {
Expand Down

0 comments on commit fd41937

Please sign in to comment.