From 507a149488c9b7882ecadc1ce347cab003802229 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Tue, 16 Jan 2018 18:49:05 +0900 Subject: [PATCH] cio: add WithFIFODir opt Signed-off-by: Akihiro Suda --- cio/io.go | 18 +++++++++++++++--- cmd/ctr/commands/run/run.go | 8 +++++++- cmd/ctr/commands/tasks/exec.go | 9 +++++++-- cmd/ctr/commands/tasks/start.go | 12 +++++++++--- cmd/ctr/commands/tasks/tasks_unix.go | 6 +++--- cmd/ctr/commands/tasks/tasks_windows.go | 6 +++--- defaults/defaults_unix.go | 3 +++ defaults/defaults_windows.go | 3 +++ 8 files changed, 50 insertions(+), 15 deletions(-) diff --git a/cio/io.go b/cio/io.go index 1b4a4dc25..e337c5870 100644 --- a/cio/io.go +++ b/cio/io.go @@ -6,6 +6,8 @@ import ( "io" "os" "sync" + + "github.com/containerd/containerd/defaults" ) // Config holds the IO configurations. @@ -68,6 +70,7 @@ type Streams struct { Stdout io.Writer Stderr io.Writer Terminal bool + FIFODir string } // Opt customize options for creating a Creator or Attach @@ -92,16 +95,25 @@ func WithStreams(stdin io.Reader, stdout, stderr io.Writer) Opt { } } +// WithFIFODir sets the fifo directory. +// e.g. "/run/containerd/fifo", "/run/users/1001/containerd/fifo" +func WithFIFODir(dir string) Opt { + return func(opt *Streams) { + opt.FIFODir = dir + } +} + // NewCreator returns an IO creator from the options func NewCreator(opts ...Opt) Creator { streams := &Streams{} for _, opt := range opts { opt(streams) } + if streams.FIFODir == "" { + streams.FIFODir = defaults.DefaultFIFODir + } return func(id string) (IO, error) { - // TODO: accept root as a param - root := "/run/containerd/fifo" - fifos, err := NewFIFOSetInDir(root, id, streams.Terminal) + fifos, err := NewFIFOSetInDir(streams.FIFODir, id, streams.Terminal) if err != nil { return nil, err } diff --git a/cmd/ctr/commands/run/run.go b/cmd/ctr/commands/run/run.go index c1990cc20..00aad9326 100644 --- a/cmd/ctr/commands/run/run.go +++ b/cmd/ctr/commands/run/run.go @@ -9,6 +9,7 @@ import ( "github.com/containerd/console" "github.com/containerd/containerd" + "github.com/containerd/containerd/cio" "github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/cmd/ctr/commands/tasks" "github.com/containerd/containerd/containers" @@ -170,6 +171,10 @@ var Command = cli.Command{ Name: "detach,d", Usage: "detach from the task after it has started execution", }, + cli.StringFlag{ + Name: "fifo-dir", + Usage: "directory used for storing IO FIFOs", + }, }, commands.SnapshotterFlags...), Action: func(context *cli.Context) error { var ( @@ -200,7 +205,8 @@ var Command = cli.Command{ defer container.Delete(ctx, containerd.WithSnapshotCleanup) } opts := getNewTaskOpts(context) - task, err := tasks.NewTask(ctx, client, container, context.String("checkpoint"), tty, context.Bool("null-io"), opts...) + ioOpts := []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))} + task, err := tasks.NewTask(ctx, client, container, context.String("checkpoint"), tty, context.Bool("null-io"), ioOpts, opts...) if err != nil { return err } diff --git a/cmd/ctr/commands/tasks/exec.go b/cmd/ctr/commands/tasks/exec.go index 67011db49..5e2ccc0a1 100644 --- a/cmd/ctr/commands/tasks/exec.go +++ b/cmd/ctr/commands/tasks/exec.go @@ -28,6 +28,10 @@ var execCommand = cli.Command{ Name: "exec-id", Usage: "exec specific id for the process", }, + cli.StringFlag{ + Name: "fifo-dir", + Usage: "directory used for storing IO FIFOs", + }, }, Action: func(context *cli.Context) error { var ( @@ -60,10 +64,11 @@ var execCommand = cli.Command{ pspec.Terminal = tty pspec.Args = args - ioCreator := cio.NewCreator(cio.WithStdio) + cioOpts := []cio.Opt{cio.WithStdio, cio.WithFIFODir(context.String("fifo-dir"))} if tty { - ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal) + cioOpts = append(cioOpts, cio.WithTerminal) } + ioCreator := cio.NewCreator(cioOpts...) process, err := task.Exec(ctx, context.String("exec-id"), pspec, ioCreator) if err != nil { return err diff --git a/cmd/ctr/commands/tasks/start.go b/cmd/ctr/commands/tasks/start.go index 0fe8f0699..62e0a2dd5 100644 --- a/cmd/ctr/commands/tasks/start.go +++ b/cmd/ctr/commands/tasks/start.go @@ -2,6 +2,7 @@ package tasks import ( "github.com/containerd/console" + "github.com/containerd/containerd/cio" "github.com/containerd/containerd/cmd/ctr/commands" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -17,6 +18,10 @@ var startCommand = cli.Command{ Name: "null-io", Usage: "send all IO to /dev/null", }, + cli.StringFlag{ + Name: "fifo-dir", + Usage: "directory used for storing IO FIFOs", + }, }, Action: func(context *cli.Context) error { var ( @@ -42,10 +47,11 @@ var startCommand = cli.Command{ } var ( - tty = spec.Process.Terminal - opts = getNewTaskOpts(context) + tty = spec.Process.Terminal + opts = getNewTaskOpts(context) + ioOpts = []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))} ) - task, err := NewTask(ctx, client, container, "", tty, context.Bool("null-io"), opts...) + task, err := NewTask(ctx, client, container, "", tty, context.Bool("null-io"), ioOpts, opts...) if err != nil { return err } diff --git a/cmd/ctr/commands/tasks/tasks_unix.go b/cmd/ctr/commands/tasks/tasks_unix.go index 89be7afee..09270e43d 100644 --- a/cmd/ctr/commands/tasks/tasks_unix.go +++ b/cmd/ctr/commands/tasks/tasks_unix.go @@ -51,12 +51,12 @@ func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol } // NewTask creates a new task -func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, tty, nullIO bool, opts ...containerd.NewTaskOpts) (containerd.Task, error) { - stdio := cio.NewCreator(cio.WithStdio) +func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, tty, nullIO bool, ioOpts []cio.Opt, opts ...containerd.NewTaskOpts) (containerd.Task, error) { + stdio := cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...) if checkpoint == "" { ioCreator := stdio if tty { - ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal) + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStdio, cio.WithTerminal}, ioOpts...)...) } if nullIO { if tty { diff --git a/cmd/ctr/commands/tasks/tasks_windows.go b/cmd/ctr/commands/tasks/tasks_windows.go index 843ac72bf..2f4ee1625 100644 --- a/cmd/ctr/commands/tasks/tasks_windows.go +++ b/cmd/ctr/commands/tasks/tasks_windows.go @@ -42,10 +42,10 @@ func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol } // NewTask creates a new task -func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty, nullIO bool, opts ...containerd.NewTaskOpts) (containerd.Task, error) { - ioCreator := cio.NewCreator(cio.WithStdio) +func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty, nullIO bool, ioOpts []cio.Opt, opts ...containerd.NewTaskOpts) (containerd.Task, error) { + ioCreator := cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...) if tty { - ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal) + ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStdio, cio.WithTerminal}, ioOpts...)...) } if nullIO { if tty { diff --git a/defaults/defaults_unix.go b/defaults/defaults_unix.go index b98d0dd94..1a76d569f 100644 --- a/defaults/defaults_unix.go +++ b/defaults/defaults_unix.go @@ -13,4 +13,7 @@ const ( DefaultAddress = "/run/containerd/containerd.sock" // DefaultDebugAddress is the default unix socket address for pprof data DefaultDebugAddress = "/run/containerd/debug.sock" + // DefaultFIFODir is the default location used by client-side cio library + // to store FIFOs. + DefaultFIFODir = "/run/containerd/fifo" ) diff --git a/defaults/defaults_windows.go b/defaults/defaults_windows.go index 977279962..7d74d22e4 100644 --- a/defaults/defaults_windows.go +++ b/defaults/defaults_windows.go @@ -21,4 +21,7 @@ const ( DefaultAddress = `\\.\pipe\containerd-containerd` // DefaultDebugAddress is the default winpipe address for pprof data DefaultDebugAddress = `\\.\pipe\containerd-debug` + // DefaultFIFODir is the default location used by client-side cio library + // to store FIFOs. Unused on Windows. + DefaultFIFODir = "" )