From 7cfa4f2b4ef251efd9e75b78b5db3260aa2dbb6b Mon Sep 17 00:00:00 2001 From: Gaurav Mehta Date: Wed, 12 Apr 2023 14:39:17 +1000 Subject: [PATCH] minor changes to reconcile kubevirtCR based on feedback --- .../pcideviceclaim/pcideviceclaim_controller.go | 9 +++++---- .../pcideviceclaim_controller_test.go | 14 ++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/controller/pcideviceclaim/pcideviceclaim_controller.go b/pkg/controller/pcideviceclaim/pcideviceclaim_controller.go index c4511d7d..d2a8237c 100644 --- a/pkg/controller/pcideviceclaim/pcideviceclaim_controller.go +++ b/pkg/controller/pcideviceclaim/pcideviceclaim_controller.go @@ -336,9 +336,8 @@ func (h *Handler) permitHostDeviceInKubeVirt(pd *v1beta1.PCIDevice) error { return errors.New(msg) } - kvCopy := kv.DeepCopy() - reconcileKubevirtCR(kvCopy, pd) - if !reflect.DeepEqual(kv, kvCopy) { + kvCopy := reconcileKubevirtCR(kv, pd) + if !reflect.DeepEqual(kv.Spec.Configuration.PermittedHostDevices, kvCopy.Spec.Configuration.PermittedHostDevices) { _, err := h.virtClient.KubeVirt(DefaultNS).Update(kvCopy) return err } @@ -346,7 +345,8 @@ func (h *Handler) permitHostDeviceInKubeVirt(pd *v1beta1.PCIDevice) error { return nil } -func reconcileKubevirtCR(kv *kubevirtv1.KubeVirt, pd *v1beta1.PCIDevice) { +func reconcileKubevirtCR(kvObj *kubevirtv1.KubeVirt, pd *v1beta1.PCIDevice) *kubevirtv1.KubeVirt { + kv := kvObj.DeepCopy() if kv.Spec.Configuration.PermittedHostDevices == nil { kv.Spec.Configuration.PermittedHostDevices = &kubevirtv1.PermittedHostDevices{ PciHostDevices: []kubevirtv1.PciHostDevice{}, @@ -377,6 +377,7 @@ func reconcileKubevirtCR(kv *kubevirtv1.KubeVirt, pd *v1beta1.PCIDevice) { } kv.Spec.Configuration.PermittedHostDevices.PciHostDevices = append(permittedPCIDevices, devToPermit) } + return kv } func (h *Handler) getPCIDeviceForClaim(pdc *v1beta1.PCIDeviceClaim) (*v1beta1.PCIDevice, error) { diff --git a/pkg/controller/pcideviceclaim/pcideviceclaim_controller_test.go b/pkg/controller/pcideviceclaim/pcideviceclaim_controller_test.go index bda68cb3..0f40b700 100644 --- a/pkg/controller/pcideviceclaim/pcideviceclaim_controller_test.go +++ b/pkg/controller/pcideviceclaim/pcideviceclaim_controller_test.go @@ -142,16 +142,15 @@ var ( func Test_permitHostDeviceInKubevirtWithNoDevices(t *testing.T) { assert := require.New(t) - reconcileKubevirtCR(kubevirtCR, pd) - assert.Len(kubevirtCR.Spec.Configuration.PermittedHostDevices.PciHostDevices, 1, "expected to find one device added") + kvCopy := reconcileKubevirtCR(kubevirtCR, pd) + assert.Len(kvCopy.Spec.Configuration.PermittedHostDevices.PciHostDevices, 1, "expected to find one device added") } func Test_permitHostDeviceInKubevirtWithoutExternalResourceDevices(t *testing.T) { assert := require.New(t) kubevirtCR.Spec.Configuration.PermittedHostDevices = permittedHostDevices - kvCopy := kubevirtCR.DeepCopy() - reconcileKubevirtCR(kvCopy, pd) - assert.False(reflect.DeepEqual(kvCopy, kubevirtCR), "expected to find changes in the kubevirt CR") + kvCopy := reconcileKubevirtCR(kubevirtCR, pd) + assert.False(reflect.DeepEqual(kvCopy.Spec.Configuration.PermittedHostDevices, kubevirtCR.Spec.Configuration.PermittedHostDevices), "expected to find changes in the kubevirt CR") assert.Len(kvCopy.Spec.Configuration.PermittedHostDevices.PciHostDevices, 1, "expected to find one device added") assert.True(kvCopy.Spec.Configuration.PermittedHostDevices.PciHostDevices[0].ExternalResourceProvider, "expected external resource provider to be updated") } @@ -160,9 +159,8 @@ func Test_permitHostDeviceInKubevirtWithExternalResourceDevices(t *testing.T) { assert := require.New(t) permittedHostDevices.PciHostDevices[0].ExternalResourceProvider = true kubevirtCR.Spec.Configuration.PermittedHostDevices = permittedHostDevices - kvCopy := kubevirtCR.DeepCopy() - reconcileKubevirtCR(kvCopy, pd) - assert.True(reflect.DeepEqual(kvCopy, kubevirtCR), "expected to find no changes in the kubevirt CR") + kvCopy := reconcileKubevirtCR(kubevirtCR, pd) + assert.True(reflect.DeepEqual(kvCopy.Spec.Configuration.PermittedHostDevices, kubevirtCR.Spec.Configuration.PermittedHostDevices), "expected to find no changes in the kubevirt CR") assert.Len(kvCopy.Spec.Configuration.PermittedHostDevices.PciHostDevices, 1, "expected to find one device added") assert.True(kvCopy.Spec.Configuration.PermittedHostDevices.PciHostDevices[0].ExternalResourceProvider, "expected external resource provider to be updated") }