@@ -41,8 +41,8 @@ type GCECompute interface {
4141 // Disk Methods
4242 GetDisk (ctx context.Context , volumeKey * meta.Key ) (* CloudDisk , error )
4343 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
4646 DeleteDisk (ctx context.Context , volumeKey * meta.Key ) error
4747 AttachDisk (ctx context.Context , volKey * meta.Key , readWrite , diskType , instanceZone , instanceName string ) error
4848 DetachDisk (ctx context.Context , deviceName string , instanceZone , instanceName string ) error
@@ -212,8 +212,8 @@ func (cloud *CloudProvider) getRegionURI(region string) string {
212212 region )
213213}
214214
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 )
217217 if resp == nil {
218218 return fmt .Errorf ("disk does not exist" )
219219 }
@@ -225,44 +225,59 @@ func (cloud *CloudProvider) ValidateExistingDisk(ctx context.Context, resp *Clou
225225 reqBytes , common .GbToBytes (resp .GetSizeGb ()), limBytes )
226226 }
227227
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 )
234236 }
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+
235250 return nil
236251}
237252
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 {
239254 klog .V (5 ).Infof ("Inserting disk %v" , volKey )
240255 switch volKey .Type () {
241256 case meta .Zonal :
242- return cloud .insertZonalDisk (ctx , volKey , diskType , capBytes , capacityRange , snapshotID , diskEncryptionKmsKey )
257+ return cloud .insertZonalDisk (ctx , volKey , params , capBytes , capacityRange , snapshotID )
243258 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 )
245260 default :
246261 return fmt .Errorf ("could not insert disk, key was neither zonal nor regional, instead got: %v" , volKey .String ())
247262 }
248263}
249264
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 {
251266 diskToCreate := & computev1.Disk {
252267 Name : volKey .Name ,
253268 SizeGb : common .BytesToGb (capBytes ),
254269 Description : "Regional disk created by GCE-PD CSI Driver" ,
255- Type : cloud .GetDiskTypeURI (volKey , diskType ),
270+ Type : cloud .GetDiskTypeURI (volKey , params . DiskType ),
256271 }
257272 if snapshotID != "" {
258273 diskToCreate .SourceSnapshot = snapshotID
259274 }
260275 if len (replicaZones ) != 0 {
261276 diskToCreate .ReplicaZones = replicaZones
262277 }
263- if diskEncryptionKmsKey != "" {
278+ if params . DiskEncryptionKMSKey != "" {
264279 diskToCreate .DiskEncryptionKey = & computev1.CustomerEncryptionKey {
265- KmsKeyName : diskEncryptionKmsKey ,
280+ KmsKeyName : params . DiskEncryptionKMSKey ,
266281 }
267282 }
268283
@@ -273,7 +288,7 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
273288 if err != nil {
274289 return err
275290 }
276- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
291+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
277292 int64 (capacityRange .GetRequiredBytes ()),
278293 int64 (capacityRange .GetLimitBytes ()))
279294 if err != nil {
@@ -292,7 +307,7 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
292307 if err != nil {
293308 return err
294309 }
295- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
310+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
296311 int64 (capacityRange .GetRequiredBytes ()),
297312 int64 (capacityRange .GetLimitBytes ()))
298313 if err != nil {
@@ -306,21 +321,21 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
306321 return nil
307322}
308323
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 {
310325 diskToCreate := & computev1.Disk {
311326 Name : volKey .Name ,
312327 SizeGb : common .BytesToGb (capBytes ),
313328 Description : "Disk created by GCE-PD CSI Driver" ,
314- Type : cloud .GetDiskTypeURI (volKey , diskType ),
329+ Type : cloud .GetDiskTypeURI (volKey , params . DiskType ),
315330 }
316331
317332 if snapshotID != "" {
318333 diskToCreate .SourceSnapshot = snapshotID
319334 }
320335
321- if diskEncryptionKmsKey != "" {
336+ if params . DiskEncryptionKMSKey != "" {
322337 diskToCreate .DiskEncryptionKey = & computev1.CustomerEncryptionKey {
323- KmsKeyName : diskEncryptionKmsKey ,
338+ KmsKeyName : params . DiskEncryptionKMSKey ,
324339 }
325340 }
326341
@@ -332,7 +347,7 @@ func (cloud *CloudProvider) insertZonalDisk(ctx context.Context, volKey *meta.Ke
332347 if err != nil {
333348 return err
334349 }
335- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
350+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
336351 int64 (capacityRange .GetRequiredBytes ()),
337352 int64 (capacityRange .GetLimitBytes ()))
338353 if err != nil {
@@ -352,7 +367,7 @@ func (cloud *CloudProvider) insertZonalDisk(ctx context.Context, volKey *meta.Ke
352367 if err != nil {
353368 return err
354369 }
355- err = cloud .ValidateExistingDisk (ctx , disk , diskType ,
370+ err = cloud .ValidateExistingDisk (ctx , disk , params ,
356371 int64 (capacityRange .GetRequiredBytes ()),
357372 int64 (capacityRange .GetLimitBytes ()))
358373 if err != nil {
0 commit comments