diff --git a/pkg/cmd/run/run.go b/pkg/cmd/run/run.go index d5498453a..032473e0d 100644 --- a/pkg/cmd/run/run.go +++ b/pkg/cmd/run/run.go @@ -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 }