cli/command/container: ForwardAllSignals: rewrite to use ContainerAPIClient

This function only needed the ContainerAPIClient, and not the whole CLI. This
patch refactors it to use the shallower interface.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-11-06 10:34:37 +01:00
parent ad861cdb39
commit 3cd77c9d54
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
6 changed files with 13 additions and 17 deletions

View File

@ -106,7 +106,7 @@ func RunAttach(ctx context.Context, dockerCli command.Cli, target string, opts *
if opts.Proxy && !c.Config.Tty { if opts.Proxy && !c.Config.Tty {
sigc := notifyAllSignals() sigc := notifyAllSignals()
go ForwardAllSignals(ctx, dockerCli, target, sigc) go ForwardAllSignals(ctx, apiClient, target, sigc)
defer signal.StopCatch(sigc) defer signal.StopCatch(sigc)
} }

View File

@ -121,7 +121,7 @@ func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copt
func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptions, containerCfg *containerConfig) error { func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptions, containerCfg *containerConfig) error {
config := containerCfg.Config config := containerCfg.Config
stdout, stderr := dockerCli.Out(), dockerCli.Err() stdout, stderr := dockerCli.Out(), dockerCli.Err()
client := dockerCli.Client() apiClient := dockerCli.Client()
config.ArgsEscaped = false config.ArgsEscaped = false
@ -150,7 +150,7 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
} }
if opts.sigProxy { if opts.sigProxy {
sigc := notifyAllSignals() sigc := notifyAllSignals()
go ForwardAllSignals(ctx, dockerCli, containerID, sigc) go ForwardAllSignals(ctx, apiClient, containerID, sigc)
defer signal.StopCatch(sigc) defer signal.StopCatch(sigc)
} }
@ -186,10 +186,10 @@ func runContainer(dockerCli command.Cli, opts *runOptions, copts *containerOptio
defer closeFn() defer closeFn()
} }
statusChan := waitExitOrRemoved(ctx, dockerCli.Client(), containerID, copts.autoRemove) statusChan := waitExitOrRemoved(ctx, apiClient, containerID, copts.autoRemove)
// start the container // start the container
if err := client.ContainerStart(ctx, containerID, container.StartOptions{}); err != nil { if err := apiClient.ContainerStart(ctx, containerID, container.StartOptions{}); err != nil {
// If we have hijackedIOStreamer, we should notify // If we have hijackedIOStreamer, we should notify
// hijackedIOStreamer we are going to exit and wait // hijackedIOStreamer we are going to exit and wait
// to avoid the terminal are not restored. // to avoid the terminal are not restored.

View File

@ -5,7 +5,7 @@ import (
"os" "os"
gosignal "os/signal" gosignal "os/signal"
"github.com/docker/cli/cli/command" "github.com/docker/docker/client"
"github.com/moby/sys/signal" "github.com/moby/sys/signal"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -13,7 +13,7 @@ import (
// ForwardAllSignals forwards signals to the container // ForwardAllSignals forwards signals to the container
// //
// The channel you pass in must already be setup to receive any signals you want to forward. // The channel you pass in must already be setup to receive any signals you want to forward.
func ForwardAllSignals(ctx context.Context, cli command.Cli, cid string, sigc <-chan os.Signal) { func ForwardAllSignals(ctx context.Context, apiClient client.ContainerAPIClient, cid string, sigc <-chan os.Signal) {
var ( var (
s os.Signal s os.Signal
ok bool ok bool
@ -48,7 +48,7 @@ func ForwardAllSignals(ctx context.Context, cli command.Cli, cid string, sigc <-
continue continue
} }
if err := cli.Client().ContainerKill(ctx, cid, sig); err != nil { if err := apiClient.ContainerKill(ctx, cid, sig); err != nil {
logrus.Debugf("Error sending signal: %s", err) logrus.Debugf("Error sending signal: %s", err)
} }
} }

View File

@ -6,7 +6,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/docker/cli/internal/test"
"github.com/moby/sys/signal" "github.com/moby/sys/signal"
) )
@ -15,16 +14,15 @@ func TestForwardSignals(t *testing.T) {
defer cancel() defer cancel()
called := make(chan struct{}) called := make(chan struct{})
client := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error { apiClient := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error {
close(called) close(called)
return nil return nil
}} }}
cli := test.NewFakeCli(client)
sigc := make(chan os.Signal) sigc := make(chan os.Signal)
defer close(sigc) defer close(sigc)
go ForwardAllSignals(ctx, cli, t.Name(), sigc) go ForwardAllSignals(ctx, apiClient, t.Name(), sigc)
timer := time.NewTimer(30 * time.Second) timer := time.NewTimer(30 * time.Second)
defer timer.Stop() defer timer.Stop()

View File

@ -9,7 +9,6 @@ import (
"testing" "testing"
"time" "time"
"github.com/docker/cli/internal/test"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
@ -23,18 +22,17 @@ func TestIgnoredSignals(t *testing.T) {
defer cancel() defer cancel()
var called bool var called bool
client := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error { apiClient := &fakeClient{containerKillFunc: func(ctx context.Context, container, signal string) error {
called = true called = true
return nil return nil
}} }}
cli := test.NewFakeCli(client)
sigc := make(chan os.Signal) sigc := make(chan os.Signal)
defer close(sigc) defer close(sigc)
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
ForwardAllSignals(ctx, cli, t.Name(), sigc) ForwardAllSignals(ctx, apiClient, t.Name(), sigc)
close(done) close(done)
}() }()

View File

@ -93,7 +93,7 @@ func RunStart(dockerCli command.Cli, opts *StartOptions) error {
// We always use c.ID instead of container to maintain consistency during `docker start` // We always use c.ID instead of container to maintain consistency during `docker start`
if !c.Config.Tty { if !c.Config.Tty {
sigc := notifyAllSignals() sigc := notifyAllSignals()
go ForwardAllSignals(ctx, dockerCli, c.ID, sigc) go ForwardAllSignals(ctx, dockerCli.Client(), c.ID, sigc)
defer signal.StopCatch(sigc) defer signal.StopCatch(sigc)
} }