Skip to content

kubelet: enhance exec probe logging with pod and container context #132744

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xigang
Copy link
Member

@xigang xigang commented Jul 5, 2025

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

This improves debugging capabilities by providing more context in exec probe error messages, making it easier to identify which specific pod and container encountered issues during probe execution.

Which issue(s) this PR is related to:

#132714

Special notes for your reviewer:

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. 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. 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. labels Jul 5, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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.

@k8s-ci-robot k8s-ci-robot added needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jul 5, 2025
@k8s-ci-robot k8s-ci-robot requested review from dims and odinuge July 5, 2025 14:23
@xigang
Copy link
Member Author

xigang commented Jul 5, 2025

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Jul 5, 2025
@xigang
Copy link
Member Author

xigang commented Jul 5, 2025

/cc @bart0sh @SergeyKanzhelev @dims
Please take a look. Thanks!

@xigang
Copy link
Member Author

xigang commented Jul 7, 2025

/cc @pacoxu
PTAL. Thanks!

@k8s-ci-robot k8s-ci-robot requested a review from pacoxu July 7, 2025 11:18
@pacoxu
Copy link
Member

pacoxu commented Jul 8, 2025

/cc @tzneal @NoicFank @bart0sh
/assign @tallclair

@xigang
Copy link
Member Author

xigang commented Jul 9, 2025

@bart0sh Would you have some time to review this PR? Thank you!

@@ -152,7 +152,7 @@ func (pb *prober) runProbe(ctx context.Context, probeType probeType, p *v1.Probe
case p.Exec != nil:
klog.V(4).InfoS("Exec-Probe runProbe", "pod", klog.KObj(pod), "containerName", container.Name, "execCommand", p.Exec.Command)
command := kubecontainer.ExpandContainerCommandOnlyStatic(p.Exec.Command, container.Env)
return pb.exec.Probe(pb.newExecInContainer(ctx, container, containerID, command, timeout))
return pb.exec.Probe(pb.newExecInContainer(ctx, pod, container, containerID, command, timeout))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this only implemented for exec probes?

Copy link
Member Author

Choose a reason for hiding this comment

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

For other Probes, when they fail, they return an error and the upper-level caller logs it with the podName and containerName. However, when an exec Probe succeeds, it writes the command output to the data stream. If eic.writer.Write(data) returns an error, the resulting log doesn't include the podName or containerName.

func (eic *execInContainer) Start() error {
data, err := eic.run()
if eic.writer != nil {
// only record the write error, do not cover the command run error
if p, err := eic.writer.Write(data); err != nil {
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p)
}
}
return err
}

Errors from other Probes are consistently logged at this point.

result, output, err := pb.runProbeWithRetries(ctx, probeType, probeSpec, pod, status, container, containerID, maxProbeRetries)
if err != nil {
// Handle probe error
klog.V(1).ErrorS(err, "Probe errored", "probeType", probeType, "pod", klog.KObj(pod), "podUID", pod.UID, "containerName", container.Name, "probeResult", result)
pb.recordContainerEvent(pod, &container, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored and resulted in %s state: %s", probeType, result, err)
return results.Failure, err
}

@@ -253,7 +259,7 @@ func (eic *execInContainer) Start() error {
if eic.writer != nil {
// only record the write error, do not cover the command run error
if p, err := eic.writer.Write(data); err != nil {
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p)
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p, "pod", klog.KObj(eic.pod), "containerName", eic.containerName)
Copy link
Member

Choose a reason for hiding this comment

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

you don't have to cascade the pod object here, just get the error in https://github.com/kubernetes/kubernetes/pull/132744/files#r2197504163 and log out there, don't create this dependency

Copy link
Member Author

Choose a reason for hiding this comment

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

Good suggestion. 👍🏻

Copy link
Contributor

@zerkms zerkms Jul 11, 2025

Choose a reason for hiding this comment

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

@aojea but write error is not returned from the execInContainer::Start(). The probe can be successful with unsuccessful write.

func (eic *execInContainer) Start() error {
data, err := eic.run()
if eic.writer != nil {
// only record the write error, do not cover the command run error
if p, err := eic.writer.Write(data); err != nil {
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p)
}
}
return err
}

Copy link
Member Author

Choose a reason for hiding this comment

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

you don't have to cascade the pod object here, just get the error in https://github.com/kubernetes/kubernetes/pull/132744/files#r2197504163 and log out there, don't create this dependency

When an exec Probe succeeds but eic.writer.Write(data) fails, the error isn't propagated to the upper-level function runProbeWithRetries, so no error log is printed there. That's why this log should include the podName and containerName.

result, output, err := pb.runProbeWithRetries(ctx, probeType, probeSpec, pod, status, container, containerID, maxProbeRetries)
if err != nil {
// Handle probe error
klog.V(1).ErrorS(err, "Probe errored", "probeType", probeType, "pod", klog.KObj(pod), "podUID", pod.UID, "containerName", container.Name, "probeResult", result)
pb.recordContainerEvent(pod, &container, v1.EventTypeWarning, events.ContainerUnhealthy, "%s probe errored and resulted in %s state: %s", probeType, result, err)
return results.Failure, err
}

@aojea
Copy link
Member

aojea commented Jul 10, 2025

you don't have to modify the existing struct and methods, and check the returned value on the probe execution to log

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 10, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: xigang
Once this PR has been reviewed and has the lgtm label, please ask for approval from tallclair. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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 size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 10, 2025
@xigang xigang force-pushed the probe_exec branch 2 times, most recently from 8d737c1 to 6ecec84 Compare July 10, 2025 14:53
@xigang
Copy link
Member Author

xigang commented Jul 10, 2025

@aojea @bart0sh PTAL

@zerkms
Copy link
Contributor

zerkms commented Jul 11, 2025

Just in case my message in threads does not get enough attention:

you don't have to modify the existing struct and methods, and check the returned value on the probe execution to log

@aojea

func (eic *execInContainer) Start() error {
data, err := eic.run()
if eic.writer != nil {
// only record the write error, do not cover the command run error
if p, err := eic.writer.Write(data); err != nil {
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p)
}
}
return err
}

Given that Write error IS NOT returned from the prober - I believe you cannot simply check the returned value on the probe execution: probe can be successful while writer has failed (due to write limiter).

Eg:

cat /dev/random | head -c 15000

☝️ this would be a successful probe with failing writer.

@xigang xigang force-pushed the probe_exec branch 2 times, most recently from ddc00a7 to 8a46065 Compare July 11, 2025 02:26
Signed-off-by: xigang <wangxigang2014@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/kubelet 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. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/node Categorizes an issue or PR as relevant to SIG Node. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Development

Successfully merging this pull request may close these issues.

7 participants