Skip to content

Commit

Permalink
Add generics to source, split the interface, add builder methods
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Apr 16, 2024
1 parent 204048d commit 3d2371d
Show file tree
Hide file tree
Showing 18 changed files with 419 additions and 451 deletions.
8 changes: 0 additions & 8 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
cfg "sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -34,9 +33,6 @@ import (
// Builder builds an Application ControllerManagedBy (e.g. Operator) and returns a manager.Manager to start it.
type Builder = builder.Builder

// WatchObject defines an interface on the watch object with inherited type info.
type WatchObject = builder.WatchObject

// Request contains the information necessary to reconcile a Kubernetes object. This includes the
// information to uniquely identify the object - its Name and Namespace. It does NOT contain information about
// any specific Event or the object contents itself.
Expand Down Expand Up @@ -161,7 +157,3 @@ var (
// SetLogger sets a concrete logging implementation for all deferred Loggers.
SetLogger = log.SetLogger
)

func Object[T client.Object](obj T) WatchObject {
return builder.Object(obj)
}
9 changes: 5 additions & 4 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/types"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -84,10 +85,10 @@ func GenericExample() {
os.Exit(1)
}

err = ctrl.
NewControllerManagedBy(manager). // Create the Controller
With(ctrl.Object(&appsv1.ReplicaSet{})). // ReplicaSet is the Application API
Own(ctrl.Object(&corev1.Pod{})). // ReplicaSet owns Pods created by it
b := ctrl.NewControllerManagedBy(manager) // Create the Controller
// ReplicaSet is the Application API
b.Add(builder.For(b, &appsv1.ReplicaSet{})).
Add(builder.Owns(b, &appsv1.ReplicaSet{}, &corev1.Pod{})). // ReplicaSet owns Pods created by it
Complete(&ReplicaSetReconciler{Client: manager.GetClient()})
if err != nil {
log.Error(err, "could not create controller")
Expand Down
9 changes: 6 additions & 3 deletions examples/builtins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ func main() {
}

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

// Watch Pods and enqueue owning ReplicaSet key
if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}),
handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.ReplicaSet{}, handler.OnlyControllerOwner())); err != nil {
src = source.Kind(mgr.GetCache(), &corev1.Pod{})
src.Prepare(handler.EnqueueRequestForOwner(mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.ReplicaSet{}, handler.OnlyControllerOwner()))
if err := c.Watch(src); err != nil {
entryLog.Error(err, "unable to watch Pods")
os.Exit(1)
}
Expand Down
Loading

0 comments on commit 3d2371d

Please sign in to comment.