diff --git a/images/virtualization-artifact/pkg/controller/indexer/indexer.go b/images/virtualization-artifact/pkg/controller/indexer/indexer.go index 146d3f75ca..d8289ccb1e 100644 --- a/images/virtualization-artifact/pkg/controller/indexer/indexer.go +++ b/images/virtualization-artifact/pkg/controller/indexer/indexer.go @@ -34,6 +34,7 @@ const ( IndexFieldVMByVD = "spec.blockDeviceRefs.VirtualDisk" IndexFieldVMByVI = "spec.blockDeviceRefs.VirtualImage" IndexFieldVMByCVI = "spec.blockDeviceRefs.ClusterVirtualImage" + IndexFieldVMByIP = "status.ipAddress" IndexFieldVMByNode = "status.node" IndexFieldVDByVDSnapshot = "vd,spec.DataSource.ObjectRef.Name,.Kind=VirtualDiskSnapshot" @@ -64,6 +65,7 @@ var IndexGetters = []IndexGetter{ IndexVMByVD, IndexVMByVI, IndexVMByCVI, + IndexVMByIP, IndexVMByNode, IndexVMSnapshotByVM, IndexVMSnapshotByVDSnapshot, @@ -121,6 +123,16 @@ func IndexVMByCVI() (obj client.Object, field string, extractValue client.Indexe } } +func IndexVMByIP() (obj client.Object, field string, extractValue client.IndexerFunc) { + return &v1alpha2.VirtualMachine{}, IndexFieldVMByIP, func(object client.Object) []string { + vm, ok := object.(*v1alpha2.VirtualMachine) + if !ok || vm == nil || vm.Status.IPAddress == "" { + return nil + } + return []string{vm.Status.IPAddress} + } +} + func IndexVMByNode() (obj client.Object, field string, extractValue client.IndexerFunc) { return &v1alpha2.VirtualMachine{}, IndexFieldVMByNode, func(object client.Object) []string { vm, ok := object.(*v1alpha2.VirtualMachine) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go b/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go index e581028475..9c45483364 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/inuse.go @@ -23,6 +23,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" virtv1 "kubevirt.io/api/core/v1" @@ -33,6 +34,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/object" commonvd "github.com/deckhouse/virtualization-controller/pkg/common/vd" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" + "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" ) @@ -153,7 +155,8 @@ func (h InUseHandler) checkImageUsage(ctx context.Context, vd *v1alpha2.VirtualD func (h InUseHandler) updateAttachedVirtualMachines(ctx context.Context, vd *v1alpha2.VirtualDisk) error { var vms v1alpha2.VirtualMachineList err := h.client.List(ctx, &vms, &client.ListOptions{ - Namespace: vd.GetNamespace(), + Namespace: vd.Namespace, + FieldSelector: fields.OneTermEqualSelector(indexer.IndexFieldVMByVD, vd.Name), }) if err != nil { return fmt.Errorf("error getting virtual machines: %w", err) diff --git a/images/virtualization-artifact/pkg/controller/vd/internal/inuse_test.go b/images/virtualization-artifact/pkg/controller/vd/internal/inuse_test.go index fe5e4b17b0..c11fd330d2 100644 --- a/images/virtualization-artifact/pkg/controller/vd/internal/inuse_test.go +++ b/images/virtualization-artifact/pkg/controller/vd/internal/inuse_test.go @@ -27,9 +27,9 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" virtv1 "kubevirt.io/api/core/v1" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client/fake" "github.com/deckhouse/virtualization-controller/pkg/common/annotations" + "github.com/deckhouse/virtualization-controller/pkg/common/testutil" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition" @@ -150,7 +150,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vm, vm2, vm3).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vm, vm2, vm3) + Expect(err).ToNot(HaveOccurred()) handler = &InUseHandler{client: k8sClient} result, err := handler.Handle(ctx, vd) @@ -215,7 +216,9 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vm).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vm) + Expect(err).ToNot(HaveOccurred()) + handler = &InUseHandler{client: k8sClient} result, err := handler.Handle(ctx, vd) @@ -243,7 +246,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) handler = &InUseHandler{client: k8sClient} result, err := handler.Handle(ctx, vd) @@ -272,7 +276,9 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) + handler = &InUseHandler{client: k8sClient} result, err := handler.Handle(ctx, vd) @@ -300,7 +306,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -331,7 +338,8 @@ var _ = Describe("InUseHandler", func() { } vd.Generation = 3 - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -380,7 +388,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vm).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vm) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -431,7 +440,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vm).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vm) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -477,7 +487,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vi).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vi) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -525,7 +536,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, cvi).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, cvi) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -587,7 +599,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vi, vm).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vi, vm) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -635,7 +648,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vm).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vm) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -688,7 +702,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, vi).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, vi) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -720,7 +735,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -752,7 +768,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) @@ -792,7 +809,8 @@ var _ = Describe("InUseHandler", func() { }, } - k8sClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vd, pvc).Build() + k8sClient, err := testutil.NewFakeClientWithObjects(vd, pvc) + Expect(err).ToNot(HaveOccurred()) handler = NewInUseHandler(k8sClient) result, err := handler.Handle(ctx, vd) diff --git a/images/virtualization-artifact/pkg/controller/vmip/internal/attached_handler.go b/images/virtualization-artifact/pkg/controller/vmip/internal/attached_handler.go index 9bf4a651b0..0190343d3c 100644 --- a/images/virtualization-artifact/pkg/controller/vmip/internal/attached_handler.go +++ b/images/virtualization-artifact/pkg/controller/vmip/internal/attached_handler.go @@ -22,6 +22,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -29,6 +30,7 @@ import ( "github.com/deckhouse/virtualization-controller/pkg/common/annotations" "github.com/deckhouse/virtualization-controller/pkg/common/object" "github.com/deckhouse/virtualization-controller/pkg/controller/conditions" + "github.com/deckhouse/virtualization-controller/pkg/controller/indexer" "github.com/deckhouse/virtualization-controller/pkg/eventrecord" "github.com/deckhouse/virtualization/api/core/v1alpha2" "github.com/deckhouse/virtualization/api/core/v1alpha2/vmipcondition" @@ -83,7 +85,10 @@ func (h *AttachedHandler) Handle(ctx context.Context, vmip *v1alpha2.VirtualMach func (h *AttachedHandler) getAttachedVirtualMachine(ctx context.Context, vmip *v1alpha2.VirtualMachineIPAddress) (*v1alpha2.VirtualMachine, error) { var vms v1alpha2.VirtualMachineList - err := h.client.List(ctx, &vms, &client.ListOptions{Namespace: vmip.Namespace}) + err := h.client.List(ctx, &vms, &client.ListOptions{ + Namespace: vmip.Namespace, + FieldSelector: fields.OneTermEqualSelector(indexer.IndexFieldVMByIP, vmip.Status.Address), + }) if err != nil { return nil, fmt.Errorf("list vms: %w", err) }