X Tutup
Skip to content

Commit 507a149

Browse files
committed
cio: add WithFIFODir opt
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
1 parent bbb5b2f commit 507a149

File tree

8 files changed

+50
-15
lines changed

8 files changed

+50
-15
lines changed

cio/io.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"io"
77
"os"
88
"sync"
9+
10+
"github.com/containerd/containerd/defaults"
911
)
1012

1113
// Config holds the IO configurations.
@@ -68,6 +70,7 @@ type Streams struct {
6870
Stdout io.Writer
6971
Stderr io.Writer
7072
Terminal bool
73+
FIFODir string
7174
}
7275

7376
// Opt customize options for creating a Creator or Attach
@@ -92,16 +95,25 @@ func WithStreams(stdin io.Reader, stdout, stderr io.Writer) Opt {
9295
}
9396
}
9497

98+
// WithFIFODir sets the fifo directory.
99+
// e.g. "/run/containerd/fifo", "/run/users/1001/containerd/fifo"
100+
func WithFIFODir(dir string) Opt {
101+
return func(opt *Streams) {
102+
opt.FIFODir = dir
103+
}
104+
}
105+
95106
// NewCreator returns an IO creator from the options
96107
func NewCreator(opts ...Opt) Creator {
97108
streams := &Streams{}
98109
for _, opt := range opts {
99110
opt(streams)
100111
}
112+
if streams.FIFODir == "" {
113+
streams.FIFODir = defaults.DefaultFIFODir
114+
}
101115
return func(id string) (IO, error) {
102-
// TODO: accept root as a param
103-
root := "/run/containerd/fifo"
104-
fifos, err := NewFIFOSetInDir(root, id, streams.Terminal)
116+
fifos, err := NewFIFOSetInDir(streams.FIFODir, id, streams.Terminal)
105117
if err != nil {
106118
return nil, err
107119
}

cmd/ctr/commands/run/run.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/containerd/console"
1111
"github.com/containerd/containerd"
12+
"github.com/containerd/containerd/cio"
1213
"github.com/containerd/containerd/cmd/ctr/commands"
1314
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
1415
"github.com/containerd/containerd/containers"
@@ -170,6 +171,10 @@ var Command = cli.Command{
170171
Name: "detach,d",
171172
Usage: "detach from the task after it has started execution",
172173
},
174+
cli.StringFlag{
175+
Name: "fifo-dir",
176+
Usage: "directory used for storing IO FIFOs",
177+
},
173178
}, commands.SnapshotterFlags...),
174179
Action: func(context *cli.Context) error {
175180
var (
@@ -200,7 +205,8 @@ var Command = cli.Command{
200205
defer container.Delete(ctx, containerd.WithSnapshotCleanup)
201206
}
202207
opts := getNewTaskOpts(context)
203-
task, err := tasks.NewTask(ctx, client, container, context.String("checkpoint"), tty, context.Bool("null-io"), opts...)
208+
ioOpts := []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))}
209+
task, err := tasks.NewTask(ctx, client, container, context.String("checkpoint"), tty, context.Bool("null-io"), ioOpts, opts...)
204210
if err != nil {
205211
return err
206212
}

cmd/ctr/commands/tasks/exec.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ var execCommand = cli.Command{
2828
Name: "exec-id",
2929
Usage: "exec specific id for the process",
3030
},
31+
cli.StringFlag{
32+
Name: "fifo-dir",
33+
Usage: "directory used for storing IO FIFOs",
34+
},
3135
},
3236
Action: func(context *cli.Context) error {
3337
var (
@@ -60,10 +64,11 @@ var execCommand = cli.Command{
6064
pspec.Terminal = tty
6165
pspec.Args = args
6266

63-
ioCreator := cio.NewCreator(cio.WithStdio)
67+
cioOpts := []cio.Opt{cio.WithStdio, cio.WithFIFODir(context.String("fifo-dir"))}
6468
if tty {
65-
ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal)
69+
cioOpts = append(cioOpts, cio.WithTerminal)
6670
}
71+
ioCreator := cio.NewCreator(cioOpts...)
6772
process, err := task.Exec(ctx, context.String("exec-id"), pspec, ioCreator)
6873
if err != nil {
6974
return err

cmd/ctr/commands/tasks/start.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tasks
22

33
import (
44
"github.com/containerd/console"
5+
"github.com/containerd/containerd/cio"
56
"github.com/containerd/containerd/cmd/ctr/commands"
67
"github.com/pkg/errors"
78
"github.com/sirupsen/logrus"
@@ -17,6 +18,10 @@ var startCommand = cli.Command{
1718
Name: "null-io",
1819
Usage: "send all IO to /dev/null",
1920
},
21+
cli.StringFlag{
22+
Name: "fifo-dir",
23+
Usage: "directory used for storing IO FIFOs",
24+
},
2025
},
2126
Action: func(context *cli.Context) error {
2227
var (
@@ -42,10 +47,11 @@ var startCommand = cli.Command{
4247
}
4348

4449
var (
45-
tty = spec.Process.Terminal
46-
opts = getNewTaskOpts(context)
50+
tty = spec.Process.Terminal
51+
opts = getNewTaskOpts(context)
52+
ioOpts = []cio.Opt{cio.WithFIFODir(context.String("fifo-dir"))}
4753
)
48-
task, err := NewTask(ctx, client, container, "", tty, context.Bool("null-io"), opts...)
54+
task, err := NewTask(ctx, client, container, "", tty, context.Bool("null-io"), ioOpts, opts...)
4955
if err != nil {
5056
return err
5157
}

cmd/ctr/commands/tasks/tasks_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol
5151
}
5252

5353
// NewTask creates a new task
54-
func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, checkpoint string, tty, nullIO bool, opts ...containerd.NewTaskOpts) (containerd.Task, error) {
55-
stdio := cio.NewCreator(cio.WithStdio)
54+
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) {
55+
stdio := cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...)
5656
if checkpoint == "" {
5757
ioCreator := stdio
5858
if tty {
59-
ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal)
59+
ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStdio, cio.WithTerminal}, ioOpts...)...)
6060
}
6161
if nullIO {
6262
if tty {

cmd/ctr/commands/tasks/tasks_windows.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ func HandleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol
4242
}
4343

4444
// NewTask creates a new task
45-
func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty, nullIO bool, opts ...containerd.NewTaskOpts) (containerd.Task, error) {
46-
ioCreator := cio.NewCreator(cio.WithStdio)
45+
func NewTask(ctx gocontext.Context, client *containerd.Client, container containerd.Container, _ string, tty, nullIO bool, ioOpts []cio.Opt, opts ...containerd.NewTaskOpts) (containerd.Task, error) {
46+
ioCreator := cio.NewCreator(append([]cio.Opt{cio.WithStdio}, ioOpts...)...)
4747
if tty {
48-
ioCreator = cio.NewCreator(cio.WithStdio, cio.WithTerminal)
48+
ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStdio, cio.WithTerminal}, ioOpts...)...)
4949
}
5050
if nullIO {
5151
if tty {

defaults/defaults_unix.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ const (
1313
DefaultAddress = "/run/containerd/containerd.sock"
1414
// DefaultDebugAddress is the default unix socket address for pprof data
1515
DefaultDebugAddress = "/run/containerd/debug.sock"
16+
// DefaultFIFODir is the default location used by client-side cio library
17+
// to store FIFOs.
18+
DefaultFIFODir = "/run/containerd/fifo"
1619
)

defaults/defaults_windows.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ const (
2121
DefaultAddress = `\\.\pipe\containerd-containerd`
2222
// DefaultDebugAddress is the default winpipe address for pprof data
2323
DefaultDebugAddress = `\\.\pipe\containerd-debug`
24+
// DefaultFIFODir is the default location used by client-side cio library
25+
// to store FIFOs. Unused on Windows.
26+
DefaultFIFODir = ""
2427
)

0 commit comments

Comments
 (0)
X Tutup