@@ -24,14 +24,19 @@ import (
2424 "os/signal"
2525 "time"
2626
27+ "google.golang.org/grpc"
28+
2729 "k8s.io/client-go/kubernetes"
2830 "k8s.io/client-go/kubernetes/scheme"
2931 "k8s.io/client-go/rest"
3032 "k8s.io/client-go/tools/clientcmd"
3133 "k8s.io/klog"
3234
33- "github.com/kubernetes-csi/external-snapshotter/pkg/connection"
35+ "github.com/container-storage-interface/spec/lib/go/csi"
36+ "github.com/kubernetes-csi/csi-lib-utils/connection"
37+ csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc"
3438 "github.com/kubernetes-csi/external-snapshotter/pkg/controller"
39+ "github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
3540
3641 clientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned"
3742 snapshotscheme "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/scheme"
@@ -50,7 +55,7 @@ const (
5055
5156// Command line flags
5257var (
53- snapshotter = flag .String ("snapshotter" , "" , "This option is deprecated." )
58+ snapshotterName = flag .String ("snapshotter" , "" , "This option is deprecated." )
5459 kubeconfig = flag .String ("kubeconfig" , "" , "Absolute path to the kubeconfig file. Required only when running out of cluster." )
5560 connectionTimeout = flag .Duration ("connection-timeout" , 0 , "The --connection-timeout flag is deprecated" )
5661 csiAddress = flag .String ("csi-address" , "/run/csi/socket" , "Address of the CSI driver socket." )
@@ -80,7 +85,8 @@ func main() {
8085 if * connectionTimeout != 0 {
8186 klog .Warning ("--connection-timeout is deprecated and will have no effect" )
8287 }
83- if * snapshotter != "" {
88+
89+ if * snapshotterName != "" {
8490 klog .Warning ("--snapshotter is deprecated and will have no effect" )
8591 }
8692
@@ -124,9 +130,9 @@ func main() {
124130 snapshotscheme .AddToScheme (scheme .Scheme )
125131
126132 // Connect to CSI.
127- csiConn , err := connection .New (* csiAddress )
133+ csiConn , err := connection .Connect (* csiAddress )
128134 if err != nil {
129- klog .Error ( err . Error () )
135+ klog .Errorf ( "error connecting to CSI driver: %v" , err )
130136 os .Exit (1 )
131137 }
132138
@@ -135,27 +141,29 @@ func main() {
135141 defer cancel ()
136142
137143 // Find driver name
138- * snapshotter , err = csiConn .GetDriverName (ctx )
144+ * snapshotterName , err = csirpc .GetDriverName (ctx , csiConn )
139145 if err != nil {
140- klog .Error ( err . Error () )
146+ klog .Errorf ( "error getting CSI driver name: %v" , err )
141147 os .Exit (1 )
142148 }
143- klog .V (2 ).Infof ("CSI driver name: %q" , * snapshotter )
149+
150+ klog .V (2 ).Infof ("CSI driver name: %q" , * snapshotterName )
144151
145152 // Check it's ready
146- if err = waitForDriverReady (csiConn , * connectionTimeout ); err != nil {
147- klog .Error ( err . Error () )
153+ if err = csirpc . ProbeForever (csiConn , csiTimeout ); err != nil {
154+ klog .Errorf ( "error waiting for CSI driver to be ready: %v" , err )
148155 os .Exit (1 )
156+
149157 }
150158
151159 // Find out if the driver supports create/delete snapshot.
152- supportsCreateSnapshot , err := csiConn . SupportsControllerCreateSnapshot (ctx )
160+ supportsCreateSnapshot , err := supportsControllerCreateSnapshot (ctx , csiConn )
153161 if err != nil {
154- klog .Error ( err . Error () )
162+ klog .Errorf ( "error determining if driver supports create/delete snapshot operations: %v" , err )
155163 os .Exit (1 )
156164 }
157165 if ! supportsCreateSnapshot {
158- klog .Errorf ("CSI driver %s does not support ControllerCreateSnapshot" , * snapshotter )
166+ klog .Errorf ("CSI driver %s does not support ControllerCreateSnapshot" , * snapshotterName )
159167 os .Exit (1 )
160168 }
161169
@@ -164,19 +172,20 @@ func main() {
164172 os .Exit (1 )
165173 }
166174
167- klog .V (2 ).Infof ("Start NewCSISnapshotController with snapshotter [%s] kubeconfig [%s] connectionTimeout [%+v] csiAddress [%s] createSnapshotContentRetryCount [%d] createSnapshotContentInterval [%+v] resyncPeriod [%+v] snapshotNamePrefix [%s] snapshotNameUUIDLength [%d]" , * snapshotter , * kubeconfig , * connectionTimeout , * csiAddress , createSnapshotContentRetryCount , * createSnapshotContentInterval , * resyncPeriod , * snapshotNamePrefix , snapshotNameUUIDLength )
175+ klog .V (2 ).Infof ("Start NewCSISnapshotController with snapshotter [%s] kubeconfig [%s] connectionTimeout [%+v] csiAddress [%s] createSnapshotContentRetryCount [%d] createSnapshotContentInterval [%+v] resyncPeriod [%+v] snapshotNamePrefix [%s] snapshotNameUUIDLength [%d]" , * snapshotterName , * kubeconfig , * connectionTimeout , * csiAddress , createSnapshotContentRetryCount , * createSnapshotContentInterval , * resyncPeriod , * snapshotNamePrefix , snapshotNameUUIDLength )
168176
177+ snapShotter := snapshotter .NewSnapshotter (csiConn )
169178 ctrl := controller .NewCSISnapshotController (
170179 snapClient ,
171180 kubeClient ,
172- * snapshotter ,
181+ * snapshotterName ,
173182 factory .Volumesnapshot ().V1alpha1 ().VolumeSnapshots (),
174183 factory .Volumesnapshot ().V1alpha1 ().VolumeSnapshotContents (),
175184 factory .Volumesnapshot ().V1alpha1 ().VolumeSnapshotClasses (),
176185 coreFactory .Core ().V1 ().PersistentVolumeClaims (),
177186 * createSnapshotContentRetryCount ,
178187 * createSnapshotContentInterval ,
179- csiConn ,
188+ snapShotter ,
180189 * connectionTimeout ,
181190 * resyncPeriod ,
182191 * snapshotNamePrefix ,
@@ -203,24 +212,11 @@ func buildConfig(kubeconfig string) (*rest.Config, error) {
203212 return rest .InClusterConfig ()
204213}
205214
206- func waitForDriverReady (csiConn connection.CSIConnection , timeout time.Duration ) error {
207- now := time .Now ()
208- finish := now .Add (timeout )
209- var err error
210- for {
211- ctx , cancel := context .WithTimeout (context .Background (), csiTimeout )
212- defer cancel ()
213- err = csiConn .Probe (ctx )
214- if err == nil {
215- klog .V (2 ).Infof ("Probe succeeded" )
216- return nil
217- }
218- klog .V (2 ).Infof ("Probe failed with %s" , err )
219-
220- now := time .Now ()
221- if now .After (finish ) {
222- return fmt .Errorf ("failed to probe the controller: %s" , err )
223- }
224- time .Sleep (time .Second )
215+ func supportsControllerCreateSnapshot (ctx context.Context , conn * grpc.ClientConn ) (bool , error ) {
216+ capabilities , err := csirpc .GetControllerCapabilities (ctx , conn )
217+ if err != nil {
218+ return false , err
225219 }
220+
221+ return capabilities [csi .ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT ], nil
226222}
0 commit comments