From 3ae8e8a30b83a1887747475ec9f39b7487f3a6bb Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 4 Dec 2018 11:43:52 -0500 Subject: [PATCH] Add shim config for shim binary options Fixes #2855 Signed-off-by: Michael Crosby --- runtime/v2/shim/shim.go | 31 +++++++++++++++++++++++++------ runtime/v2/shim/shim_unix.go | 17 +++++++++++++++-- runtime/v2/shim/shim_windows.go | 2 +- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/runtime/v2/shim/shim.go b/runtime/v2/shim/shim.go index 39484c191..d60d49663 100644 --- a/runtime/v2/shim/shim.go +++ b/runtime/v2/shim/shim.go @@ -62,6 +62,17 @@ type Opts struct { Debug bool } +// BinaryOpts allows the configuration of a shims binary setup +type BinaryOpts func(*Config) + +// Config of shim binary options provided by shim implementations +type Config struct { + // NoSubreaper disables setting the shim as a child subreaper + NoSubreaper bool + // NoReaper disables the shim binary from reaping any child process implicitly + NoReaper bool +} + var ( debugFlag bool idFlag string @@ -118,27 +129,34 @@ func setLogger(ctx context.Context, id string) error { } // Run initializes and runs a shim server -func Run(id string, initFunc Init) { - if err := run(id, initFunc); err != nil { +func Run(id string, initFunc Init, opts ...BinaryOpts) { + var config Config + for _, o := range opts { + o(&config) + } + if err := run(id, initFunc, config); err != nil { fmt.Fprintf(os.Stderr, "%s: %s\n", id, err) os.Exit(1) } } -func run(id string, initFunc Init) error { +func run(id string, initFunc Init, config Config) error { parseFlags() setRuntime() - signals, err := setupSignals() + signals, err := setupSignals(config) if err != nil { return err } - if err := subreaper(); err != nil { - return err + if !config.NoSubreaper { + if err := subreaper(); err != nil { + return err + } } publisher := &remoteEventsPublisher{ address: addressFlag, containerdBinaryPath: containerdBinaryFlag, + noReaper: config.NoReaper, } if namespaceFlag == "" { return fmt.Errorf("shim namespace cannot be empty") @@ -266,4 +284,5 @@ func dumpStacks(logger *logrus.Entry) { type remoteEventsPublisher struct { address string containerdBinaryPath string + noReaper bool } diff --git a/runtime/v2/shim/shim_unix.go b/runtime/v2/shim/shim_unix.go index 937aaaf0d..08668dc1c 100644 --- a/runtime/v2/shim/shim_unix.go +++ b/runtime/v2/shim/shim_unix.go @@ -39,9 +39,13 @@ import ( // setupSignals creates a new signal handler for all signals and sets the shim as a // sub-reaper so that the container processes are reparented -func setupSignals() (chan os.Signal, error) { +func setupSignals(config Config) (chan os.Signal, error) { signals := make(chan os.Signal, 32) - signal.Notify(signals, unix.SIGTERM, unix.SIGINT, unix.SIGCHLD, unix.SIGPIPE) + smp := []os.Signal{unix.SIGTERM, unix.SIGINT, unix.SIGPIPE} + if !config.NoReaper { + smp = append(smp, unix.SIGCHLD) + } + signal.Notify(signals, smp...) return signals, nil } @@ -102,6 +106,15 @@ func (l *remoteEventsPublisher) Publish(ctx context.Context, topic string, event } cmd := exec.CommandContext(ctx, l.containerdBinaryPath, "--address", l.address, "publish", "--topic", topic, "--namespace", ns) cmd.Stdin = bytes.NewReader(data) + if l.noReaper { + if err := cmd.Start(); err != nil { + return err + } + if err := cmd.Wait(); err != nil { + return errors.Wrap(err, "failed to publish event") + } + return nil + } c, err := Default.Start(cmd) if err != nil { return err diff --git a/runtime/v2/shim/shim_windows.go b/runtime/v2/shim/shim_windows.go index bd3f62aea..3bac959db 100644 --- a/runtime/v2/shim/shim_windows.go +++ b/runtime/v2/shim/shim_windows.go @@ -40,7 +40,7 @@ import ( ) // setupSignals creates a new signal handler for all signals -func setupSignals() (chan os.Signal, error) { +func setupSignals(config Config) (chan os.Signal, error) { signals := make(chan os.Signal, 32) return signals, nil }