controller-runtime: Setting Logger Does Not Propagate To Client-Go

I am a user, in my repo I am making use of the controller-runtime client. I wish to see the debug/trace-level logging that I would get from a client-go client by passing -v 10 to the binary. I attempted to do the following:

controllerruntime.SetLogger(zap.New(zap.UseFlagOptions(&zap.Options{
	Development: true,
	DestWriter:  os.Stdout,
	Level:       zapcore.DebugLevel,
})))

This is equivalent to binding the zap flags and passing flags to the binary at runtime.

This does not cause any change in the logging output from client methods, as they are configured to use the klog logger by default. The result is that if I want to change the logging level of the client, I must pass flags to the klog logger, regardless of which logger I have controller-runtime using. This is fairly challenging to understand and less than ideal. If I instruct controller-runtime to use zap, I’d expect that to propagate through to the klog.SetLogger() call.

Minimum working example that shows no logs from the client:

package main
import (
	"context"
	"flag"
	"os"

	"go.uber.org/zap/zapcore"
	coreapi "k8s.io/api/core/v1"
	kerrors "k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/client-go/tools/clientcmd"
	controllerruntime "sigs.k8s.io/controller-runtime"
	ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
func main() {
	o := &zap.Options{
		Development: true,
		DestWriter:  os.Stdout,
		Level:       zapcore.DebugLevel,
	}
	o.BindFlags(flag.CommandLine)
	flag.Parse()
	controllerruntime.SetLogger(zap.New(zap.UseFlagOptions(o)))
	credentials, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
	if err != nil {
		panic(err)
	}
	clusterConfig, err := clientcmd.NewDefaultClientConfig(*credentials, &clientcmd.ConfigOverrides{}).ClientConfig()
	if err != nil {
		panic(err)
	}
	crclient, err := ctrlruntimeclient.New(clusterConfig, ctrlruntimeclient.Options{})
	if err != nil {
		panic(err)
	}
	existing := &coreapi.Pod{}
	if getErr := crclient.Get(
		context.TODO(),
		ctrlruntimeclient.ObjectKey{Namespace: "test", Name: "test"},
		existing,
	); getErr != nil {
		panic(err)
	}
}

In order to get client-level logging out of this, I need to bind the klog flags and set them.

/cc @DirectXMan12 @estroz

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (12 by maintainers)

Most upvoted comments

I’m not really sure there is anything we can do about this until client-go also uses logr? That is in-progress but happening slowly (kubelet is targeted for 1.21, for example).