logs: Use resource builder

This commit is contained in:
kargakis
2015-10-18 16:20:28 +02:00
parent 32956b7dd3
commit 4fdb6d1331
10 changed files with 257 additions and 137 deletions

View File

@@ -205,7 +205,7 @@ func Run(f *cmdutil.Factory, cmdIn io.Reader, cmdOut, cmdErr io.Writer, cmd *cob
if err != nil {
return err
}
return handleAttachPod(client, attachablePod, opts)
return handleAttachPod(f, client, attachablePod, opts)
}
outputFormat := cmdutil.GetFlagString(cmd, "output")
@@ -244,20 +244,40 @@ func waitForPodRunning(c *client.Client, pod *api.Pod, out io.Writer) (status ap
}
}
func handleAttachPod(c *client.Client, pod *api.Pod, opts *AttachOptions) error {
func handleAttachPod(f *cmdutil.Factory, c *client.Client, pod *api.Pod, opts *AttachOptions) error {
status, err := waitForPodRunning(c, pod, opts.Out)
if err != nil {
return err
}
if status == api.PodSucceeded || status == api.PodFailed {
return handleLog(c, pod.Namespace, pod.Name, &api.PodLogOptions{Container: opts.GetContainerName(pod)}, opts.Out)
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: opts.GetContainerName(pod)})
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.Client = c
opts.PodName = pod.Name
opts.Namespace = pod.Namespace
if err := opts.Run(); err != nil {
fmt.Fprintf(opts.Out, "Error attaching, falling back to logs: %v\n", err)
return handleLog(c, pod.Namespace, pod.Name, &api.PodLogOptions{Container: opts.GetContainerName(pod)}, opts.Out)
req, err := f.LogsForObject(pod, &api.PodLogOptions{Container: opts.GetContainerName(pod)})
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 nil
}