Skip to content

Commit 8a46065

Browse files
committed
enhance exec probe logging with pod and container context
Signed-off-by: xigang <wangxigang2014@gmail.com>
1 parent a6f32c0 commit 8a46065

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

pkg/kubelet/prober/prober.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (pb *prober) runProbe(ctx context.Context, probeType probeType, p *v1.Probe
152152
case p.Exec != nil:
153153
klog.V(4).InfoS("Exec-Probe runProbe", "pod", klog.KObj(pod), "containerName", container.Name, "execCommand", p.Exec.Command)
154154
command := kubecontainer.ExpandContainerCommandOnlyStatic(p.Exec.Command, container.Env)
155-
return pb.exec.Probe(pb.newExecInContainer(ctx, container, containerID, command, timeout))
155+
return pb.exec.Probe(pb.newExecInContainer(ctx, pod, container, containerID, command, timeout))
156156

157157
case p.HTTPGet != nil:
158158
req, err := httpprobe.NewRequestForHTTPGetAction(p.HTTPGet, &container, status.PodIP, "probe")
@@ -202,14 +202,18 @@ func (pb *prober) runProbe(ctx context.Context, probeType probeType, p *v1.Probe
202202
type execInContainer struct {
203203
// run executes a command in a container. Combined stdout and stderr output is always returned. An
204204
// error is returned if one occurred.
205-
run func() ([]byte, error)
206-
writer io.Writer
205+
run func() ([]byte, error)
206+
writer io.Writer
207+
pod *v1.Pod
208+
container v1.Container
207209
}
208210

209-
func (pb *prober) newExecInContainer(ctx context.Context, container v1.Container, containerID kubecontainer.ContainerID, cmd []string, timeout time.Duration) exec.Cmd {
210-
return &execInContainer{run: func() ([]byte, error) {
211-
return pb.runner.RunInContainer(ctx, containerID, cmd, timeout)
212-
}}
211+
func (pb *prober) newExecInContainer(ctx context.Context, pod *v1.Pod, container v1.Container, containerID kubecontainer.ContainerID, cmd []string, timeout time.Duration) exec.Cmd {
212+
return &execInContainer{
213+
run: func() ([]byte, error) { return pb.runner.RunInContainer(ctx, containerID, cmd, timeout) },
214+
pod: pod,
215+
container: container,
216+
}
213217
}
214218

215219
func (eic *execInContainer) Run() error {
@@ -253,7 +257,7 @@ func (eic *execInContainer) Start() error {
253257
if eic.writer != nil {
254258
// only record the write error, do not cover the command run error
255259
if p, err := eic.writer.Write(data); err != nil {
256-
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p)
260+
klog.ErrorS(err, "Unable to write all bytes from execInContainer", "expectedBytes", len(data), "actualBytes", p, "pod", klog.KObj(eic.pod), "containerName", eic.container.Name)
257261
}
258262
}
259263
return err

pkg/kubelet/prober/prober_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func TestNewExecInContainer(t *testing.T) {
321321
container := v1.Container{}
322322
containerID := kubecontainer.ContainerID{Type: "docker", ID: "containerID"}
323323
cmd := []string{"/foo", "bar"}
324-
exec := prober.newExecInContainer(ctx, container, containerID, cmd, 0)
324+
exec := prober.newExecInContainer(ctx, &v1.Pod{}, container, containerID, cmd, 0)
325325

326326
var dataBuffer bytes.Buffer
327327
writer := ioutils.LimitWriter(&dataBuffer, int64(limit))

0 commit comments

Comments
 (0)