From 9a1d25a647bf0ad6dc45acf6226f4dfc47a4d158 Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Thu, 23 Apr 2026 10:53:58 +0200 Subject: [PATCH] fix: honor klog -stderrthreshold even when -logtostderr is true MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit klog v2 defaults -logtostderr to true, which silently ignores -stderrthreshold — all log levels go to stderr unconditionally. This has been an open issue since 2020 (kubernetes/klog#212). klog v2.140.0 introduced a fix behind an opt-in flag (legacy_stderr_threshold_behavior). This commit bumps klog to v2.140.0 and enables the fix so that -stderrthreshold is honored, while preserving the current default behavior (stderrthreshold=INFO means all logs still go to stderr unless the user overrides it on the command line). Ref: kubernetes/klog#212 Ref: kubernetes/klog#432 Signed-off-by: Pierluigi Lenoci --- go.mod | 2 +- go.sum | 4 ++-- main.go | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e6ffa43..16c693d 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( k8s.io/apimachinery v0.32.3 k8s.io/client-go v0.32.3 k8s.io/component-base v0.32.3 - k8s.io/klog/v2 v2.130.1 + k8s.io/klog/v2 v2.140.0 k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 sigs.k8s.io/cluster-api v1.10.5 sigs.k8s.io/cluster-api/test v1.10.5 diff --git a/go.sum b/go.sum index 3350c7b..6364089 100644 --- a/go.sum +++ b/go.sum @@ -425,8 +425,8 @@ k8s.io/cluster-bootstrap v0.32.3 h1:AqIpsUhB6MUeaAsl1WvaUw54AHRd2hfZrESlKChtd8s= k8s.io/cluster-bootstrap v0.32.3/go.mod h1:CHbBwgOb6liDV6JFUTkx5t85T2xidy0sChBDoyYw344= k8s.io/component-base v0.32.3 h1:98WJvvMs3QZ2LYHBzvltFSeJjEx7t5+8s71P7M74u8k= k8s.io/component-base v0.32.3/go.mod h1:LWi9cR+yPAv7cu2X9rZanTiFKB2kHA+JjmhkKjCZRpI= -k8s.io/klog/v2 v2.130.1 h1:n9Xl7H1Xvksem4KFG4PYbdQCQxqc/tTUyrgXaOhHSzk= -k8s.io/klog/v2 v2.130.1/go.mod h1:3Jpz1GvMt720eyJH1ckRHK1EDfpxISzJ7I9OYgaDtPE= +k8s.io/klog/v2 v2.140.0 h1:Tf+J3AH7xnUzZyVVXhTgGhEKnFqye14aadWv7bzXdzc= +k8s.io/klog/v2 v2.140.0/go.mod h1:o+/RWfJ6PwpnFn7OyAG3QnO47BFsymfEfrz6XyYSSp0= k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f h1:GA7//TjRY9yWGy1poLzYYJJ4JRdzg3+O6e8I+e+8T5Y= k8s.io/kube-openapi v0.0.0-20241105132330-32ad38e42d3f/go.mod h1:R/HEjbvWI0qdfb8viZUeVZm0X6IZnxAydC7YU42CMw4= k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 h1:M3sRQVHv7vB20Xc2ybTt7ODCeFj6JSWYFzOFnYeS6Ro= diff --git a/main.go b/main.go index eb72510..8a1a5ee 100644 --- a/main.go +++ b/main.go @@ -204,6 +204,11 @@ func initFlags(fs *pflag.FlagSet) { func main() { klog.InitFlags(nil) + // Opt into the new klog behavior so that -stderrthreshold is honored even + // when -logtostderr=true (the default). + // Ref: kubernetes/klog#212, kubernetes/klog#432 + flag.Set("legacy_stderr_threshold_behavior", "false") //nolint:errcheck + flag.Set("stderrthreshold", "INFO") //nolint:errcheck initFlags(pflag.CommandLine) pflag.CommandLine.AddGoFlagSet(flag.CommandLine)