@@ -195,36 +195,27 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
195
195
}, nil
196
196
}
197
197
198
- func (cs * controllerServer ) cloneFromSnapshot (ctx context.Context , snapContent * snapv1.VolumeSnapshotContent , dstName , dstNode , dstLVType , dstVGName string , dstSize int64 ) error {
199
- srcVolID := * snapContent .Spec .Source .VolumeHandle
200
- srcVol , err := cs .kubeClient .CoreV1 ().PersistentVolumes ().Get (ctx , srcVolID , metav1.GetOptions {})
201
- if err != nil {
202
- klog .Errorf ("error getting volume: %v" , err )
203
- return err
204
- }
198
+ func (cs * controllerServer ) generateVolumeActionForClone (srcVol * v1.PersistentVolume , srcLVName , dstName , dstNode , dstLVType , dstVGName string , srcSize , dstSize int64 ) (volumeAction , error ) {
205
199
ns := srcVol .Spec .NodeAffinity .Required .NodeSelectorTerms
206
200
srcNode := ns [0 ].MatchExpressions [0 ].Values [0 ]
207
201
srcVgName := srcVol .Spec .CSI .VolumeAttributes ["vgName" ]
208
- srcVgType := srcVol .Spec .CSI .VolumeAttributes ["type" ]
209
- restoreSize := * snapContent .Status .RestoreSize
202
+ srcType := srcVol .Spec .CSI .VolumeAttributes ["type" ]
210
203
211
- snapshotLVName := fmt .Sprintf ("lvm-%s" , * snapContent .Status .SnapshotHandle )
212
- //srcSnapDev := fmt.Sprintf("/dev/%s/%s", srcVgName, snapshotLVName)
213
204
srcInfo := & srcInfo {
214
- srcLVName : snapshotLVName ,
205
+ srcLVName : srcLVName ,
215
206
srcVGName : srcVgName ,
216
- srcType : srcVgType ,
207
+ srcType : srcType ,
217
208
}
218
- klog .V (4 ).Infof ("cloning volume from %s/%s " , srcVgName , snapshotLVName )
209
+ klog .V (4 ).Infof ("cloning volume from %s/%s " , srcVgName , srcLVName )
219
210
220
- if restoreSize > dstSize {
221
- return status .Error (codes .InvalidArgument , "source volume size is larger than destination volume size" )
211
+ if srcSize > dstSize {
212
+ return volumeAction {}, status .Errorf (codes .InvalidArgument , "source/snapshot volume size(%v) is larger than destination volume size(%v)" , srcSize , dstSize )
222
213
}
223
214
if srcNode != dstNode {
224
- return status .Errorf (codes .InvalidArgument , "source (%s) and destination (%s) nodes are different (not supported)" , srcNode , dstNode )
215
+ return volumeAction {}, status .Errorf (codes .InvalidArgument , "source (%s) and destination (%s) nodes are different (not supported)" , srcNode , dstNode )
225
216
}
226
217
227
- va := volumeAction {
218
+ return volumeAction {
228
219
action : actionTypeClone ,
229
220
name : dstName ,
230
221
nodeName : dstNode ,
@@ -237,7 +228,23 @@ func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *
237
228
vgName : dstVGName ,
238
229
hostWritePath : cs .hostWritePath ,
239
230
srcInfo : srcInfo ,
231
+ }, nil
232
+ }
233
+
234
+ func (cs * controllerServer ) cloneFromSnapshot (ctx context.Context , snapContent * snapv1.VolumeSnapshotContent , dstName , dstNode , dstLVType , dstVGName string , dstSize int64 ) error {
235
+ srcVolID := * snapContent .Spec .Source .VolumeHandle
236
+ srcVol , err := cs .kubeClient .CoreV1 ().PersistentVolumes ().Get (ctx , srcVolID , metav1.GetOptions {})
237
+ if err != nil {
238
+ klog .Errorf ("error getting volume: %v" , err )
239
+ return err
240
240
}
241
+ restoreSize := * snapContent .Status .RestoreSize
242
+ snapshotLVName := fmt .Sprintf ("lvm-%s" , * snapContent .Status .SnapshotHandle )
243
+ va , err := cs .generateVolumeActionForClone (srcVol , snapshotLVName , dstName , dstNode , dstLVType , dstVGName , restoreSize , dstSize )
244
+ if err != nil {
245
+ return err
246
+ }
247
+
241
248
if err := createProvisionerPod (ctx , va ); err != nil {
242
249
klog .Errorf ("error creating provisioner pod :%v" , err )
243
250
return err
@@ -247,47 +254,17 @@ func (cs *controllerServer) cloneFromSnapshot(ctx context.Context, snapContent *
247
254
}
248
255
249
256
func (cs * controllerServer ) cloneFromVolume (ctx context.Context , srcVol * v1.PersistentVolume , dstName , dstNode , dstLVType , dstVGName string , dstSize int64 ) error {
250
- ns := srcVol .Spec .NodeAffinity .Required .NodeSelectorTerms
251
- srcNode := ns [0 ].MatchExpressions [0 ].Values [0 ]
252
- srcVgName := srcVol .Spec .CSI .VolumeAttributes ["vgName" ]
253
- srcType := srcVol .Spec .CSI .VolumeAttributes ["type" ]
254
257
srcSizeStr := srcVol .Spec .CSI .VolumeAttributes ["RequiredBytes" ]
255
258
srcSize , err := strconv .ParseInt (srcSizeStr , 10 , 64 )
256
259
if err != nil {
257
- klog .Errorf ("error parsing srcSize: %v" , err )
258
- return err
260
+ return status .Errorf (codes .InvalidArgument , "error parsing srcSize: %v" , err )
259
261
}
260
-
261
- //srcDev := fmt.Sprintf("/dev/%s/%s", srcVgName, srcVol.GetName())
262
262
srcLVName := srcVol .GetName ()
263
- srcInfo := & srcInfo {
264
- srcLVName : srcLVName ,
265
- srcVGName : srcVgName ,
266
- srcType : srcType ,
267
- }
268
- klog .V (4 ).Infof ("cloning volume from %s/%s " , srcVgName , srcLVName )
269
-
270
- if srcSize > dstSize {
271
- return status .Error (codes .InvalidArgument , "source volume size is larger than destination volume size" )
272
- }
273
- if srcNode != dstNode {
274
- return status .Errorf (codes .InvalidArgument , "source (%s) and destination (%s) nodes are different (not supported)" , srcNode , dstNode )
263
+ va , err := cs .generateVolumeActionForClone (srcVol , srcLVName , dstName , dstNode , dstLVType , dstVGName , srcSize , dstSize )
264
+ if err != nil {
265
+ return err
275
266
}
276
267
277
- va := volumeAction {
278
- action : actionTypeClone ,
279
- name : dstName ,
280
- nodeName : dstNode ,
281
- size : dstSize ,
282
- lvmType : dstLVType ,
283
- pullPolicy : cs .pullPolicy ,
284
- provisionerImage : cs .provisionerImage ,
285
- kubeClient : cs .kubeClient ,
286
- namespace : cs .namespace ,
287
- vgName : dstVGName ,
288
- hostWritePath : cs .hostWritePath ,
289
- srcInfo : srcInfo ,
290
- }
291
268
if err := createProvisionerPod (ctx , va ); err != nil {
292
269
klog .Errorf ("error creating provisioner pod :%v" , err )
293
270
return err
0 commit comments