Skip to content

Commit

Permalink
Add retry on cloud functions error when pulling source from GCS (#3570)…
Browse files Browse the repository at this point in the history
… (#6476)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored May 27, 2020
1 parent e17e98b commit f76f032
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .changelog/3570.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
functions: Added retry to `google_cloudfunctions_function` creation when API returns error while pulling source from GCS
```
11 changes: 10 additions & 1 deletion google/common_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
cloudresourcemanager "google.golang.org/api/cloudresourcemanager/v1"
)

// Wraps Op.Error in an implementation of built-in Error
type CommonOpError struct {
*cloudresourcemanager.Status
}

func (e *CommonOpError) Error() string {
return fmt.Sprintf("Error code %v, message: %s", e.Code, e.Message)
}

type Waiter interface {
// State returns the current status of the operation.
State() string
Expand Down Expand Up @@ -56,7 +65,7 @@ func (w *CommonOperationWaiter) State() string {

func (w *CommonOperationWaiter) Error() error {
if w != nil && w.Op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", w.Op.Error.Code, w.Op.Error.Message)
return &CommonOpError{w.Op.Error}
}
return nil
}
Expand Down
9 changes: 9 additions & 0 deletions google/error_retry_predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,12 @@ func isPeeringOperationInProgress(err error) (bool, string) {
}
return false, ""
}

func isCloudFunctionsSourceCodeError(err error) (bool, string) {
if operr, ok := err.(*CommonOpError); ok {
if operr.Code == 3 && operr.Message == "Failed to retrieve function source code" {
return true, fmt.Sprintf("Retry on Function failing to pull code from GCS")
}
}
return false, ""
}
30 changes: 18 additions & 12 deletions google/resource_cloudfunctions_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,21 +401,27 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
}

log.Printf("[DEBUG] Creating cloud function: %s", function.Name)
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create(
cloudFuncId.locationId(), function).Do()
if err != nil {
return err
}

// Name of function should be unique
d.SetId(cloudFuncId.cloudFunctionId())
// We retry the whole create-and-wait because Cloud Functions
// will sometimes fail a creation operation entirely if it fails to pull
// source code and we need to try the whole creation again.
rerr := retryTimeDuration(func() error {
op, err := config.clientCloudFunctions.Projects.Locations.Functions.Create(
cloudFuncId.locationId(), function).Do()
if err != nil {
return err
}

// Name of function should be unique
d.SetId(cloudFuncId.cloudFunctionId())

err = cloudFunctionsOperationWait(config, op, "Creating CloudFunctions Function",
d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
return cloudFunctionsOperationWait(config, op, "Creating CloudFunctions Function",
d.Timeout(schema.TimeoutCreate))
}, d.Timeout(schema.TimeoutCreate), isCloudFunctionsSourceCodeError)
if rerr != nil {
return rerr
}

log.Printf("[DEBUG] Finished creating cloud function: %s", function.Name)
return resourceCloudFunctionsRead(d, meta)
}

Expand Down

0 comments on commit f76f032

Please sign in to comment.