Skip to content

Commit 67a0252

Browse files
committed
taskutil: introduce taskoptions to reduce argument numbers
introduce taskoptions to reduce argument numbers. Otherwise, ci would be failed by: ``` Error: pkg/taskutil/taskutil.go:51:1: argument-limit: maximum number of arguments per function exceeded; max 12 but got 13 func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container, attachStreamOpt []string, isInteractive, isTerminal, isDetach bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}, checkpointDir string) ``` Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent a58db3a commit 67a0252

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

cmd/nerdctl/container/container_run.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,18 @@ func runAction(cmd *cobra.Command, args []string) error {
431431
}
432432
logURI := lab[labels.LogURI]
433433
detachC := make(chan struct{})
434-
task, err := taskutil.NewTask(ctx, client, c, createOpt.Attach, createOpt.Interactive, createOpt.TTY, createOpt.Detach,
435-
con, logURI, createOpt.DetachKeys, createOpt.GOptions.Namespace, detachC, "")
434+
task, err := taskutil.NewTask(ctx, client, c, taskutil.TaskOptions{
435+
AttachStreamOpt: createOpt.Attach,
436+
IsInteractive: createOpt.Interactive,
437+
IsTerminal: createOpt.TTY,
438+
IsDetach: createOpt.Detach,
439+
Con: con,
440+
LogURI: logURI,
441+
DetachKeys: createOpt.DetachKeys,
442+
Namespace: createOpt.GOptions.Namespace,
443+
DetachC: detachC,
444+
CheckpointDir: "",
445+
})
436446
if err != nil {
437447
return err
438448
}

pkg/containerutil/containerutil.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,18 @@ func Start(ctx context.Context, container containerd.Container, isAttach bool, i
280280
// source: https://github.com/containerd/nerdctl/blob/main/docs/command-reference.md#whale-nerdctl-start
281281
attachStreamOpt = []string{"STDOUT", "STDERR"}
282282
}
283-
task, err := taskutil.NewTask(ctx, client, container, attachStreamOpt, isInteractive, isTerminal, true, con, logURI, detachKeys, namespace, detachC, checkpointDir)
283+
task, err := taskutil.NewTask(ctx, client, container, taskutil.TaskOptions{
284+
AttachStreamOpt: attachStreamOpt,
285+
IsInteractive: isInteractive,
286+
IsTerminal: isTerminal,
287+
IsDetach: true,
288+
Con: con,
289+
LogURI: logURI,
290+
DetachKeys: detachKeys,
291+
Namespace: namespace,
292+
DetachC: detachC,
293+
CheckpointDir: checkpointDir,
294+
})
284295
if err != nil {
285296
return err
286297
}

pkg/taskutil/taskutil.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,32 @@ import (
4949
"github.com/containerd/nerdctl/v2/pkg/infoutil"
5050
)
5151

52+
// TaskOptions contains options for creating a new task
53+
type TaskOptions struct {
54+
AttachStreamOpt []string
55+
IsInteractive bool
56+
IsTerminal bool
57+
IsDetach bool
58+
Con console.Console
59+
LogURI string
60+
DetachKeys string
61+
Namespace string
62+
DetachC chan<- struct{}
63+
CheckpointDir string
64+
}
65+
5266
// NewTask is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/tasks/tasks_unix.go#L70-L108
53-
func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container,
54-
attachStreamOpt []string, isInteractive, isTerminal, isDetach bool, con console.Console, logURI, detachKeys, namespace string, detachC chan<- struct{}, checkpointDir string) (containerd.Task, error) {
67+
func NewTask(ctx context.Context, client *containerd.Client, container containerd.Container, opts TaskOptions) (containerd.Task, error) {
5568
var (
5669
checkpoint *types.Descriptor
5770
t containerd.Task
5871
err error
5972
)
6073

61-
if checkpointDir != "" {
62-
tar := archive.Diff(ctx, "", checkpointDir)
74+
if opts.CheckpointDir != "" {
75+
tar := archive.Diff(ctx, "", opts.CheckpointDir)
6376
cs := client.ContentStore()
64-
writer, err := cs.Writer(ctx, content.WithRef(checkpointDir))
77+
writer, err := cs.Writer(ctx, content.WithRef(opts.CheckpointDir))
6578
if err != nil {
6679
return nil, err
6780
}
@@ -96,8 +109,8 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
96109
}
97110
}
98111
closer := func() {
99-
if detachC != nil {
100-
detachC <- struct{}{}
112+
if opts.DetachC != nil {
113+
opts.DetachC <- struct{}{}
101114
}
102115
// t will be set by container.NewTask at the end of this function.
103116
//
@@ -113,30 +126,30 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
113126
io.Cancel()
114127
}
115128
var ioCreator cio.Creator
116-
if len(attachStreamOpt) != 0 {
129+
if len(opts.AttachStreamOpt) != 0 {
117130
log.G(ctx).Debug("attaching output instead of using the log-uri")
118131
// when attaching a TTY we use writee for stdio and binary for log persistence
119-
if isTerminal {
132+
if opts.IsTerminal {
120133
var in io.Reader
121-
if isInteractive {
134+
if opts.IsInteractive {
122135
// FIXME: check IsTerminal on Windows too
123136
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
124137
return nil, errors.New("the input device is not a TTY")
125138
}
126139
var err error
127-
in, err = consoleutil.NewDetachableStdin(con, detachKeys, closer)
140+
in, err = consoleutil.NewDetachableStdin(opts.Con, opts.DetachKeys, closer)
128141
if err != nil {
129142
return nil, err
130143
}
131144
}
132-
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, con, nil)
145+
ioCreator = cioutil.NewContainerIO(opts.Namespace, opts.LogURI, true, in, opts.Con, nil)
133146
} else {
134-
streams := processAttachStreamsOpt(attachStreamOpt)
135-
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
147+
streams := processAttachStreamsOpt(opts.AttachStreamOpt)
148+
ioCreator = cioutil.NewContainerIO(opts.Namespace, opts.LogURI, false, streams.stdIn, streams.stdOut, streams.stdErr)
136149
}
137150

138-
} else if isTerminal && isDetach {
139-
u, err := url.Parse(logURI)
151+
} else if opts.IsTerminal && opts.IsDetach {
152+
u, err := url.Parse(opts.LogURI)
140153
if err != nil {
141154
return nil, err
142155
}
@@ -162,32 +175,32 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
162175
ioCreator = cio.TerminalBinaryIO(parsedPath, map[string]string{
163176
args[0]: args[1],
164177
})
165-
} else if isTerminal && !isDetach {
166-
if con == nil {
178+
} else if opts.IsTerminal && !opts.IsDetach {
179+
if opts.Con == nil {
167180
return nil, errors.New("got nil con with isTerminal=true")
168181
}
169182
var in io.Reader
170-
if isInteractive {
183+
if opts.IsInteractive {
171184
// FIXME: check IsTerminal on Windows too
172185
if runtime.GOOS != "windows" && !term.IsTerminal(0) {
173186
return nil, errors.New("the input device is not a TTY")
174187
}
175188
var err error
176-
in, err = consoleutil.NewDetachableStdin(con, detachKeys, closer)
189+
in, err = consoleutil.NewDetachableStdin(opts.Con, opts.DetachKeys, closer)
177190
if err != nil {
178191
return nil, err
179192
}
180193
}
181-
ioCreator = cioutil.NewContainerIO(namespace, logURI, true, in, os.Stdout, os.Stderr)
182-
} else if isDetach && logURI != "" && logURI != "none" {
183-
u, err := url.Parse(logURI)
194+
ioCreator = cioutil.NewContainerIO(opts.Namespace, opts.LogURI, true, in, os.Stdout, os.Stderr)
195+
} else if opts.IsDetach && opts.LogURI != "" && opts.LogURI != "none" {
196+
u, err := url.Parse(opts.LogURI)
184197
if err != nil {
185198
return nil, err
186199
}
187200
ioCreator = cio.LogURI(u)
188201
} else {
189202
var in io.Reader
190-
if isInteractive {
203+
if opts.IsInteractive {
191204
if sv, err := infoutil.ServerSemVer(ctx, client); err != nil {
192205
log.G(ctx).Warn(err)
193206
} else if sv.LessThan(semver.MustParse("1.6.0-0")) {
@@ -205,7 +218,7 @@ func NewTask(ctx context.Context, client *containerd.Client, container container
205218
}
206219
in = stdinC
207220
}
208-
ioCreator = cioutil.NewContainerIO(namespace, logURI, false, in, os.Stdout, os.Stderr)
221+
ioCreator = cioutil.NewContainerIO(opts.Namespace, opts.LogURI, false, in, os.Stdout, os.Stderr)
209222
}
210223

211224
taskOpts := []containerd.NewTaskOpts{

0 commit comments

Comments
 (0)