Skip to content

kubectl: avoid logging during init #132480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025

Conversation

pohly
Copy link
Contributor

@pohly pohly commented Jun 24, 2025

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

LoadTranslations gets called during the init phase:

 0  0x0000000005926c56 in k8s.io/kubectl/pkg/util/i18n.LoadTranslations
    at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:146
 1  0x0000000005926727 in k8s.io/kubectl/pkg/util/i18n.init.func1
    at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:60
 2  0x000000000592780f in k8s.io/kubectl/pkg/util/i18n.lazyLoadTranslations.func1
    at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:191
 3  0x0000000001b876e8 in sync.(*Once).doSlow
    at /nvme/gopath/go-1.24.0/src/sync/once.go:78
 4  0x0000000001b8753e in sync.(*Once).Do
    at /nvme/gopath/go-1.24.0/src/sync/once.go:69
 5  0x0000000005927565 in k8s.io/kubectl/pkg/util/i18n.lazyLoadTranslations
    at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:187
 6  0x00000000059275cd in k8s.io/kubectl/pkg/util/i18n.T
    at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:201
 7  0x000000000599fb6d in k8s.io/kubectl/pkg/cmd/apiresources.init
    at <autogenerated>:1
 8  0x0000000001b41bf4 in runtime.doInit1
    at /nvme/gopath/go-1.24.0/src/runtime/proc.go:7350
 9  0x0000000001b6bf8a in runtime.doInit
    at /nvme/gopath/go-1.24.0/src/runtime/proc.go:7317
10  0x0000000001b33910 in runtime.main
    at /nvme/gopath/go-1.24.0/src/runtime/proc.go:254
11  0x0000000001b72881 in runtime.goexit
    at /nvme/gopath/go-1.24.0/src/runtime/asm_amd64.s:1700

During init, klog verbosity is either zero (making the log call redundant because it doesn't print anything) or some other init function reconfigures logging, in which case the output is potentially confusing because it is not guaranteed that logging is reconfigured before the log call is invoked.

In other scenarios, flag parsing might switch from klog text format to something else entirely, which then leads to a mixture of text and e.g. JSON output. In general, code running during init should not log.

Which issue(s) this PR is related to:

N/A

Special notes for your reviewer:

I ran into this when experimenting with early logging configuration in test/e2e. I bet in practice no-one has ever seen this log output when invoking kubectl, so removing it shouldn't make a difference.

Does this PR introduce a user-facing change?

NONE

LoadTranslations gets called during the init phase:

     0  0x0000000005926c56 in k8s.io/kubectl/pkg/util/i18n.LoadTranslations
        at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:146
     1  0x0000000005926727 in k8s.io/kubectl/pkg/util/i18n.init.func1
        at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:60
     2  0x000000000592780f in k8s.io/kubectl/pkg/util/i18n.lazyLoadTranslations.func1
        at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:191
     3  0x0000000001b876e8 in sync.(*Once).doSlow
        at /nvme/gopath/go-1.24.0/src/sync/once.go:78
     4  0x0000000001b8753e in sync.(*Once).Do
        at /nvme/gopath/go-1.24.0/src/sync/once.go:69
     5  0x0000000005927565 in k8s.io/kubectl/pkg/util/i18n.lazyLoadTranslations
        at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:187
     6  0x00000000059275cd in k8s.io/kubectl/pkg/util/i18n.T
        at ./staging/src/k8s.io/kubectl/pkg/util/i18n/i18n.go:201
     7  0x000000000599fb6d in k8s.io/kubectl/pkg/cmd/apiresources.init
        at <autogenerated>:1
     8  0x0000000001b41bf4 in runtime.doInit1
        at /nvme/gopath/go-1.24.0/src/runtime/proc.go:7350
     9  0x0000000001b6bf8a in runtime.doInit
        at /nvme/gopath/go-1.24.0/src/runtime/proc.go:7317
    10  0x0000000001b33910 in runtime.main
        at /nvme/gopath/go-1.24.0/src/runtime/proc.go:254
    11  0x0000000001b72881 in runtime.goexit
        at /nvme/gopath/go-1.24.0/src/runtime/asm_amd64.s:1700

During init, klog verbosity is either zero (making the log call redundant
because it doesn't print anything) or some other init function reconfigures
logging, in which case the output is potentially confusing because it is not
guaranteed that logging is reconfigured before the log call is invoked.

In other scenarios, flag parsing might switch from klog text format to
something else entirely, which then leads to a mixture of text and e.g. JSON
output. In general, code running during init should not log.
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 24, 2025
@k8s-ci-robot k8s-ci-robot added area/kubectl sig/cli Categorizes an issue or PR as relevant to SIG CLI. labels Jun 24, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in SIG CLI Jun 24, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. label Jun 24, 2025
@pohly
Copy link
Contributor Author

pohly commented Jun 24, 2025

/assign @soltysh

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

This is fine for now, we'll probably need to revise how we handle that part of initialization and logging around it.

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 25, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 2bec46d8e897d615c5bc3dd8d2b29ebec31804c2

@soltysh
Copy link
Contributor

soltysh commented Jun 25, 2025

/triage accepted
/priority important-longterm

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: pohly, soltysh

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

The pull request process is described here

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jun 25, 2025
@k8s-ci-robot k8s-ci-robot merged commit b832d10 into kubernetes:master Jun 25, 2025
13 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.34 milestone Jun 25, 2025
@github-project-automation github-project-automation bot moved this from Needs Triage to Done in SIG CLI Jun 25, 2025
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. area/kubectl cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. release-note-none Denotes a PR that doesn't merit a release note. sig/cli Categorizes an issue or PR as relevant to SIG CLI. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants