@@ -15,6 +15,7 @@ limitations under the License.
1515package gcecloudprovider
1616
1717import (
18+ "encoding/json"
1819 "fmt"
1920 "strings"
2021 "time"
@@ -268,21 +269,42 @@ func ValidateDiskParameters(disk *CloudDisk, params common.DiskParameters) error
268269
269270func (cloud * CloudProvider ) InsertDisk (ctx context.Context , volKey * meta.Key , params common.DiskParameters , capBytes int64 , capacityRange * csi.CapacityRange , replicaZones []string , snapshotID string ) error {
270271 klog .V (5 ).Infof ("Inserting disk %v" , volKey )
272+
273+ description , err := encodeDiskTags (params .Tags )
274+ if err != nil {
275+ return err
276+ }
277+
271278 switch volKey .Type () {
272279 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 )
274284 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 )
276289 default :
277290 return fmt .Errorf ("could not insert disk, key was neither zonal nor regional, instead got: %v" , volKey .String ())
278291 }
279292}
280293
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+
282304 diskToCreate := & computev1.Disk {
283305 Name : volKey .Name ,
284306 SizeGb : common .BytesToGb (capBytes ),
285- Description : "Regional disk created by GCE-PD CSI Driver" ,
307+ Description : description ,
286308 Type : cloud .GetDiskTypeURI (volKey , params .DiskType ),
287309 }
288310 if snapshotID != "" {
@@ -337,11 +359,18 @@ func (cloud *CloudProvider) insertRegionalDisk(ctx context.Context, volKey *meta
337359 return nil
338360}
339361
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 {
341370 diskToCreate := & computev1.Disk {
342371 Name : volKey .Name ,
343372 SizeGb : common .BytesToGb (capBytes ),
344- Description : "Disk created by GCE-PD CSI Driver" ,
373+ Description : description ,
345374 Type : cloud .GetDiskTypeURI (volKey , params .DiskType ),
346375 }
347376
@@ -788,3 +817,18 @@ func removeCryptoKeyVersion(kmsKey string) string {
788817 }
789818 return kmsKey
790819}
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