Skip to content

Honor stderrthreshold when logtostderr is enabled#2109

Open
pierluigilenoci wants to merge 2 commits intoGoogleContainerTools:mainfrom
pierluigilenoci:fix/honor-stderrthreshold
Open

Honor stderrthreshold when logtostderr is enabled#2109
pierluigilenoci wants to merge 2 commits intoGoogleContainerTools:mainfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown

Summary

  • After klog.InitFlags, opt into the fixed stderrthreshold behavior introduced in klog v2.140.0
  • Bump k8s.io/klog/v2 from v2.130.1 to v2.140.0
  • Applied to four non-test call sites:
    • cmd/nomos/nomos.go (uses named fs flagset)
    • cmd/junit-report/main.go
    • pkg/util/log/setup.go
    • pkg/resourcegroup/controllers/log/setup.go
  • Updated vendored dependencies

Motivation

When logtostderr is enabled (the default), klog historically suppressed the stderrthreshold flag, meaning that WARNING and ERROR messages could not be selectively filtered. klog v2.140.0 introduced new flags (legacy_stderr_threshold_behavior) to opt into the corrected behavior where stderrthreshold is properly honored.

Reference: kubernetes/klog#212

Test plan

  • Verify the application still logs to stderr as expected
  • Confirm stderrthreshold flag can be used to control which severity levels appear on stderr

After calling klog.InitFlags, opt into the fixed stderrthreshold
behavior added in klog v2.140.0 so that severe-enough messages are
still mirrored to stderr when logtostderr is set.

Reference: kubernetes/klog#212
Signed-off-by: Pierluigi Lenoci <pierluigi.lenoci@gmail.com>
@google-oss-prow
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign tiffanny29631 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 26, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@google-oss-prow
Copy link
Copy Markdown

Hi @pierluigilenoci. Thanks for your PR.

I'm waiting for a GoogleContainerTools member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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/test-infra repository.

@pierluigilenoci
Copy link
Copy Markdown
Author

cc @sdowell for review

Copy link
Copy Markdown
Contributor

@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 v2.140.0 and configures several entry points to opt into the new stderrthreshold behavior by disabling legacy_stderr_threshold_behavior. The vendored klog code includes significant refactoring of key-value serialization to handle deduplication and adds a new WithHeader option to the textlogger. Feedback across multiple files indicates that errors returned by flag.Set are being ignored, which could lead to silent configuration failures and is inconsistent with existing error handling in the codebase.

Comment on lines +40 to +41
_ = flag.Set("legacy_stderr_threshold_behavior", "false")
_ = flag.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The error returned by flag.Set is being ignored. If setting these flags fails (for example, if the klog version is incorrect and the flags don't exist), the application will continue with incorrect logging behavior silently. This defeats the purpose of this change. The error should be handled, probably by logging and exiting, similar to how other errors are handled in this application.

if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil {
		klog.Fatalf("Failed to set flag %q: %v", "legacy_stderr_threshold_behavior", err)
	}
	if err := flag.Set("stderrthreshold", "INFO"); err != nil {
		klog.Fatalf("Failed to set flag %q: %v", "stderrthreshold", err)
	}

Comment on lines +74 to +75
_ = fs.Set("legacy_stderr_threshold_behavior", "false")
_ = fs.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The error returned by fs.Set is being ignored. If setting these flags fails, the application will silently fall back to the old logging behavior, which is undesirable. The error should be handled to ensure the logging is configured as expected. A fatal log on error would be appropriate here.

Suggested change
_ = fs.Set("legacy_stderr_threshold_behavior", "false")
_ = fs.Set("stderrthreshold", "INFO")
if err := fs.Set("legacy_stderr_threshold_behavior", "false"); err != nil {
klog.Fatalf("Failed to set flag %q: %v", "legacy_stderr_threshold_behavior", err)
}
if err := fs.Set("stderrthreshold", "INFO"); err != nil {
klog.Fatalf("Failed to set flag %q: %v", "stderrthreshold", err)
}

Comment on lines +31 to +32
_ = flag.Set("legacy_stderr_threshold_behavior", "false")
_ = flag.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The errors returned by flag.Set are being ignored. This is inconsistent with the error handling for flag.Set("v", "1") just a few lines below. To ensure logging is configured correctly and to maintain consistency, these errors should be handled.

if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil {
		klog.Fatalf("Failed to set flag %q: %v", "legacy_stderr_threshold_behavior", err)
	}
	if err := flag.Set("stderrthreshold", "INFO"); err != nil {
		klog.Fatalf("Failed to set flag %q: %v", "stderrthreshold", err)
	}

Comment on lines +28 to +29
_ = flag.Set("legacy_stderr_threshold_behavior", "false")
_ = flag.Set("stderrthreshold", "INFO")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The errors returned by flag.Set are being ignored, which is inconsistent with the error handling for flag.Set("logtostderr", "true") that follows. To ensure robust initialization, these errors should be handled, for example by calling klog.Fatal(err).

Suggested change
_ = flag.Set("legacy_stderr_threshold_behavior", "false")
_ = flag.Set("stderrthreshold", "INFO")
if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil {
klog.Fatal(err)
}
if err := flag.Set("stderrthreshold", "INFO"); err != nil {
klog.Fatal(err)
}

@pierluigilenoci
Copy link
Copy Markdown
Author

@googlebot I signed it.

Address review feedback by wrapping flag.Set() calls with proper
error checking instead of silently discarding errors.

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

I've addressed the review feedback — all flag.Set() / fs.Set() calls are now wrapped with proper error handling instead of discarding errors with _ =. The updated code uses if err := ...; err != nil { klog.Fatalf(...) } pattern consistently across all 4 files.

Could an org member run /ok-to-test to trigger CI? Thank you!

@Camila-B
Copy link
Copy Markdown
Contributor

/ok-to-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants