Skip to content

fix: honor -stderrthreshold flag in klog v2#3086

Merged
kubesphere-prow[bot] merged 2 commits intokubesphere:mainfrom
pierluigilenoci:fix/honor-stderrthreshold
May 7, 2026
Merged

fix: honor -stderrthreshold flag in klog v2#3086
kubesphere-prow[bot] merged 2 commits intokubesphere:mainfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown
Contributor

@pierluigilenoci pierluigilenoci commented Apr 24, 2026

What this PR does

klog v2 defaults -logtostderr=true, which silently ignores the
-stderrthreshold flag (kubernetes/klog#212).
Users who set -stderrthreshold expect it to control which severity
level is written to stderr, but the flag has no effect because
-logtostderr takes precedence.

klog v2.140.0 introduced the legacy_stderr_threshold_behavior
flag to restore the intended behaviour
(kubernetes/klog#432).

Changes

  • cmd/kk/app/options/other.go and
    cmd/controller-manager/app/options/common.go: after every
    klog.InitFlags() call, set logtostderr=false and
    legacy_stderr_threshold_behavior=true so that -stderrthreshold
    works as expected.
  • go.mod: bump k8s.io/klog/v2 from v2.130.1 to v2.140.0.

Why

Without this fix, any -stderrthreshold value passed to kk or
controller-manager is silently ignored, making it impossible to
suppress low-severity log output on stderr.

How to test

go build ./cmd/kk/...
go build ./cmd/controller-manager/...

Both binaries compile and the new flag values are set programmatically
(no user-facing CLI change).

NONE

klog v2 defaults -logtostderr=true, which silently ignores the
-stderrthreshold flag (kubernetes/klog#212). klog v2.140.0
introduced the legacy_stderr_threshold_behavior flag to fix this
(kubernetes/klog#432).

After every klog.InitFlags() call, set logtostderr=false and
legacy_stderr_threshold_behavior=true so that -stderrthreshold
works as expected.

Bump k8s.io/klog/v2 from v2.130.1 to v2.140.0.

Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@kubesphere-prow kubesphere-prow Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/release-note-label-needed labels Apr 24, 2026
@kubesphere-prow
Copy link
Copy Markdown

Welcome @pierluigilenoci! It looks like this is your first PR to kubesphere/kubekey 🎉

@kubesphere-prow kubesphere-prow Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 24, 2026
Set legacy_stderr_threshold_behavior=false (not true) to enable the fix,
and set stderrthreshold=INFO (not logtostderr=false) to preserve default
behavior while allowing users to override on the command line.

Signed-off-by: Pierluigi Lenoci <pierluigi.lenoci@gmail.com>
Signed-off-by: Pierluigi Lenoci <pierluigilenoci@gmail.com>
@kubesphere-prow
Copy link
Copy Markdown

This PR has multiple commits, and the default merge method is: squash.
You can request commits to be merged using the label: tide/merge-method-merge

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the k8s.io/klog/v2 dependency to version 2.140.0 and configures the logtostderr and legacy_stderr_threshold_behavior flags in cmd/controller-manager/app/options/common.go and cmd/kk/app/options/other.go to resolve an issue where -stderrthreshold was ignored. The review feedback recommends adding error handling for the local.Set calls to prevent silent configuration failures during application startup.

Comment on lines +164 to +165
_ = local.Set("legacy_stderr_threshold_behavior", "false")
_ = local.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The errors returned by local.Set are being ignored. While it's unlikely to fail in this case, it's a good practice to handle errors during application initialization. A failure to set these flags would lead to incorrect logging behavior that could be difficult to debug. Consider checking the error and logging a fatal error if setting the flag fails, to ensure the application exits if it cannot be configured correctly.

	if err := local.Set("logtostderr", "false"); err != nil {
		klog.Fatalf("failed to set klog logtostderr flag: %v", err)
	}
	if err := local.Set("legacy_stderr_threshold_behavior", "true"); err != nil {
		klog.Fatalf("failed to set klog legacy_stderr_threshold_behavior flag: %v", err)
	}

Comment on lines +165 to +166
_ = local.Set("legacy_stderr_threshold_behavior", "false")
_ = local.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The errors returned by local.Set are being ignored. While it's unlikely to fail in this case, it's a good practice to handle errors during application initialization. A failure to set these flags would lead to incorrect logging behavior that could be difficult to debug. Consider checking the error and logging a fatal error if setting the flag fails, to ensure the application exits if it cannot be configured correctly.

	if err := local.Set("logtostderr", "false"); err != nil {
		klog.Fatalf("failed to set klog logtostderr flag: %v", err)
	}
	if err := local.Set("legacy_stderr_threshold_behavior", "true"); err != nil {
		klog.Fatalf("failed to set klog legacy_stderr_threshold_behavior flag: %v", err)
	}

@redscholar
Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubesphere-prow kubesphere-prow Bot added the lgtm Indicates that a PR is ready to be merged. label May 7, 2026
@kubesphere-prow
Copy link
Copy Markdown

LGTM label has been added.

DetailsGit tree hash: 2cce731267077e3d264eff65f073314dba1c8d1e

@kubesphere-prow
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pierluigilenoci, redscholar

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubesphere-prow kubesphere-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label May 7, 2026
@kubesphere-prow kubesphere-prow Bot merged commit 4a66aeb into kubesphere:main May 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. release-note-none size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants