@@ -30,14 +30,16 @@ func (c baseController) Name() string {
3030 return c .name
3131}
3232
33- func waitForNamedCacheSync (controllerName string , stopCh <- chan struct {}, cacheSyncs ... cache.InformerSynced ) error {
34- klog .Infof ("Waiting for caches to sync for %s" , controllerName )
33+ func waitForNamedCacheSync (ctx context.Context , controllerName string , stopCh <- chan struct {}, cacheSyncs ... cache.InformerSynced ) error {
34+ logger := klog .FromContext (ctx )
35+
36+ logger .Info ("Waiting for caches to sync" )
3537
3638 if ! cache .WaitForCacheSync (stopCh , cacheSyncs ... ) {
3739 return fmt .Errorf ("unable to sync caches for %s" , controllerName )
3840 }
3941
40- klog . Infof ("Caches are synced for %s " , controllerName )
42+ logger . Info ("Caches are synced" )
4143
4244 return nil
4345}
@@ -47,10 +49,13 @@ func (c *baseController) SyncContext() SyncContext {
4749}
4850
4951func (c * baseController ) Run (ctx context.Context , workers int ) {
52+ logger := klog .FromContext (ctx ).WithName (c .name )
53+ ctx = klog .NewContext (ctx , logger )
54+
5055 // give caches 10 minutes to sync
5156 cacheSyncCtx , cacheSyncCancel := context .WithTimeout (ctx , c .cacheSyncTimeout )
5257 defer cacheSyncCancel ()
53- err := waitForNamedCacheSync (c .name , cacheSyncCtx .Done (), c .cachesToSync ... )
58+ err := waitForNamedCacheSync (ctx , c .name , cacheSyncCtx .Done (), c .cachesToSync ... )
5459 if err != nil {
5560 select {
5661 case <- ctx .Done ():
@@ -67,19 +72,19 @@ func (c *baseController) Run(ctx context.Context, workers int) {
6772
6873 var workerWg sync.WaitGroup
6974 defer func () {
70- defer klog . Infof ("All %s workers have been terminated" , c . name )
75+ defer logger . Info ("All workers have been terminated" )
7176 workerWg .Wait ()
7277 }()
7378
7479 // queueContext is used to track and initiate queue shutdown
75- queueContext , queueContextCancel := context .WithCancel (context . TODO () )
80+ queueContext , queueContextCancel := context .WithCancel (ctx )
7681
7782 for i := 1 ; i <= workers ; i ++ {
78- klog . Infof ("Starting #%d worker of %s controller ..." , i , c . name )
83+ logger . Info ("Starting worker of controller ..." , "numberOfWorkers" , i )
7984 workerWg .Add (1 )
8085 go func () {
8186 defer func () {
82- klog . Infof ("Shutting down worker of %s controller ..." , c . name )
87+ logger . Info ("Shutting down worker of controller ..." )
8388 workerWg .Done ()
8489 }()
8590 c .runWorker (queueContext )
@@ -104,7 +109,7 @@ func (c *baseController) Run(ctx context.Context, workers int) {
104109 // Wait for all workers to finish their job.
105110 // at this point the Run() can hang and caller have to implement the logic that will kill
106111 // this controller (SIGKILL).
107- klog . Infof ("Shutting down %s ..." , c . name )
112+ logger . Info ("Shutting down ..." )
108113}
109114
110115func (c * baseController ) Sync (ctx context.Context , syncCtx SyncContext , key string ) error {
@@ -140,6 +145,8 @@ func (c *baseController) runWorker(queueCtx context.Context) {
140145}
141146
142147func (c * baseController ) processNextWorkItem (queueCtx context.Context ) {
148+ logger := klog .FromContext (queueCtx )
149+
143150 key , quit := c .syncContext .Queue ().Get ()
144151 if quit {
145152 return
@@ -150,10 +157,10 @@ func (c *baseController) processNextWorkItem(queueCtx context.Context) {
150157 queueKey := key
151158
152159 if err := c .sync (queueCtx , syncCtx , queueKey ); err != nil {
153- if klog .V (4 ).Enabled () || key != "key" {
154- utilruntime .HandleError ( fmt . Errorf ( "%q controller failed to sync %q, err: %w" , c . name , key , err ) )
160+ if logger .V (4 ).Enabled () || key != "key" {
161+ utilruntime .HandleErrorWithContext ( queueCtx , err , " controller failed to sync" , "key" , key , "error" , err )
155162 } else {
156- utilruntime .HandleError ( fmt . Errorf ( "%s reconciliation failed: %w " , c . name , err ) )
163+ utilruntime .HandleErrorWithContext ( queueCtx , err , " reconciliation failed" , "error" , err )
157164 }
158165 c .syncContext .Queue ().AddRateLimited (key )
159166 return
0 commit comments