Skip to content

Commit eef257b

Browse files
committed
Adopt Informer.RunWithContext, WatchErrorHandlerWithContext & HandleCrashWithLogger
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent 9fcd561 commit eef257b

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

pkg/cache/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ type Options struct {
207207
// to reduce the caches memory usage.
208208
DefaultTransform toolscache.TransformFunc
209209

210-
// DefaultWatchErrorHandler will be used to the WatchErrorHandler which is called
210+
// DefaultWatchErrorHandler will be used to set the WatchErrorHandler which is called
211211
// whenever ListAndWatch drops the connection with an error.
212212
//
213213
// After calling this handler, the informer will backoff and retry.
214-
DefaultWatchErrorHandler toolscache.WatchErrorHandler
214+
DefaultWatchErrorHandler toolscache.WatchErrorHandlerWithContext
215215

216216
// DefaultUnsafeDisableDeepCopy is the default for UnsafeDisableDeepCopy
217217
// for everything that doesn't specify this.

pkg/cache/internal/informers.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
4449
type 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

111117
type 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
}

pkg/internal/controller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (c *Controller[request]) Start(ctx context.Context) error {
168168
defer c.mu.Unlock()
169169

170170
// TODO(pwittrock): Reconsider HandleCrash
171-
defer utilruntime.HandleCrash()
171+
defer utilruntime.HandleCrashWithLogger(c.LogConstructor(nil))
172172

173173
// NB(directxman12): launch the sources *before* trying to wait for the
174174
// caches to sync so that they have a chance to register their intended

pkg/log/log_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ var _ = Describe("logging", func() {
194194
}()
195195
go func() {
196196
defer GinkgoRecover()
197-
delegLog.WithValues("with-value")
197+
delegLog.WithValues("key", "with-value")
198198
close(withValuesDone)
199199
}()
200200
go func() {
201201
defer GinkgoRecover()
202-
child.WithValues("grandchild")
202+
child.WithValues("key", "grandchild")
203203
close(grandChildDone)
204204
}()
205205
go func() {

0 commit comments

Comments
 (0)