fix: honor --stderrthreshold flag when --logtostderr is enabled#1485
fix: honor --stderrthreshold flag when --logtostderr is enabled#1485pierluigilenoci wants to merge 1 commit intoopenshift:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (6)
✅ Files skipped from review due to trivial changes (2)
🚧 Files skipped from review as they are similar to previous changes (3)
WalkthroughUpdated klog dependency to v2.140.0 and applied new stderr threshold configuration flags ( Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Assessment against linked issues
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.11.4)Error: can't load config: unsupported version of the configuration: "" See https://golangci-lint.run/docs/product/migration-guide for migration instructions Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hi @pierluigilenoci. Thanks for your PR. I'm waiting for a openshift member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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. |
|
cc @JoelSpeed @damdo — This is a straightforward klog fix to honor the `--stderrthreshold` flag when `--logtostderr` is enabled. Part of a broader campaign to fix this across the Kubernetes ecosystem (see kubernetes/klog#432). Would appreciate a review when you get a chance! |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cmd/machine-healthcheck/main.go`:
- Around line 81-89: After calling klog.InitFlags(nil) in main (klog.InitFlags),
set the logtostderr flag to "true" like the other entry points do so the stderr
threshold behavior is applied consistently; add a flag.Set("logtostderr",
"true") immediately after klog.InitFlags(nil) alongside the existing flag.Set
calls for "legacy_stderr_threshold_behavior" and "stderrthreshold" to ensure
klog writes logs to stderr as expected.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 7a5fba2b-55e2-4542-9ca5-20c81ca353ad
⛔ Files ignored due to path filters (11)
go.sumis excluded by!**/*.sumvendor/k8s.io/klog/v2/README.mdis excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/internal/serialize/keyvalues.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/internal/serialize/keyvalues_no_slog.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/internal/serialize/keyvalues_slog.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/klog.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/klogr.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/klogr_slog.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/textlogger/options.gois excluded by!vendor/**,!**/vendor/**vendor/k8s.io/klog/v2/textlogger/textlogger.gois excluded by!vendor/**,!**/vendor/**vendor/modules.txtis excluded by!vendor/**,!**/vendor/**
📒 Files selected for processing (7)
cmd/machine-api-operator/start.gocmd/machine-api-operator/version.gocmd/machine-healthcheck/main.gocmd/machineset/main.gocmd/nodelink-controller/main.gocmd/vsphere/main.gogo.mod
cmd/machine-healthcheck/main.go
Outdated
| klog.InitFlags(nil) | ||
| // Opt into the fixed klog behavior so the --stderrthreshold flag is honored | ||
| // even when --logtostderr is enabled. See https://github.com/kubernetes/klog/issues/432 | ||
| if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil { | ||
| klog.Fatalf("failed to set klog flag legacy_stderr_threshold_behavior: %v", err) | ||
| } | ||
| if err := flag.Set("stderrthreshold", "INFO"); err != nil { | ||
| klog.Fatalf("failed to set klog flag stderrthreshold: %v", err) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Check which entry points set logtostderr=true vs the new stderr flags
echo "=== Entry points setting logtostderr ==="
rg -n 'flag\.Set\("logtostderr"' cmd/
echo ""
echo "=== Entry points setting legacy_stderr_threshold_behavior ==="
rg -n 'flag\.Set\("legacy_stderr_threshold_behavior"' cmd/Repository: openshift/machine-api-operator
Length of output: 1291
🏁 Script executed:
# Examine the code around lines 81-89 in machine-healthcheck/main.go
sed -n '75,95p' cmd/machine-healthcheck/main.goRepository: openshift/machine-api-operator
Length of output: 1167
Missing logtostderr=true flag that other entry points set.
This file sets the new klog flags but does not set logtostderr=true, which is set in all other entry points that configure the stderr threshold behavior. Add the following after klog.InitFlags(nil):
klog.InitFlags(nil)
+ if err := flag.Set("logtostderr", "true"); err != nil {
+ klog.Fatalf("failed to set logtostderr flag: %v", err)
+ }
// Opt into the fixed klog behavior so the --stderrthreshold flag is honored
// even when --logtostderr is enabled. See https://github.com/kubernetes/klog/issues/432📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| klog.InitFlags(nil) | |
| // Opt into the fixed klog behavior so the --stderrthreshold flag is honored | |
| // even when --logtostderr is enabled. See https://github.com/kubernetes/klog/issues/432 | |
| if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil { | |
| klog.Fatalf("failed to set klog flag legacy_stderr_threshold_behavior: %v", err) | |
| } | |
| if err := flag.Set("stderrthreshold", "INFO"); err != nil { | |
| klog.Fatalf("failed to set klog flag stderrthreshold: %v", err) | |
| } | |
| klog.InitFlags(nil) | |
| if err := flag.Set("logtostderr", "true"); err != nil { | |
| klog.Fatalf("failed to set logtostderr flag: %v", err) | |
| } | |
| // Opt into the fixed klog behavior so the --stderrthreshold flag is honored | |
| // even when --logtostderr is enabled. See https://github.com/kubernetes/klog/issues/432 | |
| if err := flag.Set("legacy_stderr_threshold_behavior", "false"); err != nil { | |
| klog.Fatalf("failed to set klog flag legacy_stderr_threshold_behavior: %v", err) | |
| } | |
| if err := flag.Set("stderrthreshold", "INFO"); err != nil { | |
| klog.Fatalf("failed to set klog flag stderrthreshold: %v", err) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@cmd/machine-healthcheck/main.go` around lines 81 - 89, After calling
klog.InitFlags(nil) in main (klog.InitFlags), set the logtostderr flag to "true"
like the other entry points do so the stderr threshold behavior is applied
consistently; add a flag.Set("logtostderr", "true") immediately after
klog.InitFlags(nil) alongside the existing flag.Set calls for
"legacy_stderr_threshold_behavior" and "stderrthreshold" to ensure klog writes
logs to stderr as expected.
All five entry points (vsphere, machineset, machine-api-operator start/version, nodelink-controller) set logtostderr=true, which causes klog's legacy behavior to silently ignore the stderrthreshold flag. This opts into the fixed behavior introduced in klog v2.130.0 by setting legacy_stderr_threshold_behavior=false in all entry points. See: kubernetes/klog#432 Signed-off-by: Pierluigi Lenoci <pierluigi.lenoci@gmail.com>
e6831c9 to
994d225
Compare
What this PR does
When
--logtostderris enabled (which is the default in klog and explicitly set in all entry points), klog's legacy behavior silently ignores the--stderrthresholdflag. This means users cannot filter log output by severity level, even though the flag appears to be available.This PR opts into the fixed behavior introduced in klog v2.140.0 by setting:
legacy_stderr_threshold_behavior=false— disables the legacy overridestderrthreshold=INFO— preserves the current default behaviorAfter this change, users can set
--stderrthreshold=WARNING(or any level) and it will work as expected.All entry points in
cmd/are updated:cmd/machinesetcmd/nodelink-controllercmd/vspherecmd/machine-api-operator(start + version subcommands)cmd/machine-healthcheckklog dependency updated from v2.130.1 to v2.140.0 (which introduces the
legacy_stderr_threshold_behaviorflag).Why
See upstream fix: kubernetes/klog#432
Testing
--stderrthreshold=WARNINGnow correctly filters output when--logtostderris enabled