plugin: Executor.Signal() accept syscall.Signal

This helps reducing some type-juggling / conversions further up
the stack.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-05-02 00:52:16 +02:00
parent 21df9a04e0
commit 521807837b
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 25 additions and 35 deletions

View file

@ -113,8 +113,8 @@ func (e *Executor) IsRunning(id string) (bool, error) {
}
// Signal sends the specified signal to the container
func (e *Executor) Signal(id string, signal int) error {
return e.client.SignalProcess(context.Background(), id, libcontainerdtypes.InitProcessName, syscall.Signal(signal))
func (e *Executor) Signal(id string, signal syscall.Signal) error {
return e.client.SignalProcess(context.Background(), id, libcontainerdtypes.InitProcessName, signal)
}
// ProcessEvent handles events from containerd

View file

@ -11,6 +11,7 @@ import (
"sort"
"strings"
"sync"
"syscall"
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/content/local"
@ -37,7 +38,7 @@ type Executor interface {
Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error
IsRunning(id string) (bool, error)
Restore(id string, stdout, stderr io.WriteCloser) (alive bool, err error)
Signal(id string, signal int) error
Signal(id string, signal syscall.Signal) error
}
// EndpointResolver provides looking up registry endpoints for pulling.

View file

@ -154,31 +154,30 @@ const shutdownTimeout = 10 * time.Second
func shutdownPlugin(p *v2.Plugin, ec chan bool, executor Executor) {
pluginID := p.GetID()
err := executor.Signal(pluginID, int(unix.SIGTERM))
if err != nil {
if err := executor.Signal(pluginID, unix.SIGTERM); err != nil {
logrus.Errorf("Sending SIGTERM to plugin failed with error: %v", err)
} else {
return
}
timeout := time.NewTimer(shutdownTimeout)
defer timeout.Stop()
timeout := time.NewTimer(shutdownTimeout)
defer timeout.Stop()
select {
case <-ec:
logrus.Debug("Clean shutdown of plugin")
case <-timeout.C:
logrus.Debug("Force shutdown plugin")
if err := executor.Signal(pluginID, unix.SIGKILL); err != nil {
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
}
timeout.Reset(shutdownTimeout)
select {
case <-ec:
logrus.Debug("Clean shutdown of plugin")
logrus.Debug("SIGKILL plugin shutdown")
case <-timeout.C:
logrus.Debug("Force shutdown plugin")
if err := executor.Signal(pluginID, int(unix.SIGKILL)); err != nil {
logrus.Errorf("Sending SIGKILL to plugin failed with error: %v", err)
}
timeout.Reset(shutdownTimeout)
select {
case <-ec:
logrus.Debug("SIGKILL plugin shutdown")
case <-timeout.C:
logrus.WithField("plugin", p.Name).Warn("Force shutdown plugin FAILED")
}
logrus.WithField("plugin", p.Name).Warn("Force shutdown plugin FAILED")
}
}
}

View file

@ -5,6 +5,7 @@ import (
"net"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/docker/docker/api/types"
@ -87,24 +88,13 @@ func newTestPlugin(t *testing.T, name, cap, root string) *v2.Plugin {
}
type simpleExecutor struct {
Executor
}
func (e *simpleExecutor) Create(id string, spec specs.Spec, stdout, stderr io.WriteCloser) error {
return errors.New("Create failed")
}
func (e *simpleExecutor) Restore(id string, stdout, stderr io.WriteCloser) (bool, error) {
return false, nil
}
func (e *simpleExecutor) IsRunning(id string) (bool, error) {
return false, nil
}
func (e *simpleExecutor) Signal(id string, signal int) error {
return nil
}
func TestCreateFailed(t *testing.T) {
root, err := os.MkdirTemp("", "test-create-failed")
if err != nil {
@ -165,7 +155,7 @@ func (e *executorWithRunning) Restore(id string, stdout, stderr io.WriteCloser)
return true, nil
}
func (e *executorWithRunning) Signal(id string, signal int) error {
func (e *executorWithRunning) Signal(id string, signal syscall.Signal) error {
ch := e.exitChans[id]
ch <- struct{}{}
<-ch