Skip to content

Commit 13d0882

Browse files
committed
feat: support for --sig-proxy in run
Signed-off-by: CodeChanning <[email protected]>
1 parent a7dde33 commit 13d0882

File tree

7 files changed

+91
-3
lines changed

7 files changed

+91
-3
lines changed

cmd/nerdctl/container_run.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func setCreateFlags(cmd *cobra.Command) {
7979
cmd.Flags().Bool("help", false, "show help")
8080

8181
cmd.Flags().BoolP("tty", "t", false, "Allocate a pseudo-TTY")
82+
cmd.Flags().Bool("sig-proxy", true, "Allow signal proxying")
8283
cmd.Flags().BoolP("interactive", "i", false, "Keep STDIN open even if not attached")
8384
cmd.Flags().String("restart", "no", `Restart policy to apply when a container exits (implemented values: "no"|"always|on-failure:n|unless-stopped")`)
8485
cmd.RegisterFlagCompletionFunc("restart", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
@@ -287,10 +288,19 @@ func processCreateCommandFlagsInRun(cmd *cobra.Command) (opt types.ContainerCrea
287288

288289
opt.InRun = true
289290

291+
opt.SigProxy, err = cmd.Flags().GetBool("sig-proxy")
292+
if err != nil {
293+
return
294+
}
295+
290296
opt.Interactive, err = cmd.Flags().GetBool("interactive")
291297
if err != nil {
292298
return
293299
}
300+
opt.SigProxy, err = cmd.Flags().GetBool("sig-proxy")
301+
if err != nil {
302+
return
303+
}
294304
opt.Detach, err = cmd.Flags().GetBool("detach")
295305
if err != nil {
296306
return
@@ -394,8 +404,10 @@ func runAction(cmd *cobra.Command, args []string) error {
394404
log.L.WithError(err).Error("console resize")
395405
}
396406
} else {
397-
sigC := signalutil.ForwardAllSignals(ctx, task)
398-
defer signalutil.StopCatch(sigC)
407+
if createOpt.SigProxy {
408+
sigC := signalutil.ForwardAllSignals(ctx, task)
409+
defer signalutil.StopCatch(sigC)
410+
}
399411
}
400412

401413
statusC, err := task.Wait(ctx)

cmd/nerdctl/container_run_linux_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"runtime"
3030
"strconv"
3131
"strings"
32+
"syscall"
3233
"testing"
3334
"time"
3435

@@ -311,6 +312,61 @@ func TestRunTTY(t *testing.T) {
311312
assert.Equal(t, 0, res.ExitCode, res.Combined())
312313
}
313314

315+
func TestRunSigProxyDefault(t *testing.T) {
316+
t.Parallel()
317+
base := testutil.NewBase(t)
318+
testContainerName := testutil.Identifier(t)
319+
defer base.Cmd("rm", "-f", testContainerName).Run()
320+
321+
process := base.Cmd("run", "--name", testContainerName, testutil.CommonImage, "sh", "-c", testutil.SigProxyTestScript).Start()
322+
323+
// This sleep waits for until we reach the trap command in the shell script, if sigint is send before that we dont enter the while loop.
324+
time.Sleep(1 * time.Second)
325+
326+
syscall.Kill(process.Cmd.Process.Pid, syscall.SIGINT)
327+
process.Cmd.Wait()
328+
assert.Assert(base.T, strings.Contains(process.Stdout(), "got sigint"), fmt.Sprintf("expected output to contain %q: %q", "got sigint", process.Stdout()))
329+
330+
process.Cmd.Wait()
331+
}
332+
333+
func TestRunSigProxyTrue(t *testing.T) {
334+
t.Parallel()
335+
base := testutil.NewBase(t)
336+
testContainerName := testutil.Identifier(t)
337+
defer base.Cmd("rm", "-f", testContainerName).Run()
338+
339+
process := base.Cmd("run", "--sig-proxy=true", "--name", testContainerName, testutil.CommonImage, "sh", "-c", testutil.SigProxyTestScript).Start()
340+
341+
// This sleep waits for until we reach the trap command in the shell script, if sigint is send before that we dont enter the while loop.
342+
time.Sleep(1 * time.Second)
343+
344+
syscall.Kill(process.Cmd.Process.Pid, syscall.SIGINT)
345+
process.Cmd.Wait()
346+
assert.Assert(base.T, strings.Contains(process.Stdout(), "got sigint"), fmt.Sprintf("expected output to contain %q: %q", "got sigint", process.Stdout()))
347+
348+
process.Cmd.Wait()
349+
}
350+
351+
func TestRunSigProxyFalse(t *testing.T) {
352+
t.Parallel()
353+
base := testutil.NewBase(t)
354+
testContainerName := testutil.Identifier(t)
355+
defer base.Cmd("rm", "-f", testContainerName).Run()
356+
357+
process := base.Cmd("run", "--sig-proxy=false", "--name", testContainerName, testutil.CommonImage, "sh", "-c", testutil.SigProxyTestScript).Start()
358+
359+
// This sleep waits for until we reach the trap command in the shell script, if sigint is send before that we dont enter the while loop.
360+
time.Sleep(1 * time.Second)
361+
362+
syscall.Kill(process.Cmd.Process.Pid, syscall.SIGINT)
363+
process.Cmd.Wait()
364+
assert.Assert(base.T, !strings.Contains(process.Stdout(), "got sigint"), fmt.Sprintf("expected output to contain %q: %q", "got sigint", process.Stdout()))
365+
366+
process.Cmd.Wait()
367+
368+
}
369+
314370
func TestRunWithFluentdLogDriver(t *testing.T) {
315371
if runtime.GOOS == "windows" {
316372
t.Skip("fluentd log driver is not yet implemented on Windows")

docs/command-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ IPFS flags:
388388
Unimplemented `docker run` flags:
389389
`--attach`, `--blkio-weight-device`, `--cpu-rt-*`, `--device-*`,
390390
`--disable-content-trust`, `--domainname`, `--expose`, `--health-*`, `--isolation`, `--no-healthcheck`,
391-
`--link*`, `--mac-address`, `--publish-all`, `--sig-proxy`, `--storage-opt`,
391+
`--link*`, `--mac-address`, `--publish-all`, `--storage-opt`,
392392
`--userns`, `--volume-driver`
393393

394394
### :whale: :blue_square: nerdctl exec

pkg/api/types/container_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ type ContainerCreateOptions struct {
6060
// #region for basic flags
6161
// Interactive keep STDIN open even if not attached
6262
Interactive bool
63+
// SigProxy specifies whether to proxy all received signals to the process
64+
SigProxy bool
6365
// TTY specifies whether to allocate a pseudo-TTY for the container
6466
TTY bool
6567
// Detach runs container in background and print container ID
@@ -394,6 +396,8 @@ type ContainerAttachOptions struct {
394396
GOptions GlobalCommandOptions
395397
// DetachKeys is the key sequences to detach from the container.
396398
DetachKeys string
399+
// SigProxy specifies whether to proxy all received signals to the process
400+
SigProxy bool
397401
}
398402

399403
// ContainerExecOptions specifies options for `nerdctl (container) exec`

pkg/composer/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type RunOptions struct {
4545
Detach bool
4646
NoDeps bool
4747
Tty bool
48+
SigProxy bool
4849
Interactive bool
4950
Rm bool
5051
User string

pkg/testutil/testutil.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ func (c *Cmd) Run() *icmd.Result {
350350
return icmd.RunCmd(c.Cmd)
351351
}
352352

353+
func (c *Cmd) Start() *icmd.Result {
354+
c.Base.T.Helper()
355+
return icmd.StartCmd(c.Cmd)
356+
}
357+
353358
func (c *Cmd) CmdOption(cmdOptions ...func(*Cmd)) *Cmd {
354359
for _, opt := range cmdOptions {
355360
opt(c)

pkg/testutil/testutil_linux.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ var (
5656
// It should be "connection refused" as per the TCP RFC.
5757
// https://www.rfc-editor.org/rfc/rfc793
5858
ExpectedConnectionRefusedError = "connection refused"
59+
60+
SigProxyTestScript = `#!/bin/sh
61+
set -eu
62+
trap 'quit=1' INT
63+
quit=0
64+
while [ $quit -ne 1 ]; do
65+
echo "wait"
66+
sleep 1
67+
done
68+
printf "got sigint"`
5969
)
6070

6171
const (

0 commit comments

Comments
 (0)