NewTask avoid unnecessary cio.NewCreator calls

Signed-off-by: Guanjun Gong <gongguanjun@hotmail.com>
This commit is contained in:
Guanjun Gong 2020-09-10 15:12:44 +08:00 committed by gongguan
parent d43d546a8d
commit 0dea724fc0

View File

@ -72,7 +72,6 @@ func NewTask(ctx gocontext.Context, client *containerd.Client, container contain
stdinC := &stdinCloser{ stdinC := &stdinCloser{
stdin: os.Stdin, stdin: os.Stdin,
} }
stdio := cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...)
if checkpoint != "" { if checkpoint != "" {
im, err := client.GetImage(ctx, checkpoint) im, err := client.GetImage(ctx, checkpoint)
if err != nil { if err != nil {
@ -80,22 +79,22 @@ func NewTask(ctx gocontext.Context, client *containerd.Client, container contain
} }
opts = append(opts, containerd.WithTaskCheckpoint(im)) opts = append(opts, containerd.WithTaskCheckpoint(im))
} }
ioCreator := stdio var ioCreator cio.Creator
if con != nil { if con != nil {
ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...)
}
if nullIO { if nullIO {
if con != nil {
return nil, errors.New("tty and null-io cannot be used together") return nil, errors.New("tty and null-io cannot be used together")
} }
ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(con, con, nil), cio.WithTerminal}, ioOpts...)...)
} else if nullIO {
ioCreator = cio.NullIO ioCreator = cio.NullIO
} } else if logURI != "" {
if logURI != "" {
u, err := url.Parse(logURI) u, err := url.Parse(logURI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ioCreator = cio.LogURI(u) ioCreator = cio.LogURI(u)
} else {
ioCreator = cio.NewCreator(append([]cio.Opt{cio.WithStreams(stdinC, os.Stdout, os.Stderr)}, ioOpts...)...)
} }
t, err := container.NewTask(ctx, ioCreator, opts...) t, err := container.NewTask(ctx, ioCreator, opts...)
if err != nil { if err != nil {