Skip to content

Commit ee70eab

Browse files
authored
Fix StatefulSetPartitionReconciler event filtering (#879)
* Add predicates tests for StatefulSetPartitionReconciler event filtering Signed-off-by: sho-iizuka <sho-iizuka@cybozu.co.jp> * Prevent the partition controller from reconciling non-MOCO StatefulSets Signed-off-by: sho-iizuka <sho-iizuka@cybozu.co.jp> --------- Signed-off-by: sho-iizuka <sho-iizuka@cybozu.co.jp>
1 parent 154998b commit ee70eab

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

controllers/partition_controller.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ func (r *StatefulSetPartitionReconciler) Reconcile(ctx context.Context, req reco
107107
}
108108

109109
func (r *StatefulSetPartitionReconciler) SetupWithManager(mgr ctrl.Manager) error {
110+
stsPrct := partitionControllerPredicate()
111+
podPrct := partitionControllerPredicate()
112+
113+
return ctrl.NewControllerManagedBy(mgr).
114+
For(&appsv1.StatefulSet{}, builder.WithPredicates(stsPrct)).
115+
Owns(&corev1.Pod{}, builder.WithPredicates(podPrct)).
116+
WithOptions(
117+
controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles},
118+
).
119+
Complete(r)
120+
}
121+
122+
// partitionControllerPredicate filters StatefulSets and Pods managed by MOCO.
123+
func partitionControllerPredicate() predicate.Funcs {
110124
// Predicate function for StatefulSets and Pods. They have a prefixed name and specific labels.
111125
prctFunc := func(o client.Object) bool {
112126
if !strings.HasPrefix(o.GetName(), "moco-") {
@@ -123,23 +137,12 @@ func (r *StatefulSetPartitionReconciler) SetupWithManager(mgr ctrl.Manager) erro
123137
return true
124138
}
125139

126-
stsPrct := predicate.Funcs{
127-
UpdateFunc: func(e event.UpdateEvent) bool { return prctFunc(e.ObjectNew) },
128-
CreateFunc: func(e event.CreateEvent) bool { return prctFunc(e.Object) },
140+
return predicate.Funcs{
141+
UpdateFunc: func(e event.UpdateEvent) bool { return prctFunc(e.ObjectNew) },
142+
CreateFunc: func(e event.CreateEvent) bool { return prctFunc(e.Object) },
143+
DeleteFunc: func(e event.DeleteEvent) bool { return prctFunc(e.Object) },
144+
GenericFunc: func(e event.GenericEvent) bool { return prctFunc(e.Object) },
129145
}
130-
131-
podPrct := predicate.Funcs{
132-
UpdateFunc: func(e event.UpdateEvent) bool { return prctFunc(e.ObjectNew) },
133-
CreateFunc: func(e event.CreateEvent) bool { return prctFunc(e.Object) },
134-
}
135-
136-
return ctrl.NewControllerManagedBy(mgr).
137-
For(&appsv1.StatefulSet{}, builder.WithPredicates(stsPrct)).
138-
Owns(&corev1.Pod{}, builder.WithPredicates(podPrct)).
139-
WithOptions(
140-
controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles},
141-
).
142-
Complete(r)
143146
}
144147

145148
// isRolloutReady returns true if the StatefulSet is ready for rolling update.

controllers/partition_controller_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
ctrl "sigs.k8s.io/controller-runtime"
2424
"sigs.k8s.io/controller-runtime/pkg/client"
2525
"sigs.k8s.io/controller-runtime/pkg/config"
26+
"sigs.k8s.io/controller-runtime/pkg/event"
2627
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2728
)
2829

@@ -329,3 +330,55 @@ var _ = Describe("StatefulSet reconciler", func() {
329330
)
330331
})
331332
})
333+
334+
var _ = Describe("StatefulSetPartitionReconciler predicates", func() {
335+
DescribeTable("should filter events",
336+
func(obj client.Object, expect bool) {
337+
prct := partitionControllerPredicate()
338+
Expect(prct.Update(event.UpdateEvent{ObjectNew: obj})).To(Equal(expect))
339+
Expect(prct.Create(event.CreateEvent{Object: obj})).To(Equal(expect))
340+
Expect(prct.Delete(event.DeleteEvent{Object: obj})).To(Equal(expect))
341+
Expect(prct.Generic(event.GenericEvent{Object: obj})).To(Equal(expect))
342+
},
343+
Entry("statefulset with moco labels", &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{
344+
Name: "moco-test",
345+
Labels: map[string]string{
346+
constants.LabelAppName: constants.AppNameMySQL,
347+
constants.LabelAppCreatedBy: constants.AppCreator,
348+
},
349+
}}, true),
350+
Entry("pod with moco labels", &corev1.Pod{ObjectMeta: metav1.ObjectMeta{
351+
Name: "moco-test-0",
352+
Labels: map[string]string{
353+
constants.LabelAppName: constants.AppNameMySQL,
354+
constants.LabelAppCreatedBy: constants.AppCreator,
355+
},
356+
}}, true),
357+
Entry("statefulset without prefix", &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{
358+
Name: "test",
359+
Labels: map[string]string{
360+
constants.LabelAppName: constants.AppNameMySQL,
361+
constants.LabelAppCreatedBy: constants.AppCreator,
362+
},
363+
}}, false),
364+
Entry("statefulset without app label", &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{
365+
Name: "moco-test",
366+
Labels: map[string]string{
367+
constants.LabelAppCreatedBy: constants.AppCreator,
368+
},
369+
}}, false),
370+
Entry("statefulset without creator label", &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{
371+
Name: "moco-test",
372+
Labels: map[string]string{
373+
constants.LabelAppName: constants.AppNameMySQL,
374+
},
375+
}}, false),
376+
Entry("statefulset with wrong labels", &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{
377+
Name: "moco-test",
378+
Labels: map[string]string{
379+
constants.LabelAppName: constants.AppNameMySQL,
380+
constants.LabelAppCreatedBy: "other",
381+
},
382+
}}, false),
383+
)
384+
})

0 commit comments

Comments
 (0)