Skip to content

Commit d195a7c

Browse files
authored
Merge pull request #18 from jsafrane/add-termination-log
Add option to exit on reconnect
2 parents bd468a0 + 4690c17 commit d195a7c

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

connection/connection.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"io/ioutil"
2324
"net"
2425
"strings"
2526
"time"
@@ -35,6 +36,8 @@ const (
3536
connectionLoggingInterval = 10 * time.Second
3637
)
3738

39+
const terminationLogPath = "/dev/termination-log"
40+
3841
// Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to UNIX domain socket
3942
// file or have format '<protocol>://', following gRPC name resolution mechanism at
4043
// https://github.com/grpc/grpc/blob/master/doc/naming.md.
@@ -63,14 +66,27 @@ type Option func(o *options)
6366

6467
// OnConnectionLoss registers a callback that will be invoked when the
6568
// connection got lost. If that callback returns true, the connection
66-
// is restablished. Otherwise the connection is left as it is and
69+
// is reestablished. Otherwise the connection is left as it is and
6770
// all future gRPC calls using it will fail with status.Unavailable.
6871
func OnConnectionLoss(reconnect func() bool) Option {
6972
return func(o *options) {
7073
o.reconnect = reconnect
7174
}
7275
}
7376

77+
// ExitOnConnectionLoss returns callback for OnConnectionLoss() that writes
78+
// an error to /dev/termination-log and exits.
79+
func ExitOnConnectionLoss() func() bool {
80+
return func() bool {
81+
terminationMsg := "Lost connection to CSI driver, exiting"
82+
if err := ioutil.WriteFile(terminationLogPath, []byte(terminationMsg), 0644); err != nil {
83+
klog.Errorf("%s: %s", terminationLogPath, err)
84+
}
85+
klog.Fatalf(terminationMsg)
86+
return false
87+
}
88+
}
89+
7490
type options struct {
7591
reconnect func() bool
7692
}

0 commit comments

Comments
 (0)