Skip to content

Commit

Permalink
client-go, VMI: Add context to the List() method
Browse files Browse the repository at this point in the history
Currently, the `List()` 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 VMI's `List()` method.
Add the `context.Background()` to all calls to VMI's
`List()` method.

Signed-off-by: Orel Misan <[email protected]>
  • Loading branch information
orelmisan committed Jan 1, 2023
1 parent 5ed55fc commit ac33b1c
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 49 deletions.
3 changes: 2 additions & 1 deletion pkg/util/lookup/lookup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lookup

import (
"context"
"fmt"

v1 "k8s.io/api/core/v1"
Expand All @@ -16,7 +17,7 @@ func VirtualMachinesOnNode(cli kubecli.KubevirtClient, nodeName string) ([]*virt
if err != nil {
return nil, err
}
list, err := cli.VirtualMachineInstance(v1.NamespaceAll).List(&metav1.ListOptions{
list, err := cli.VirtualMachineInstance(v1.NamespaceAll).List(context.Background(), &metav1.ListOptions{
LabelSelector: labelSelector.String(),
})

Expand Down
8 changes: 5 additions & 3 deletions pkg/util/lookup/lookup_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lookup

import (
"context"

"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -42,7 +44,7 @@ var _ = Describe("Lookup", func() {
vmi2 := createVirtualMachineInstance("vmi2", "node01", virtv1.Failed)
vmis := []virtv1.VirtualMachineInstance{*vmi1, *vmi2}

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
Items: vmis,
}, nil)

Expand All @@ -56,7 +58,7 @@ var _ = Describe("Lookup", func() {
vmi2 := createVirtualMachineInstance("vmi2", "node01", virtv1.Failed)
vmis := []virtv1.VirtualMachineInstance{*vmi1, *vmi2}

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
Items: vmis,
}, nil)

Expand All @@ -69,7 +71,7 @@ var _ = Describe("Lookup", func() {
DescribeTable("should filter out nonactive vmis", func(phase virtv1.VirtualMachineInstancePhase) {
vmi := createVirtualMachineInstance("vmi2", "node01", phase)

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{
Items: []virtv1.VirtualMachineInstance{*vmi},
}, nil)

Expand Down
13 changes: 7 additions & 6 deletions pkg/virt-controller/watch/node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package watch

import (
"context"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -154,7 +155,7 @@ var _ = Describe("Node controller with", func() {
return true, nil, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{}, nil)

controller.Execute()
testutils.ExpectEvent(recorder, NodeUnresponsiveReason)
Expand Down Expand Up @@ -204,7 +205,7 @@ var _ = Describe("Node controller with", func() {
return true, &k8sv1.PodList{}, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().Patch(vmi.Name, types.JSONPatchType, gomock.Any(), &v1.PatchOptions{})

controller.Execute()
Expand Down Expand Up @@ -238,7 +239,7 @@ var _ = Describe("Node controller with", func() {
return true, &k8sv1.PodList{}, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().Patch(vmi.Name, types.JSONPatchType, gomock.Any(), &v1.PatchOptions{})

controller.Execute()
Expand All @@ -259,7 +260,7 @@ var _ = Describe("Node controller with", func() {
return true, &k8sv1.PodList{}, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().Patch(vmi.Name, types.JSONPatchType, gomock.Any(), &v1.PatchOptions{})

controller.Execute()
Expand All @@ -280,7 +281,7 @@ var _ = Describe("Node controller with", func() {
return true, &k8sv1.PodList{}, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().Patch(vmi.Name, types.JSONPatchType, gomock.Any(), &v1.PatchOptions{})

controller.Execute()
Expand All @@ -301,7 +302,7 @@ var _ = Describe("Node controller with", func() {
return true, &k8sv1.PodList{Items: []k8sv1.Pod{*NewUnhealthyPodForVirtualMachine("whatever", vmi)}}, nil
})

vmiInterface.EXPECT().List(gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&virtv1.VirtualMachineInstanceList{Items: []virtv1.VirtualMachineInstance{*vmi}}, nil)
vmiInterface.EXPECT().Patch(vmi.Name, types.JSONPatchType, gomock.Any(), &v1.PatchOptions{})

controller.Execute()
Expand Down
3 changes: 2 additions & 1 deletion pkg/virt-operator/webhooks/webhook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webhooks

import (
"context"
"fmt"

admissionv1 "k8s.io/api/admission/v1"
Expand Down Expand Up @@ -59,7 +60,7 @@ func (k *KubeVirtDeletionAdmitter) Admit(review *admissionv1.AdmissionReview) *a
return validating_webhooks.NewPassingAdmissionResponse()
}

vmis, err := k.client.VirtualMachineInstance(metav1.NamespaceAll).List(&metav1.ListOptions{Limit: 2})
vmis, err := k.client.VirtualMachineInstance(metav1.NamespaceAll).List(context.Background(), &metav1.ListOptions{Limit: 2})

if err != nil {
return webhookutils.ToAdmissionResponseError(err)
Expand Down
15 changes: 8 additions & 7 deletions pkg/virt-operator/webhooks/webhook_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webhooks

import (
"context"
"fmt"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -51,7 +52,7 @@ var _ = Describe("Webhook", func() {
})

It("should allow the deletion if no workload exists", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineList{}, nil)
vmirsInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceReplicaSetList{}, nil)

Expand All @@ -60,22 +61,22 @@ var _ = Describe("Webhook", func() {
})

It("should deny the deletion if a VMI exists", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{Items: []k6tv1.VirtualMachineInstance{{}}}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{Items: []k6tv1.VirtualMachineInstance{{}}}, nil)

response := admitter.Admit(&admissionv1.AdmissionReview{Request: &admissionv1.AdmissionRequest{Namespace: "test", Name: "kubevirt"}})
Expect(response.Allowed).To(BeFalse())
})

It("should deny the deletion if a VM exists", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineList{Items: []k6tv1.VirtualMachine{{}}}, nil)

response := admitter.Admit(&admissionv1.AdmissionReview{Request: &admissionv1.AdmissionRequest{Namespace: "test", Name: "kubevirt"}})
Expect(response.Allowed).To(BeFalse())
})

It("should deny the deletion if a VMIRS exists", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineList{}, nil)
vmirsInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceReplicaSetList{Items: []k6tv1.VirtualMachineInstanceReplicaSet{{}}}, nil)

Expand All @@ -84,22 +85,22 @@ var _ = Describe("Webhook", func() {
})

It("should deny the deletion if checking VMIs fails", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, fmt.Errorf("whatever"))
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, fmt.Errorf("whatever"))

response := admitter.Admit(&admissionv1.AdmissionReview{Request: &admissionv1.AdmissionRequest{Namespace: "test", Name: "kubevirt"}})
Expect(response.Allowed).To(BeFalse())
})

It("should deny the deletion if checking VMs fails", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineList{}, fmt.Errorf("whatever"))

response := admitter.Admit(&admissionv1.AdmissionReview{Request: &admissionv1.AdmissionRequest{Namespace: "test", Name: "kubevirt"}})
Expect(response.Allowed).To(BeFalse())
})

It("should deny the deletion if checking VMIRS fails", func() {
vmiInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmiInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&k6tv1.VirtualMachineInstanceList{}, nil)
vmInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineList{}, nil)
vmirsInterface.EXPECT().List(gomock.Any()).Return(&k6tv1.VirtualMachineInstanceReplicaSetList{}, fmt.Errorf("whatever"))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -35,7 +36,7 @@ func main() {
if err != nil {
log.Fatalf("cannot obtain KubeVirt vm list: %v\n", err)
}
vmiList, err := virtClient.VirtualMachineInstance(namespace).List(&k8smetav1.ListOptions{})
vmiList, err := virtClient.VirtualMachineInstance(namespace).List(context.Background(), &k8smetav1.ListOptions{})
if err != nil {
log.Fatalf("cannot obtain KubeVirt vmi list: %v\n", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,15 @@ func (_mr *_MockVirtualMachineInstanceInterfaceRecorder) Get(arg0, arg1, arg2 in
return _mr.mock.ctrl.RecordCall(_mr.mock, "Get", arg0, arg1, arg2)
}

func (_m *MockVirtualMachineInstanceInterface) List(opts *v12.ListOptions) (*v120.VirtualMachineInstanceList, error) {
ret := _m.ctrl.Call(_m, "List", opts)
func (_m *MockVirtualMachineInstanceInterface) List(ctx context.Context, opts *v12.ListOptions) (*v120.VirtualMachineInstanceList, error) {
ret := _m.ctrl.Call(_m, "List", ctx, opts)
ret0, _ := ret[0].(*v120.VirtualMachineInstanceList)
ret1, _ := ret[1].(error)
return ret0, ret1
}

func (_mr *_MockVirtualMachineInstanceInterfaceRecorder) List(arg0 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "List", arg0)
func (_mr *_MockVirtualMachineInstanceInterfaceRecorder) List(arg0, arg1 interface{}) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "List", arg0, arg1)
}

func (_m *MockVirtualMachineInstanceInterface) Create(instance *v120.VirtualMachineInstance) (*v120.VirtualMachineInstance, 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 @@ -230,7 +230,7 @@ type StreamInterface interface {

type VirtualMachineInstanceInterface interface {
Get(ctx context.Context, name string, options *metav1.GetOptions) (*v1.VirtualMachineInstance, error)
List(opts *metav1.ListOptions) (*v1.VirtualMachineInstanceList, error)
List(ctx context.Context, opts *metav1.ListOptions) (*v1.VirtualMachineInstanceList, error)
Create(instance *v1.VirtualMachineInstance) (*v1.VirtualMachineInstance, error)
Update(*v1.VirtualMachineInstance) (*v1.VirtualMachineInstance, error)
Delete(name string, options *metav1.DeleteOptions) error
Expand Down
4 changes: 2 additions & 2 deletions staging/src/kubevirt.io/client-go/kubecli/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ func (v *vmis) Get(ctx context.Context, name string, options *k8smetav1.GetOptio
return
}

func (v *vmis) List(options *k8smetav1.ListOptions) (vmiList *v1.VirtualMachineInstanceList, err error) {
func (v *vmis) List(ctx context.Context, options *k8smetav1.ListOptions) (vmiList *v1.VirtualMachineInstanceList, err error) {
vmiList = &v1.VirtualMachineInstanceList{}
err = v.restClient.Get().
Resource(v.resource).
Namespace(v.namespace).
VersionedParams(options, scheme.ParameterCodec).
Do(context.Background()).
Do(ctx).
Into(vmiList)
for _, vmi := range vmiList.Items {
vmi.SetGroupVersionKind(v1.VirtualMachineInstanceGroupVersionKind)
Expand Down
2 changes: 1 addition & 1 deletion staging/src/kubevirt.io/client-go/kubecli/vmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ = Describe("Kubevirt VirtualMachineInstance Client", func() {
ghttp.VerifyRequest("GET", path.Join(proxyPath, basePath)),
ghttp.RespondWithJSONEncoded(http.StatusOK, NewVMIList(*vmi)),
))
fetchedVMIList, err := client.VirtualMachineInstance(k8sv1.NamespaceDefault).List(&k8smetav1.ListOptions{})
fetchedVMIList, err := client.VirtualMachineInstance(k8sv1.NamespaceDefault).List(context.Background(), &k8smetav1.ListOptions{})

Expect(server.ReceivedRequests()).To(HaveLen(1))
Expect(err).ToNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/matcher/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func AllVMIs(namespace string) func() ([]virtv1.VirtualMachineInstance, error) {
if err != nil {
return nil, err
}
list, err := virtClient.VirtualMachineInstance(namespace).List(&k8smetav1.ListOptions{})
list, err := virtClient.VirtualMachineInstance(namespace).List(context.Background(), &k8smetav1.ListOptions{})
return list.Items, err
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/libreplicaset/replicaset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package libreplicaset

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -36,7 +37,7 @@ func DoScaleWithScaleSubresource(virtClient kubecli.KubevirtClient, name string,
return s.Status.Replicas
}, 90*time.Second, time.Second).Should(Equal(scale))

vmis, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).List(&v12.ListOptions{})
vmis, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).List(context.Background(), &v12.ListOptions{})
ExpectWithOffset(1, err).ToNot(HaveOccurred())
ExpectWithOffset(1, tests.NotDeleted(vmis)).To(HaveLen(int(scale)))
}
2 changes: 1 addition & 1 deletion tests/network/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ var _ = SIGDescribe("[rfe_id:253][crit:medium][vendor:[email protected]][level:c
// TODO: add label to list options
// check size of list
// remove check for owner
vms, err := virtClient.VirtualMachineInstance(vmrs.ObjectMeta.Namespace).List(&k8smetav1.ListOptions{})
vms, err := virtClient.VirtualMachineInstance(vmrs.ObjectMeta.Namespace).List(context.Background(), &k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, vm := range vms.Items {
if vm.OwnerReferences != nil {
Expand Down
4 changes: 2 additions & 2 deletions tests/network/vmi_multus.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ var _ = SIGDescribe("[Serial]Multus", Serial, func() {

// Multus tests need to ensure that old VMIs are gone
Eventually(func() []v1.VirtualMachineInstance {
list1, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).List(&v13.ListOptions{})
list1, err := virtClient.VirtualMachineInstance(testsuite.GetTestNamespace(nil)).List(context.Background(), &v13.ListOptions{})
Expect(err).ToNot(HaveOccurred())
list2, err := virtClient.VirtualMachineInstance(testsuite.NamespaceTestAlternative).List(&v13.ListOptions{})
list2, err := virtClient.VirtualMachineInstance(testsuite.NamespaceTestAlternative).List(context.Background(), &v13.ListOptions{})
Expect(err).ToNot(HaveOccurred())
return append(list1.Items, list2.Items...)
}, 6*time.Minute, 1*time.Second).Should(BeEmpty())
Expand Down
2 changes: 1 addition & 1 deletion tests/performance/density.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func createVMISpecWithResources() *kvv1.VirtualMachineInstance {

func waitRunningVMI(virtClient kubecli.KubevirtClient, vmiCount int, timeout time.Duration) {
Eventually(func() int {
vmis, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).List(&metav1.ListOptions{})
vmis, err := virtClient.VirtualMachineInstance(util.NamespaceTestDefault).List(context.Background(), &metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
running := 0
for _, vmi := range vmis.Items {
Expand Down
2 changes: 1 addition & 1 deletion tests/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("[sig-compute]VirtualMachinePool", func() {

waitForVMIs := func(namespace string, expectedCount int) {
Eventually(func() error {
vmis, err := virtClient.VirtualMachineInstance(namespace).List(&v12.ListOptions{})
vmis, err := virtClient.VirtualMachineInstance(namespace).List(context.Background(), &v12.ListOptions{})
Expect(err).ToNot(HaveOccurred())
if len(vmis.Items) != expectedCount {
return fmt.Errorf("Only %d vmis exist, expected %d", len(vmis.Items), expectedCount)
Expand Down
Loading

0 comments on commit ac33b1c

Please sign in to comment.