-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Rolling update support for instance group manager #1137
Merged
nat-henderson
merged 3 commits into
hashicorp:master
from
wayfair-archive:ishashchuk_rollingRestarts
Mar 15, 2018
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ import ( | |
) | ||
|
||
var InstanceGroupManagerBaseApiVersion = v1 | ||
var InstanceGroupManagerVersionedFeatures = []Feature{Feature{Version: v0beta, Item: "auto_healing_policies"}} | ||
var InstanceGroupManagerVersionedFeatures = []Feature{ | ||
Feature{Version: v0beta, Item: "auto_healing_policies"}, | ||
Feature{Version: v0beta, Item: "rolling_update_policy"}, | ||
} | ||
|
||
func resourceComputeInstanceGroupManager() *schema.Resource { | ||
return &schema.Resource{ | ||
|
@@ -102,7 +105,7 @@ func resourceComputeInstanceGroupManager() *schema.Resource { | |
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "RESTART", | ||
ValidateFunc: validation.StringInSlice([]string{"RESTART", "NONE"}, false), | ||
ValidateFunc: validation.StringInSlice([]string{"RESTART", "NONE", "ROLLING_UPDATE"}, false), | ||
}, | ||
|
||
"target_pools": &schema.Schema{ | ||
|
@@ -140,6 +143,60 @@ func resourceComputeInstanceGroupManager() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
"rolling_update_policy": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"minimal_action": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"RESTART", "REPLACE"}, false), | ||
}, | ||
|
||
"type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"OPPORTUNISTIC", "PROACTIVE"}, false), | ||
}, | ||
|
||
"max_surge_fixed": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
ConflictsWith: []string{"rolling_update_policy.0.max_surge_percent"}, | ||
}, | ||
|
||
"max_surge_percent": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ConflictsWith: []string{"rolling_update_policy.0.max_surge_fixed"}, | ||
ValidateFunc: validation.IntBetween(0, 100), | ||
}, | ||
|
||
"max_unavailable_fixed": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
ConflictsWith: []string{"rolling_update_policy.0.max_unavailable_percent"}, | ||
}, | ||
|
||
"max_unavailable_percent": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ConflictsWith: []string{"rolling_update_policy.0.max_unavailable_fixed"}, | ||
ValidateFunc: validation.IntBetween(0, 100), | ||
}, | ||
|
||
"min_ready_sec": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ValidateFunc: validation.IntBetween(0, 3600), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -184,6 +241,12 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte | |
return err | ||
} | ||
|
||
_, ok := d.GetOk("rolling_update_policy") | ||
|
||
if d.Get("update_strategy") == "ROLLING_UPDATE" && !ok { | ||
return fmt.Errorf("[rolling_update_policy] must be set when 'update_strategy' is set to 'ROLLING_UPDATE'") | ||
} | ||
|
||
// Build the parameter | ||
manager := &computeBeta.InstanceGroupManager{ | ||
Name: d.Get("name").(string), | ||
|
@@ -380,6 +443,11 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte | |
|
||
d.Partial(true) | ||
|
||
_, ok := d.GetOk("rolling_update_policy") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment - include in the if statement if possible. |
||
if d.Get("update_strategy") == "ROLLING_UPDATE" && !ok { | ||
return fmt.Errorf("[rolling_update_policy] must be set when 'update_strategy' is set to 'ROLLING_UPDATE'") | ||
} | ||
|
||
// If target_pools changes then update | ||
if d.HasChange("target_pools") { | ||
targetPools := convertStringSet(d.Get("target_pools").(*schema.Set)) | ||
|
@@ -536,6 +604,23 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte | |
} | ||
} | ||
|
||
if d.Get("update_strategy").(string) == "ROLLING_UPDATE" { | ||
manager := &computeBeta.InstanceGroupManager{ | ||
UpdatePolicy: expandUpdatePolicy(d.Get("rolling_update_policy").([]interface{})), | ||
} | ||
|
||
op, err = config.clientComputeBeta.InstanceGroupManagers.Patch( | ||
project, zone, d.Id(), manager).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error updating managed group instances: %s", err) | ||
} | ||
|
||
err = computeSharedOperationWait(config.clientCompute, op, project, "Updating managed group instances") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
d.SetPartial("instance_template") | ||
} | ||
|
||
|
@@ -732,6 +817,44 @@ func expandAutoHealingPolicies(configured []interface{}) []*computeBeta.Instance | |
return autoHealingPolicies | ||
} | ||
|
||
func expandUpdatePolicy(configured []interface{}) *computeBeta.InstanceGroupManagerUpdatePolicy { | ||
updatePolicy := &computeBeta.InstanceGroupManagerUpdatePolicy{} | ||
|
||
for _, raw := range configured { | ||
data := raw.(map[string]interface{}) | ||
|
||
updatePolicy.MinimalAction = data["minimal_action"].(string) | ||
updatePolicy.Type = data["type"].(string) | ||
|
||
// percent and fixed values are conflicting | ||
// when the percent values are set, the fixed values will be ignored | ||
if v := data["max_surge_percent"]; v.(int) > 0 { | ||
updatePolicy.MaxSurge = &computeBeta.FixedOrPercent{ | ||
Percent: int64(v.(int)), | ||
} | ||
} else { | ||
updatePolicy.MaxSurge = &computeBeta.FixedOrPercent{ | ||
Fixed: int64(data["max_surge_fixed"].(int)), | ||
} | ||
} | ||
|
||
if v := data["max_unavailable_percent"]; v.(int) > 0 { | ||
updatePolicy.MaxUnavailable = &computeBeta.FixedOrPercent{ | ||
Percent: int64(v.(int)), | ||
} | ||
} else { | ||
updatePolicy.MaxUnavailable = &computeBeta.FixedOrPercent{ | ||
Fixed: int64(data["max_unavailable_fixed"].(int)), | ||
} | ||
} | ||
|
||
if v, ok := data["min_ready_sec"]; ok { | ||
updatePolicy.MinReadySec = int64(v.(int)) | ||
} | ||
} | ||
return updatePolicy | ||
} | ||
|
||
func flattenAutoHealingPolicies(autoHealingPolicies []*computeBeta.InstanceGroupManagerAutoHealingPolicy) []map[string]interface{} { | ||
autoHealingPoliciesSchema := make([]map[string]interface{}, 0, len(autoHealingPolicies)) | ||
for _, autoHealingPolicy := range autoHealingPolicies { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Seems like it'd be neater as part of the if block below.