From 7b7279671df890809dc83ce0157b66f7ab16a0e0 Mon Sep 17 00:00:00 2001 From: YaoZengzeng Date: Tue, 23 Jan 2018 16:13:54 +0800 Subject: [PATCH] feature: bytes.Buffer container io backend Signed-off-by: YaoZengzeng --- daemon/containerio/mem_buffer.go | 38 ++++++++++++++++++++++++++++++++ daemon/containerio/options.go | 13 +++++++++++ daemon/mgr/container.go | 12 +++++++--- daemon/mgr/container_types.go | 12 +++++++--- 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 daemon/containerio/mem_buffer.go diff --git a/daemon/containerio/mem_buffer.go b/daemon/containerio/mem_buffer.go new file mode 100644 index 000000000..e7f113a2a --- /dev/null +++ b/daemon/containerio/mem_buffer.go @@ -0,0 +1,38 @@ +package containerio + +import ( + "bytes" + "io" +) + +func init() { + Register(func() Backend { + return &memBuffer{} + }) +} + +type memBuffer struct { + buffer *bytes.Buffer +} + +func (b *memBuffer) Name() string { + return "memBuffer" +} + +func (b *memBuffer) Init(opt *Option) error { + b.buffer = opt.memBuffer + return nil +} + +func (b *memBuffer) Out() io.Writer { + return b.buffer +} + +func (b *memBuffer) In() io.Reader { + return b.buffer +} + +func (b *memBuffer) Close() error { + // Don't need to close bytes.Buffer. + return nil +} diff --git a/daemon/containerio/options.go b/daemon/containerio/options.go index a1f6f3d20..d4907ba61 100644 --- a/daemon/containerio/options.go +++ b/daemon/containerio/options.go @@ -1,6 +1,7 @@ package containerio import ( + "bytes" "net/http" ) @@ -12,6 +13,7 @@ type Option struct { hijack http.Hijacker hijackUpgrade bool stdinBackend string + memBuffer *bytes.Buffer } // NewOption creates the Option instance. @@ -77,3 +79,14 @@ func WithStdinHijack() func(*Option) { opt.stdinBackend = "hijack" } } + +// WithMemBuffer specified the memory buffer backend. +func WithMemBuffer(memBuffer *bytes.Buffer) func(*Option) { + return func(opt *Option) { + if opt.backends == nil { + opt.backends = make(map[string]struct{}) + } + opt.backends["memBuffer"] = struct{}{} + opt.memBuffer = memBuffer + } +} diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 2cf169703..bbeefa262 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -618,9 +618,15 @@ func (mgr *ContainerManager) openIO(id string, attach *AttachConfig, exec bool) } if attach != nil { - options = append(options, containerio.WithHijack(attach.Hijack, attach.Upgrade)) - if attach.Stdin { - options = append(options, containerio.WithStdinHijack()) + if attach.Hijack != nil { + // Attaching using http. + options = append(options, containerio.WithHijack(attach.Hijack, attach.Upgrade)) + if attach.Stdin { + options = append(options, containerio.WithStdinHijack()) + } + } else if attach.MemBuffer != nil { + // Attaching using memory buffer. + options = append(options, containerio.WithMemBuffer(attach.MemBuffer)) } } else if !exec { options = append(options, containerio.WithRawFile()) diff --git a/daemon/mgr/container_types.go b/daemon/mgr/container_types.go index 5bc5449d1..6746d4392 100644 --- a/daemon/mgr/container_types.go +++ b/daemon/mgr/container_types.go @@ -1,6 +1,7 @@ package mgr import ( + "bytes" "net/http" "sync" "time" @@ -30,11 +31,16 @@ type containerExecConfig struct { // AttachConfig wraps some infos of attaching. type AttachConfig struct { + Stdin bool + Stdout bool + Stderr bool + + // Attach using http. Hijack http.Hijacker - Stdin bool - Stdout bool - Stderr bool Upgrade bool + + // Attach using memory buffer. + MemBuffer *bytes.Buffer } // ContainerRemoveOption wraps the container remove interface params.