Refactor AttachToContainer and Logs.

This commit is contained in:
Random-Liu 2016-04-17 13:00:52 -07:00
parent de5f407058
commit d33b69a0de
5 changed files with 37 additions and 55 deletions

View File

@ -68,13 +68,13 @@ type DockerInterface interface {
ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error) ListImages(opts docker.ListImagesOptions) ([]docker.APIImages, error)
PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error
RemoveImage(image string) error RemoveImage(image string) error
Logs(opts docker.LogsOptions) error Logs(string, dockertypes.ContainerLogsOptions, StreamOptions) error
Version() (*docker.Env, error) Version() (*docker.Env, error)
Info() (*docker.Env, error) Info() (*docker.Env, error)
CreateExec(string, dockertypes.ExecConfig) (*dockertypes.ContainerExecCreateResponse, error) CreateExec(string, dockertypes.ExecConfig) (*dockertypes.ContainerExecCreateResponse, error)
StartExec(string, dockertypes.ExecStartCheck, StreamOptions) error StartExec(string, dockertypes.ExecStartCheck, StreamOptions) error
InspectExec(id string) (*dockertypes.ContainerExecInspect, error) InspectExec(id string) (*dockertypes.ContainerExecInspect, error)
AttachToContainer(opts docker.AttachToContainerOptions) error AttachToContainer(string, dockertypes.ContainerAttachOptions, StreamOptions) error
} }
// KubeletContainerName encapsulates a pod name and a Kubernetes container name. // KubeletContainerName encapsulates a pod name and a Kubernetes container name.

View File

@ -412,7 +412,7 @@ func (f *FakeDockerClient) RemoveContainer(id string, opts dockertypes.Container
// Logs is a test-spy implementation of DockerInterface.Logs. // Logs is a test-spy implementation of DockerInterface.Logs.
// It adds an entry "logs" to the internal method call record. // It adds an entry "logs" to the internal method call record.
func (f *FakeDockerClient) Logs(opts docker.LogsOptions) error { func (f *FakeDockerClient) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.called = append(f.called, "logs") f.called = append(f.called, "logs")
@ -462,7 +462,7 @@ func (f *FakeDockerClient) StartExec(startExec string, opts dockertypes.ExecStar
return nil return nil
} }
func (f *FakeDockerClient) AttachToContainer(opts docker.AttachToContainerOptions) error { func (f *FakeDockerClient) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
f.Lock() f.Lock()
defer f.Unlock() defer f.Unlock()
f.called = append(f.called, "attach") f.called = append(f.called, "attach")

View File

@ -139,11 +139,11 @@ func (in instrumentedDockerInterface) RemoveImage(image string) error {
return err return err
} }
func (in instrumentedDockerInterface) Logs(opts docker.LogsOptions) error { func (in instrumentedDockerInterface) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
const operation = "logs" const operation = "logs"
defer recordOperation(operation, time.Now()) defer recordOperation(operation, time.Now())
err := in.client.Logs(opts) err := in.client.Logs(id, opts, sopts)
recordError(operation, err) recordError(operation, err)
return err return err
} }
@ -193,11 +193,11 @@ func (in instrumentedDockerInterface) InspectExec(id string) (*dockertypes.Conta
return out, err return out, err
} }
func (in instrumentedDockerInterface) AttachToContainer(opts docker.AttachToContainerOptions) error { func (in instrumentedDockerInterface) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
const operation = "attach" const operation = "attach"
defer recordOperation(operation, time.Now()) defer recordOperation(operation, time.Now())
err := in.client.AttachToContainer(opts) err := in.client.AttachToContainer(id, opts, sopts)
recordError(operation, err) recordError(operation, err)
return err return err
} }

View File

@ -23,7 +23,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"strconv"
"time" "time"
"github.com/docker/docker/pkg/stdcopy" "github.com/docker/docker/pkg/stdcopy"
@ -210,21 +209,14 @@ func (d *kubeDockerClient) RemoveImage(image string) error {
return err return err
} }
func (d *kubeDockerClient) Logs(opts docker.LogsOptions) error { func (d *kubeDockerClient) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
resp, err := d.client.ContainerLogs(getDefaultContext(), dockertypes.ContainerLogsOptions{ opts.ContainerID = id
ContainerID: opts.Container, resp, err := d.client.ContainerLogs(getDefaultContext(), opts)
ShowStdout: opts.Stdout,
ShowStderr: opts.Stderr,
Since: strconv.FormatInt(opts.Since, 10),
Timestamps: opts.Timestamps,
Follow: opts.Follow,
Tail: opts.Tail,
})
if err != nil { if err != nil {
return err return err
} }
defer resp.Close() defer resp.Close()
return d.redirectResponseToOutputStream(opts.RawTerminal, opts.OutputStream, opts.ErrorStream, resp) return d.redirectResponseToOutputStream(sopts.RawTerminal, sopts.OutputStream, sopts.ErrorStream, resp)
} }
func (d *kubeDockerClient) Version() (*docker.Env, error) { func (d *kubeDockerClient) Version() (*docker.Env, error) {
@ -276,24 +268,14 @@ func (d *kubeDockerClient) InspectExec(id string) (*dockertypes.ContainerExecIns
return &resp, nil return &resp, nil
} }
func (d *kubeDockerClient) AttachToContainer(opts docker.AttachToContainerOptions) error { func (d *kubeDockerClient) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
resp, err := d.client.ContainerAttach(getDefaultContext(), dockertypes.ContainerAttachOptions{ opts.ContainerID = id
ContainerID: opts.Container, resp, err := d.client.ContainerAttach(getDefaultContext(), opts)
Stream: opts.Stream,
Stdin: opts.Stdin,
Stdout: opts.Stdout,
Stderr: opts.Stderr,
// TODO: How to deal with the *Logs* here? There is no *Logs* field in the engine-api.
})
if err != nil { if err != nil {
return err return err
} }
defer resp.Close() defer resp.Close()
if opts.Success != nil { return d.holdHijackedConnection(sopts.RawTerminal, sopts.InputStream, sopts.OutputStream, sopts.ErrorStream, resp)
opts.Success <- struct{}{}
<-opts.Success
}
return d.holdHijackedConnection(opts.RawTerminal, opts.InputStream, opts.OutputStream, opts.ErrorStream, resp)
} }
// redirectResponseToOutputStream redirect the response stream to stdout and stderr. When tty is true, all stream will // redirectResponseToOutputStream redirect the response stream to stdout and stderr. When tty is true, all stream will

View File

@ -274,23 +274,22 @@ func (dm *DockerManager) GetContainerLogs(pod *api.Pod, containerID kubecontaine
if logOptions.SinceTime != nil { if logOptions.SinceTime != nil {
since = logOptions.SinceTime.Unix() since = logOptions.SinceTime.Unix()
} }
opts := docker.LogsOptions{ opts := dockertypes.ContainerLogsOptions{
Container: containerID.ID, ShowStdout: true,
Stdout: true, ShowStderr: true,
Stderr: true, Since: strconv.FormatInt(since, 10),
OutputStream: stdout, Timestamps: logOptions.Timestamps,
ErrorStream: stderr, Follow: logOptions.Follow,
Timestamps: logOptions.Timestamps,
Since: since,
Follow: logOptions.Follow,
RawTerminal: false,
} }
if logOptions.TailLines != nil { if logOptions.TailLines != nil {
opts.Tail = strconv.FormatInt(*logOptions.TailLines, 10) opts.Tail = strconv.FormatInt(*logOptions.TailLines, 10)
} }
sopts := StreamOptions{
err = dm.client.Logs(opts) OutputStream: stdout,
ErrorStream: stderr,
RawTerminal: false,
}
err = dm.client.Logs(containerID.ID, opts, sopts)
return return
} }
@ -1096,19 +1095,20 @@ func (dm *DockerManager) ExecInContainer(containerID kubecontainer.ContainerID,
} }
func (dm *DockerManager) AttachContainer(containerID kubecontainer.ContainerID, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool) error { func (dm *DockerManager) AttachContainer(containerID kubecontainer.ContainerID, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool) error {
opts := docker.AttachToContainerOptions{ // TODO(random-liu): Do we really use the *Logs* field here?
Container: containerID.ID, opts := dockertypes.ContainerAttachOptions{
Stream: true,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: stderr != nil,
}
sopts := StreamOptions{
InputStream: stdin, InputStream: stdin,
OutputStream: stdout, OutputStream: stdout,
ErrorStream: stderr, ErrorStream: stderr,
Stream: true,
Logs: true,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: stderr != nil,
RawTerminal: tty, RawTerminal: tty,
} }
return dm.client.AttachToContainer(opts) return dm.client.AttachToContainer(containerID.ID, opts, sopts)
} }
func noPodInfraContainerError(podName, podNamespace string) error { func noPodInfraContainerError(podName, podNamespace string) error {