-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from 3scale-ops/feat/dynamic-watches
feat/dynamic-watches
- Loading branch information
Showing
14 changed files
with
232 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package reconciler | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
|
||
"github.com/3scale-ops/basereconciler/util" | ||
"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/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// ReconcilerWithTypeTracker is a reconciler with a TypeTracker | ||
// The type tracker is used by the "resource pruner" and "dynamic watches" | ||
// features | ||
type ReconcilerWithTypeTracker interface { | ||
reconcile.Reconciler | ||
BuildTypeTracker(ctrl controller.Controller) | ||
} | ||
|
||
// SetupWithDynamicTypeWatches is a helper to build a controller that can watch resource | ||
// types dynamically. It is typically used within the "SetupWithManager" function. | ||
// Example usage: | ||
// | ||
// func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
// return reconciler.SetupWithDynamicTypeWatches(r, | ||
// ctrl.NewControllerManagedBy(mgr). | ||
// For(&v1alpha1.Test{}). | ||
// // add any other watches here | ||
// Watches(...}.Watches(...), | ||
// ) | ||
// } | ||
func SetupWithDynamicTypeWatches(r ReconcilerWithTypeTracker, bldr *builder.Builder) error { | ||
ctrl, err := bldr.Build(r) | ||
if err != nil { | ||
return err | ||
} | ||
r.BuildTypeTracker(ctrl) | ||
return nil | ||
} | ||
|
||
type typeTracker struct { | ||
seenTypes []schema.GroupVersionKind | ||
ctrl controller.Controller | ||
mu sync.Mutex | ||
} | ||
|
||
func (tt *typeTracker) trackType(gvk schema.GroupVersionKind) bool { | ||
if !util.ContainsBy(tt.seenTypes, func(x schema.GroupVersionKind) bool { | ||
return reflect.DeepEqual(x, gvk) | ||
}) { | ||
tt.mu.Lock() | ||
defer tt.mu.Unlock() | ||
tt.seenTypes = append(tt.seenTypes, gvk) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (r *Reconciler) watchOwned(gvk schema.GroupVersionKind, owner client.Object) error { | ||
o, err := util.NewObjectFromGVK(gvk, r.Scheme) | ||
if err != nil { | ||
return err | ||
} | ||
r.typeTracker.mu.Lock() | ||
defer r.typeTracker.mu.Unlock() | ||
err = r.typeTracker.ctrl.Watch(&source.Kind{Type: o}, &handler.EnqueueRequestForOwner{OwnerType: owner, IsController: true}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// BuildTypeTracker passes the controller to the reconciler so watches | ||
// can be added dynamically | ||
func (r *Reconciler) BuildTypeTracker(ctrl controller.Controller) { | ||
r.typeTracker = typeTracker{ | ||
seenTypes: []schema.GroupVersionKind{}, | ||
ctrl: ctrl, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package resource contains types and methods to reconcile controller owned resources | ||
// It is generalized to work with any GroupVersionKind. | ||
package resource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package test contains a test controller with its test suite | ||
package test |
Oops, something went wrong.