@@ -1264,9 +1264,8 @@ func opIsDone(op *computev1.Operation) (bool, error) {
12641264
12651265func (cloud * CloudProvider ) GetInstanceOrError (ctx context.Context , instanceZone , instanceName string ) (* computev1.Instance , error ) {
12661266 klog .V (5 ).Infof ("Getting instance %v from zone %v" , instanceName , instanceZone )
1267- svc := cloud .service
12681267 project := cloud .project
1269- instance , err := svc .Instances .Get (project , instanceZone , instanceName ).Do ()
1268+ instance , err := cloud . service .Instances .Get (project , instanceZone , instanceName ).Do ()
12701269 if err != nil {
12711270 return nil , err
12721271 }
@@ -1275,9 +1274,9 @@ func (cloud *CloudProvider) GetInstanceOrError(ctx context.Context, instanceZone
12751274
12761275func (cloud * CloudProvider ) GetSnapshot (ctx context.Context , project , snapshotName string ) (* computev1.Snapshot , error ) {
12771276 klog .V (5 ).Infof ("Getting snapshot %v" , snapshotName )
1278- svc := cloud .service
1279- snapshot , err := svc .Snapshots .Get (project , snapshotName ).Context (ctx ).Do ()
1277+ snapshot , err := cloud .service .Snapshots .Get (project , snapshotName ).Context (ctx ).Do ()
12801278 if err != nil {
1279+ klog .V (5 ).Infof ("Error getting snapshot %v: %v" , snapshotName , err )
12811280 return nil , err
12821281 }
12831282 return snapshot , nil
@@ -1313,15 +1312,34 @@ func (cloud *CloudProvider) CreateSnapshot(ctx context.Context, project string,
13131312 if description == "" {
13141313 description = "Snapshot created by GCE-PD CSI Driver"
13151314 }
1316- return cloud .createZonalDiskSnapshot (ctx , project , volKey , snapshotName , snapshotParams , description )
13171315 case meta .Regional :
13181316 if description == "" {
13191317 description = "Regional Snapshot created by GCE-PD CSI Driver"
13201318 }
1321- return cloud .createRegionalDiskSnapshot (ctx , project , volKey , snapshotName , snapshotParams , description )
13221319 default :
13231320 return nil , fmt .Errorf ("could not create snapshot, key was neither zonal nor regional, instead got: %v" , volKey .String ())
13241321 }
1322+
1323+ snapshotToCreate := & computev1.Snapshot {
1324+ Name : snapshotName ,
1325+ StorageLocations : snapshotParams .StorageLocations ,
1326+ Description : description ,
1327+ Labels : snapshotParams .Labels ,
1328+ SourceDisk : cloud .GetDiskSourceURI (project , volKey ),
1329+ }
1330+ _ , err = cloud .service .Snapshots .Insert (project , snapshotToCreate ).Context (ctx ).Do ()
1331+
1332+ if err != nil {
1333+ return nil , err
1334+ }
1335+
1336+ snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1337+
1338+ if err == nil {
1339+ err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1340+ }
1341+
1342+ return snapshot , err
13251343}
13261344
13271345func (cloud * CloudProvider ) CreateImage (ctx context.Context , project string , volKey * meta.Key , imageName string , snapshotParams common.SnapshotParameters ) (* computev1.Image , error ) {
@@ -1497,52 +1515,6 @@ func (cloud *CloudProvider) resizeRegionalDisk(ctx context.Context, project stri
14971515 return requestGb , nil
14981516}
14991517
1500- func (cloud * CloudProvider ) createZonalDiskSnapshot (ctx context.Context , project string , volKey * meta.Key , snapshotName string , snapshotParams common.SnapshotParameters , description string ) (* computev1.Snapshot , error ) {
1501- snapshotToCreate := & computev1.Snapshot {
1502- Name : snapshotName ,
1503- StorageLocations : snapshotParams .StorageLocations ,
1504- Description : description ,
1505- Labels : snapshotParams .Labels ,
1506- }
1507-
1508- _ , err := cloud .service .Disks .CreateSnapshot (project , volKey .Zone , volKey .Name , snapshotToCreate ).Context (ctx ).Do ()
1509-
1510- if err != nil {
1511- return nil , err
1512- }
1513-
1514- snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1515-
1516- if err == nil {
1517- err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1518- }
1519-
1520- return snapshot , err
1521- }
1522-
1523- func (cloud * CloudProvider ) createRegionalDiskSnapshot (ctx context.Context , project string , volKey * meta.Key , snapshotName string , snapshotParams common.SnapshotParameters , description string ) (* computev1.Snapshot , error ) {
1524- snapshotToCreate := & computev1.Snapshot {
1525- Name : snapshotName ,
1526- StorageLocations : snapshotParams .StorageLocations ,
1527- Description : description ,
1528- Labels : snapshotParams .Labels ,
1529- }
1530-
1531- _ , err := cloud .service .RegionDisks .CreateSnapshot (project , volKey .Region , volKey .Name , snapshotToCreate ).Context (ctx ).Do ()
1532- if err != nil {
1533- return nil , err
1534- }
1535-
1536- snapshot , err := cloud .waitForSnapshotCreation (ctx , project , snapshotName )
1537-
1538- if err == nil {
1539- err = cloud .attachTagsToResource (ctx , snapshotParams .ResourceTags , project , snapshot .Id , snapshotsType , "" , false , resourceManagerHostSubPath )
1540- }
1541-
1542- return snapshot , err
1543-
1544- }
1545-
15461518func (cloud * CloudProvider ) waitForSnapshotCreation (ctx context.Context , project , snapshotName string ) (* computev1.Snapshot , error ) {
15471519 ticker := time .NewTicker (time .Second )
15481520 defer ticker .Stop ()
@@ -1558,7 +1530,7 @@ func (cloud *CloudProvider) waitForSnapshotCreation(ctx context.Context, project
15581530 klog .Warningf ("Error in getting snapshot %s, %v" , snapshotName , err .Error ())
15591531 } else if snapshot != nil {
15601532 if snapshot .Status != "CREATING" {
1561- klog .V (6 ).Infof ("Snapshot %s status is %s" , snapshotName , snapshot .Status )
1533+ klog .V (5 ).Infof ("Snapshot %s status is %s" , snapshotName , snapshot .Status )
15621534 return snapshot , nil
15631535 } else {
15641536 klog .V (6 ).Infof ("Snapshot %s is still creating ..." , snapshotName )
0 commit comments