diff --git a/connection/connection.go b/connection/connection.go index 5e66ae78..0a9903f4 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 }