Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry on cloud functions error when pulling source from GCS #2116

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-beta/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-beta/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-beta/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