@@ -15,6 +15,7 @@ limitations under the License.
15
15
package gcecloudprovider
16
16
17
17
import (
18
+ "encoding/json"
18
19
"fmt"
19
20
"strings"
20
21
"time"
@@ -268,21 +269,42 @@ func ValidateDiskParameters(disk *CloudDisk, params common.DiskParameters) error
268
269
269
270
func (cloud * CloudProvider ) InsertDisk (ctx context.Context , volKey * meta.Key , params common.DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error {
270
271
klog .V (5 ).Infof ("Inserting disk %v" , volKey )
272
+
273
+ description , err := encodeDiskTags (params .Tags )
274
+ if err != nil {
275
+ return err
276
+ }
277
+
271
278
switch volKey .Type () {
272
279
case meta .Zonal :
273
- return cloud .insertZonalDisk (ctx , volKey , params , capBytes , capacityRange , snapshotID )
280
+ if description == "" {
281
+ description = "Disk created by GCE-PD CSI Driver"
282
+ }
283
+ return cloud .insertZonalDisk (ctx , volKey , params , capBytes , capacityRange , snapshotID , description )
274
284
case meta .Regional :
275
- return cloud .insertRegionalDisk (ctx , volKey , params , capBytes , capacityRange , replicaZones , snapshotID )
285
+ if description == "" {
286
+ description = "Regional disk created by GCE-PD CSI Driver"
287
+ }
288
+ return cloud .insertRegionalDisk (ctx , volKey , params , capBytes , capacityRange , replicaZones , snapshotID , description )
276
289
default :
277
290
return fmt .Errorf ("could not insert disk, key was neither zonal nor regional, instead got: %v" , volKey .String ())
278
291
}
279
292
}
280
293
281
- func (cloud * CloudProvider ) insertRegionalDisk (ctx context.Context , volKey * meta.Key , params common.DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error {
294
+ func (cloud * CloudProvider ) insertRegionalDisk (
295
+ ctx context.Context ,
296
+ volKey * meta.Key ,
297
+ params common.DiskParameters ,
298
+ capBytes int64 ,
299
+ capacityRange * csi.CapacityRange ,
300
+ replicaZones []string ,
301
+ snapshotID string ,
302
+ description string ) error {
303
+
282
304
diskToCreate := & computev1.Disk {
283
305
Name : volKey .Name ,
284
306
SizeGb : common .BytesToGb (capBytes ),
285
- Description : "Regional disk created by GCE-PD CSI Driver" ,
307
+ Description : description ,
286
308
Type : cloud .GetDiskTypeURI (volKey , params .DiskType ),
287
309
}
288
310
if snapshotID != "" {
@@ -337,11 +359,18 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
337
359
return nil
338
360
}
339
361
340
- func (cloud * CloudProvider ) insertZonalDisk (ctx context.Context , volKey * meta.Key , params common.DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , snapshotID string ) error {
362
+ func (cloud * CloudProvider ) insertZonalDisk (
363
+ ctx context.Context ,
364
+ volKey * meta.Key ,
365
+ params common.DiskParameters ,
366
+ capBytes int64 ,
367
+ capacityRange * csi.CapacityRange ,
368
+ snapshotID string ,
369
+ description string ) error {
341
370
diskToCreate := & computev1.Disk {
342
371
Name : volKey .Name ,
343
372
SizeGb : common .BytesToGb (capBytes ),
344
- Description : "Disk created by GCE-PD CSI Driver" ,
373
+ Description : description ,
345
374
Type : cloud .GetDiskTypeURI (volKey , params .DiskType ),
346
375
}
347
376
@@ -788,3 +817,18 @@ func removeCryptoKeyVersion(kmsKey string) string {
788
817
}
789
818
return kmsKey
790
819
}
820
+
821
+ // encodeDiskTags encodes requested volume tags into JSON string, as GCE does
822
+ // not support tags on GCE PDs and we use Description field as fallback.
823
+ func encodeDiskTags (tags map [string ]string ) (string , error ) {
824
+ if len (tags ) == 0 {
825
+ // No tags -> empty JSON
826
+ return "" , nil
827
+ }
828
+
829
+ enc , err := json .Marshal (tags )
830
+ if err != nil {
831
+ return "" , fmt .Errorf ("failed to encodeDiskTags %v: %v" , tags , err )
832
+ }
833
+ return string (enc ), nil
834
+ }
0 commit comments