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
Convert cmd/kubelet/app/server.go to structured logging #98334
Conversation
/assign @vishh |
/hold Bug wasn't accepted and I'm not sure this is an improved user experience. |
Note: I think Fatal logs are going away anyways with the structured logging migration this release, so this PR will be replaced soon anyways. See: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#replacing-fatal-calls It would be better to migrate this entire file to use structured logging rather than just making this change. |
Understand, if possible, I want this pr to fix this bug alone (keep focus). Then reopen a pr to migrate the entire file to use structured logs. What do you think? |
I would prefer you fix it all in one go, because migrating away from using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this! I have a bit of feedback on the log changes.
cmd/kubelet/app/server.go
Outdated
@@ -117,7 +117,8 @@ func NewKubeletCommand() *cobra.Command { | |||
kubeletConfig, err := options.NewKubeletConfiguration() | |||
// programmer error | |||
if err != nil { | |||
klog.Fatal(err) | |||
klog.ErrorS(err, "Failed create a new kubelet configuration") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
klog.ErrorS(err, "Failed create a new kubelet configuration") | |
klog.ErrorS(err, "Failed to create a new kubelet configuration") |
cmd/kubelet/app/server.go
Outdated
@@ -117,7 +117,8 @@ func NewKubeletCommand() *cobra.Command { | |||
kubeletConfig, err := options.NewKubeletConfiguration() | |||
// programmer error | |||
if err != nil { | |||
klog.Fatal(err) | |||
klog.ErrorS(err, "Failed create a new kubelet configuration") | |||
os.Exit(255) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use exit code 1 throughout this file, and not 255, to be consistent with the rest of the files in cmd/
: https://cs.k8s.io/?q=os.Exit&i=nope&files=cmd%2F.*&repos=kubernetes/kubernetes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the error code of klog.Fatalf
will return 255, in order not to break compatibility, I returned 255.
So I kept the status quo. Does it need to be adjusted to 1 here? Does it need to be discussed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll ask in Slack about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not be using 255, but rather 1.
also idealy applications should have a single point of os.Exit() instead of multiple ones.
but as mentioned on slack this change would need an ACTION REQUIRED if users are expecting an exact value and not != 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also idealy applications should have a single point of os.Exit() instead of multiple ones.
There are various reasons for calling os.Exit(1)
. If only one os.Exit()
is needed, do you have any good suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deferring to the kubelet maintainers, but ideally errors should bubble up to a single point of os.Exit(x).
and there, depending on the different error types different exit codes can be returned.
kubernetes components tend to just os.Exit at arbitrary locations with fixed error codes, which is not a great pattern.
cmd/kubelet/app/server.go
Outdated
} | ||
|
||
// check if there are non-flag arguments in the command line | ||
cmds := cleanFlagSet.Args() | ||
if len(cmds) > 0 { | ||
cmd.Usage() | ||
klog.Fatalf("unknown command: %s", cmds[0]) | ||
klog.InfoS("Unknown command", "command", cmds[0]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fatal calls should use ErrorS
, you can pass it a nil error rather than use InfoS
: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#using-errors
/cc @serathius
cmd/kubelet/app/server.go
Outdated
@@ -534,7 +548,7 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend | |||
return err | |||
} | |||
if cloud != nil { | |||
klog.V(2).Infof("Successfully initialized cloud provider: %q from the config file: %q\n", s.CloudProvider, s.CloudConfigFile) | |||
klog.V(2).InfoS("Successfully initialized cloud provider", "cloud provider", s.CloudProvider, "config file", s.CloudConfigFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keys should not have spaces. I think the convention is to use camelCase (i.e. "cloudProvider"
)
cmd/kubelet/app/server.go
Outdated
@@ -611,14 +625,14 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend | |||
cgroupRoots = append(cgroupRoots, nodeAllocatableRoot) | |||
kubeletCgroup, err := cm.GetKubeletContainer(s.KubeletCgroups) | |||
if err != nil { | |||
klog.Warningf("failed to get the kubelet's cgroup: %v. Kubelet system container metrics may be missing.", err) | |||
klog.InfoS("Failed to get the kubelet's cgroup. Kubelet system container metrics may be missing.", "error", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use err
instead of error
for the key: https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments (same for the rest of the file)
cmd/kubelet/app/server.go
Outdated
@@ -677,15 +691,15 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend | |||
|
|||
if reservedSystemCPUs.Size() > 0 { | |||
// at cmd option valication phase it is tested either --system-reserved-cgroup or --kube-reserved-cgroup is specified, so overwrite should be ok | |||
klog.Infof("Option --reserved-cpus is specified, it will overwrite the cpu setting in KubeReserved=\"%v\", SystemReserved=\"%v\".", s.KubeReserved, s.SystemReserved) | |||
klog.InfoS("Option --reserved-cpus is specified, it will overwrite the cpu setting in KubeReserved=\"%v\", SystemReserved=\"%v\".", s.KubeReserved, s.SystemReserved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This log line needs to be updated, it still has string formatting
cmd/kubelet/app/server.go
Outdated
if s.KubeReserved != nil { | ||
delete(s.KubeReserved, "cpu") | ||
} | ||
if s.SystemReserved == nil { | ||
s.SystemReserved = make(map[string]string) | ||
} | ||
s.SystemReserved["cpu"] = strconv.Itoa(reservedSystemCPUs.Size()) | ||
klog.Infof("After cpu setting is overwritten, KubeReserved=\"%v\", SystemReserved=\"%v\"", s.KubeReserved, s.SystemReserved) | ||
klog.InfoS("After cpu setting is overwritten, KubeReserved=\"%v\", SystemReserved=\"%v\"", s.KubeReserved, s.SystemReserved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also needs its string formatting updated
cmd/kubelet/app/server.go
Outdated
@@ -791,7 +805,7 @@ func run(ctx context.Context, s *options.KubeletServer, kubeDeps *kubelet.Depend | |||
go wait.Until(func() { | |||
err := http.ListenAndServe(net.JoinHostPort(s.HealthzBindAddress, strconv.Itoa(int(s.HealthzPort))), mux) | |||
if err != nil { | |||
klog.Errorf("Starting healthz server failed: %v", err) | |||
klog.ErrorS(err, "Failed to starting healthz server") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
klog.ErrorS(err, "Failed to starting healthz server") | |
klog.ErrorS(err, "Failed to start healthz server") |
cmd/kubelet/app/server.go
Outdated
@@ -1093,7 +1107,7 @@ func RunKubelet(kubeServer *options.KubeletServer, kubeDeps *kubelet.Dependencie | |||
for _, ip := range strings.Split(kubeServer.NodeIP, ",") { | |||
parsedNodeIP := net.ParseIP(strings.TrimSpace(ip)) | |||
if parsedNodeIP == nil { | |||
klog.Warningf("Could not parse --node-ip value %q; ignoring", ip) | |||
klog.InfoS("Could not parse --node-ip ignoring", "nodeIp", ip) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
klog.InfoS("Could not parse --node-ip ignoring", "nodeIp", ip) | |
klog.InfoS("Could not parse --node-ip ignoring", "nodeIP", ip) |
cmd/kubelet/app/server.go
Outdated
@@ -1114,7 +1128,7 @@ func RunKubelet(kubeServer *options.KubeletServer, kubeDeps *kubelet.Dependencie | |||
}) | |||
|
|||
credentialprovider.SetPreferredDockercfgPath(kubeServer.RootDirectory) | |||
klog.V(2).Infof("Using root directory: %v", kubeServer.RootDirectory) | |||
klog.V(2).InfoS("Using root directory", "directory", kubeServer.RootDirectory) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@serathius do you have an opinion on directory vs. dir?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"path" :P
/priority important-longterm |
/remove-kind bug |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: mrunalp, wawa0210 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 |
… instead of the entire stack log
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
/retest |
/remove-label needs-rebase |
@pacoxu: The label(s) In response to this:
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. |
/retest |
/test pull-kubernetes-e2e-kind-ipv6 |
What type of PR is this?
/kind bug
What this PR does / why we need it:
When the parameters are incorrect, kubelet only outputs the error log instead of the entire stack log
This way the user experience will be better and consistent with other components (such as kube-proxy)
Which issue(s) this PR fixes:
Fixes #98292
Special notes for your reviewer:
For compatibility, refer to
klog.Fatalf
Exit code remains 255Does this PR introduce a user-facing change?:
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:
/sig node
/area kubelet