Skip to content

Commit 33efec2

Browse files
committed
Extract ControllerRing mapper
1 parent 8dca3f5 commit 33efec2

File tree

3 files changed

+160
-26
lines changed

3 files changed

+160
-26
lines changed

pkg/controller/shardlease/add.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,23 @@ limitations under the License.
1717
package shardlease
1818

1919
import (
20-
"context"
21-
2220
coordinationv1 "k8s.io/api/coordination/v1"
2321
"k8s.io/utils/clock"
2422
"sigs.k8s.io/controller-runtime/pkg/builder"
25-
"sigs.k8s.io/controller-runtime/pkg/client"
2623
"sigs.k8s.io/controller-runtime/pkg/controller"
2724
"sigs.k8s.io/controller-runtime/pkg/event"
2825
"sigs.k8s.io/controller-runtime/pkg/handler"
29-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3026
"sigs.k8s.io/controller-runtime/pkg/manager"
3127
"sigs.k8s.io/controller-runtime/pkg/predicate"
32-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3328

3429
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
30+
shardinghandler "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/handler"
3531
shardingpredicate "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/predicate"
3632
)
3733

3834
// ControllerName is the name of this controller.
3935
const ControllerName = "shardlease"
4036

41-
var handlerLog = logf.Log.WithName("controller").WithName(ControllerName)
42-
4337
// AddToManager adds Reconciler to the given manager.
4438
func (r *Reconciler) AddToManager(mgr manager.Manager) error {
4539
if r.Client == nil {
@@ -53,7 +47,11 @@ func (r *Reconciler) AddToManager(mgr manager.Manager) error {
5347
Named(ControllerName).
5448
For(&coordinationv1.Lease{}, builder.WithPredicates(r.LeasePredicate())).
5549
// enqueue all Leases belonging to a ControllerRing when it is created or the spec is updated
56-
Watches(&shardingv1alpha1.ControllerRing{}, handler.EnqueueRequestsFromMapFunc(r.MapControllerRingToLeases), builder.WithPredicates(shardingpredicate.ControllerRingCreatedOrUpdated())).
50+
Watches(
51+
&shardingv1alpha1.ControllerRing{},
52+
handler.EnqueueRequestsFromMapFunc(shardinghandler.MapControllerRingToLeases(r.Client)),
53+
builder.WithPredicates(shardingpredicate.ControllerRingCreatedOrUpdated()),
54+
).
5755
WithOptions(controller.Options{
5856
MaxConcurrentReconciles: 5,
5957
}).
@@ -70,21 +68,3 @@ func (r *Reconciler) LeasePredicate() predicate.Predicate {
7068
},
7169
)
7270
}
73-
74-
func (r *Reconciler) MapControllerRingToLeases(ctx context.Context, obj client.Object) []reconcile.Request {
75-
controllerRing := obj.(*shardingv1alpha1.ControllerRing)
76-
77-
leaseList := &coordinationv1.LeaseList{}
78-
if err := r.Client.List(ctx, leaseList, client.MatchingLabelsSelector{Selector: controllerRing.LeaseSelector()}); err != nil {
79-
handlerLog.Error(err, "failed listing Leases for ControllerRing", "controllerRing", client.ObjectKeyFromObject(controllerRing))
80-
return nil
81-
}
82-
83-
requests := make([]reconcile.Request, 0, len(leaseList.Items))
84-
for _, l := range leaseList.Items {
85-
lease := l
86-
requests = append(requests, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&lease)})
87-
}
88-
89-
return requests
90-
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package handler
18+
19+
import (
20+
"context"
21+
22+
coordinationv1 "k8s.io/api/coordination/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"sigs.k8s.io/controller-runtime/pkg/handler"
25+
logf "sigs.k8s.io/controller-runtime/pkg/log"
26+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
27+
28+
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
29+
)
30+
31+
var handlerLog = logf.Log.WithName("handler")
32+
33+
// MapControllerRingToLeases maps a ControllerRing to all matching shard leases.
34+
func MapControllerRingToLeases(reader client.Reader) handler.MapFunc {
35+
return func(ctx context.Context, obj client.Object) []reconcile.Request {
36+
controllerRing, ok := obj.(*shardingv1alpha1.ControllerRing)
37+
if !ok {
38+
return nil
39+
}
40+
41+
leaseList := &coordinationv1.LeaseList{}
42+
if err := reader.List(ctx, leaseList, client.MatchingLabelsSelector{Selector: controllerRing.LeaseSelector()}); err != nil {
43+
handlerLog.Error(err, "failed listing Leases for ControllerRing", "controllerRing", client.ObjectKeyFromObject(controllerRing))
44+
return nil
45+
}
46+
47+
requests := make([]reconcile.Request, 0, len(leaseList.Items))
48+
for _, lease := range leaseList.Items {
49+
requests = append(requests, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&lease)})
50+
}
51+
52+
return requests
53+
}
54+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package handler_test
18+
19+
import (
20+
"context"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
coordinationv1 "k8s.io/api/coordination/v1"
25+
corev1 "k8s.io/api/core/v1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
30+
"sigs.k8s.io/controller-runtime/pkg/handler"
31+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
32+
33+
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
34+
. "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/handler"
35+
)
36+
37+
var _ = Describe("ControllerRing", func() {
38+
var (
39+
ctx context.Context
40+
41+
fakeClient client.Client
42+
)
43+
44+
BeforeEach(func() {
45+
ctx = context.Background()
46+
47+
fakeClient = fakeclient.NewFakeClient()
48+
})
49+
50+
Describe("#MapControllerRingToLeases", func() {
51+
var (
52+
mapFunc handler.MapFunc
53+
54+
obj *shardingv1alpha1.ControllerRing
55+
)
56+
57+
BeforeEach(func() {
58+
mapFunc = MapControllerRingToLeases(fakeClient)
59+
60+
obj = &shardingv1alpha1.ControllerRing{
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: "foo",
63+
},
64+
}
65+
66+
lease := &coordinationv1.Lease{
67+
ObjectMeta: metav1.ObjectMeta{
68+
Name: "foo-1",
69+
Namespace: "foo-system",
70+
Labels: map[string]string{
71+
"alpha.sharding.timebertt.dev/controllerring": "foo",
72+
},
73+
},
74+
}
75+
Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed())
76+
77+
lease.Name = "foo-2"
78+
Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed())
79+
80+
lease.Name = "foo-3"
81+
lease.Labels["alpha.sharding.timebertt.dev/controllerring"] = "bar"
82+
Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed())
83+
84+
lease.Name = "foo-4"
85+
lease.Labels = nil
86+
Expect(fakeClient.Create(ctx, lease.DeepCopy())).To(Succeed())
87+
})
88+
89+
It("should ignore other object kinds", func() {
90+
Expect(mapFunc(ctx, &corev1.Pod{})).To(BeEmpty())
91+
})
92+
93+
It("should return requests for all matching leases", func() {
94+
Expect(mapFunc(ctx, obj)).To(ConsistOf(
95+
reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "foo-system", Name: "foo-1"}},
96+
reconcile.Request{NamespacedName: types.NamespacedName{Namespace: "foo-system", Name: "foo-2"}},
97+
))
98+
})
99+
})
100+
})

0 commit comments

Comments
 (0)