@@ -41,8 +41,8 @@ type GCECompute interface {
41
41
// Disk Methods
42
42
GetDisk (ctx context.Context , volumeKey * meta.Key ) (* CloudDisk , error )
43
43
RepairUnderspecifiedVolumeKey (ctx context.Context , volumeKey * meta.Key ) (* meta.Key , error )
44
- ValidateExistingDisk (ctx context.Context , disk * CloudDisk , diskType string , reqBytes , limBytes int64 ) error
45
- InsertDisk (ctx context.Context , volKey * meta.Key , diskType string , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID , diskEncryptionKmsKey string ) error
44
+ ValidateExistingDisk (ctx context.Context , disk * CloudDisk , params common. DiskParameters , reqBytes , limBytes int64 ) error
45
+ InsertDisk (ctx context.Context , volKey * meta.Key , params common. DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error
46
46
DeleteDisk (ctx context.Context , volumeKey * meta.Key ) error
47
47
AttachDisk (ctx context.Context , volKey * meta.Key , readWrite , diskType , instanceZone , instanceName string ) error
48
48
DetachDisk (ctx context.Context , deviceName string , instanceZone , instanceName string ) error
@@ -212,8 +212,8 @@ func (cloud *CloudProvider) getRegionURI(region string) string {
212
212
region )
213
213
}
214
214
215
- func (cloud * CloudProvider ) ValidateExistingDisk (ctx context.Context , resp * CloudDisk , diskType string , reqBytes , limBytes int64 ) error {
216
- klog .V (5 ).Infof ("Validating existing disk %v with diskType: %s, reqested bytes: %v, limit bytes: %v" , resp , diskType , reqBytes , limBytes )
215
+ func (cloud * CloudProvider ) ValidateExistingDisk (ctx context.Context , resp * CloudDisk , params common. DiskParameters , reqBytes , limBytes int64 ) error {
216
+ klog .V (5 ).Infof ("Validating existing disk %v with diskType: %s, reqested bytes: %v, limit bytes: %v" , resp , params . DiskType , reqBytes , limBytes )
217
217
if resp == nil {
218
218
return fmt .Errorf ("disk does not exist" )
219
219
}
@@ -225,44 +225,59 @@ func (cloud *CloudProvider) ValidateExistingDisk(ctx context.Context, resp *Clou
225
225
reqBytes , common .GbToBytes (resp .GetSizeGb ()), limBytes )
226
226
}
227
227
228
- respType := strings .Split (resp .GetType (), "/" )
229
- typeMatch := strings .TrimSpace (respType [len (respType )- 1 ]) == strings .TrimSpace (diskType )
230
- typeDefault := diskType == "" && strings .TrimSpace (respType [len (respType )- 1 ]) == "pd-standard"
231
- if ! typeMatch && ! typeDefault {
232
- return fmt .Errorf ("disk already exists with incompatible type. Need %v. Got %v" ,
233
- diskType , respType [len (respType )- 1 ])
228
+ return ValidateDiskParameters (resp , params )
229
+ }
230
+
231
+ // ValidateDiskParameters takes a CloudDisk and returns true if the parameters
232
+ // specified validly describe the disk provided, and false otherwise.
233
+ func ValidateDiskParameters (disk * CloudDisk , params common.DiskParameters ) error {
234
+ if disk .GetPDType () != params .DiskType {
235
+ return fmt .Errorf ("actual pd type %s did not match the expected param %s" , disk .GetPDType (), params .DiskType )
234
236
}
237
+
238
+ if params .ReplicationType == "none" && disk .Type () != Zonal {
239
+ return fmt .Errorf ("actual disk replication type %v did not match expected param %s" , disk .Type (), params .ReplicationType )
240
+ }
241
+
242
+ if params .ReplicationType == "regional-pd" && disk .Type () != Regional {
243
+ return fmt .Errorf ("actual disk replication type %v did not match expected param %s" , disk .Type (), "regional-pd" )
244
+ }
245
+
246
+ if disk .GetKMSKeyName () != params .DiskEncryptionKMSKey {
247
+ return fmt .Errorf ("actual disk KMS key name %s did not match expected param %s" , disk .GetKMSKeyName (), params .DiskEncryptionKMSKey )
248
+ }
249
+
235
250
return nil
236
251
}
237
252
238
- func (cloud * CloudProvider ) InsertDisk (ctx context.Context , volKey * meta.Key , diskType string , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID , diskEncryptionKmsKey string ) error {
253
+ func (cloud * CloudProvider ) InsertDisk (ctx context.Context , volKey * meta.Key , params common. DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error {
239
254
klog .V (5 ).Infof ("Inserting disk %v" , volKey )
240
255
switch volKey .Type () {
241
256
case meta .Zonal :
242
- return cloud .insertZonalDisk (ctx , volKey , diskType , capBytes , capacityRange , snapshotID , diskEncryptionKmsKey )
257
+ return cloud .insertZonalDisk (ctx , volKey , params , capBytes , capacityRange , snapshotID )
243
258
case meta .Regional :
244
- return cloud .insertRegionalDisk (ctx , volKey , diskType , capBytes , capacityRange , replicaZones , snapshotID , diskEncryptionKmsKey )
259
+ return cloud .insertRegionalDisk (ctx , volKey , params , capBytes , capacityRange , replicaZones , snapshotID )
245
260
default :
246
261
return fmt .Errorf ("could not insert disk, key was neither zonal nor regional, instead got: %v" , volKey .String ())
247
262
}
248
263
}
249
264
250
- func (cloud * CloudProvider ) insertRegionalDisk (ctx context.Context , volKey * meta.Key , diskType string , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID , diskEncryptionKmsKey string ) error {
265
+ func (cloud * CloudProvider ) insertRegionalDisk (ctx context.Context , volKey * meta.Key , params common. DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error {
251
266
diskToCreate := & computev1.Disk {
252
267
Name : volKey .Name ,
253
268
SizeGb : common .BytesToGb (capBytes ),
254
269
Description : "Regional disk created by GCE-PD CSI Driver" ,
255
- Type : cloud .GetDiskTypeURI (volKey , diskType ),
270
+ Type : cloud .GetDiskTypeURI (volKey , params . DiskType ),
256
271
}
257
272
if snapshotID != "" {
258
273
diskToCreate .SourceSnapshot = snapshotID
259
274
}
260
275
if len (replicaZones ) != 0 {
261
276
diskToCreate .ReplicaZones = replicaZones
262
277
}
263
- if diskEncryptionKmsKey != "" {
278
+ if params . DiskEncryptionKMSKey != "" {
264
279
diskToCreate .DiskEncryptionKey = & computev1.CustomerEncryptionKey {
265
- KmsKeyName : diskEncryptionKmsKey ,
280
+ KmsKeyName : params . DiskEncryptionKMSKey ,
266
281
}
267
282
}
268
283
@@ -273,7 +288,7 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
273
288
if err != nil {
274
289
return err
275
290
}
276
- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
291
+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
277
292
int64 (capacityRange .GetRequiredBytes ()),
278
293
int64 (capacityRange .GetLimitBytes ()))
279
294
if err != nil {
@@ -292,7 +307,7 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
292
307
if err != nil {
293
308
return err
294
309
}
295
- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
310
+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
296
311
int64 (capacityRange .GetRequiredBytes ()),
297
312
int64 (capacityRange .GetLimitBytes ()))
298
313
if err != nil {
@@ -306,21 +321,21 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
306
321
return nil
307
322
}
308
323
309
- func (cloud * CloudProvider ) insertZonalDisk (ctx context.Context , volKey * meta.Key , diskType string , capBytes int64 , capacityRange * csi.CapacityRange , snapshotID , diskEncryptionKmsKey string ) error {
324
+ func (cloud * CloudProvider ) insertZonalDisk (ctx context.Context , volKey * meta.Key , params common. DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , snapshotID string ) error {
310
325
diskToCreate := & computev1.Disk {
311
326
Name : volKey .Name ,
312
327
SizeGb : common .BytesToGb (capBytes ),
313
328
Description : "Disk created by GCE-PD CSI Driver" ,
314
- Type : cloud .GetDiskTypeURI (volKey , diskType ),
329
+ Type : cloud .GetDiskTypeURI (volKey , params . DiskType ),
315
330
}
316
331
317
332
if snapshotID != "" {
318
333
diskToCreate .SourceSnapshot = snapshotID
319
334
}
320
335
321
- if diskEncryptionKmsKey != "" {
336
+ if params . DiskEncryptionKMSKey != "" {
322
337
diskToCreate .DiskEncryptionKey = & computev1.CustomerEncryptionKey {
323
- KmsKeyName : diskEncryptionKmsKey ,
338
+ KmsKeyName : params . DiskEncryptionKMSKey ,
324
339
}
325
340
}
326
341
@@ -332,7 +347,7 @@ func (cloud *CloudProvider) insertZonalDisk(ctx context.Context, volKey *meta.Ke
332
347
if err != nil {
333
348
return err
334
349
}
335
- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
350
+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
336
351
int64 (capacityRange .GetRequiredBytes ()),
337
352
int64 (capacityRange .GetLimitBytes ()))
338
353
if err != nil {
@@ -352,7 +367,7 @@ func (cloud *CloudProvider) insertZonalDisk(ctx context.Context, volKey *meta.Ke
352
367
if err != nil {
353
368
return err
354
369
}
355
- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
370
+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
356
371
int64 (capacityRange .GetRequiredBytes ()),
357
372
int64 (capacityRange .GetLimitBytes ()))
358
373
if err != nil {
0 commit comments