mirror of
https://github.com/kubernetes/kubectl.git
synced 2026-05-28 04:35:50 -04:00
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:
parent
68ab95bc49
commit
6a6eb98f02
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