From 4b6d706cb68e85b318795ca92f1253d6495adbdb Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Thu, 23 Apr 2026 10:54:32 +0200 Subject: [PATCH 1/5] 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 in both entry points 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 --- cmd/addon-manager/main.go | 5 +++++ go.mod | 2 +- go.sum | 4 ++-- pkg/config/args_log.go | 5 +++++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/addon-manager/main.go b/cmd/addon-manager/main.go index 2227acbc..c52aef99 100644 --- a/cmd/addon-manager/main.go +++ b/cmd/addon-manager/main.go @@ -47,6 +47,11 @@ func main() { logger := klogr.New() klog.SetOutput(os.Stdout) klog.InitFlags(flag.CommandLine) + // 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.CommandLine.Set("legacy_stderr_threshold_behavior", "false") //nolint:errcheck + flag.CommandLine.Set("stderrthreshold", "INFO") //nolint:errcheck flag.StringVar(&metricsAddr, "metrics-bind-address", ":48080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":48081", "The address the probe endpoint binds to.") flag.BoolVar(&enableLeaderElection, "leader-elect", false, diff --git a/go.mod b/go.mod index cda5acf3..4250af2e 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( k8s.io/client-go v0.31.10 k8s.io/component-base v0.31.10 k8s.io/klog v1.0.0 - k8s.io/klog/v2 v2.130.1 + k8s.io/klog/v2 v2.140.0 k8s.io/kube-aggregator v0.31.10 k8s.io/kube-openapi v0.0.0-20250610211856-8b98d1ed966a k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 diff --git a/go.sum b/go.sum index 304a6aa2..eb39ad34 100644 --- a/go.sum +++ b/go.sum @@ -309,8 +309,8 @@ k8s.io/component-base v0.31.10 h1:8daIQBYMhcnuXMD1otGkjpx4d4b0UIcg18xieLTAGA0= k8s.io/component-base v0.31.10/go.mod h1:qoSFFg2SO854XgeCJwFL/LPY/oJU1vqJHhNCEgI6xhA= k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8= k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I= -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/kms v0.31.10 h1:jD/+hMQ8WT4ht419sXNs1u4dqeJidG34rFcnb2nP6CM= k8s.io/kms v0.31.10/go.mod h1:OZKwl1fan3n3N5FFxnW5C4V3ygrah/3YXeJWS3O6+94= k8s.io/kube-aggregator v0.31.10 h1:NeqnNYGEpJx8NTN56q48vR1XFyLG/8Rif/5iazNSTwE= diff --git a/pkg/config/args_log.go b/pkg/config/args_log.go index 0007d1cb..27469529 100644 --- a/pkg/config/args_log.go +++ b/pkg/config/args_log.go @@ -27,5 +27,10 @@ import ( func AddLogFlags(set *pflag.FlagSet) { fs := flag.NewFlagSet("", 0) klog.InitFlags(fs) + // Opt into the new klog behavior so that -stderrthreshold is honored even + // when -logtostderr=true (the default). + // Ref: kubernetes/klog#212, kubernetes/klog#432 + _ = fs.Set("legacy_stderr_threshold_behavior", "false") + _ = fs.Set("stderrthreshold", "INFO") set.AddGoFlagSet(fs) } From ebac99102d24fadf6fd7beb8a3f70fae7d65a2e5 Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Mon, 27 Apr 2026 20:19:03 +0200 Subject: [PATCH 2/5] test: add unit test for AddLogFlags covering stderrthreshold config Cover the new klog flag overrides (legacy_stderr_threshold_behavior and stderrthreshold) introduced in the previous commit to satisfy the codecov patch coverage threshold (70%). Signed-off-by: Pierluigi Lenoci Signed-off-by: Pierluigi Lenoci --- pkg/config/args_log_test.go | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 pkg/config/args_log_test.go diff --git a/pkg/config/args_log_test.go b/pkg/config/args_log_test.go new file mode 100644 index 00000000..e0bc939a --- /dev/null +++ b/pkg/config/args_log_test.go @@ -0,0 +1,44 @@ +/* +Copyright 2022 The KubeVela Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +import ( + "testing" + + "github.com/spf13/pflag" + "github.com/stretchr/testify/assert" +) + +func TestAddLogFlags(t *testing.T) { + set := pflag.NewFlagSet("test", pflag.ContinueOnError) + AddLogFlags(set) + + // klog flags should be registered + f := set.Lookup("v") + assert.NotNil(t, f, "expected klog -v flag to be registered") + + // legacy_stderr_threshold_behavior should be opted out + legacy := set.Lookup("legacy_stderr_threshold_behavior") + assert.NotNil(t, legacy, "expected legacy_stderr_threshold_behavior flag") + assert.Equal(t, "false", legacy.Value.String()) + + // stderrthreshold should default to INFO + threshold := set.Lookup("stderrthreshold") + assert.NotNil(t, threshold, "expected stderrthreshold flag") + // klog maps INFO to severity 0 + assert.Equal(t, "0", threshold.Value.String()) +} From 61a3fd9e575ab1f5772c73da9548bbe4ae72790e Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Sat, 2 May 2026 23:12:22 +0200 Subject: [PATCH 3/5] chore: retrigger CI Signed-off-by: Pierluigi Lenoci From a378e5dd4386b848bfe6c5f2a9fc9aac35512fe9 Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Sun, 3 May 2026 10:02:42 +0200 Subject: [PATCH 4/5] chore: retrigger CI Signed-off-by: Pierluigi Lenoci From 1e01bdee9463ee94189910478f543cb8a4a0e56d Mon Sep 17 00:00:00 2001 From: Pierluigi Lenoci Date: Thu, 4 Jun 2026 10:20:03 +0200 Subject: [PATCH 5/5] test: use require.NotNil to fail fast on nil pointer dereference Signed-off-by: Pierluigi Lenoci --- pkg/config/args_log_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/config/args_log_test.go b/pkg/config/args_log_test.go index e0bc939a..5d4280fb 100644 --- a/pkg/config/args_log_test.go +++ b/pkg/config/args_log_test.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/pflag" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAddLogFlags(t *testing.T) { @@ -33,12 +34,12 @@ func TestAddLogFlags(t *testing.T) { // legacy_stderr_threshold_behavior should be opted out legacy := set.Lookup("legacy_stderr_threshold_behavior") - assert.NotNil(t, legacy, "expected legacy_stderr_threshold_behavior flag") + require.NotNil(t, legacy, "expected legacy_stderr_threshold_behavior flag") assert.Equal(t, "false", legacy.Value.String()) // stderrthreshold should default to INFO threshold := set.Lookup("stderrthreshold") - assert.NotNil(t, threshold, "expected stderrthreshold flag") + require.NotNil(t, threshold, "expected stderrthreshold flag") // klog maps INFO to severity 0 assert.Equal(t, "0", threshold.Value.String()) }