Skip to content

Commit 5f9834b

Browse files
committed
Add option to exit on reconnect
1 parent bd468a0 commit 5f9834b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

connection/connection.go

Lines changed: 24 additions & 0 deletions
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"
@@ -33,6 +34,9 @@ import (
3334
const (
3435
// Interval of logging connection errors
3536
connectionLoggingInterval = 10 * time.Second
37+
38+
// Default path to termination log
39+
TerminationLogPath = "/dev/termination-log"
3640
)
3741

3842
// 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)
6569
// connection got lost. If that callback returns true, the connection
6670
// is restablished. Otherwise the connection is left as it is and
6771
// all future gRPC calls using it will fail with status.Unavailable.
72+
// Only one of ExitOnConnectionLoss and OnConnectionLoss can be used.
6873
func OnConnectionLoss(reconnect func() bool) Option {
6974
return func(o *options) {
7075
o.reconnect = reconnect
7176
}
7277
}
7378

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+
7498
type options struct {
7599
reconnect func() bool
76100
}

0 commit comments

Comments
 (0)