Skip to content

Commit

Permalink
fix: Fixes required for latest controller-runtime version
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed May 17, 2023
1 parent 101815b commit d6a9279
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
5 changes: 4 additions & 1 deletion controllers/base_template_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/source"
"sync"
)

type BaseTemplateReconciler struct {
client.Client

Manager manager.Manager
Scheme *runtime.Scheme
FieldManager string

Expand Down Expand Up @@ -73,7 +76,7 @@ func (r *BaseTemplateReconciler) addWatchForKind(ctx context.Context, gvk schema
var dummy unstructured.Unstructured
dummy.SetGroupVersionKind(gvk)

err := r.controller.Watch(&source.Kind{Type: &dummy}, eventHandler)
err := r.controller.Watch(source.Kind(r.Manager.GetCache(), &dummy), eventHandler)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions controllers/comments/base_comment_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"strings"
)

Expand Down Expand Up @@ -201,7 +200,7 @@ func (r *BaseCommentReconciler) baseSetupWithManager(mgr ctrl.Manager, r2 reconc
return fmt.Errorf("failed setting index fields: %w", err)
}

watchHandler := handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request {
watchHandler := handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
list := buildList()

var ref templatesv1alpha1.ObjectRef
Expand All @@ -214,7 +213,7 @@ func (r *BaseCommentReconciler) baseSetupWithManager(mgr ctrl.Manager, r2 reconc
default:
return nil
}
if err := r.List(context.Background(), list, client.MatchingFields{
if err := r.List(ctx, list, client.MatchingFields{
indexKey: ref.String(),
}); err != nil {
return nil
Expand All @@ -230,7 +229,7 @@ func (r *BaseCommentReconciler) baseSetupWithManager(mgr ctrl.Manager, r2 reconc
For(obj, builder.WithPredicates(
predicate.GenerationChangedPredicate{},
)).
Watches(&source.Kind{Type: &corev1.ConfigMap{}}, watchHandler).
Watches(&source.Kind{Type: &templatesv1alpha1.TextTemplate{}}, watchHandler).
Watches(&corev1.ConfigMap{}, watchHandler).
Watches(&templatesv1alpha1.TextTemplate{}, watchHandler).
Complete(r2)
}
13 changes: 10 additions & 3 deletions controllers/objecthandler/objecthandler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"sync"
Expand All @@ -44,6 +46,8 @@ const forObjectIndexKey = "spec.forObject"
// ObjectHandlerReconciler reconciles a ObjectHandler object
type ObjectHandlerReconciler struct {
client.Client
Manager manager.Manager

Scheme *runtime.Scheme
FieldManager string

Expand Down Expand Up @@ -97,6 +101,7 @@ func (r *ObjectHandlerReconciler) Reconcile(ctx context.Context, req ctrl.Reques

// SetupWithManager sets up the controller with the Manager.
func (r *ObjectHandlerReconciler) SetupWithManager(mgr ctrl.Manager, concurrent int) error {
r.Manager = mgr
r.watchedKinds = map[schema.GroupVersionKind]bool{}

// Index the ObjectHandler by the objects they are for.
Expand All @@ -113,7 +118,9 @@ func (r *ObjectHandlerReconciler) SetupWithManager(mgr ctrl.Manager, concurrent
c, err := ctrl.NewControllerManagedBy(mgr).
For(&templatesv1alpha1.ObjectHandler{}).
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrent,
Controller: config.Controller{
MaxConcurrentReconciles: concurrent,
},
}).
Build(r)
if err != nil {
Expand Down Expand Up @@ -256,9 +263,9 @@ func (r *ObjectHandlerReconciler) addWatchForKind(ctx context.Context, sr *templ
var dummy unstructured.Unstructured
dummy.SetGroupVersionKind(gvk)

err = r.controller.Watch(&source.Kind{Type: &dummy}, handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
err = r.controller.Watch(source.Kind(r.Manager.GetCache(), &dummy), handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
var list templatesv1alpha1.ObjectHandlerList
err := r.List(context.Background(), &list, client.MatchingFields{
err := r.List(ctx, &list, client.MatchingFields{
forObjectIndexKey: controllers.BuildObjectIndexValue(object),
})
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions controllers/objecttemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
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/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
Expand Down Expand Up @@ -427,6 +428,8 @@ func (r *ObjectTemplateReconciler) renderTemplates(j2 *jinja2.Jinja2, rt *templa

// SetupWithManager sets up the controller with the Manager.
func (r *ObjectTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent int) error {
r.Manager = mgr

// Index the ObjectHandler by the objects they are for.
if err := mgr.GetCache().IndexField(context.TODO(), &templatesv1alpha1.ObjectTemplate{}, forMatrixObjectKey,
func(object client.Object) []string {
Expand All @@ -447,7 +450,9 @@ func (r *ObjectTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent
predicate.Or(predicate.GenerationChangedPredicate{}),
)).
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrent,
Controller: config.Controller{
MaxConcurrentReconciles: concurrent,
},
}).
Build(r)
if err != nil {
Expand All @@ -459,7 +464,7 @@ func (r *ObjectTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent
}

func (r *ObjectTemplateReconciler) buildWatchEventHandler(indexField string) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
var list templatesv1alpha1.ObjectTemplateList

err := r.List(context.Background(), &list, client.MatchingFields{
Expand Down
10 changes: 7 additions & 3 deletions controllers/texttemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
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/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -191,6 +192,7 @@ func (r *TextTemplateReconciler) doReconcile(ctx context.Context, tt *templatesv

// SetupWithManager sets up the controller with the Manager.
func (r *TextTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent int) error {
r.Manager = mgr
r.watchedKinds = map[schema.GroupVersionKind]bool{}

// Index the TextTemplate by the objects they are for.
Expand Down Expand Up @@ -225,7 +227,9 @@ func (r *TextTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent i
predicate.Or(predicate.GenerationChangedPredicate{}),
)).
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrent,
Controller: config.Controller{
MaxConcurrentReconciles: concurrent,
},
}).
Build(r)
if err != nil {
Expand All @@ -237,10 +241,10 @@ func (r *TextTemplateReconciler) SetupWithManager(mgr ctrl.Manager, concurrent i
}

func (r *TextTemplateReconciler) buildWatchEventHandler(indexField string) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
var list templatesv1alpha1.TextTemplateList

err := r.List(context.Background(), &list, client.MatchingFields{
err := r.List(ctx, &list, client.MatchingFields{
indexField: BuildObjectIndexValue(object),
})
if err != nil {
Expand Down

0 comments on commit d6a9279

Please sign in to comment.