Skip to content

Commit

Permalink
Merge pull request #11639 from fossedihelm/embed-generated-vmim
Browse files Browse the repository at this point in the history
Generated client: embed generated methods into VMIM interface
  • Loading branch information
kubevirt-bot committed Apr 11, 2024
2 parents 9426199 + 108d5de commit fa25a88
Show file tree
Hide file tree
Showing 31 changed files with 226 additions and 222 deletions.
4 changes: 2 additions & 2 deletions pkg/util/status/status.go
Expand Up @@ -184,7 +184,7 @@ func (u *updater) updateUnstructured(obj runtime.Object) (oldStatus interface{},
return oldObj.Status, newObj.Status, nil
case *v1.VirtualMachineInstanceMigration:
oldObj := obj.(*v1.VirtualMachineInstanceMigration)
newObj, err := u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).Update(oldObj)
newObj, err := u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).Update(context.Background(), oldObj, metav1.UpdateOptions{})
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (u *updater) updateStatusUnstructured(obj runtime.Object) (err error) {
_, err = u.cli.ReplicaSet(a.GetNamespace()).UpdateStatus(oldObj)
case *v1.VirtualMachineInstanceMigration:
oldObj := obj.(*v1.VirtualMachineInstanceMigration)
_, err = u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).UpdateStatus(oldObj)
_, err = u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).UpdateStatus(context.Background(), oldObj, metav1.UpdateOptions{})
case *v1.KubeVirt:
oldObj := obj.(*v1.KubeVirt)
_, err = u.cli.KubeVirt(a.GetNamespace()).UpdateStatus(oldObj)
Expand Down
43 changes: 21 additions & 22 deletions pkg/util/status/status_test.go
Expand Up @@ -9,7 +9,6 @@ import (
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -92,19 +91,19 @@ var _ = Describe("Status", func() {
Context("for PATCH operations", func() {
It("should continuously use the /status subresource if no errors occur", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := v12.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
})

It("should fall back on a 404 error on the /status subresource to an ordinary update", func() {
updater := NewVMStatusUpdater(virtClient)
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := v12.PatchOptions{}
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expand All @@ -113,8 +112,8 @@ var _ = Describe("Status", func() {

It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that objects may have disappeared", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := v12.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expand All @@ -124,8 +123,8 @@ var _ = Describe("Status", func() {

It("should fall back on a 404 error on the /status subresource to an ordinary update but keep in mind that the subresource may get enabled directly afterwards", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := v12.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(1)
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expand All @@ -135,8 +134,8 @@ var _ = Describe("Status", func() {

It("should stick with /status if an arbitrary error occurs", func() {
updater := NewVMStatusUpdater(virtClient)
patchOptions := v12.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
patchOptions := metav1.PatchOptions{}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, fmt.Errorf("I am not a 404 error")).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(vm, nil).Times(1)
Expand Down Expand Up @@ -193,10 +192,10 @@ var _ = Describe("Status", func() {
It("should stick with a normal update if the resource version did change", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := v12.PatchOptions{}
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expand All @@ -205,10 +204,10 @@ var _ = Describe("Status", func() {
It("should stick with a normal update if we get a 404 error", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := v12.PatchOptions{}
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, errors.NewNotFound(schema.GroupResource{}, "something")).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expand All @@ -217,10 +216,10 @@ var _ = Describe("Status", func() {
It("should stick with a normal update if we get an arbitrary error", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
newVM.SetResourceVersion("2")
patchOptions := v12.PatchOptions{}
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(nil, fmt.Errorf("I am an arbitrary error")).Times(2)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).ToNot(Succeed())
Expand All @@ -229,9 +228,9 @@ var _ = Describe("Status", func() {
It("should fall back to /status if the status did not change and stick to it", func() {
updater := NewVMStatusUpdater(virtClient)
updater.updater.subresource = false
vm := &v1.VirtualMachine{ObjectMeta: v12.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
vm := &v1.VirtualMachine{ObjectMeta: metav1.ObjectMeta{Name: "test", ResourceVersion: "1"}, Status: v1.VirtualMachineStatus{Ready: true}}
newVM := vm.DeepCopy()
patchOptions := v12.PatchOptions{}
patchOptions := metav1.PatchOptions{}
vmInterface.EXPECT().Patch(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(1)
vmInterface.EXPECT().PatchStatus(context.Background(), vm.Name, types.JSONPatchType, []byte("test"), patchOptions).Return(newVM, nil).Times(1)
Expect(updater.PatchStatus(vm, types.JSONPatchType, []byte("test"), &patchOptions)).To(Succeed())
Expand Down Expand Up @@ -266,7 +265,7 @@ var _ = Describe("Status", func() {
By("checking the VirtualMachineInstanceMigration resource")
migrationUpdater := NewMigrationStatusUpdater(virtClient)
migration := &v1.VirtualMachineInstanceMigration{Status: v1.VirtualMachineInstanceMigrationStatus{Phase: v1.MigrationPhaseUnset}}
migrationInterface.EXPECT().UpdateStatus(migration).Return(migration, nil).Times(1)
migrationInterface.EXPECT().UpdateStatus(context.Background(), migration, metav1.UpdateOptions{}).Return(migration, nil).Times(1)
Expect(migrationUpdater.UpdateStatus(migration)).To(Succeed())
})

Expand All @@ -284,7 +283,7 @@ var _ = Describe("Status", func() {
vmUpdater := NewVMStatusUpdater(virtClient)
vmUpdater.updater.subresource = false
vm := &v1.VirtualMachine{Status: v1.VirtualMachineStatus{Ready: true}}
vmInterface.EXPECT().Update(context.Background(), vm, v12.UpdateOptions{}).Return(vm, nil).Times(1)
vmInterface.EXPECT().Update(context.Background(), vm, metav1.UpdateOptions{}).Return(vm, nil).Times(1)
Expect(vmUpdater.UpdateStatus(vm)).To(Succeed())

By("checking the VirtualMachineInstanceReplicaSet resource")
Expand All @@ -298,7 +297,7 @@ var _ = Describe("Status", func() {
migrationUpdater := NewMigrationStatusUpdater(virtClient)
migrationUpdater.updater.subresource = false
migration := &v1.VirtualMachineInstanceMigration{Status: v1.VirtualMachineInstanceMigrationStatus{Phase: v1.MigrationPhaseUnset}}
migrationInterface.EXPECT().Update(migration).Return(migration, nil).Times(1)
migrationInterface.EXPECT().Update(context.Background(), migration, metav1.UpdateOptions{}).Return(migration, nil).Times(1)
Expect(migrationUpdater.UpdateStatus(migration)).To(Succeed())
})
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-api/rest/subresource.go
Expand Up @@ -328,14 +328,14 @@ func (app *SubresourceAPIApp) MigrateVMRequestHandler(request *restful.Request,
}

createMigrationJob := func() *errors.StatusError {
_, err := app.virtCli.VirtualMachineInstanceMigration(namespace).Create(&v1.VirtualMachineInstanceMigration{
_, err := app.virtCli.VirtualMachineInstanceMigration(namespace).Create(context.Background(), &v1.VirtualMachineInstanceMigration{
ObjectMeta: k8smetav1.ObjectMeta{
GenerateName: "kubevirt-migrate-vm-",
},
Spec: v1.VirtualMachineInstanceMigrationSpec{
VMIName: name,
},
}, &k8smetav1.CreateOptions{DryRun: bodyStruct.DryRun})
}, k8smetav1.CreateOptions{DryRun: bodyStruct.DryRun})
if err != nil {
return errors.NewInternalError(err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-api/rest/subresource_test.go
Expand Up @@ -1725,7 +1725,7 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {

vmClient.EXPECT().Get(context.Background(), testVMName, k8smetav1.GetOptions{}).Return(&vm, nil)
vmiClient.EXPECT().Get(context.Background(), testVMName, k8smetav1.GetOptions{}).Return(&vmi, nil)
migrateClient.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil, errors.NewInternalError(fmt.Errorf("error creating object")))
migrateClient.EXPECT().Create(context.Background(), gomock.Any(), gomock.Any()).Return(nil, errors.NewInternalError(fmt.Errorf("error creating object")))
app.MigrateVMRequestHandler(request, response)

ExpectStatusErrorWithCode(recorder, http.StatusInternalServerError)
Expand Down Expand Up @@ -1753,8 +1753,8 @@ var _ = Describe("VirtualMachineInstance Subresources", func() {
vmClient.EXPECT().Get(context.Background(), testVMName, k8smetav1.GetOptions{}).Return(&vm, nil)
vmiClient.EXPECT().Get(context.Background(), testVMName, k8smetav1.GetOptions{}).Return(&vmi, nil)

migrateClient.EXPECT().Create(gomock.Any(), gomock.Any()).Do(
func(obj interface{}, opts *k8smetav1.CreateOptions) {
migrateClient.EXPECT().Create(context.Background(), gomock.Any(), gomock.Any()).Do(
func(ctx context.Context, obj interface{}, opts k8smetav1.CreateOptions) {
Expect(opts.DryRun).To(BeEquivalentTo(migrateOptions.DryRun))
}).Return(&migration, nil)
app.MigrateVMRequestHandler(request, response)
Expand Down
Expand Up @@ -59,7 +59,7 @@ func EnsureNoMigrationConflict(virtClient kubecli.KubevirtClient, vmiName string
if err != nil {
return err
}
list, err := virtClient.VirtualMachineInstanceMigration(namespace).List(&metav1.ListOptions{
list, err := virtClient.VirtualMachineInstanceMigration(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labelSelector.String(),
})
if err != nil {
Expand Down
Expand Up @@ -95,7 +95,7 @@ var _ = Describe("Validating MigrationCreate Admitter", func() {
},
}
mockVMIClient.EXPECT().Get(context.Background(), inFlightMigration.Spec.VMIName, gomock.Any()).Return(vmi, nil)
migrationInterface.EXPECT().List(gomock.Any()).Return(kubecli.NewMigrationList(inFlightMigration), nil).AnyTimes()
migrationInterface.EXPECT().List(context.Background(), gomock.Any()).Return(kubecli.NewMigrationList(inFlightMigration), nil).AnyTimes()

migration := v1.VirtualMachineInstanceMigration{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -125,7 +125,7 @@ var _ = Describe("Validating MigrationCreate Admitter", func() {
Context("with no conflicting migration", func() {

BeforeEach(func() {
migrationInterface.EXPECT().List(gomock.Any()).Return(&v1.VirtualMachineInstanceMigrationList{}, nil).MaxTimes(1)
migrationInterface.EXPECT().List(context.Background(), gomock.Any()).Return(&v1.VirtualMachineInstanceMigrationList{}, nil).MaxTimes(1)

})

Expand Down
3 changes: 2 additions & 1 deletion pkg/virt-controller/watch/drain/evacuation/evacuation.go
@@ -1,6 +1,7 @@
package evacuation

import (
"context"
"fmt"
"math"
"sync"
Expand Down Expand Up @@ -446,7 +447,7 @@ func (c *EvacuationController) sync(node *k8sv1.Node, vmisOnNode []*virtv1.Virtu
for _, vmi := range selectedCandidates {
go func(vmi *virtv1.VirtualMachineInstance) {
defer wg.Done()
createdMigration, err := c.clientset.VirtualMachineInstanceMigration(vmi.Namespace).Create(GenerateNewMigration(vmi.Name, node.Name), &v1.CreateOptions{})
createdMigration, err := c.clientset.VirtualMachineInstanceMigration(vmi.Namespace).Create(context.Background(), GenerateNewMigration(vmi.Name, node.Name), v1.CreateOptions{})
if err != nil {
c.migrationExpectations.CreationObserved(node.Name)
c.recorder.Eventf(vmi, k8sv1.EventTypeWarning, FailedCreateVirtualMachineInstanceMigrationReason, "Error creating a Migration: %v", err)
Expand Down

0 comments on commit fa25a88

Please sign in to comment.