fix duplicate logs suring run command when attachment to pod fails

Signed-off-by: olalekan odukoya <odukoyaonline@gmail.com>

Kubernetes-commit: e5af11b313630ad9ae4eb3201a1f06934fa2c1aa
This commit is contained in:
olalekan odukoya 2026-05-15 16:00:58 +01:00 committed by Kubernetes Publisher
parent 68ab95bc49
commit 6a6eb98f02

View file

@ -520,24 +520,28 @@ func handleAttachPod(f cmdutil.Factory, podClient corev1client.PodsGetter, ns, n
}
// Fetch and display any logs that were printed before attach connects.
var logsSinceTime *metav1.Time
ctrName, err := opts.GetContainerName(pod)
if err == nil {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if logErr := logOpts(ctx, f, pod, opts, &corev1.PodLogOptions{
Container: ctrName,
Follow: false,
}); logErr != nil {
if opts.ErrOut != nil {
//nolint:errcheck
fmt.Fprintf(opts.ErrOut, "warning: couldn't fetch pre-attach logs: %v\n", logErr)
}
}); logErr == nil {
t := metav1.Now()
logsSinceTime = &t
} else if opts.ErrOut != nil {
//nolint:errcheck
fmt.Fprintf(opts.ErrOut, "warning: couldn't fetch pre-attach logs: %v\n", logErr)
}
cancel()
}
if err := opts.Run(); err != nil {
fmt.Fprintf(opts.ErrOut, "warning: couldn't attach to pod/%s, falling back to streaming logs: %v\n", name, err)
return logOpts(context.Background(), f, pod, opts, nil)
return logOpts(context.Background(), f, pod, opts, &corev1.PodLogOptions{
SinceTime: logsSinceTime,
})
}
return nil
}