Skip to content

Commit

Permalink
Add additional fingerprint error to check for when updating metadata (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
selmanj authored Jul 19, 2017
1 parent adaae73 commit 4c8f62e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions google/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package google
import (
"fmt"
"log"
"strings"

"google.golang.org/api/compute/v1"
)

const FINGERPRINT_RETRIES = 10
const FINGERPRINT_FAIL = "Invalid fingerprint."

var FINGERPRINT_FAIL_ERRORS = []string{"Invalid fingerprint.", "Supplied fingerprint does not match current metadata fingerprint."}

// Since the google compute API uses optimistic locking, there is a chance
// we need to resubmit our updated metadata. To do this, you need to provide
Expand All @@ -17,11 +19,25 @@ func MetadataRetryWrapper(update func() error) error {
attempt := 0
for attempt < FINGERPRINT_RETRIES {
err := update()
if err != nil && err.Error() == FINGERPRINT_FAIL {
attempt++
} else {
if err == nil {
return nil
}

// Check to see if the error matches any of our fingerprint-related failure messages
var fingerprintError bool
for _, msg := range FINGERPRINT_FAIL_ERRORS {
if strings.Contains(err.Error(), msg) {
fingerprintError = true
break
}
}

if !fingerprintError {
// Something else went wrong, don't retry
return err
}

attempt++
}

return fmt.Errorf("Failed to update metadata after %d retries", attempt)
Expand Down

0 comments on commit 4c8f62e

Please sign in to comment.