Commit a1933f8e authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

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
parents 6d91ad2d 59c31c89
...@@ -455,42 +455,44 @@ func handleAttachPod(f cmdutil.Factory, podClient coreclient.PodsGetter, ns, nam ...@@ -455,42 +455,44 @@ func handleAttachPod(f cmdutil.Factory, podClient coreclient.PodsGetter, ns, nam
if err != nil && err != conditions.ErrPodCompleted { if err != nil && err != conditions.ErrPodCompleted {
return err return err
} }
ctrName, err := opts.GetContainerName(pod)
if err != nil {
return err
}
if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed { if pod.Status.Phase == api.PodSucceeded || pod.Status.Phase == api.PodFailed {
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName}, opts.GetPodTimeout) return logOpts(f, pod, opts)
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
} }
opts.PodClient = podClient opts.PodClient = podClient
opts.PodName = name opts.PodName = name
opts.Namespace = ns opts.Namespace = ns
// TODO: opts.Run sets opts.Err to nil, we need to find a better way // TODO: opts.Run sets opts.Err to nil, we need to find a better way
stderr := opts.Err stderr := opts.Err
if err := opts.Run(); err != nil { if err := opts.Run(); err != nil {
fmt.Fprintf(stderr, "Error attaching, falling back to logs: %v\n", err) fmt.Fprintf(stderr, "Error attaching, falling back to logs: %v\n", err)
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: ctrName}, opts.GetPodTimeout) return logOpts(f, pod, opts)
if err != nil { }
return err return nil
} }
readCloser, err := req.Stream()
if err != nil { // logOpts logs output from opts to the pods log.
return err func logOpts(f cmdutil.Factory, pod *api.Pod, opts *AttachOptions) error {
} ctrName, err := opts.GetContainerName(pod)
defer readCloser.Close() if err != nil {
_, err = io.Copy(opts.Out, readCloser) 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 err
} }
return nil return nil
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment