Skip to content

Commit ae66a85

Browse files
committed
Improve assertion
1 parent 25d4810 commit ae66a85

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pkg/reconciler/reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ func (r *Reconciler) setupWatches(mgr ctrl.Manager, c controller.Controller) err
10251025

10261026
var preds []predicate.Predicate
10271027

1028-
if r.labelSelector.Size() > 0 {
1028+
if len(r.labelSelector.MatchLabels) > 0 || len(r.labelSelector.MatchExpressions) > 0 {
10291029
selectorPredicate, err := predicate.LabelSelectorPredicate(r.labelSelector)
10301030
if err != nil {
10311031
return err

pkg/reconciler/reconciler_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"slices"
2425
"strconv"
2526
"sync"
2627
"time"
@@ -1537,10 +1538,12 @@ var _ = Describe("Reconciler", func() {
15371538
trackingHook := hook.PostHookFunc(func(obj *unstructured.Unstructured, _ release.Release, _ logr.Logger) error {
15381539
mu.Lock()
15391540
defer mu.Unlock()
1540-
reconciledCRs = append(reconciledCRs, obj.GetName())
1541+
if !slices.Contains(reconciledCRs, obj.GetName()) {
1542+
reconciledCRs = append(reconciledCRs, obj.GetName())
1543+
}
15411544
return nil
15421545
})
1543-
mgr = setupManagerWithPostHook(ctx, trackingHook, matchingLabels)
1546+
mgr = setupManagerWithSelectorAndPostHook(ctx, trackingHook, matchingLabels)
15441547

15451548
labeledObj = testutil.BuildTestCR(gvk)
15461549
labeledObj.SetName("labeled-cr")
@@ -1608,10 +1611,12 @@ var _ = Describe("Reconciler", func() {
16081611
postHook := hook.PostHookFunc(func(obj *unstructured.Unstructured, _ release.Release, _ logr.Logger) error {
16091612
mu.Lock()
16101613
defer mu.Unlock()
1611-
anotherReconciledCRs = append(anotherReconciledCRs, obj.GetName())
1614+
if !slices.Contains(anotherReconciledCRs, obj.GetName()) {
1615+
anotherReconciledCRs = append(anotherReconciledCRs, obj.GetName())
1616+
}
16121617
return nil
16131618
})
1614-
_ = setupManagerWithPostHook(ctx, postHook, anotherMatchingLabels)
1619+
_ = setupManagerWithSelectorAndPostHook(ctx, postHook, anotherMatchingLabels)
16151620
})
16161621

16171622
By("creating a CR with matching labels for the first manager", func() {
@@ -1633,8 +1638,10 @@ var _ = Describe("Reconciler", func() {
16331638
})
16341639

16351640
By("creating a CR with matching labels for the second manager", func() {
1636-
anotherObj.SetLabels(anotherMatchingLabels)
16371641
Expect(mgr.GetClient().Create(ctx, anotherObj)).To(Succeed())
1642+
Expect(mgr.GetClient().Get(ctx, anotherObjKey, anotherObj)).To(Succeed())
1643+
anotherObj.SetLabels(anotherMatchingLabels)
1644+
Expect(mgr.GetClient().Update(ctx, anotherObj)).To(Succeed())
16381645
})
16391646

16401647
By("verifying that both managers reconcile only matching labels CRs", func() {
@@ -1863,7 +1870,7 @@ func ensureDeleteCR(ctx context.Context, mgr manager.Manager, crKey types.Namesp
18631870
Expect(mgr.GetClient().Delete(ctx, cr)).To(Succeed())
18641871
}
18651872

1866-
func setupManagerWithPostHook(ctx context.Context, postHook hook.PostHook, matchingLabels map[string]string) manager.Manager {
1873+
func setupManagerWithSelectorAndPostHook(ctx context.Context, postHook hook.PostHook, matchingLabels map[string]string) manager.Manager {
18671874
mgr := getManagerOrFail()
18681875
r, err := New(
18691876
WithGroupVersionKind(gvk),

0 commit comments

Comments
 (0)