@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"fmt"
23
+ "io/ioutil"
23
24
"net"
24
25
"strings"
25
26
"time"
@@ -33,6 +34,9 @@ import (
33
34
const (
34
35
// Interval of logging connection errors
35
36
connectionLoggingInterval = 10 * time .Second
37
+
38
+ // Default path to termination log
39
+ TerminationLogPath = "/dev/termination-log"
36
40
)
37
41
38
42
// Connect opens insecure gRPC connection to a CSI driver. Address must be either absolute path to UNIX domain socket
@@ -65,12 +69,32 @@ type Option func(o *options)
65
69
// connection got lost. If that callback returns true, the connection
66
70
// is restablished. Otherwise the connection is left as it is and
67
71
// all future gRPC calls using it will fail with status.Unavailable.
72
+ // Only one of ExitOnConnectionLoss and OnConnectionLoss can be used.
68
73
func OnConnectionLoss (reconnect func () bool ) Option {
69
74
return func (o * options ) {
70
75
o .reconnect = reconnect
71
76
}
72
77
}
73
78
79
+ // ExitOnConnectionLoss exits when connection gets lost. Optionally it
80
+ // writes error to given file. Use with /dev/termination-log to get a nice
81
+ // error in Kubernetes.
82
+ // Only one of ExitOnConnectionLoss and OnConnectionLoss can be used.
83
+ func ExitOnConnectionLoss (terminationLogPath string ) Option {
84
+ return func (o * options ) {
85
+ o .reconnect = func () bool {
86
+ terminationMsg := "Lost connection to CSI driver, exiting"
87
+ if terminationLogPath != "" {
88
+ if err := ioutil .WriteFile (terminationLogPath , []byte (terminationMsg ), 0644 ); err != nil {
89
+ klog .Errorf ("%s: %s" , terminationLogPath , err )
90
+ }
91
+ }
92
+ klog .Fatalf (terminationMsg )
93
+ return false
94
+ }
95
+ }
96
+ }
97
+
74
98
type options struct {
75
99
reconnect func () bool
76
100
}
0 commit comments