@@ -25,6 +25,7 @@ import (
25
25
26
26
"github.com/go-logr/logr"
27
27
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28
+ "k8s.io/apimachinery/pkg/util/uuid"
28
29
"k8s.io/client-go/util/workqueue"
29
30
"sigs.k8s.io/controller-runtime/pkg/handler"
30
31
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
@@ -83,8 +84,11 @@ type Controller struct {
83
84
// startWatches maintains a list of sources, handlers, and predicates to start when the controller is started.
84
85
startWatches []watchDescription
85
86
86
- // Log is used to log messages to users during reconciliation, or for example when a watch is started.
87
- Log logr.Logger
87
+ // LogConstructor is used to construct a logger to then log messages to users during reconciliation,
88
+ // or for example when a watch is started.
89
+ // Note: LogConstructor has to be able to handle nil requests as we are also using it
90
+ // outside the context of a reconciliation.
91
+ LogConstructor func (request * reconcile.Request ) logr.Logger
88
92
89
93
// RecoverPanic indicates whether the panic caused by reconcile should be recovered.
90
94
RecoverPanic bool
@@ -99,7 +103,6 @@ type watchDescription struct {
99
103
100
104
// Reconcile implements reconcile.Reconciler.
101
105
func (c * Controller ) Reconcile (ctx context.Context , req reconcile.Request ) (_ reconcile.Result , err error ) {
102
- log := c .Log .WithValues ("name" , req .Name , "namespace" , req .Namespace )
103
106
defer func () {
104
107
if r := recover (); r != nil {
105
108
if c .RecoverPanic {
@@ -110,11 +113,11 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (_ re
110
113
return
111
114
}
112
115
116
+ log := logf .FromContext (ctx )
113
117
log .Info (fmt .Sprintf ("Observed a panic in reconciler: %v" , r ))
114
118
panic (r )
115
119
}
116
120
}()
117
- ctx = logf .IntoContext (ctx , log )
118
121
return c .Do .Reconcile (ctx , req )
119
122
}
120
123
@@ -144,7 +147,7 @@ func (c *Controller) Watch(src source.Source, evthdler handler.EventHandler, prc
144
147
return nil
145
148
}
146
149
147
- c .Log .Info ("Starting EventSource" , "source" , src )
150
+ c .LogConstructor ( nil ) .Info ("Starting EventSource" , "source" , src )
148
151
return src .Start (c .ctx , evthdler , c .Queue , prct ... )
149
152
}
150
153
@@ -179,15 +182,15 @@ func (c *Controller) Start(ctx context.Context) error {
179
182
// caches to sync so that they have a chance to register their intendeded
180
183
// caches.
181
184
for _ , watch := range c .startWatches {
182
- c .Log .Info ("Starting EventSource" , "source" , fmt .Sprintf ("%s" , watch .src ))
185
+ c .LogConstructor ( nil ) .Info ("Starting EventSource" , "source" , fmt .Sprintf ("%s" , watch .src ))
183
186
184
187
if err := watch .src .Start (ctx , watch .handler , c .Queue , watch .predicates ... ); err != nil {
185
188
return err
186
189
}
187
190
}
188
191
189
192
// Start the SharedIndexInformer factories to begin populating the SharedIndexInformer caches
190
- c .Log .Info ("Starting Controller" )
193
+ c .LogConstructor ( nil ) .Info ("Starting Controller" )
191
194
192
195
for _ , watch := range c .startWatches {
193
196
syncingSource , ok := watch .src .(source.SyncingSource )
@@ -204,7 +207,7 @@ func (c *Controller) Start(ctx context.Context) error {
204
207
// is an error or a timeout
205
208
if err := syncingSource .WaitForSync (sourceStartCtx ); err != nil {
206
209
err := fmt .Errorf ("failed to wait for %s caches to sync: %w" , c .Name , err )
207
- c .Log .Error (err , "Could not wait for Cache to sync" )
210
+ c .LogConstructor ( nil ) .Error (err , "Could not wait for Cache to sync" )
208
211
return err
209
212
}
210
213
@@ -221,7 +224,7 @@ func (c *Controller) Start(ctx context.Context) error {
221
224
c .startWatches = nil
222
225
223
226
// Launch workers to process resources
224
- c .Log .Info ("Starting workers" , "worker count" , c .MaxConcurrentReconciles )
227
+ c .LogConstructor ( nil ) .Info ("Starting workers" , "worker count" , c .MaxConcurrentReconciles )
225
228
wg .Add (c .MaxConcurrentReconciles )
226
229
for i := 0 ; i < c .MaxConcurrentReconciles ; i ++ {
227
230
go func () {
@@ -241,9 +244,9 @@ func (c *Controller) Start(ctx context.Context) error {
241
244
}
242
245
243
246
<- ctx .Done ()
244
- c .Log .Info ("Shutdown signal received, waiting for all workers to finish" )
247
+ c .LogConstructor ( nil ) .Info ("Shutdown signal received, waiting for all workers to finish" )
245
248
wg .Wait ()
246
- c .Log .Info ("All workers finished" )
249
+ c .LogConstructor ( nil ) .Info ("All workers finished" )
247
250
return nil
248
251
}
249
252
@@ -295,19 +298,21 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
295
298
c .updateMetrics (time .Since (reconcileStartTS ))
296
299
}()
297
300
298
- // Make sure that the the object is a valid request.
301
+ // Make sure that the object is a valid request.
299
302
req , ok := obj .(reconcile.Request )
300
303
if ! ok {
301
304
// As the item in the workqueue is actually invalid, we call
302
305
// Forget here else we'd go into a loop of attempting to
303
306
// process a work item that is invalid.
304
307
c .Queue .Forget (obj )
305
- c .Log .Error (nil , "Queue item was not a Request" , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
308
+ c .LogConstructor ( nil ) .Error (nil , "Queue item was not a Request" , "type" , fmt .Sprintf ("%T" , obj ), "value" , obj )
306
309
// Return true, don't take a break
307
310
return
308
311
}
309
312
310
- log := c .Log .WithValues ("name" , req .Name , "namespace" , req .Namespace )
313
+ log := c .LogConstructor (& req )
314
+
315
+ log = log .WithValues ("reconcileID" , uuid .NewUUID ())
311
316
ctx = logf .IntoContext (ctx , log )
312
317
313
318
// RunInformersAndControllers the syncHandler, passing it the Namespace/Name string of the
@@ -340,7 +345,7 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
340
345
341
346
// GetLogger returns this controller's logger.
342
347
func (c * Controller ) GetLogger () logr.Logger {
343
- return c .Log
348
+ return c .LogConstructor ( nil )
344
349
}
345
350
346
351
// InjectFunc implement SetFields.Injector.
0 commit comments