Skip to content

Commit

Permalink
🧹 improve error handling for gcp snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Sep 18, 2023
1 parent 602bef1 commit 82b4290
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
14 changes: 8 additions & 6 deletions motor/providers/gcpinstancesnapshot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ func New(pCfg *providers.Config) (*Provider, error) {
return nil, errors.New("invalid target type")
}

// attach created disk to the scanner instance
err = sc.attachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.diskUrl, mi.deviceName)
if err != nil {
return nil, err
}

errorHandler := func() {
// use different err variable to ensure it does not overshadow the real error
dErr := sc.detachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.deviceName)
Expand All @@ -202,6 +196,14 @@ func New(pCfg *providers.Config) (*Provider, error) {
}
}

// attach created disk to the scanner instance
log.Debug().Str("device-name", mi.deviceName).Msg("attach created disk to the scanner instance")
err = sc.attachDisk(scanner.projectID, scanner.zone, scanner.instanceName, mi.diskUrl, mi.deviceName)
if err != nil {
errorHandler()
return nil, err
}

// mount volume
shell := []string{"sh", "-c"}
volumeMounter := snapshot.NewVolumeMounter(shell)
Expand Down
11 changes: 8 additions & 3 deletions motor/providers/gcpinstancesnapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcpinstancesnapshot

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -203,7 +204,8 @@ func (sc *SnapshotCreator) createDisk(disk *compute.Disk, projectID, zone, diskN
}
if operation.Status == "DONE" {
if operation.Error != nil {
return clonedDiskUrl, fmt.Errorf("operation failed: %+v", operation.Error.Errors)
data, _ := json.Marshal(operation.Error.Errors)
return clonedDiskUrl, fmt.Errorf("could not create disk: " + string(data))
}
clonedDiskUrl = operation.TargetLink
break
Expand Down Expand Up @@ -235,7 +237,7 @@ func (sc *SnapshotCreator) cloneDisk(sourceDisk, projectID, zone, diskName strin
return sc.createDisk(disk, projectID, zone, diskName)
}

// attachDisk attaches a disk to an instanc
// attachDisk attaches a disk to an instance
func (sc *SnapshotCreator) attachDisk(projectID, zone, instanceName, sourceDiskUrl, deviceName string) error {
ctx := context.Background()

Expand All @@ -253,18 +255,21 @@ func (sc *SnapshotCreator) attachDisk(projectID, zone, instanceName, sourceDiskU
// attach the disk to the instance
op, err := computeService.Instances.AttachDisk(projectID, zone, instanceName, attachedDisk).Context(ctx).Do()
if err != nil {
log.Debug().Err(err).Str("device-name", deviceName).Msg("could not attach disk")
return err
}

// wait for the operation to complete
for {
operation, err := computeService.ZoneOperations.Get(projectID, zone, op.Name).Context(ctx).Do()
if err != nil {
log.Debug().Err(err).Str("device-name", deviceName).Msg("could not find operation for disk attachment")
return err
}
if operation.Status == "DONE" {
if operation.Error != nil {
return fmt.Errorf("operation failed: %+v", operation.Error.Errors)
data, _ := json.Marshal(operation.Error.Errors)
return fmt.Errorf("disk attachment failed: " + string(data))
}
break
}
Expand Down

0 comments on commit 82b4290

Please sign in to comment.