@@ -1219,9 +1219,8 @@ func opIsDone(op *computev1.Operation) (bool, error) {
12191219
12201220func (cloud * CloudProvider ) GetInstanceOrError (ctx context.Context , instanceZone , instanceName string ) (* computev1.Instance , error ) {
12211221 klog .V (5 ).Infof ("Getting instance %v from zone %v" , instanceName , instanceZone )
1222- svc := cloud .service
12231222 project := cloud .project
1224- instance , err := svc .Instances .Get (project , instanceZone , instanceName ).Do ()
1223+ instance , err := cloud . service .Instances .Get (project , instanceZone , instanceName ).Do ()
12251224 if err != nil {
12261225 return nil , err
12271226 }
@@ -1230,9 +1229,9 @@ func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone
12301229
12311230func (cloud * CloudProvider ) GetSnapshot (ctx context.Context , project , snapshotName string ) (* computev1.Snapshot , error ) {
12321231 klog .V (5 ).Infof ("Getting snapshot %v" , snapshotName )
1233- svc := cloud .service
1234- snapshot , err := svc .Snapshots .Get (project , snapshotName ).Context (ctx ).Do ()
1232+ snapshot , err := cloud .service .Snapshots .Get (project , snapshotName ).Context (ctx ).Do ()
12351233 if err != nil {
1234+ klog .V (5 ).Infof ("Error getting snapshot %v: %v" , snapshotName , err )
12361235 return nil , err
12371236 }
12381237 return snapshot , nil
@@ -1268,15 +1267,34 @@ func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, project string,
12681267 if description == "" {
12691268 description = "Snapshot created by GCE-PD CSI Driver"
12701269 }
1271- return cloud .createZonalDiskSnapshot (ctx , project , volKey , snapshotName , snapshotParams , description )
12721270 case meta .Regional :
12731271 if description == "" {
12741272 description = "Regional Snapshot created by GCE-PD CSI Driver"
12751273 }
1276- return cloud .createRegionalDiskSnapshot (ctx , project , volKey , snapshotName , snapshotParams , description )
12771274 default :
12781275 return nil , fmt .Errorf ("could not create snapshot, key was neither zonal nor regional, instead got: %v" , volKey .String ())
12791276 }
1277+
1278+ snapshotToCreate := & computev1.Snapshot {
1279+ Name : snapshotName ,
1280+ StorageLocations : snapshotParams .StorageLocations ,
1281+ Description : description ,
1282+ Labels : snapshotParams .Labels ,
1283+ SourceDisk : cloud .GetDiskSourceURI (project , volKey ),
1284+ }
1285+ _ , err = cloud .service .Snapshots .Insert (project , snapshotToCreate ).Context (ctx ).Do ()
1286+
1287+ if err != nil {
1288+ return nil , err
1289+ }
1290+
1291+ snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1292+
1293+ if err == nil {
1294+ err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1295+ }
1296+
1297+ return snapshot , err
12801298}
12811299
12821300func (cloud * CloudProvider ) CreateImage (ctx context.Context , project string , volKey * meta.Key , imageName string , snapshotParams common.SnapshotParameters ) (* computev1.Image , error ) {
@@ -1452,52 +1470,6 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, project stri
14521470 return requestGb , nil
14531471}
14541472
1455- func (cloud * CloudProvider ) createZonalDiskSnapshot (ctx context.Context , project string , volKey * meta.Key , snapshotName string , snapshotParams common.SnapshotParameters , description string ) (* computev1.Snapshot , error ) {
1456- snapshotToCreate := & computev1.Snapshot {
1457- Name : snapshotName ,
1458- StorageLocations : snapshotParams .StorageLocations ,
1459- Description : description ,
1460- Labels : snapshotParams .Labels ,
1461- }
1462-
1463- _ , err := cloud .service .Disks .CreateSnapshot (project , volKey .Zone , volKey .Name , snapshotToCreate ).Context (ctx ).Do ()
1464-
1465- if err != nil {
1466- return nil , err
1467- }
1468-
1469- snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1470-
1471- if err == nil {
1472- err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1473- }
1474-
1475- return snapshot , err
1476- }
1477-
1478- func (cloud * CloudProvider ) createRegionalDiskSnapshot (ctx context.Context , project string , volKey * meta.Key , snapshotName string , snapshotParams common.SnapshotParameters , description string ) (* computev1.Snapshot , error ) {
1479- snapshotToCreate := & computev1.Snapshot {
1480- Name : snapshotName ,
1481- StorageLocations : snapshotParams .StorageLocations ,
1482- Description : description ,
1483- Labels : snapshotParams .Labels ,
1484- }
1485-
1486- _ , err := cloud .service .RegionDisks .CreateSnapshot (project , volKey .Region , volKey .Name , snapshotToCreate ).Context (ctx ).Do ()
1487- if err != nil {
1488- return nil , err
1489- }
1490-
1491- snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1492-
1493- if err == nil {
1494- err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1495- }
1496-
1497- return snapshot , err
1498-
1499- }
1500-
15011473func (cloud * CloudProvider ) waitForSnapshotCreation (ctx context.Context , project , snapshotName string ) (* computev1.Snapshot , error ) {
15021474 ticker := time .NewTicker (time .Second )
15031475 defer ticker .Stop ()
@@ -1513,7 +1485,7 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project
15131485 klog .Warningf ("Error in getting snapshot %s, %v" , snapshotName , err .Error ())
15141486 } else if snapshot != nil {
15151487 if snapshot .Status != "CREATING" {
1516- klog .V (6 ).Infof ("Snapshot %s status is %s" , snapshotName , snapshot .Status )
1488+ klog .V (5 ).Infof ("Snapshot %s status is %s" , snapshotName , snapshot .Status )
15171489 return snapshot , nil
15181490 } else {
15191491 klog .V (6 ).Infof ("Snapshot %s is still creating ..." , snapshotName )
0 commit comments