Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: memory buffer container io backend #625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions daemon/containerio/mem_buffer.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions daemon/containerio/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package containerio

import (
"bytes"
"net/http"
)

Expand All @@ -12,6 +13,7 @@ type Option struct {
hijack http.Hijacker
hijackUpgrade bool
stdinBackend string
memBuffer *bytes.Buffer
}

// NewOption creates the Option instance.
Expand Down Expand Up @@ -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
}
}
12 changes: 9 additions & 3 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 9 additions & 3 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mgr

import (
"bytes"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -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.
Expand Down