From 79285c97444c92ec3eb6b507c1673a732e21e64c Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Wed, 11 Jan 2023 14:24:46 +0100 Subject: [PATCH] fix: Fix watch handlers for TextTemplate --- .../comments/base_comment_reconciler.go | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/controllers/comments/base_comment_reconciler.go b/controllers/comments/base_comment_reconciler.go index 139bd67..2e62679 100644 --- a/controllers/comments/base_comment_reconciler.go +++ b/controllers/comments/base_comment_reconciler.go @@ -201,36 +201,36 @@ 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 { + list := buildList() + + var ref templatesv1alpha1.ObjectRef + ref.Name = obj.GetName() + switch obj.(type) { + case *corev1.ConfigMap: + ref.Kind = "ConfigMap" + case *templatesv1alpha1.TextTemplate: + ref.Kind = "TextTemplate" + default: + return nil + } + if err := r.List(context.Background(), list, client.MatchingFields{ + indexKey: ref.String(), + }); err != nil { + return nil + } + var ret []reconcile.Request + for _, o := range list.GetItems() { + ret = append(ret, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(o)}) + } + return ret + }) + return ctrl.NewControllerManagedBy(mgr). For(obj, builder.WithPredicates( predicate.GenerationChangedPredicate{}, )). - Watches( - &source.Kind{Type: &corev1.ConfigMap{}}, - handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request { - list := buildList() - - var ref templatesv1alpha1.ObjectRef - ref.Name = obj.GetName() - switch obj.(type) { - case *corev1.ConfigMap: - ref.Kind = "ConfigMap" - case *templatesv1alpha1.TextTemplate: - ref.Kind = "TextTemplate" - default: - return nil - } - if err := r.List(context.Background(), list, client.MatchingFields{ - indexKey: ref.String(), - }); err != nil { - return nil - } - var ret []reconcile.Request - for _, o := range list.GetItems() { - ret = append(ret, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(o)}) - } - return ret - }), - ). + Watches(&source.Kind{Type: &corev1.ConfigMap{}}, watchHandler). + Watches(&source.Kind{Type: &templatesv1alpha1.TextTemplate{}}, watchHandler). Complete(r2) }