Skip to content

Commit 138478e

Browse files
committed
Reuse predicate logic
1 parent 57ef0b3 commit 138478e

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

internal/controller/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (r *Registry) registerControllers(c cluster.Cluster, ap atlas.Provider) {
144144

145145
// deprecatedPredicates are to be phased out in favor of defaultPredicates
146146
func (r *Registry) deprecatedPredicates() []predicate.Predicate {
147-
return append(r.sharedPredicates, watch.DeprecatedCommonPredicates())
147+
return append(r.sharedPredicates, watch.DeprecatedCommonPredicates[client.Object]())
148148
}
149149

150150
// defaultPredicates minimize the reconciliations controllers actually do, avoiding

internal/controller/watch/predicates.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,22 @@ import (
2828
// DeprecatedCommonPredicates returns the predicate which filter out the changes done to any field except for spec (e.g. status)
2929
// Also we should reconcile if finalizers have changed (see https://blog.openshift.com/kubernetes-operators-best-practices/)
3030
// This will be phased out gradually to be replaced by DefaultPredicates
31-
func DeprecatedCommonPredicates() predicate.Funcs {
32-
return predicate.Funcs{
33-
UpdateFunc: func(e event.UpdateEvent) bool {
34-
if e.ObjectOld.GetResourceVersion() == e.ObjectNew.GetResourceVersion() {
35-
// resource version didn't change, so this is a resync, allow reconciliation.
36-
return true
37-
}
38-
39-
if customresource.ReconciliationShouldBeSkipped(e.ObjectOld) &&
40-
!customresource.ReconciliationShouldBeSkipped(e.ObjectNew) {
31+
func DeprecatedCommonPredicates[T metav1.Object]() predicate.TypedPredicate[T] {
32+
return predicate.Or(
33+
SkipAnnotationRemovedPredicate[T](),
34+
predicate.TypedFuncs[T]{
35+
UpdateFunc: func(e event.TypedUpdateEvent[T]) bool {
36+
if e.ObjectOld.GetResourceVersion() == e.ObjectNew.GetResourceVersion() {
37+
// resource version didn't change, so this is a resync, allow reconciliation.
38+
return true
39+
}
40+
41+
if e.ObjectOld.GetGeneration() == e.ObjectNew.GetGeneration() && reflect.DeepEqual(e.ObjectNew.GetFinalizers(), e.ObjectOld.GetFinalizers()) {
42+
return false
43+
}
4144
return true
42-
}
43-
44-
if e.ObjectOld.GetGeneration() == e.ObjectNew.GetGeneration() && reflect.DeepEqual(e.ObjectNew.GetFinalizers(), e.ObjectOld.GetFinalizers()) {
45-
return false
46-
}
47-
return true
48-
},
49-
}
45+
},
46+
})
5047
}
5148

5249
func SelectNamespacesPredicate(namespaces []string) predicate.Funcs {
@@ -65,9 +62,9 @@ func SelectNamespacesPredicate(namespaces []string) predicate.Funcs {
6562
})
6663
}
6764

68-
// ReconcileAgainPredicate reconciles on updates when the skip reconcile
65+
// SkipAnnotationRemovedPredicate reconciles on updates when the skip reconcile
6966
// annotation is removed or changed
70-
func ReconcileAgainPredicate[T metav1.Object]() predicate.TypedPredicate[T] {
67+
func SkipAnnotationRemovedPredicate[T metav1.Object]() predicate.TypedPredicate[T] {
7168
return predicate.TypedFuncs[T]{
7269
UpdateFunc: func(e event.TypedUpdateEvent[T]) bool {
7370
if customresource.ReconciliationShouldBeSkipped(e.ObjectOld) &&
@@ -103,7 +100,7 @@ func DefaultPredicates[T metav1.Object]() predicate.TypedPredicate[T] {
103100
return predicate.And( // or the generation changed or timed out (except for deletions)
104101
predicate.Or(
105102
GlobalResyncAwareGenerationChangePredicate[T](),
106-
ReconcileAgainPredicate[T](),
103+
SkipAnnotationRemovedPredicate[T](),
107104
),
108105
IgnoreDeletedPredicate[T](),
109106
)

internal/controller/watch/predicates_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stretchr/testify/assert"
2121
corev1 "k8s.io/api/core/v1"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
2324
"sigs.k8s.io/controller-runtime/pkg/event"
2425

2526
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
@@ -115,8 +116,8 @@ func TestDeprecatedCommonPredicates(t *testing.T) {
115116
},
116117
} {
117118
t.Run(tc.title, func(t *testing.T) {
118-
f := watch.DeprecatedCommonPredicates()
119-
assert.Equal(t, tc.want, f.UpdateFunc(
119+
f := watch.DeprecatedCommonPredicates[client.Object]()
120+
assert.Equal(t, tc.want, f.Update(
120121
event.UpdateEvent{ObjectOld: tc.old, ObjectNew: tc.new}))
121122
})
122123
}

0 commit comments

Comments
 (0)