Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 馃尡 Enable per-reconcile logger customization #1812

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
}()
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done twice before. Here and one layer above

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