Skip to content

Commit

Permalink
Add state waiting to google_container_node_pool (#3114) (#5662)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Feb 12, 2020
1 parent e6062e1 commit 31926fa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changelog/3114.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
container: `google_container_node_pool` will allow importing / updating / deleting node pools in error states and will wait for a stable state after any changes.
```
```release-note:enhancement
container: `google_container_node_pool` resources created in an error state will be marked as tainted on creation.
```
66 changes: 65 additions & 1 deletion google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,20 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e

log.Printf("[INFO] GKE NodePool %s has been created", nodePool.Name)

return resourceContainerNodePoolRead(d, meta)
if err = resourceContainerNodePoolRead(d, meta); err != nil {
return err
}

state, err := containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutCreate))
if err != nil {
return err
}

if containerNodePoolRestingStates[state] == ErrorState {
return fmt.Errorf("NodePool %s was created in the error state %q", nodePool.Name, state)
}

return nil
}

func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -327,12 +340,22 @@ func resourceContainerNodePoolUpdate(d *schema.ResourceData, meta interface{}) e
return err
}

_, err = containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}

d.Partial(true)
if err := nodePoolUpdate(d, meta, nodePoolInfo, "", timeoutInMinutes); err != nil {
return err
}
d.Partial(false)

_, err = containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return err
}

return resourceContainerNodePoolRead(d, meta)
}

Expand All @@ -346,6 +369,11 @@ func resourceContainerNodePoolDelete(d *schema.ResourceData, meta interface{}) e

name := getNodePoolName(d.Id())

_, err = containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}

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

mutexKV.Lock(nodePoolInfo.lockKey())
Expand Down Expand Up @@ -416,8 +444,13 @@ func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interfa
if err != nil {
return nil, err
}

d.SetId(id)

if _, err := containerNodePoolAwaitRestingState(config, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}

Expand Down Expand Up @@ -735,3 +768,34 @@ func getNodePoolName(id string) string {
splits := strings.Split(id, "/")
return splits[len(splits)-1]
}

var containerNodePoolRestingStates = RestingStates{
"RUNNING": ReadyState,
"RUNNING_WITH_ERROR": ErrorState,
"ERROR": ErrorState,
}

// takes in a config object, full node pool name, and the current CRUD action timeout
// returns a state with no error if the state is a resting state, and the last state with an error otherwise
func containerNodePoolAwaitRestingState(config *Config, name string, timeout time.Duration) (state string, err error) {
err = resource.Retry(timeout, func() *resource.RetryError {
nodePool, gErr := config.clientContainerBeta.Projects.Locations.Clusters.NodePools.Get(name).Do()
if gErr != nil {
return resource.NonRetryableError(gErr)
}

state = nodePool.Status
switch stateType := containerNodePoolRestingStates[state]; stateType {
case ReadyState:
log.Printf("[DEBUG] NodePool %q has status %q with message %q.", name, state, nodePool.StatusMessage)
return nil
case ErrorState:
log.Printf("[DEBUG] NodePool %q has error state %q with message %q.", name, state, nodePool.StatusMessage)
return nil
default:
return resource.RetryableError(fmt.Errorf("NodePool %q has state %q with message %q", name, state, nodePool.StatusMessage))
}
})

return state, err
}

0 comments on commit 31926fa

Please sign in to comment.