Skip to content

Commit

Permalink
Avoid a race condition while initializing a ChildReconciler (#227)
Browse files Browse the repository at this point in the history
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 <andrewssc@vmware.com>
  • Loading branch information
scothis committed May 5, 2022
1 parent c8e0a01 commit fe9dc70
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions reconcilers/reconcilers.go
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"reflect"
"sync"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fe9dc70

Please sign in to comment.