Skip to content

Commit

Permalink
Enable per-reconcile logger customization
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Feb 16, 2022
1 parent 3fb2cfd commit 42d8935
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pkg/builder/controller.go
Expand Up @@ -154,6 +154,13 @@ func (blder *Builder) WithLogger(log logr.Logger) *Builder {
return blder
}

// WithLoggerCustomizer sets a LoggerCustomizer which allows per-reconcile customization
// of the logger.
func (blder *Builder) WithLoggerCustomizer(loggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger) *Builder {
blder.ctrlOptions.LoggerCustomizer = loggerCustomizer
return blder
}

// Named sets the name of the controller to the given name. The name shows up
// in metrics, among other things, and thus should be a prometheus compatible name
// (underscores and alphanumeric characters only).
Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/controller.go
Expand Up @@ -50,6 +50,9 @@ type Options struct {
// request via the context field.
Log logr.Logger

// LoggerCustomizer customizes the logger for individual reconciliations.
LoggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger

// CacheSyncTimeout refers to the time limit set to wait for syncing caches.
// Defaults to 2 minutes if not set.
CacheSyncTimeout time.Duration
Expand Down Expand Up @@ -137,6 +140,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
SetFields: mgr.SetFields,
Name: name,
Log: options.Log.WithName("controller").WithName(name).WithValues("controller", name),
LoggerCustomizer: options.LoggerCustomizer,
RecoverPanic: options.RecoverPanic,
}, nil
}
13 changes: 10 additions & 3 deletions pkg/internal/controller/controller.go
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/go-logr/logr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/util/workqueue"

"sigs.k8s.io/controller-runtime/pkg/handler"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/internal/controller/metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -63,6 +64,9 @@ type Controller struct {
// Deprecated: the caller should handle injected fields itself.
SetFields func(i interface{}) error

// LoggerCustomizer customizes the logger for individual reconciliations.
LoggerCustomizer func(logr.Logger, reconcile.Request) logr.Logger

// mu is used to synchronize Controller setup
mu sync.Mutex

Expand Down Expand Up @@ -109,8 +113,6 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (_ re
}
}()
}
log := c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
ctx = logf.IntoContext(ctx, log)
return c.Do.Reconcile(ctx, req)
}

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

log := c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
var log logr.Logger
if c.LoggerCustomizer != nil {
log = c.LoggerCustomizer(c.Log, req)
} else {
log = c.Log.WithValues("name", req.Name, "namespace", req.Namespace)
}
ctx = logf.IntoContext(ctx, log)

// RunInformersAndControllers the syncHandler, passing it the Namespace/Name string of the
Expand Down

0 comments on commit 42d8935

Please sign in to comment.