Skip to content

fix: honor --stderrthreshold flag when --logtostderr is enabled#1485

Open
pierluigilenoci wants to merge 1 commit intoopenshift:mainfrom
pierluigilenoci:fix/honor-stderrthreshold
Open

fix: honor --stderrthreshold flag when --logtostderr is enabled#1485
pierluigilenoci wants to merge 1 commit intoopenshift:mainfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown

What this PR does

When --logtostderr is enabled (which is the default in klog and explicitly set in all entry points), klog's legacy behavior silently ignores the --stderrthreshold flag. 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 override
  • stderrthreshold=INFO — preserves the current default behavior

After this change, users can set --stderrthreshold=WARNING (or any level) and it will work as expected.

All entry points in cmd/ are updated:

  • cmd/machineset
  • cmd/nodelink-controller
  • cmd/vsphere
  • cmd/machine-api-operator (start + version subcommands)
  • cmd/machine-healthcheck

klog dependency updated from v2.130.1 to v2.140.0 (which introduces the legacy_stderr_threshold_behavior flag).

Why

See upstream fix: kubernetes/klog#432

Testing

  • Verified machine-api-operator builds successfully with updated dependency
  • Verified --stderrthreshold=WARNING now correctly filters output when --logtostderr is enabled

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 087877f8-0cc1-4c01-b3c4-2483e5a4000c

📥 Commits

Reviewing files that changed from the base of the PR and between e6831c9 and 994d225.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (6)
  • cmd/machine-api-operator/start.go
  • cmd/machine-api-operator/version.go
  • cmd/machineset/main.go
  • cmd/nodelink-controller/main.go
  • cmd/vsphere/main.go
  • go.mod
✅ Files skipped from review due to trivial changes (2)
  • go.mod
  • cmd/nodelink-controller/main.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • cmd/machine-api-operator/start.go
  • cmd/vsphere/main.go
  • cmd/machineset/main.go

Walkthrough

Updated klog dependency to v2.140.0 and applied new stderr threshold configuration flags (legacy_stderr_threshold_behavior=false, stderrthreshold=INFO) across five command-line entry points to enable severity-based log filtering when logtostderr is enabled.

Changes

Cohort / File(s) Summary
klog Dependency Update
go.mod
Updated k8s.io/klog/v2 from v2.130.1 to v2.140.0 to support new stderr threshold behavior flags.
Stderr Threshold Configuration
cmd/machine-api-operator/start.go, cmd/machine-api-operator/version.go, cmd/machineset/main.go, cmd/nodelink-controller/main.go, cmd/vsphere/main.go
Added explicit klog flag configuration after logtostderr=true: disabled legacy_stderr_threshold_behavior and set stderrthreshold to INFO. Each flag assignment includes error handling via klog.Fatalf() if flag setting fails.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix stderrthreshold being ignored when logtostderr=true [#432]
Enable opt-in severity filtering via legacy_stderr_threshold_behavior flag [#432]
Set stderrthreshold to a specific level for filtering [#432]
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
The command is terminated due to an 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci bot commented Mar 28, 2026

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 /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-sigs/prow repository.

@openshift-ci openshift-ci bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 28, 2026
@pierluigilenoci
Copy link
Copy Markdown
Author

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!

@openshift-ci openshift-ci bot requested review from damdo and nrb March 28, 2026 21:47
@openshift-ci
Copy link
Copy Markdown
Contributor

openshift-ci bot commented Mar 28, 2026

[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 racheljpg for approval. For more information see the 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

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between b8fd345 and e6831c9.

⛔ Files ignored due to path filters (11)
  • go.sum is excluded by !**/*.sum
  • vendor/k8s.io/klog/v2/README.md is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/internal/serialize/keyvalues.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/internal/serialize/keyvalues_no_slog.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/internal/serialize/keyvalues_slog.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/klog.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/klogr.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/klogr_slog.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/textlogger/options.go is excluded by !vendor/**, !**/vendor/**
  • vendor/k8s.io/klog/v2/textlogger/textlogger.go is excluded by !vendor/**, !**/vendor/**
  • vendor/modules.txt is excluded by !vendor/**, !**/vendor/**
📒 Files selected for processing (7)
  • cmd/machine-api-operator/start.go
  • cmd/machine-api-operator/version.go
  • cmd/machine-healthcheck/main.go
  • cmd/machineset/main.go
  • cmd/nodelink-controller/main.go
  • cmd/vsphere/main.go
  • go.mod

Comment on lines +81 to +89
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)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 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.go

Repository: 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.

Suggested change
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>
@pierluigilenoci pierluigilenoci force-pushed the fix/honor-stderrthreshold branch from e6831c9 to 994d225 Compare March 28, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant