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

fix: set verbosity log level only for debug #5981

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 0 additions & 6 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
klog "k8s.io/klog/v2"

logging "github.com/prometheus-operator/prometheus-operator/internal/log"
"github.com/prometheus-operator/prometheus-operator/pkg/admission"
Expand Down Expand Up @@ -168,7 +167,6 @@ var (
)

func init() {
// With migration to klog-gokit, calling klogv2.InitFlags(flagset) is not applicable.
flagset.StringVar(&cfg.ListenAddress, "web.listen-address", ":8080", "Address on which to expose metrics and web interface.")
flagset.BoolVar(&serverTLS, "web.enable-tls", false, "Activate prometheus operator web server TLS. "+
" This is useful for example when using the rule validation webhook.")
Expand Down Expand Up @@ -245,10 +243,6 @@ func run() int {
stdlog.Fatal(err)
}

// Above level 6, the k8s client would log bearer tokens in clear-text.
klog.ClampLevel(6)
klog.SetLogger(log.With(logger, "component", "k8s_client_runtime"))

level.Info(logger).Log("msg", "Starting Prometheus Operator", "version", version.Info())
level.Info(logger).Log("build_context", version.BuildContext())

Expand Down
9 changes: 9 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/go-kit/log"
loglevel "github.com/go-kit/log/level"
klogv2 "k8s.io/klog/v2"
)

const (
Expand All @@ -46,10 +47,15 @@ func NewLogger(level string, format string) (log.Logger, error) {
lvlOption loglevel.Option
)

// For log levels other than debug, the klog verbosity level is 0.
klogv2.ClampLevel(0)
switch strings.ToLower(level) {
case LevelAll:
lvlOption = loglevel.AllowAll()
case LevelDebug:
// When the log level is set to debug, we set the klog verbosity level to 6.
// Above level 6, the k8s client would log bearer tokens in clear-text.
klogv2.ClampLevel(6)
lvlOption = loglevel.AllowDebug()
case LevelInfo:
lvlOption = loglevel.AllowInfo()
Expand All @@ -75,6 +81,9 @@ func NewLogger(level string, format string) (log.Logger, error) {
logger = loglevel.NewFilter(logger, lvlOption)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)

klogv2.SetLogger(log.With(logger, "component", "k8s_client_runtime"))

return logger, nil
}

Expand Down