Skip to content

Commit

Permalink
fix: set logger to controller-runtime
Browse files Browse the repository at this point in the history
Set zap logger to all deferred loggers of controller-runtime
to prevent it from complaining about log.SetLogger never
being called

Signed-off-by: Alex Szakaly <alex.szakaly@gmail.com>
  • Loading branch information
alex1989hu committed Oct 13, 2024
1 parent debdbf3 commit b3d06f8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package cmd

import (
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/spf13/cobra"
uberzap "go.uber.org/zap"
Expand Down Expand Up @@ -72,7 +71,7 @@ func startServer() {
uberzap.Bool("debug", isDebug))

// Forward client-go klog calls to zap
klog.SetLogger(zapr.NewLogger(&setupLog))
klog.SetLogger(zapr.NewLogger(logger.CreateLogger().Named("client-go")))

setupLog.Info("Try to talk to Kubernetes API Server, will exit in case of failure")

Expand All @@ -84,6 +83,10 @@ func startServer() {
setupLog.Info("pprof will be enabled", uberzap.String("port", pProfBindAddress))
}

// Set zap logger to all deferred loggers of controller-runtime to prevent it from complaining
// about log.SetLogger never being called
ctrllog.SetLogger(zapr.NewLogger(logger.CreateLogger().Named("ctrllog")))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Client: ctrlclient.Options{
Cache: &ctrlclient.CacheOptions{
Expand All @@ -99,9 +102,7 @@ func startServer() {
LeaderElectionNamespace: namespace,
LeaderElectionResourceLock: "leases",
LeaderElectionID: "kubelet-serving-certificate-approver",
// Set NullLogger: https://github.com/kubernetes-sigs/controller-runtime/issues/1122
Logger: logr.New(ctrllog.NullLogSink{}),
PprofBindAddress: pProfBindAddress,
PprofBindAddress: pProfBindAddress,
})
if err != nil {
setupLog.Fatal("Unable to start manager", uberzap.Error(err))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ toolchain go1.23.1

require (
github.com/cucumber/godog v0.14.1
github.com/go-logr/logr v1.4.2
github.com/go-logr/zapr v1.3.0
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/common v0.55.0
Expand Down Expand Up @@ -34,6 +33,7 @@ require (
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand Down

0 comments on commit b3d06f8

Please sign in to comment.