@@ -44,10 +44,11 @@ const (
4444 secretNamespaceKey = "csiProvisionerSecretNamespace"
4545)
4646
47+ // CSIProvisioner struct
4748type csiProvisioner struct {
4849 client kubernetes.Interface
4950 csiClient csi.ControllerClient
50- driverName string
51+ grpcClient * grpc. ClientConn
5152 timeout time.Duration
5253 identity string
5354 volumeNamePrefix string
@@ -80,7 +81,7 @@ func logGRPC(ctx context.Context, method string, req, reply interface{}, cc *grp
8081 return err
8182}
8283
83- func connect (address string , timeout time.Duration ) (* grpc.ClientConn , error ) {
84+ func Connect (address string , timeout time.Duration ) (* grpc.ClientConn , error ) {
8485 glog .V (2 ).Infof ("Connecting to %s" , address )
8586 dialOptions := []grpc.DialOption {
8687 grpc .WithInsecure (),
@@ -102,7 +103,7 @@ func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) {
102103 for {
103104 if ! conn .WaitForStateChange (ctx , conn .GetState ()) {
104105 glog .V (4 ).Infof ("Connection timed out" )
105- return conn , nil // return nil, subsequent GetPluginInfo will show the real connection error
106+ return conn , fmt . Errorf ( "Connection timed out" )
106107 }
107108 if conn .GetState () == connectivity .Ready {
108109 glog .V (3 ).Infof ("Connected" )
@@ -185,34 +186,19 @@ func supportsControllerCreateVolume(conn *grpc.ClientConn, timeout time.Duration
185186 return false , nil
186187}
187188
188- func NewCSIProvisioner (client kubernetes.Interface , csiEndpoint string , connectionTimeout time.Duration , identity string , volumeNamePrefix string , volumeNameUUIDLength int ) controller.Provisioner {
189- grpcClient , err := connect (csiEndpoint , connectionTimeout )
190- if err != nil || grpcClient == nil {
191- glog .Fatalf ("failed to connect to csi endpoint :%v" , err )
192- }
193- ok , err := supportsPluginControllerService (grpcClient , connectionTimeout )
194- if err != nil {
195- glog .Fatalf ("failed to get support info :%v" , err )
196- }
197- if ! ok {
198- glog .Fatalf ("no plugin controller service support detected" )
199- }
200- ok , err = supportsControllerCreateVolume (grpcClient , connectionTimeout )
201- if err != nil {
202- glog .Fatalf ("failed to get support info :%v" , err )
203- }
204- if ! ok {
205- glog .Fatalf ("no create/delete volume support detected" )
206- }
207- driver , err := getDriverName (grpcClient , connectionTimeout )
208- if err != nil {
209- glog .Fatalf ("failed to get driver info :%v" , err )
210- }
189+ // NewCSIProvisioner creates new CSI provisioner
190+ func NewCSIProvisioner (client kubernetes.Interface ,
191+ csiEndpoint string ,
192+ connectionTimeout time.Duration ,
193+ identity string ,
194+ volumeNamePrefix string ,
195+ volumeNameUUIDLength int ,
196+ grpcClient * grpc.ClientConn ) controller.Provisioner {
211197
212198 csiClient := csi .NewControllerClient (grpcClient )
213199 provisioner := & csiProvisioner {
214200 client : client ,
215- driverName : driver ,
201+ grpcClient : grpcClient ,
216202 csiClient : csiClient ,
217203 timeout : connectionTimeout ,
218204 identity : identity ,
@@ -222,10 +208,45 @@ func NewCSIProvisioner(client kubernetes.Interface, csiEndpoint string, connecti
222208 return provisioner
223209}
224210
211+ // This function get called before any attepmt to communicate with the driver.
212+ // Before initiating Create/Delete API calls provisioner checks if Capabilities:
213+ // PluginControllerService, ControllerCreateVolume sre supported and gets the driver name.
214+ func checkDriverState (grpcClient * grpc.ClientConn , timeout time.Duration ) (string , error ) {
215+ ok , err := supportsPluginControllerService (grpcClient , timeout )
216+ if err != nil {
217+ glog .Errorf ("failed to get support info :%v" , err )
218+ return "" , err
219+ }
220+ if ! ok {
221+ glog .Errorf ("no plugin controller service support detected" )
222+ return "" , fmt .Errorf ("no plugin controller service support detected" )
223+ }
224+ ok , err = supportsControllerCreateVolume (grpcClient , timeout )
225+ if err != nil {
226+ glog .Errorf ("failed to get support info :%v" , err )
227+ return "" , err
228+ }
229+ if ! ok {
230+ glog .Error ("no create/delete volume support detected" )
231+ return "" , fmt .Errorf ("no create/delete volume support detected" )
232+ }
233+ driverName , err := getDriverName (grpcClient , timeout )
234+ if err != nil {
235+ glog .Errorf ("failed to get driver info :%v" , err )
236+ return "" , err
237+ }
238+ return driverName , nil
239+ }
240+
225241func (p * csiProvisioner ) Provision (options controller.VolumeOptions ) (* v1.PersistentVolume , error ) {
226242 if options .PVC .Spec .Selector != nil {
227243 return nil , fmt .Errorf ("claim Selector is not supported" )
228244 }
245+
246+ driverName , err := checkDriverState (p .grpcClient , p .timeout )
247+ if err != nil {
248+ return nil , err
249+ }
229250 // create random share name
230251 share := fmt .Sprintf ("%s-%s" , p .volumeNamePrefix , strings .Replace (string (uuid .NewUUID ()), "-" , "" , - 1 )[0 :p .volumeNameUUIDLength ])
231252 capacity := options .PVC .Spec .Resources .Requests [v1 .ResourceName (v1 .ResourceStorage )]
@@ -283,7 +304,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
283304 // TODO wait for CSI VolumeSource API
284305 PersistentVolumeSource : v1.PersistentVolumeSource {
285306 CSI : & v1.CSIPersistentVolumeSource {
286- Driver : p . driverName ,
307+ Driver : driverName ,
287308 VolumeHandle : p .volumeIdToHandle (rep .Volume .Id ),
288309 VolumeAttributes : volumeAttributes ,
289310 },
@@ -304,6 +325,11 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error {
304325 }
305326 volumeId := p .volumeHandleToId (volume .Spec .CSI .VolumeHandle )
306327
328+ _ , err := checkDriverState (p .grpcClient , p .timeout )
329+ if err != nil {
330+ return err
331+ }
332+
307333 req := csi.DeleteVolumeRequest {
308334 VolumeId : volumeId ,
309335 }
@@ -322,7 +348,7 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error {
322348 ctx , cancel := context .WithTimeout (context .Background (), p .timeout )
323349 defer cancel ()
324350
325- _ , err : = p .csiClient .DeleteVolume (ctx , & req )
351+ _ , err = p .csiClient .DeleteVolume (ctx , & req )
326352
327353 return err
328354}
0 commit comments