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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚠️ Bump github.com/go-logr/zapr & github.com/go-logr/logr to v1.1.0 #1593

Closed
wants to merge 1 commit into from
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
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.16
require (
github.com/evanphx/json-patch v4.11.0+incompatible
github.com/fsnotify/fsnotify v1.4.9
github.com/go-logr/logr v0.4.0
github.com/go-logr/zapr v0.4.0
github.com/go-logr/logr v1.2.0
github.com/go-logr/zapr v1.2.0
github.com/imdario/mergo v0.3.12 // indirect
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.15.0
Expand All @@ -23,6 +23,7 @@ require (
k8s.io/apimachinery v0.22.2
k8s.io/client-go v0.22.2
k8s.io/component-base v0.22.2
k8s.io/klog/v2 v2.20.0 // indirect
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a
sigs.k8s.io/yaml v1.2.0
)
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/zapr v0.4.0 h1:uc1uML3hRYL9/ZZPdgHS/n8Nzo+eaYL/Efxkkamf7OM=
github.com/go-logr/zapr v0.4.0/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk=
github.com/go-logr/logr v1.0.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.0 h1:QK40JKJyMdUDz+h+xvCsru/bJhvG0UxvePV0ufL/AcE=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/zapr v1.2.0 h1:n4JnPI1T3Qq1SFEi/F8rwLrZERp2bso19PJZDB9dayk=
github.com/go-logr/zapr v1.2.0/go.mod h1:Qa4Bsj2Vb+FAVeAKsLD8RLQ+YRJB8YDmOAKxaBQf7Ro=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
Expand Down Expand Up @@ -752,8 +754,9 @@ k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8
k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.9.0 h1:D7HV+n1V57XeZ0m6tdRkfknthUaM06VFbWldOFh8kzM=
k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.20.0 h1:tlyxlSvd63k7axjhuchckaRJm+a92z5GSOrTOQY5sHw=
k8s.io/klog/v2 v2.20.0/go.mod h1:Gm8eSIfQN6457haJuPaMxZw4wyP5k+ykPFlrhQDvhvw=
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e h1:KLHHjkdQFomZy8+06csTWZ0m1343QqxZhR2LJ1OxCYM=
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
k8s.io/utils v0.0.0-20210819203725-bdf08cb9a70a h1:8dYfu/Fc9Gz2rNJKB9IQRGgQOh2clmRzNIPPY1xLY5g=
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (blder *Builder) doController(r reconcile.Reconciler) error {
}

// Setup the logger.
if ctrlOptions.Log == nil {
if ctrlOptions.Log.GetSink() == nil {
ctrlOptions.Log = blder.mgr.GetLogger()
}
ctrlOptions.Log = ctrlOptions.Log.WithValues("reconciler group", gvk.Group, "reconciler kind", gvk.Kind)
Expand Down
26 changes: 19 additions & 7 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,26 @@ func (typedNoop) Reconcile(context.Context, reconcile.Request) (reconcile.Result
return reconcile.Result{}, nil
}

type testLogger struct {
type testLogSink struct {
logr.Logger
}

func (l *testLogger) WithName(_ string) logr.Logger {
func (l *testLogSink) Init(info logr.RuntimeInfo) {
}

func (l *testLogSink) Enabled(level int) bool {
return true
}

func (l *testLogSink) Info(level int, msg string, keysAndValues ...interface{}) {

}

func (l *testLogSink) WithValues(keysAndValues ...interface{}) logr.LogSink {
return l
}
func (l *testLogger) WithValues(_ ...interface{}) logr.Logger {

func (l *testLogSink) WithName(name string) logr.LogSink {
return l
}

Expand Down Expand Up @@ -225,12 +237,12 @@ var _ = Describe("application", func() {

It("should override logger during creation of controller", func() {

logger := &testLogger{}
logSink := &testLogSink{}
newController = func(name string, mgr manager.Manager, options controller.Options) (controller.Controller, error) {
if options.Log == logger {
if options.Log.GetSink() == logSink {
return controller.New(name, mgr, options)
}
return nil, fmt.Errorf("logger expected %T but found %T", logger, options.Log)
return nil, fmt.Errorf("logSink expected %T but found %T", logSink, options.Log)
}

By("creating a controller manager")
Expand All @@ -240,7 +252,7 @@ var _ = Describe("application", func() {
instance, err := ControllerManagedBy(m).
For(&appsv1.ReplicaSet{}).
Owns(&appsv1.ReplicaSet{}).
WithLogger(logger).
WithLogger(logr.New(logSink)).
Build(noop)
Expect(err).NotTo(HaveOccurred())
Expect(instance).NotTo(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func setOptionsDefaults(options Options) Options {
}
}

if options.Logger == nil {
if options.Logger.GetSink() == nil {
options.Logger = logf.RuntimeLog.WithName("cluster")
}

Expand Down
1 change: 1 addition & 0 deletions pkg/config/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewUnmanaged(name string, mgr manager.Manager, options Options) (Controller
return nil, fmt.Errorf("must specify Name for Controller")
}

if options.Log == nil {
if options.Log.GetSink() == nil {
options.Log = mgr.GetLogger()
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("recorder.Provider", func() {
makeBroadcaster := func() (record.EventBroadcaster, bool) { return record.NewBroadcaster(), true }
Describe("NewProvider", func() {
It("should return a provider instance and a nil error.", func() {
provider, err := recorder.NewProvider(cfg, scheme.Scheme, logr.DiscardLogger{}, makeBroadcaster)
provider, err := recorder.NewProvider(cfg, scheme.Scheme, logr.Discard(), makeBroadcaster)
Expect(provider).NotTo(BeNil())
Expect(err).NotTo(HaveOccurred())
})
Expand All @@ -38,14 +38,14 @@ var _ = Describe("recorder.Provider", func() {
// Invalid the config
cfg1 := *cfg
cfg1.Host = "invalid host"
_, err := recorder.NewProvider(&cfg1, scheme.Scheme, logr.DiscardLogger{}, makeBroadcaster)
_, err := recorder.NewProvider(&cfg1, scheme.Scheme, logr.Discard(), makeBroadcaster)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("failed to init client"))
})
})
Describe("GetEventRecorder", func() {
It("should return a recorder instance.", func() {
provider, err := recorder.NewProvider(cfg, scheme.Scheme, logr.DiscardLogger{}, makeBroadcaster)
provider, err := recorder.NewProvider(cfg, scheme.Scheme, logr.Discard(), makeBroadcaster)
Expect(err).NotTo(HaveOccurred())

recorder := provider.GetEventRecorderFor("test")
Expand Down
121 changes: 49 additions & 72 deletions pkg/log/deleg.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ import (
// loggerPromise knows how to populate a concrete logr.Logger
// with options, given an actual base logger later on down the line.
type loggerPromise struct {
logger *DelegatingLogger
logger *delegatingLogSink
childPromises []*loggerPromise
promisesLock sync.Mutex

name *string
tags []interface{}
level int
name *string
tags []interface{}
}

func (p *loggerPromise) WithName(l *DelegatingLogger, name string) *loggerPromise {
func (p *loggerPromise) WithName(l *delegatingLogSink, name string) *loggerPromise {
res := &loggerPromise{
logger: l,
name: &name,
Expand All @@ -48,7 +47,7 @@ func (p *loggerPromise) WithName(l *DelegatingLogger, name string) *loggerPromis
}

// WithValues provides a new Logger with the tags appended.
func (p *loggerPromise) WithValues(l *DelegatingLogger, tags ...interface{}) *loggerPromise {
func (p *loggerPromise) WithValues(l *delegatingLogSink, tags ...interface{}) *loggerPromise {
res := &loggerPromise{
logger: l,
tags: tags,
Expand All @@ -61,61 +60,54 @@ func (p *loggerPromise) WithValues(l *DelegatingLogger, tags ...interface{}) *lo
return res
}

func (p *loggerPromise) V(l *DelegatingLogger, level int) *loggerPromise {
res := &loggerPromise{
logger: l,
level: level,
promisesLock: sync.Mutex{},
}
// Fulfill instantiates the Logger with the provided logSink.
func (p *loggerPromise) Fulfill(parentLogSink logr.LogSink) {
sink := parentLogSink

p.promisesLock.Lock()
defer p.promisesLock.Unlock()
p.childPromises = append(p.childPromises, res)
return res
}

// Fulfill instantiates the Logger with the provided logger.
func (p *loggerPromise) Fulfill(parentLogger logr.Logger) {
logger := logr.WithCallDepth(parentLogger, 1)
if p.name != nil {
logger = logger.WithName(*p.name)
sink = sink.WithName(*p.name)
}

if p.tags != nil {
logger = logger.WithValues(p.tags...)
}
if p.level != 0 {
logger = logger.V(p.level)
sink = sink.WithValues(p.tags...)
}

p.logger.lock.Lock()
p.logger.logger = logger
p.logger.logSink = sink
p.logger.promise = nil
p.logger.lock.Unlock()

for _, childPromise := range p.childPromises {
childPromise.Fulfill(logger)
childPromise.Fulfill(sink)
}
}

// DelegatingLogger is a logr.Logger that delegates to another logr.Logger.
// delegatingLogSink is a logr.LogSink that delegates to another logr.LogSink.
// If the underlying promise is not nil, it registers calls to sub-loggers with
// the logging factory to be populated later, and returns a new delegating
// logger. It expects to have *some* logr.Logger set at all times (generally
// a no-op logger before the promises are fulfilled).
type DelegatingLogger struct {
// logSink. It expects to have *some* logr.Logger set at all times (generally
// a no-op logSink before the promises are fulfilled).
type delegatingLogSink struct {
lock sync.RWMutex
logger logr.Logger
logSink logr.LogSink
promise *loggerPromise
info logr.RuntimeInfo
}

// Init implements logr.LogSink.
func (l *delegatingLogSink) Init(info logr.RuntimeInfo) {
l.lock.Lock()
defer l.lock.Unlock()
l.info = info
}

// Enabled tests whether this Logger is enabled. For example, commandline
// flags might be used to set the logging verbosity and disable some info
// logs.
func (l *DelegatingLogger) Enabled() bool {
func (l *delegatingLogSink) Enabled(v int) bool {
l.lock.RLock()
defer l.lock.RUnlock()
return l.logger.Enabled()
return l.logSink.Enabled(v)
}

// Info logs a non-error message with the given key/value pairs as context.
Expand All @@ -124,10 +116,10 @@ func (l *DelegatingLogger) Enabled() bool {
// the log line. The key/value pairs can then be used to add additional
// variable information. The key/value pairs should alternate string
// keys and arbitrary values.
func (l *DelegatingLogger) Info(msg string, keysAndValues ...interface{}) {
func (l *delegatingLogSink) Info(level int, msg string, keysAndValues ...interface{}) {
l.lock.RLock()
defer l.lock.RUnlock()
l.logger.Info(msg, keysAndValues...)
l.logSink.Info(level, msg, keysAndValues...)
}

// Error logs an error, with the given message and key/value pairs as context.
Expand All @@ -138,79 +130,64 @@ func (l *DelegatingLogger) Info(msg string, keysAndValues ...interface{}) {
// The msg field should be used to add context to any underlying error,
// while the err field should be used to attach the actual error that
// triggered this log line, if present.
func (l *DelegatingLogger) Error(err error, msg string, keysAndValues ...interface{}) {
func (l *delegatingLogSink) Error(err error, msg string, keysAndValues ...interface{}) {
l.lock.RLock()
defer l.lock.RUnlock()
l.logger.Error(err, msg, keysAndValues...)
}

// V returns an Logger value for a specific verbosity level, relative to
// this Logger. In other words, V values are additive. V higher verbosity
// level means a log message is less important. It's illegal to pass a log
// level less than zero.
func (l *DelegatingLogger) V(level int) logr.Logger {
l.lock.RLock()
defer l.lock.RUnlock()

if l.promise == nil {
return l.logger.V(level)
}

res := &DelegatingLogger{logger: l.logger}
promise := l.promise.V(res, level)
res.promise = promise

return res
l.logSink.Error(err, msg, keysAndValues...)
}

// WithName provides a new Logger with the name appended.
func (l *DelegatingLogger) WithName(name string) logr.Logger {
func (l *delegatingLogSink) WithName(name string) logr.LogSink {
l.lock.RLock()
defer l.lock.RUnlock()

if l.promise == nil {
return l.logger.WithName(name)
return l.logSink.WithName(name)
}

res := &DelegatingLogger{logger: l.logger}
res := &delegatingLogSink{logSink: l.logSink}
promise := l.promise.WithName(res, name)
res.promise = promise

return res
}

// WithValues provides a new Logger with the tags appended.
func (l *DelegatingLogger) WithValues(tags ...interface{}) logr.Logger {
func (l *delegatingLogSink) WithValues(tags ...interface{}) logr.LogSink {
l.lock.RLock()
defer l.lock.RUnlock()

if l.promise == nil {
return l.logger.WithValues(tags...)
return l.logSink.WithValues(tags...)
}

res := &DelegatingLogger{logger: l.logger}
res := &delegatingLogSink{logSink: l.logSink}
promise := l.promise.WithValues(res, tags...)
res.promise = promise

return res
}

// Fulfill switches the logger over to use the actual logger
// Fulfill switches the logSink over to use the actual logSink
// provided, instead of the temporary initial one, if this method
// has not been previously called.
func (l *DelegatingLogger) Fulfill(actual logr.Logger) {
func (l *delegatingLogSink) Fulfill(actual logr.LogSink) {
if l.promise != nil {
l.promise.Fulfill(actual)
}
}

// NewDelegatingLogger constructs a new DelegatingLogger which uses
// the given logger before it's promise is fulfilled.
func NewDelegatingLogger(initial logr.Logger) *DelegatingLogger {
l := &DelegatingLogger{
logger: initial,
// NewDelegatingLogger constructs a new delegatingLogSink which uses
// the given logSink before it's promise is fulfilled.
func NewDelegatingLogger(logSink logr.LogSink) logr.Logger {
l := &delegatingLogSink{
logSink: logSink,
promise: &loggerPromise{promisesLock: sync.Mutex{}},
}
l.promise.logger = l
return l
return logr.New(l)
}

type canFulfill interface {
Fulfill(actual logr.LogSink)
}