From 441a3051f16e094c3199a8500c53f22a4bf3cdc7 Mon Sep 17 00:00:00 2001 From: Lee Yarwood Date: Wed, 16 Aug 2023 17:11:40 +0100 Subject: [PATCH] common-instancetypes: Use IgnoreObjectOwnedByVirtOperator Signed-off-by: Lee Yarwood --- .../common-instancetypes/reconcile.go | 2 + .../common-instancetypes/reconcile_test.go | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/internal/operands/common-instancetypes/reconcile.go b/internal/operands/common-instancetypes/reconcile.go index bc8f44f4e..7c85e0a9c 100644 --- a/internal/operands/common-instancetypes/reconcile.go +++ b/internal/operands/common-instancetypes/reconcile.go @@ -324,6 +324,7 @@ func (c *CommonInstancetypes) reconcileVirtualMachineClusterInstancetypesFuncs() return common.CreateOrUpdate(request). ClusterResource(clusterInstancetype). WithAppLabels(operandName, operandComponent). + IgnoreObjectOwnedByVirtOperator(). Reconcile() }) } @@ -338,6 +339,7 @@ func (c *CommonInstancetypes) reconcileVirtualMachineClusterPreferencesFuncs() [ return common.CreateOrUpdate(request). ClusterResource(clusterPreference). WithAppLabels(operandName, operandComponent). + IgnoreObjectOwnedByVirtOperator(). Reconcile() }) } diff --git a/internal/operands/common-instancetypes/reconcile_test.go b/internal/operands/common-instancetypes/reconcile_test.go index be145a67e..1d06cc076 100644 --- a/internal/operands/common-instancetypes/reconcile_test.go +++ b/internal/operands/common-instancetypes/reconcile_test.go @@ -26,6 +26,7 @@ import ( "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" + kubevirtCorev1 "kubevirt.io/api/core/v1" instancetypeapi "kubevirt.io/api/instancetype" instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1" ssp "kubevirt.io/ssp-operator/api/v1beta2" @@ -251,6 +252,56 @@ var _ = Describe("Common-Instancetypes operand", func() { ExpectResourceExists(preference, request) }) + It("should ignore virt-operator owned objects during reconcile when also provided by bundle", func() { + _, err = operand.Reconcile(&request) + Expect(err).ToNot(HaveOccurred()) + + instancetypeList := &instancetypev1beta1.VirtualMachineClusterInstancetypeList{} + Expect(request.Client.List(request.Context, instancetypeList, &client.ListOptions{})).To(Succeed()) + Expect(len(instancetypeList.Items) > 0).To(BeTrue()) + + preferenceList := &instancetypev1beta1.VirtualMachineClusterPreferenceList{} + Expect(request.Client.List(request.Context, preferenceList, &client.ListOptions{})).To(Succeed()) + Expect(len(preferenceList.Items) > 0).To(BeTrue()) + + // Mutate the instance type while also adding the labels for virt-operator + instancetypeToUpdate := instancetypeList.Items[0] + updatedCPUGuestCount := instancetypeToUpdate.Spec.CPU.Guest + 1 + instancetypeToUpdate.Spec.CPU.Guest = updatedCPUGuestCount + instancetypeToUpdate.Labels = map[string]string{ + kubevirtCorev1.ManagedByLabel: kubevirtCorev1.ManagedByLabelOperatorValue, + } + Expect(request.Client.Update(request.Context, &instancetypeToUpdate, &client.UpdateOptions{})).To(Succeed()) + + // Mutate the preference while also adding the labels for virt-operator + preferenceToUpdate := preferenceList.Items[0] + updatedPreferredCPUTopology := instancetypev1beta1.PreferCores + updatedPreferenceCPU := &instancetypev1beta1.CPUPreferences{ + PreferredCPUTopology: &updatedPreferredCPUTopology, + } + preferenceToUpdate.Spec.CPU = updatedPreferenceCPU + preferenceToUpdate.Labels = map[string]string{ + kubevirtCorev1.ManagedByLabel: kubevirtCorev1.ManagedByLabelOperatorValue, + } + Expect(request.Client.Update(request.Context, &preferenceToUpdate, &client.UpdateOptions{})).To(Succeed()) + + results, err := operand.Reconcile(&request) + Expect(err).ToNot(HaveOccurred()) + + // Assert that we have reported ignoring the attempt to reconcile the objects owned by virt-operator + for _, res := range results { + if res.Resource.GetName() == instancetypeToUpdate.Name || res.Resource.GetName() == preferenceToUpdate.Name { + Expect(res.OperationResult).To(Equal(common.OperationResultIgnored)) + } + } + + // Assert that the mutations made above persist as the reconcile is being ignored + Expect(request.Client.Get(request.Context, client.ObjectKeyFromObject(&instancetypeToUpdate), &instancetypeToUpdate, &client.GetOptions{})).To(Succeed()) + Expect(instancetypeToUpdate.Spec.CPU.Guest).To(Equal(updatedCPUGuestCount)) + Expect(request.Client.Get(request.Context, client.ObjectKeyFromObject(&preferenceToUpdate), &preferenceToUpdate, &client.GetOptions{})).To(Succeed()) + Expect(preferenceToUpdate.Spec.CPU).To(Equal(updatedPreferenceCPU)) + }) + It("should create and cleanup resources from an external URL", func() { // Generate a mock ResMap and resources for the test mockResMap, virtualMachineClusterInstancetypes, virtualMachineClusterPreferences, err := newMockResources(10, 10)