From 4690c17b0f5b3b397ca2ef43dd73955e9f4dc3a9 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Thu, 14 Feb 2019 15:16:40 +0100 Subject: [PATCH] Add option to exit on reconnect --- connection/connection.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/connection/connection.go b/connection/connection.go index 5e66ae780..0a9903f40 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "io/ioutil" "net" "strings" "time" @@ -35,6 +36,8 @@ const ( connectionLoggingInterval = 10 * time.Second ) +const terminationLogPath = "/dev/termination-log" + // Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to UNIX domain socket // file or have format '://', following gRPC name resolution mechanism at // https://github.com/grpc/grpc/blob/master/doc/naming.md. @@ -63,7 +66,7 @@ type Option func(o *options) // OnConnectionLoss registers a callback that will be invoked when the // connection got lost. If that callback returns true, the connection -// is restablished. Otherwise the connection is left as it is and +// is reestablished. Otherwise the connection is left as it is and // all future gRPC calls using it will fail with status.Unavailable. func OnConnectionLoss(reconnect func() bool) Option { return func(o *options) { @@ -71,6 +74,19 @@ func OnConnectionLoss(reconnect func() bool) Option { } } +// ExitOnConnectionLoss returns callback for OnConnectionLoss() that writes +// an error to /dev/termination-log and exits. +func ExitOnConnectionLoss() func() bool { + return func() bool { + terminationMsg := "Lost connection to CSI driver, exiting" + if err := ioutil.WriteFile(terminationLogPath, []byte(terminationMsg), 0644); err != nil { + klog.Errorf("%s: %s", terminationLogPath, err) + } + klog.Fatalf(terminationMsg) + return false + } +} + type options struct { reconnect func() bool }