kilo/vendor/github.com/oklog/run/actors.go
leonnicolas 086b2e1ddd
cmd/kg/*: sub command peer validation webhook
This commit adds a sub command `webhook` to Kilo.
It will start a https web server that answeres request from a Kubernetes
API server to validate updates and creations of Kilo peers.

It also updates the "Peer Validation" docs to enable users to
install the web hook server and generate the self signed certificates in
the cluster by only applying a manifest.

Signed-off-by: leonnicolas <leonloechner@gmx.de>

Apply suggestions from code review

Co-authored-by: Lucas Servén Marín <lserven@gmail.com>
2021-09-06 21:14:44 +02:00

39 lines
949 B
Go

package run
import (
"context"
"fmt"
"os"
"os/signal"
)
// SignalHandler returns an actor, i.e. an execute and interrupt func, that
// terminates with SignalError when the process receives one of the provided
// signals, or the parent context is canceled.
func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error)) {
ctx, cancel := context.WithCancel(ctx)
return func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, signals...)
select {
case sig := <-c:
return SignalError{Signal: sig}
case <-ctx.Done():
return ctx.Err()
}
}, func(error) {
cancel()
}
}
// SignalError is returned by the signal handler's execute function
// when it terminates due to a received signal.
type SignalError struct {
Signal os.Signal
}
// Error implements the error interface.
func (e SignalError) Error() string {
return fmt.Sprintf("received signal %s", e.Signal)
}