Skip to content

retry node pool writes on failed precondition #1660

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

Merged
merged 2 commits into from
Jun 22, 2018
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
28 changes: 19 additions & 9 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,20 +218,34 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e
NodePool: nodePool,
}

operation, err := config.clientContainerBeta.
Projects.Locations.Clusters.NodePools.Create(nodePoolInfo.parent(), req).Do()
timeout := d.Timeout(schema.TimeoutCreate)
startTime := time.Now()

var operation *containerBeta.Operation
err = resource.Retry(timeout, func() *resource.RetryError {
operation, err = config.clientContainerBeta.
Projects.Locations.Clusters.NodePools.Create(nodePoolInfo.parent(), req).Do()

if err != nil {
if isFailedPreconditionError(err) {
// We get failed precondition errors if the cluster is updating
// while we try to add the node pool.
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("error creating NodePool: %s", err)
}
timeout -= time.Since(startTime)

d.SetId(fmt.Sprintf("%s/%s/%s", nodePoolInfo.location, nodePoolInfo.cluster, nodePool.Name))

timeoutInMinutes := int(d.Timeout(schema.TimeoutCreate).Minutes())

waitErr := containerBetaOperationWait(config,
operation, nodePoolInfo.project,
nodePoolInfo.location, "creating GKE NodePool", timeoutInMinutes, 3)
nodePoolInfo.location, "creating GKE NodePool", int(timeout.Minutes()), 3)

if waitErr != nil {
// The resource didn't actually create
Expand Down Expand Up @@ -259,10 +273,6 @@ func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) err
nodePool, err = config.clientContainerBeta.
Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name)).Do()

if err != nil {
return resource.NonRetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}
Expand Down
19 changes: 19 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ func isApiNotEnabledError(err error) bool {
return false
}

func isFailedPreconditionError(err error) bool {
gerr, ok := errwrap.GetType(err, &googleapi.Error{}).(*googleapi.Error)
if !ok {
return false
}
if gerr == nil {
return false
}
if gerr.Code != 400 {
return false
}
for _, e := range gerr.Errors {
if e.Reason == "failedPrecondition" {
return true
}
}
return false
}

func isConflictError(err error) bool {
if e, ok := err.(*googleapi.Error); ok && e.Code == 409 {
return true
Expand Down