mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #50225 from tcharding/kubectl-run-log
Automatic merge from submit-queue (batch tested with PRs 49847, 49743, 49853, 50225, 50479) Remove duplicate logging code **What this PR does / why we need it**: Currently function `handleAttachPod` contains duplicate code which copies the AttachOptions output writer to the pod logging writer. This code can be refactored into a separate function. **Special notes for your reviewer**: Refactor only, does not change program logic. **Release note**: ```release-note NONE ``` /sig cli /kind cleanup
This commit is contained in:
commit
a1933f8efb
@ -455,42 +455,44 @@ func handleAttachPod(f cmdutil.Factory, podClient coreclient.PodsGetter, ns, nam
|
||||
if err != nil && err != conditions.ErrPodCompleted {
|
||||
return err
|
||||
}
|
||||
ctrName, err := opts.GetContainerName(pod)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
|
||||
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName}, opts.GetPodTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
readCloser, err := req.Stream()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer readCloser.Close()
|
||||
_, err = io.Copy(opts.Out, readCloser)
|
||||
return err
|
||||
return logOpts(f, pod, opts)
|
||||
}
|
||||
|
||||
opts.PodClient = podClient
|
||||
|
||||
opts.PodName = name
|
||||
opts.Namespace = ns
|
||||
|
||||
// TODO: opts.Run sets opts.Err to nil, we need to find a better way
|
||||
stderr := opts.Err
|
||||
if err := opts.Run(); err != nil {
|
||||
fmt.Fprintf(stderr, "Error attaching, falling back to logs: %v\n", err)
|
||||
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName}, opts.GetPodTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
readCloser, err := req.Stream()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer readCloser.Close()
|
||||
_, err = io.Copy(opts.Out, readCloser)
|
||||
return logOpts(f, pod, opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// logOpts logs output from opts to the pods log.
|
||||
func logOpts(f cmdutil.Factory, pod *api.Pod, opts *AttachOptions) error {
|
||||
ctrName, err := opts.GetContainerName(pod)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName}, opts.GetPodTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
readCloser, err := req.Stream()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer readCloser.Close()
|
||||
|
||||
_, err = io.Copy(opts.Out, readCloser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user