@@ -19,14 +19,11 @@ package connection
1919import (
2020 "context"
2121 "fmt"
22- "net"
23- "strings"
24- "time"
2522
2623 "github.com/container-storage-interface/spec/lib/go/csi"
24+ "github.com/kubernetes-csi/csi-lib-utils/connection"
2725 "github.com/kubernetes-csi/csi-lib-utils/protosanitizer"
2826 "google.golang.org/grpc"
29- "google.golang.org/grpc/connectivity"
3027 "k8s.io/klog"
3128)
3229
5350)
5451
5552// NewConnection establishes new connection to CSI driver
56- func NewConnection (
57- address string , timeout time.Duration ) (CSIConnection , error ) {
58- conn , err := connect (address , timeout )
53+ func NewConnection (address string ) (CSIConnection , error ) {
54+ conn , err := connection .Connect (address )
5955 if err != nil {
6056 return nil , err
6157 }
@@ -64,38 +60,6 @@ func NewConnection(
6460 }, nil
6561}
6662
67- func connect (address string , timeout time.Duration ) (* grpc.ClientConn , error ) {
68- klog .Infof ("Connecting to %s" , address )
69- dialOptions := []grpc.DialOption {
70- grpc .WithInsecure (),
71- grpc .WithBackoffMaxDelay (time .Second ),
72- grpc .WithUnaryInterceptor (logGRPC ),
73- }
74- if strings .HasPrefix (address , "/" ) {
75- dialOptions = append (dialOptions , grpc .WithDialer (func (addr string , timeout time.Duration ) (net.Conn , error ) {
76- return net .DialTimeout ("unix" , addr , timeout )
77- }))
78- }
79- conn , err := grpc .Dial (address , dialOptions ... )
80-
81- if err != nil {
82- return nil , err
83- }
84- ctx , cancel := context .WithTimeout (context .Background (), timeout )
85- defer cancel ()
86- for {
87- if ! conn .WaitForStateChange (ctx , conn .GetState ()) {
88- klog .Infof ("Connection timed out" )
89- return conn , nil // return nil, subsequent GetPluginInfo will show the real connection error
90- }
91- if conn .GetState () == connectivity .Ready {
92- klog .Infof ("Connected" )
93- return conn , nil
94- }
95- klog .Infof ("Still trying, connection is %s" , conn .GetState ())
96- }
97- }
98-
9963func (c * csiConnection ) GetDriverName (ctx context.Context ) (string , error ) {
10064 client := csi .NewIdentityClient (c .conn )
10165
0 commit comments