Skip to content

Commit 699d878

Browse files
authored
Narrow down the target of StatefulSetPartitionReconciler (#781)
Signed-off-by: Masayuki Ishii <[email protected]>
1 parent 51de904 commit 699d878

File tree

2 files changed

+49
-38
lines changed

2 files changed

+49
-38
lines changed

controllers/partition_controller.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"sort"
7+
"strings"
78
"time"
89

910
appsv1 "k8s.io/api/apps/v1"
@@ -19,7 +20,6 @@ import (
1920
"sigs.k8s.io/controller-runtime/pkg/client"
2021
"sigs.k8s.io/controller-runtime/pkg/controller"
2122
"sigs.k8s.io/controller-runtime/pkg/event"
22-
"sigs.k8s.io/controller-runtime/pkg/handler"
2323
crlog "sigs.k8s.io/controller-runtime/pkg/log"
2424
"sigs.k8s.io/controller-runtime/pkg/predicate"
2525
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -99,37 +99,35 @@ func (r *StatefulSetPartitionReconciler) Reconcile(ctx context.Context, req reco
9999
}
100100

101101
func (r *StatefulSetPartitionReconciler) SetupWithManager(mgr ctrl.Manager) error {
102-
mapFn := handler.EnqueueRequestsFromMapFunc(
103-
func(ctx context.Context, obj client.Object) []ctrl.Request {
104-
return []ctrl.Request{
105-
{
106-
NamespacedName: client.ObjectKey{
107-
Name: obj.GetName(),
108-
Namespace: obj.GetNamespace(),
109-
},
110-
},
111-
}
112-
})
113-
114-
p := predicate.Funcs{
115-
UpdateFunc: func(e event.UpdateEvent) bool {
116-
old := e.ObjectOld.(*mocov1beta2.MySQLCluster)
117-
new := e.ObjectNew.(*mocov1beta2.MySQLCluster)
118-
return old.ResourceVersion != new.ResourceVersion
119-
},
120-
CreateFunc: func(e event.CreateEvent) bool {
121-
return true
122-
},
102+
// Predicate function for StatefulSets and Pods. They have a prefixed name and specific labels.
103+
prctFunc := func(o client.Object) bool {
104+
if !strings.HasPrefix(o.GetName(), "moco-") {
105+
return false
106+
}
107+
108+
labels := o.GetLabels()
109+
if labels[constants.LabelAppName] != constants.AppNameMySQL {
110+
return false
111+
}
112+
if labels[constants.LabelAppCreatedBy] != constants.AppCreator {
113+
return false
114+
}
115+
return true
116+
}
117+
118+
stsPrct := predicate.Funcs{
119+
UpdateFunc: func(e event.UpdateEvent) bool { return prctFunc(e.ObjectNew) },
120+
CreateFunc: func(e event.CreateEvent) bool { return prctFunc(e.Object) },
121+
}
122+
123+
podPrct := predicate.Funcs{
124+
UpdateFunc: func(e event.UpdateEvent) bool { return prctFunc(e.ObjectNew) },
125+
CreateFunc: func(e event.CreateEvent) bool { return prctFunc(e.Object) },
123126
}
124127

125128
return ctrl.NewControllerManagedBy(mgr).
126-
For(&appsv1.StatefulSet{}).
127-
Owns(&corev1.Pod{}).
128-
Watches(
129-
&mocov1beta2.MySQLCluster{},
130-
mapFn,
131-
builder.WithPredicates(p),
132-
).
129+
For(&appsv1.StatefulSet{}, builder.WithPredicates(stsPrct)).
130+
Owns(&corev1.Pod{}, builder.WithPredicates(podPrct)).
133131
WithOptions(
134132
controller.Options{MaxConcurrentReconciles: r.MaxConcurrentReconciles},
135133
).

controllers/partition_controller_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"k8s.io/utils/ptr"
1818

1919
mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2"
20+
"github.com/cybozu-go/moco/pkg/constants"
2021
ctrl "sigs.k8s.io/controller-runtime"
2122
"sigs.k8s.io/controller-runtime/pkg/client"
2223
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -27,6 +28,11 @@ func testNewStatefulSet(cluster *mocov1beta2.MySQLCluster) *appsv1.StatefulSet {
2728
ObjectMeta: metav1.ObjectMeta{
2829
Name: cluster.PrefixedName(),
2930
Namespace: cluster.Namespace,
31+
Labels: map[string]string{
32+
constants.LabelAppName: constants.AppNameMySQL,
33+
constants.LabelAppInstance: cluster.Name,
34+
constants.LabelAppCreatedBy: constants.AppCreator,
35+
},
3036
OwnerReferences: []metav1.OwnerReference{
3137
*metav1.NewControllerRef(cluster, mocov1beta2.GroupVersion.WithKind("MySQLCluster")),
3238
},
@@ -41,11 +47,19 @@ func testNewStatefulSet(cluster *mocov1beta2.MySQLCluster) *appsv1.StatefulSet {
4147
},
4248
},
4349
Selector: &metav1.LabelSelector{
44-
MatchLabels: map[string]string{"foo": "bar"},
50+
MatchLabels: map[string]string{
51+
constants.LabelAppName: constants.AppNameMySQL,
52+
constants.LabelAppInstance: cluster.Name,
53+
constants.LabelAppCreatedBy: constants.AppCreator,
54+
},
4555
},
4656
Template: corev1.PodTemplateSpec{
4757
ObjectMeta: metav1.ObjectMeta{
48-
Labels: map[string]string{"foo": "bar"},
58+
Labels: map[string]string{
59+
constants.LabelAppName: constants.AppNameMySQL,
60+
constants.LabelAppInstance: cluster.Name,
61+
constants.LabelAppCreatedBy: constants.AppCreator,
62+
},
4963
},
5064
Spec: corev1.PodSpec{
5165
Containers: []corev1.Container{
@@ -64,14 +78,13 @@ func testNewPods(sts *appsv1.StatefulSet) []*corev1.Pod {
6478
pods := make([]*corev1.Pod, 0, *sts.Spec.Replicas)
6579

6680
for i := 0; i < int(*sts.Spec.Replicas); i++ {
81+
podLabels := sts.Spec.Template.DeepCopy().Labels
82+
podLabels[appsv1.ControllerRevisionHashLabelKey] = "rev1"
6783
pods = append(pods, &corev1.Pod{
6884
ObjectMeta: metav1.ObjectMeta{
69-
Name: fmt.Sprintf("%s-%d", sts.Name, i),
70-
Namespace: sts.Namespace,
71-
Labels: map[string]string{
72-
appsv1.ControllerRevisionHashLabelKey: "rev1",
73-
"foo": "bar",
74-
},
85+
Name: fmt.Sprintf("%s-%d", sts.Name, i),
86+
Namespace: sts.Namespace,
87+
Labels: podLabels,
7588
OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(sts, appsv1.SchemeGroupVersion.WithKind("StatefulSet"))},
7689
},
7790
Spec: corev1.PodSpec{
@@ -86,7 +99,7 @@ func testNewPods(sts *appsv1.StatefulSet) []*corev1.Pod {
8699

87100
func rolloutPods(ctx context.Context, sts *appsv1.StatefulSet, rev1 int, rev2 int) {
88101
pods := &corev1.PodList{}
89-
err := k8sClient.List(ctx, pods, client.InNamespace("partition"), client.MatchingLabels(map[string]string{"foo": "bar"}))
102+
err := k8sClient.List(ctx, pods, client.InNamespace("partition"), client.MatchingLabels(sts.Spec.Template.Labels))
90103
Expect(err).NotTo(HaveOccurred())
91104
Expect(len(pods.Items)).To(Equal(rev1 + rev2))
92105

0 commit comments

Comments
 (0)