Skip to content

Commit 3d2371d

Browse files
Add generics to source, split the interface, add builder methods
Signed-off-by: Danil Grigorev <[email protected]>
1 parent 204048d commit 3d2371d

18 files changed

+419
-451
lines changed

alias.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222
"sigs.k8s.io/controller-runtime/pkg/builder"
23-
"sigs.k8s.io/controller-runtime/pkg/client"
2423
"sigs.k8s.io/controller-runtime/pkg/client/config"
2524
cfg "sigs.k8s.io/controller-runtime/pkg/config"
2625
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -34,9 +33,6 @@ import (
3433
// Builder builds an Application ControllerManagedBy (e.g. Operator) and returns a manager.Manager to start it.
3534
type Builder = builder.Builder
3635

37-
// WatchObject defines an interface on the watch object with inherited type info.
38-
type WatchObject = builder.WatchObject
39-
4036
// Request contains the information necessary to reconcile a Kubernetes object. This includes the
4137
// information to uniquely identify the object - its Name and Namespace. It does NOT contain information about
4238
// any specific Event or the object contents itself.
@@ -161,7 +157,3 @@ var (
161157
// SetLogger sets a concrete logging implementation for all deferred Loggers.
162158
SetLogger = log.SetLogger
163159
)
164-
165-
func Object[T client.Object](obj T) WatchObject {
166-
return builder.Object(obj)
167-
}

example_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/apimachinery/pkg/types"
3131

3232
ctrl "sigs.k8s.io/controller-runtime"
33+
"sigs.k8s.io/controller-runtime/pkg/builder"
3334
"sigs.k8s.io/controller-runtime/pkg/client"
3435
"sigs.k8s.io/controller-runtime/pkg/handler"
3536
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -84,10 +85,10 @@ func GenericExample() {
8485
os.Exit(1)
8586
}
8687

87-
err = ctrl.
88-
NewControllerManagedBy(manager). // Create the Controller
89-
With(ctrl.Object(&appsv1.ReplicaSet{})). // ReplicaSet is the Application API
90-
Own(ctrl.Object(&corev1.Pod{})). // ReplicaSet owns Pods created by it
88+
b := ctrl.NewControllerManagedBy(manager) // Create the Controller
89+
// ReplicaSet is the Application API
90+
b.Add(builder.For(b, &appsv1.ReplicaSet{})).
91+
Add(builder.Owns(b, &appsv1.ReplicaSet{}, &corev1.Pod{})). // ReplicaSet owns Pods created by it
9192
Complete(&ReplicaSetReconciler{Client: manager.GetClient()})
9293
if err != nil {
9394
log.Error(err, "could not create controller")

examples/builtins/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ func main() {
5959
}
6060

6161
// Watch ReplicaSets and enqueue ReplicaSet object key
62-
if err := c.Watch(source.Kind(mgr.GetCache(), &appsv1.ReplicaSet{}), &handler.EnqueueRequestForObject{}); err != nil {
62+
src := source.Kind(mgr.GetCache(), &appsv1.ReplicaSet{})
63+
src.Prepare(&handler.EnqueueRequestForObject{})
64+
if err := c.Watch(src); err != nil {
6365
entryLog.Error(err, "unable to watch ReplicaSets")
6466
os.Exit(1)
6567
}
6668

6769
// Watch Pods and enqueue owning ReplicaSet key
68-
if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}),
69-
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.ReplicaSet{}, handler.OnlyControllerOwner())); err != nil {
70+
src = source.Kind(mgr.GetCache(), &corev1.Pod{})
71+
src.Prepare(handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.ReplicaSet{}, handler.OnlyControllerOwner()))
72+
if err := c.Watch(src); err != nil {
7073
entryLog.Error(err, "unable to watch Pods")
7174
os.Exit(1)
7275
}

0 commit comments

Comments
 (0)