@@ -25,21 +25,26 @@ import (
2525 "sync"
2626 "time"
2727
28+ "github.com/go-logr/logr"
2829 apierrors "k8s.io/apimachinery/pkg/api/errors"
2930 "k8s.io/apimachinery/pkg/api/meta"
3031 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132 "k8s.io/apimachinery/pkg/runtime"
3233 "k8s.io/apimachinery/pkg/runtime/schema"
3334 "k8s.io/apimachinery/pkg/runtime/serializer"
35+ "k8s.io/apimachinery/pkg/util/wait"
3436 "k8s.io/apimachinery/pkg/watch"
3537 "k8s.io/client-go/dynamic"
3638 "k8s.io/client-go/metadata"
3739 "k8s.io/client-go/rest"
3840 "k8s.io/client-go/tools/cache"
3941 "sigs.k8s.io/controller-runtime/pkg/client/apiutil"
42+ logf "sigs.k8s.io/controller-runtime/pkg/internal/log"
4043 "sigs.k8s.io/controller-runtime/pkg/internal/syncs"
4144)
4245
46+ var log = logf .RuntimeLog .WithName ("cache" )
47+
4348// InformersOpts configures an InformerMap.
4449type InformersOpts struct {
4550 HTTPClient * http.Client
@@ -52,7 +57,7 @@ type InformersOpts struct {
5257 Transform cache.TransformFunc
5358 UnsafeDisableDeepCopy bool
5459 EnableWatchBookmarks bool
55- WatchErrorHandler cache.WatchErrorHandler
60+ WatchErrorHandler cache.WatchErrorHandlerWithContext
5661}
5762
5863// NewInformers creates a new InformersMap that can create informers under the hood.
@@ -105,7 +110,8 @@ func (c *Cache) Start(stop <-chan struct{}) {
105110 // Stop on either the whole map stopping or just this informer being removed.
106111 internalStop , cancel := syncs .MergeChans (stop , c .stop )
107112 defer cancel ()
108- c .Informer .Run (internalStop )
113+ // Convert the stop channel to a context and then add the logger.
114+ c .Informer .RunWithContext (logr .NewContext (wait .ContextForChannel (internalStop ), log ))
109115}
110116
111117type tracker struct {
@@ -181,10 +187,10 @@ type Informers struct {
181187 // NewInformer allows overriding of the shared index informer constructor for testing.
182188 newInformer func (cache.ListerWatcher , runtime.Object , time.Duration , cache.Indexers ) cache.SharedIndexInformer
183189
184- // WatchErrorHandler allows the shared index informer's
190+ // watchErrorHandler allows the shared index informer's
185191 // watchErrorHandler to be set by overriding the options
186192 // or to use the default watchErrorHandler
187- watchErrorHandler cache.WatchErrorHandler
193+ watchErrorHandler cache.WatchErrorHandlerWithContext
188194}
189195
190196// Start calls Run on each of the informers and sets started to true. Blocks on the context.
@@ -376,7 +382,7 @@ func (ip *Informers) addInformerToMap(gvk schema.GroupVersionKind, obj runtime.O
376382
377383 // Set WatchErrorHandler on SharedIndexInformer if set
378384 if ip .watchErrorHandler != nil {
379- if err := sharedIndexInformer .SetWatchErrorHandler (ip .watchErrorHandler ); err != nil {
385+ if err := sharedIndexInformer .SetWatchErrorHandlerWithContext (ip .watchErrorHandler ); err != nil {
380386 return nil , false , err
381387 }
382388 }
0 commit comments