mirror of
https://github.com/kubernetes/kubectl.git
synced 2026-06-08 16:23:21 -04:00
Merge pull request #139091 from olamilekan000/fix-duplicate-log-when-attachment-to-fials
fix duplicate logs suring run command when attachment to pod fails Kubernetes-commit: ee954c1067272102c6b2dfe02bd98d5ed493a450
This commit is contained in:
commit
af7f63e55a
1 changed files with 10 additions and 6 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue