Skip to content

Commit 42d8935

Browse files
committed
Enable per-reconcile logger customization
Signed-off-by: Stefan Büringer [email protected]
1 parent 3fb2cfd commit 42d8935

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

pkg/builder/controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ func (blder *Builder) WithLogger(log logr.Logger) *Builder {
154154
return blder
155155
}
156156

157+
// WithLoggerCustomizer sets a LoggerCustomizer which allows per-reconcile customization
158+
// of the logger.
159+
func (blder *Builder) WithLoggerCustomizer(loggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger) *Builder {
160+
blder.ctrlOptions.LoggerCustomizer = loggerCustomizer
161+
return blder
162+
}
163+
157164
// Named sets the name of the controller to the given name. The name shows up
158165
// in metrics, among other things, and thus should be a prometheus compatible name
159166
// (underscores and alphanumeric characters only).

pkg/controller/controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type Options struct {
5050
// request via the context field.
5151
Log logr.Logger
5252

53+
// LoggerCustomizer customizes the logger for individual reconciliations.
54+
LoggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger
55+
5356
// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
5457
// Defaults to 2 minutes if not set.
5558
CacheSyncTimeout time.Duration
@@ -137,6 +140,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
137140
SetFields: mgr.SetFields,
138141
Name: name,
139142
Log: options.Log.WithName("controller").WithName(name).WithValues("controller", name),
143+
LoggerCustomizer: options.LoggerCustomizer,
140144
RecoverPanic: options.RecoverPanic,
141145
}, nil
142146
}

pkg/internal/controller/controller.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/go-logr/logr"
2727
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2828
"k8s.io/client-go/util/workqueue"
29+
2930
"sigs.k8s.io/controller-runtime/pkg/handler"
3031
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
3132
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -63,6 +64,9 @@ type Controller struct {
6364
// Deprecated: the caller should handle injected fields itself.
6465
SetFields func(i interface{}) error
6566

67+
// LoggerCustomizer customizes the logger for individual reconciliations.
68+
LoggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger
69+
6670
// mu is used to synchronize Controller setup
6771
mu sync.Mutex
6872

@@ -109,8 +113,6 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (_ re
109113
}
110114
}()
111115
}
112-
log := c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
113-
ctx = logf.IntoContext(ctx, log)
114116
return c.Do.Reconcile(ctx, req)
115117
}
116118

@@ -303,7 +305,12 @@ func (c *Controller) reconcileHandler(ctx context.Context, obj interface{}) {
303305
return
304306
}
305307

306-
log := c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
308+
var log logr.Logger
309+
if c.LoggerCustomizer != nil {
310+
log = c.LoggerCustomizer(c.Log, req)
311+
} else {
312+
log = c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
313+
}
307314
ctx = logf.IntoContext(ctx, log)
308315

309316
// RunInformersAndControllers the syncHandler, passing it the Namespace/Name string of the

0 commit comments

Comments
 (0)