Skip to content

Commit

Permalink
common-instancetypes: Use IgnoreObjectOwnedByVirtOperator
Browse files Browse the repository at this point in the history
Co-authored-by: Felix Matouschek <[email protected]>
Signed-off-by: Lee Yarwood <[email protected]>
  • Loading branch information
lyarwood and 0xFelix committed Oct 23, 2023
1 parent b9023dc commit 7f6d954
Show file tree
Hide file tree
Showing 3 changed files with 94 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
41 changes: 41 additions & 0 deletions tests/common_instancetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/utils/pointer"
kubevirtCorev1 "kubevirt.io/api/core/v1"
instancetypev1beta1 "kubevirt.io/api/instancetype/v1beta1"
ssp "kubevirt.io/ssp-operator/api/v1beta2"
common_instancetypes "kubevirt.io/ssp-operator/internal/operands/common-instancetypes"
Expand Down Expand Up @@ -60,6 +61,46 @@ var _ = Describe("Common Instance Types", func() {
Expect(apiClient.Get(ctx, client.ObjectKey{Name: preference.Name}, &instancetypev1beta1.VirtualMachineClusterPreference{})).To(Succeed())
}
})
It("should ignore resources owned by virt-operator", func() {
virtualMachineClusterInstancetypes, err := common_instancetypes.FetchBundleResource[instancetypev1beta1.VirtualMachineClusterInstancetype]("../" + common_instancetypes.BundleDir + common_instancetypes.ClusterInstancetypesBundle)
Expect(err).ToNot(HaveOccurred())
Expect(virtualMachineClusterInstancetypes).ToNot(BeEmpty())

virtualMachineClusterPreferences, err := common_instancetypes.FetchBundleResource[instancetypev1beta1.VirtualMachineClusterPreference]("../" + common_instancetypes.BundleDir + common_instancetypes.ClusterPreferencesBundle)
Expect(err).ToNot(HaveOccurred())
Expect(virtualMachineClusterPreferences).ToNot(BeEmpty())

// Mutate the preference while also adding the labels for virt-operator
instancetypeToUpdate := &virtualMachineClusterInstancetypes[0]
Expect(apiClient.Get(ctx, client.ObjectKey{Name: instancetypeToUpdate.Name}, instancetypeToUpdate)).To(Succeed())
updatedCPUGuestCount := instancetypeToUpdate.Spec.CPU.Guest + 1
instancetypeToUpdate.Spec.CPU.Guest = updatedCPUGuestCount
instancetypeToUpdate.Labels = map[string]string{
kubevirtCorev1.ManagedByLabel: kubevirtCorev1.ManagedByLabelOperatorValue,
}
Expect(apiClient.Update(ctx, instancetypeToUpdate)).To(Succeed())

// Mutate the preference while also adding the labels for virt-operator
preferenceToUpdate := &virtualMachineClusterPreferences[0]
Expect(apiClient.Get(ctx, client.ObjectKey{Name: preferenceToUpdate.Name}, preferenceToUpdate)).To(Succeed())
updatedPreferredCPUTopology := instancetypev1beta1.PreferCores
updatedPreferenceCPU := &instancetypev1beta1.CPUPreferences{
PreferredCPUTopology: &updatedPreferredCPUTopology,
}
preferenceToUpdate.Spec.CPU = updatedPreferenceCPU
preferenceToUpdate.Labels = map[string]string{
kubevirtCorev1.ManagedByLabel: kubevirtCorev1.ManagedByLabelOperatorValue,
}
Expect(apiClient.Update(ctx, preferenceToUpdate)).To(Succeed())

triggerReconciliation()

// Assert that the mutations made above persist as the reconcile is being ignored
Expect(apiClient.Get(ctx, client.ObjectKey{Name: instancetypeToUpdate.Name}, instancetypeToUpdate)).To(Succeed())
Expect(instancetypeToUpdate.Spec.CPU.Guest).To(Equal(updatedCPUGuestCount))
Expect(apiClient.Get(ctx, client.ObjectKey{Name: preferenceToUpdate.Name}, preferenceToUpdate)).To(Succeed())
Expect(preferenceToUpdate.Spec.CPU).To(Equal(updatedPreferenceCPU))
})
})
Context("webhook", func() {
DescribeTable("should reject URL", func(URL string) {
Expand Down

0 comments on commit 7f6d954

Please sign in to comment.