From fe9dc7014cc04df2c0d66c31089f35e788ebd2bf Mon Sep 17 00:00:00 2001 From: Scott Andrews Date: Thu, 5 May 2022 19:41:58 -0400 Subject: [PATCH] Avoid a race condition while initializing a ChildReconciler (#227) This isn't a big deal as worst case we create an extra cache, but we can easily avoid it. Signed-off-by: Scott Andrews --- reconcilers/reconcilers.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/reconcilers/reconcilers.go b/reconcilers/reconcilers.go index 31d9436..cf7d518 100644 --- a/reconcilers/reconcilers.go +++ b/reconcilers/reconcilers.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "reflect" + "sync" "time" "github.com/go-logr/logr" @@ -557,6 +558,7 @@ type ChildReconciler struct { // mutation webhooks. This cache is used to avoid unnecessary update calls // that would actually have no effect. mutationCache *cache.Expiring + lazyInit sync.Once } func (r *ChildReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, bldr *builder.Builder) error { @@ -699,9 +701,9 @@ func (r *ChildReconciler) Reconcile(ctx context.Context, parent client.Object) ( WithValues("childType", gvk(r.ChildType, c.Scheme())) ctx = logr.NewContext(ctx, log) - if r.mutationCache == nil { + r.lazyInit.Do(func() { r.mutationCache = cache.NewExpiring() - } + }) child, err := r.reconcile(ctx, parent) if err != nil {