Skip to content

Commit

Permalink
common-instancetypes: Use IgnoreObjectOwnedByVirtOperator
Browse files Browse the repository at this point in the history
Signed-off-by: Lee Yarwood <[email protected]>
  • Loading branch information
lyarwood committed Oct 11, 2023
1 parent f7f68bc commit 02f76a1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/operands/common-instancetypes/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ func (c *CommonInstancetypes) reconcileVirtualMachineClusterInstancetypesFuncs()
return common.CreateOrUpdate(request).
ClusterResource(clusterInstancetype).
WithAppLabels(operandName, operandComponent).
IgnoreObjectOwnedByVirtOperator().
Reconcile()
})
}
Expand All @@ -338,6 +339,7 @@ func (c *CommonInstancetypes) reconcileVirtualMachineClusterPreferencesFuncs() [
return common.CreateOrUpdate(request).
ClusterResource(clusterPreference).
WithAppLabels(operandName, operandComponent).
IgnoreObjectOwnedByVirtOperator().
Reconcile()
})
}
Expand Down
51 changes: 51 additions & 0 deletions internal/operands/common-instancetypes/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"

kubevirtv1 "kubevirt.io/api/core/v1"
instancetypeapi "kubevirt.io/api/instancetype"
instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1"
ssp "kubevirt.io/ssp-operator/api/v1beta2"
Expand Down Expand Up @@ -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{
kubevirtv1.ManagedByLabel: kubevirtv1.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{
kubevirtv1.ManagedByLabel: kubevirtv1.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)
Expand Down

0 comments on commit 02f76a1

Please sign in to comment.