-
Notifications
You must be signed in to change notification settings - Fork 54
ROX-30138: Add tests for WithSelector #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,8 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
|
|
@@ -49,6 +51,7 @@ import ( | |
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/interceptor" | ||
| "sigs.k8s.io/controller-runtime/pkg/config" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
|
@@ -1492,45 +1495,6 @@ var _ = Describe("Reconciler", func() { | |
| }) | ||
| }) | ||
| }) | ||
| When("label selector set", func() { | ||
| It("reconcile only matching CR", func() { | ||
| By("adding selector to the reconciler", func() { | ||
| selectorFoo := metav1.LabelSelector{MatchLabels: map[string]string{"app": "foo"}} | ||
| Expect(WithSelector(selectorFoo)(r)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("adding not matching label to the CR", func() { | ||
| Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed()) | ||
| obj.SetLabels(map[string]string{"app": "bar"}) | ||
| Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("reconciling skipped and no actions for the release", func() { | ||
| res, err := r.Reconcile(ctx, req) | ||
| Expect(res).To(Equal(reconcile.Result{})) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| }) | ||
|
|
||
| By("verifying the release has not changed", func() { | ||
| rel, err := ac.Get(obj.GetName()) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(rel).NotTo(BeNil()) | ||
| Expect(*rel).To(Equal(*currentRelease)) | ||
| }) | ||
|
|
||
| By("adding matching label to the CR", func() { | ||
| Expect(mgr.GetClient().Get(ctx, objKey, obj)).To(Succeed()) | ||
| obj.SetLabels(map[string]string{"app": "foo"}) | ||
| Expect(mgr.GetClient().Update(ctx, obj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("successfully reconciling with correct labels", func() { | ||
| res, err := r.Reconcile(ctx, req) | ||
| Expect(res).To(Equal(reconcile.Result{})) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
@@ -1545,6 +1509,135 @@ var _ = Describe("Reconciler", func() { | |
| }) | ||
| }) | ||
|
|
||
| _ = Describe("WithSelector test", func() { | ||
| var ( | ||
| mgr manager.Manager | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| reconciledCRs []string | ||
| reconciledCRsMutex sync.Mutex | ||
| labeledObj *unstructured.Unstructured | ||
| unlabeledObj *unstructured.Unstructured | ||
| labeledObjKey types.NamespacedName | ||
| unlabeledObjKey types.NamespacedName | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| reconciledCRs = []string{} | ||
| mgr = getManagerOrFail() | ||
| matchingLabels := map[string]string{"app": "foo"} | ||
|
|
||
| r, err := New( | ||
| WithGroupVersionKind(gvk), | ||
| WithChart(chrt), | ||
| WithSelector(metav1.LabelSelector{MatchLabels: matchingLabels}), | ||
| ) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
|
|
||
| reconciler := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) { | ||
| reconciledCRsMutex.Lock() | ||
| reconciledCRs = append(reconciledCRs, req.NamespacedName.String()) | ||
| reconciledCRsMutex.Unlock() | ||
| return r.Reconcile(ctx, req) | ||
| }) | ||
|
|
||
| controllerName := fmt.Sprintf("%v-controller", strings.ToLower(gvk.Kind)) | ||
| Expect(r.addDefaults(mgr, controllerName)).To(Succeed()) | ||
| r.setupScheme(mgr) | ||
|
|
||
| c, err := controller.New(controllerName, mgr, controller.Options{ | ||
| Reconciler: reconciler, | ||
| MaxConcurrentReconciles: 1, | ||
| }) | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| Expect(r.setupWatches(mgr, c)).To(Succeed()) | ||
|
Comment on lines
+1545
to
+1554
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplicates a bunch of code from |
||
|
|
||
| labeledObj = testutil.BuildTestCR(gvk) | ||
| labeledObj.SetName("labeled-cr") | ||
| labeledObj.SetLabels(matchingLabels) | ||
| labeledObjKey = types.NamespacedName{Namespace: labeledObj.GetNamespace(), Name: labeledObj.GetName()} | ||
|
|
||
| unlabeledObj = testutil.BuildTestCR(gvk) | ||
| unlabeledObj.SetName("unlabeled-cr") | ||
| unlabeledObjKey = types.NamespacedName{Namespace: unlabeledObj.GetNamespace(), Name: unlabeledObj.GetName()} | ||
|
|
||
| ctx, cancel = context.WithCancel(context.Background()) | ||
| go func() { | ||
| Expect(mgr.Start(ctx)).To(Succeed()) | ||
| }() | ||
| Expect(mgr.GetCache().WaitForCacheSync(ctx)).To(BeTrue()) | ||
| }) | ||
|
|
||
| AfterEach(func() { | ||
| By("ensuring the labeled CR is deleted", func() { | ||
| err := mgr.GetAPIReader().Get(ctx, labeledObjKey, labeledObj) | ||
| if !apierrors.IsNotFound(err) { | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| labeledObj.SetFinalizers([]string{}) | ||
| Expect(mgr.GetClient().Update(ctx, labeledObj)).To(Succeed()) | ||
| Expect(mgr.GetClient().Delete(ctx, labeledObj)).To(Succeed()) | ||
| } | ||
| }) | ||
|
|
||
| By("ensuring the unlabeled CR is deleted", func() { | ||
| err := mgr.GetAPIReader().Get(ctx, unlabeledObjKey, unlabeledObj) | ||
| if !apierrors.IsNotFound(err) { | ||
| Expect(err).ToNot(HaveOccurred()) | ||
| unlabeledObj.SetFinalizers([]string{}) | ||
| Expect(mgr.GetClient().Update(ctx, unlabeledObj)).To(Succeed()) | ||
| Expect(mgr.GetClient().Delete(ctx, unlabeledObj)).To(Succeed()) | ||
| } | ||
kurlov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| cancel() | ||
| }) | ||
|
|
||
| It("should only reconcile CRs matching the label selector", func() { | ||
| By("creating a CR with matching labels", func() { | ||
| Expect(mgr.GetClient().Create(ctx, labeledObj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("creating a CR without matching labels", func() { | ||
| Expect(mgr.GetClient().Create(ctx, unlabeledObj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("waiting for reconciliations to complete", func() { | ||
| Eventually(func() []string { | ||
| reconciledCRsMutex.Lock() | ||
| defer reconciledCRsMutex.Unlock() | ||
| return reconciledCRs | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that since this returns a slice pointing at the same backing array, the caller will race with other accesses 🤔 |
||
| }, "5s", "100ms").Should(ContainElement(labeledObjKey.String())) | ||
| }) | ||
|
|
||
| By("verifying only the labeled CR was reconciled", func() { | ||
| reconciledCRsMutex.Lock() | ||
| defer reconciledCRsMutex.Unlock() | ||
| Expect(reconciledCRs).To(ContainElement(labeledObjKey.String())) | ||
| Expect(reconciledCRs).NotTo(ContainElement(unlabeledObjKey.String())) | ||
| }) | ||
|
|
||
| By("updating the unlabeled CR to have matching labels", func() { | ||
| Expect(mgr.GetClient().Get(ctx, unlabeledObjKey, unlabeledObj)).To(Succeed()) | ||
| unlabeledObj.SetLabels(map[string]string{"app": "foo"}) | ||
kurlov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Expect(mgr.GetClient().Update(ctx, unlabeledObj)).To(Succeed()) | ||
| }) | ||
|
|
||
| By("waiting for the previously unlabeled CR to be reconciled", func() { | ||
| Eventually(func() []string { | ||
| reconciledCRsMutex.Lock() | ||
| defer reconciledCRsMutex.Unlock() | ||
| return reconciledCRs | ||
| }, "5s", "100ms").Should(ContainElement(unlabeledObjKey.String())) | ||
| }) | ||
|
|
||
| By("verifying the previously unlabeled CR was reconciled after label change", func() { | ||
| reconciledCRsMutex.Lock() | ||
| defer reconciledCRsMutex.Unlock() | ||
| Expect(reconciledCRs).To(ContainElement(unlabeledObjKey.String())) | ||
| }) | ||
| }) | ||
|
Comment on lines
+1636
to
+1641
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think once the above |
||
| }) | ||
|
|
||
| _ = Describe("Test custom controller setup", func() { | ||
| var ( | ||
| mgr manager.Manager | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.