Skip to content

🐛 Fix builders WithLogger method #1166

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

Merged
merged 2 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pkg/builder/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Builder struct {
config *rest.Config
ctrl controller.Controller
ctrlOptions controller.Options
log logr.Logger
name string
}

Expand Down Expand Up @@ -132,7 +131,7 @@ func (blder *Builder) WithOptions(options controller.Options) *Builder {

// WithLogger overrides the controller options's logger used.
func (blder *Builder) WithLogger(log logr.Logger) *Builder {
blder.log = log
blder.ctrlOptions.Log = log
return blder
}

Expand Down
35 changes: 35 additions & 0 deletions pkg/builder/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"sync/atomic"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand All @@ -48,6 +49,17 @@ func (typedNoop) Reconcile(context.Context, reconcile.Request) (reconcile.Result
return reconcile.Result{}, nil
}

type testLogger struct {
logr.Logger
}

func (l *testLogger) WithName(_ string) logr.Logger {
return l
}
func (l *testLogger) WithValues(_ ...interface{}) logr.Logger {
return l
}

var _ = Describe("application", func() {
var stop chan struct{}

Expand Down Expand Up @@ -160,6 +172,29 @@ var _ = Describe("application", func() {
Expect(instance).NotTo(BeNil())
})

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

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

By("creating a controller manager")
m, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())

instance, err := ControllerManagedBy(m).
For(&appsv1.ReplicaSet{}).
Owns(&appsv1.ReplicaSet{}).
WithLogger(logger).
Build(noop)
Expect(err).NotTo(HaveOccurred())
Expect(instance).NotTo(BeNil())
Copy link
Member

Choose a reason for hiding this comment

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

That test will also succeed without your code changes - Please test that you get the same logger as you inserted, not that you get any logger

Copy link
Contributor Author

@databus23 databus23 Sep 17, 2020

Choose a reason for hiding this comment

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

No it does not. I tested it. Without my changes the test fails with:

logger expected log.NullLogger but found *zapr.zapLogger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While the test was working I made it more explicit by using a minimal custom testLogger.
Without my changes the test now fails with:

logger expected *builder.testLogger but found *zapr.zapLogger

})

It("should prefer reconciler from options during creation of controller", func() {
newController = func(name string, mgr manager.Manager, options controller.Options) (controller.Controller, error) {
if options.Reconciler != (typedNoop{}) {
Expand Down