Skip to content

Commit 97d0bc0

Browse files
committed
feat: add signal option to containerRestart
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 89003a5 commit 97d0bc0

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

cmd/nerdctl/container/container_restart.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewRestartCommand() *cobra.Command {
3838
SilenceErrors: true,
3939
}
4040
restartCommand.Flags().UintP("time", "t", 10, "Seconds to wait for stop before killing it")
41+
restartCommand.Flags().StringP("signal", "s", "", "Signal to send to stop the container, before killing it")
4142
return restartCommand
4243
}
4344

@@ -58,10 +59,21 @@ func processContainerRestartOptions(cmd *cobra.Command) (types.ContainerRestartO
5859
timeout = &t
5960
}
6061

62+
var signal string
63+
if cmd.Flags().Changed("signal") {
64+
// Signal to send to stop the container, before killing it
65+
sig, err := cmd.Flags().GetString("signal")
66+
if err != nil {
67+
return types.ContainerRestartOptions{}, err
68+
}
69+
signal = sig
70+
}
71+
6172
return types.ContainerRestartOptions{
6273
Stdout: cmd.OutOrStdout(),
6374
GOption: globalOptions,
6475
Timeout: timeout,
76+
Signal: signal,
6577
}, err
6678
}
6779

cmd/nerdctl/container/container_restart_linux_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,32 @@ func TestRestartWithTime(t *testing.T) {
121121
// ensure that stop took at least 5 seconds
122122
assert.Assert(t, timePostRestart.Sub(timePreRestart) >= time.Second*5)
123123
}
124+
125+
func TestRestartWithSignal(t *testing.T) {
126+
t.Parallel()
127+
base := testutil.NewBase(t)
128+
tID := testutil.Identifier(t)
129+
130+
base.Cmd("run", "-d", "--name", tID, testutil.AlpineImage, "sh", "-c", `
131+
trap 'echo "Received SIGUSR1"; exit 0' SIGUSR1
132+
echo "Starting"
133+
while true; do
134+
sleep 1
135+
done
136+
`).AssertOK()
137+
defer base.Cmd("rm", "-f", tID).Run()
138+
139+
base.EnsureContainerStarted(tID)
140+
141+
inspect := base.InspectContainer(tID)
142+
initialPid := inspect.State.Pid
143+
144+
base.Cmd("restart", "--signal", "SIGUSR1", tID).AssertOK()
145+
base.EnsureContainerStarted(tID)
146+
147+
newInspect := base.InspectContainer(tID)
148+
newPid := newInspect.State.Pid
149+
150+
assert.Assert(t, initialPid != newPid, "Container PID should have changed after restart")
151+
152+
}

pkg/api/types/container_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ type ContainerRestartOptions struct {
286286
GOption GlobalCommandOptions
287287
// Time to wait after sending a SIGTERM and before sending a SIGKILL.
288288
Timeout *time.Duration
289+
// Signal to send to stop the container, before sending SIGKILL
290+
Signal string
289291
}
290292

291293
// ContainerPauseOptions specifies options for `nerdctl (container) pause`.

pkg/cmd/container/restart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Restart(ctx context.Context, client *containerd.Client, containers []string
3535
if found.MatchCount > 1 {
3636
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
3737
}
38-
if err := containerutil.Stop(ctx, found.Container, options.Timeout, "SIGTERM"); err != nil {
38+
if err := containerutil.Stop(ctx, found.Container, options.Timeout, options.Signal); err != nil {
3939
return err
4040
}
4141
if err := containerutil.Start(ctx, found.Container, false, client, ""); err != nil {

0 commit comments

Comments
 (0)