Skip to content

fix: honor -stderrthreshold when -logtostderr=true#2494

Open
pierluigilenoci wants to merge 1 commit intokubernetes-sigs:masterfrom
pierluigilenoci:fix/honor-stderrthreshold
Open

fix: honor -stderrthreshold when -logtostderr=true#2494
pierluigilenoci wants to merge 1 commit intokubernetes-sigs:masterfrom
pierluigilenoci:fix/honor-stderrthreshold

Conversation

@pierluigilenoci
Copy link
Copy Markdown

What

Opt into the fixed klog behavior so that -stderrthreshold is respected even when -logtostderr=true (the default).

Why

klog v2 defaults -logtostderr to true. When active, the -stderrthreshold flag is silently ignored — all log severities are unconditionally written to stderr. This makes it impossible for log-aggregation systems to filter by severity.

This has been an open issue since 2020: kubernetes/klog#212

The fix was merged in klog v2.140.0 via kubernetes/klog#432 (authored by me) and introduces an opt-in flag legacy_stderr_threshold_behavior. Setting it to false enables the corrected behavior where -stderrthreshold is honored.

What changes

After klog.InitFlags(), two flags are set:

  • legacy_stderr_threshold_behavior=false — enables the fix
  • stderrthreshold=INFO — preserves current behavior (all severities still go to stderr by default)

Since these are set before flag parsing, users can now override -stderrthreshold=WARNING or -stderrthreshold=ERROR on the command line and it will actually work.

The fix is applied in three locations:

  • pkg/utils/klog/klog.go — centralized klog wrapper used by nfd-master and nfd-worker
  • cmd/nfd-gc/main.go — garbage collector (calls klog.InitFlags directly)
  • cmd/nfd-topology-updater/main.go — topology updater (calls klog.InitFlags directly)

References

klog v2 defaults -logtostderr to true, which silently ignores
-stderrthreshold — all severities are unconditionally sent to stderr.

Opt into the fixed behavior introduced in klog v2.140.0 by setting
legacy_stderr_threshold_behavior=false so that -stderrthreshold is
respected. The default is set to INFO (preserving current behavior);
users can now override it on the command line.

Ref: kubernetes/klog#212, kubernetes/klog#432

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

netlify Bot commented Apr 22, 2026

Deploy Preview for kubernetes-sigs-nfd ready!

Name Link
🔨 Latest commit 62a7af0
🔍 Latest deploy log https://app.netlify.com/projects/kubernetes-sigs-nfd/deploys/69e8cd6dbb456a000872c42a
😎 Deploy Preview https://deploy-preview-2494--kubernetes-sigs-nfd.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: pierluigilenoci
Once this PR has been reviewed and has the lgtm label, please assign arangogutierrez 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

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 22, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

@pierluigilenoci: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-node-feature-discovery-build-image-cross-generic 62a7af0 link true /test pull-node-feature-discovery-build-image-cross-generic

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

Copy link
Copy Markdown
Contributor

@ArangoGutierrez ArangoGutierrez left a comment

Choose a reason for hiding this comment

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

Thanks for this, Pierluigi — and for driving the upstream klog fix in the first place. The intent is exactly right and I want to land it.

Two things to sort before merge:

  1. go.mod still pins k8s.io/klog/v2 v2.130.1, but legacy_stderr_threshold_behavior only exists from v2.140.0. Right now every Set returns no such flag and the _ = swallows it — the fix is a no-op as shipped. Please bump klog in the same PR.
  2. In pkg/utils/klog/klog.go, the override is reverted at runtime by MergeKlogConfiguration (used by nfd-master and nfd-worker — the primary daemons). Details inline.

Smaller asks: don't swallow Set errors, and consider extracting the 4-line block into a klogutils helper so we have one place to evolve. A table test asserting -stderrthreshold=WARNING takes effect after init would pin the behavior and would have caught both blockers.

Happy to pair on the merge-path fix if easier.

Comment thread pkg/utils/klog/klog.go
// Opt into the new klog behavior so that -stderrthreshold is honored even
// when -logtostderr=true (the default).
// Ref: kubernetes/klog#212, kubernetes/klog#432
_ = flags.Set("legacy_stderr_threshold_behavior", "false") //nolint:errcheck
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.

Subtle one: setting on the inner flags FlagSet is undone later by MergeKlogConfiguration (called in pkg/nfd-master/nfd-master.go:1206 and pkg/nfd-worker/nfd-worker.go:386). That path calls KlogFlagVal.DefValue(), which returns k.flag.DefValue — klog's declared default ("true"), not the value we just Set. So unless the user explicitly passes -legacy_stderr_threshold_behavior=false on the CLI, the merge reverts it and nfd-master / nfd-worker silently regress.

Simplest fix: call Set on the outer flagset after VisitAll, so KlogFlagVal.Set is invoked and isSetFromCmdLine=true. The merge then leaves it alone.

Comment thread pkg/utils/klog/klog.go
// when -logtostderr=true (the default).
// Ref: kubernetes/klog#212, kubernetes/klog#432
_ = flags.Set("legacy_stderr_threshold_behavior", "false") //nolint:errcheck
_ = flags.Set("stderrthreshold", "INFO") //nolint:errcheck
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.

INFO is already klog's default, so this line is a no-op at runtime. I'd drop it; if the intent is documentation, a one-line comment ("we rely on klog's default stderrthreshold=INFO") reads more clearly.

Comment thread cmd/nfd-gc/main.go
// Opt into the new klog behavior so that -stderrthreshold is honored even
// when -logtostderr=true (the default).
// Ref: kubernetes/klog#212, kubernetes/klog#432
_ = flagset.Set("legacy_stderr_threshold_behavior", "false") //nolint:errcheck
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.

Two things here:

  1. This flag was added in klog v2.140.0; go.mod pins v2.130.1, so Set currently returns no such flag. Needs a klog bump in this PR.
  2. Even after the bump, I'd prefer not to swallow the error. If klog ever renames or removes the flag we want a loud failure — a klog.Fatalf (or at minimum klog.Warningf) on non-nil gives us that signal. Same applies to the equivalent block in cmd/nfd-topology-updater/main.go.

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. 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.

3 participants