Skip to content
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
1 change: 1 addition & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (rc *RunCommand) runRun(args []string) error {
config.Cmd = args[1:]
}
containerName := rc.name
config.ContainerConfig.OpenStdin = rc.stdin

ctx := context.Background()
apiClient := rc.cli.Client()
Expand Down
3 changes: 1 addition & 2 deletions ctrd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/alibaba/pouch/pkg/errtypes"

"github.com/containerd/containerd"
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/linux/runctypes"
Expand Down Expand Up @@ -164,7 +163,7 @@ func (c *Client) RecoverContainer(ctx context.Context, id string, io *containeri
return errors.Wrap(err, "failed to load container")
}

task, err := lc.Task(ctx, cio.WithAttach(io.Stdin, io.Stdout, io.Stderr))
task, err := lc.Task(ctx, containerio.WithAttach(io.Stdin, io.Stdout, io.Stderr))
if err != nil {
if !errdefs.IsNotFound(err) {
return errors.Wrap(err, "failed to get task")
Expand Down
44 changes: 31 additions & 13 deletions daemon/containerio/cio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package containerio

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -45,18 +46,8 @@ func (c *cio) Close() error {
return c.closer.Close()
}

// FIFOSet is a set of fifos for use with tasks
type FIFOSet struct {
// Dir is the directory holding the task fifos
Dir string
// In, Out, and Err fifo paths
In, Out, Err string
// Terminal returns true if a terminal is being used for the task
Terminal bool
}

// NewFifos returns a new set of fifos for the task
func NewFifos(id string, stdin bool) (*FIFOSet, error) {
func NewFifos(id string, stdin bool) (*containerdio.FIFOSet, error) {
root := "/run/containerd/fifo"
if err := os.MkdirAll(root, 0700); err != nil {
return nil, err
Expand All @@ -65,7 +56,7 @@ func NewFifos(id string, stdin bool) (*FIFOSet, error) {
if err != nil {
return nil, err
}
fifos := &FIFOSet{
fifos := &containerdio.FIFOSet{
Dir: dir,
In: filepath.Join(dir, id+"-stdin"),
Out: filepath.Join(dir, id+"-stdout"),
Expand Down Expand Up @@ -109,7 +100,7 @@ func (g *wgCloser) Cancel() {
g.cancel()
}

func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
func copyIO(fifos *containerdio.FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
var (
f io.ReadWriteCloser
set []io.Closer
Expand Down Expand Up @@ -200,3 +191,30 @@ func NewIOWithTerminal(stdin io.Reader, stdout, stderr io.Writer, terminal bool,
return i, nil
}
}

// WithAttach attaches the existing io for a task to the provided io.Reader/Writers
func WithAttach(stdin io.Reader, stdout, stderr io.Writer) containerdio.Attach {
return func(paths *containerdio.FIFOSet) (containerdio.IO, error) {
if paths == nil {
return nil, fmt.Errorf("cannot attach to existing fifos")
}
cfg := containerdio.Config{
Terminal: paths.Terminal,
Stdout: paths.Out,
Stderr: paths.Err,
Stdin: paths.In,
}
i := &cio{config: cfg}
set := &ioSet{
in: stdin,
out: stdout,
err: stderr,
}
closer, err := copyIO(paths, set, cfg.Terminal)
if err != nil {
return nil, err
}
i.closer = closer
return i, nil
}
}