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

VMSS: Enhance Upgrade Policy with Automatic OS upgrades and Rolling Upgrade Policy support #922

Merged
merged 22 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
83b20f2
vmss: add automatic os upgrades, network profile health probe, and ro…
agolomoodysaada Mar 2, 2018
979f86a
VMSS: updates to fix PR comments
katbyte Jun 6, 2018
1efd35b
Merge remote-tracking branch 'origin/master' into upgrade_policy_rolling
katbyte Jun 7, 2018
3df1cbf
Cleanup after merge
katbyte Jun 7, 2018
1cf7591
Merge branch 'master' into upgrade_policy_rolling
katbyte Jun 12, 2018
eae3c87
Fixed vmss example varible defaults
katbyte Jun 13, 2018
5c961ee
Add test for rolling autoupdate
julienstroheker Jun 15, 2018
e113177
Merge pull request #1 from julienstroheker/upgrade_policy_rolling
agolomoodysaada Jun 18, 2018
054a76b
Introduce Action Group resource of Azure Monitor (#1419)
Aug 1, 2018
ca40c07
Update CHANGELOG.md (#1703)
WodansSon Aug 1, 2018
f1a390b
Resolve conflict with upstream master
Aug 1, 2018
d3ba7f3
Resolve conflict with master branch, again
Aug 3, 2018
e90823c
Merge branch 'master' into upgrade_policy_rolling
Aug 3, 2018
990cb6f
Remove wrong updates due to the hard reset on master
Aug 4, 2018
c5186a2
Add validations to the new properties.
Aug 4, 2018
5c2e8dc
Regroup the schema related to each other.
Aug 4, 2018
648b96c
Fix destruction issue in the test case
Aug 7, 2018
017aadd
Add nil check to fix all test cases
Aug 10, 2018
7743a77
Fix code according to the feedback
Aug 11, 2018
01406cd
Add test case to update rolling upgrade policy
Sep 28, 2018
8489acb
Resolve conflict with upstream branch
Sep 28, 2018
477b881
Fix rolling_upgrade_policy diff issue when mode is not rolling
Oct 1, 2018
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
141 changes: 137 additions & 4 deletions azurerm/resource_arm_virtual_machine_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log"
"strings"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-06-01/compute"

Expand Down Expand Up @@ -119,6 +120,57 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
string(compute.Manual),
string(compute.Rolling),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"health_probe_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
},

"automatic_os_upgrade": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"rolling_upgrade_policy": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_batch_instance_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 20,
ValidateFunc: validation.IntBetween(5, 100),
},

"max_unhealthy_instance_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 20,
ValidateFunc: validation.IntBetween(5, 100),
},

"max_unhealthy_upgraded_instance_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 20,
ValidateFunc: validation.IntBetween(5, 100),
},

"pause_time_between_batches": {
Type: schema.TypeString,
Optional: true,
Default: "PT0S",
ValidateFunc: validateIso8601Duration(),
},
},
},
DiffSuppressFunc: azureRmVirtualMachineScaleSetSuppressRollingUpgradePolicyDiff,
},

"overprovision": {
Expand Down Expand Up @@ -659,6 +711,8 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {

"tags": tagsSchema(),
},

CustomizeDiff: azureRmVirtualMachineScaleSetCustomizeDiff,
}
}

Expand Down Expand Up @@ -712,14 +766,17 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
return err
}

updatePolicy := d.Get("upgrade_policy_mode").(string)
upgradePolicy := d.Get("upgrade_policy_mode").(string)
automaticOsUpgrade := d.Get("automatic_os_upgrade").(bool)
overprovision := d.Get("overprovision").(bool)
singlePlacementGroup := d.Get("single_placement_group").(bool)
priority := d.Get("priority").(string)

scaleSetProps := compute.VirtualMachineScaleSetProperties{
UpgradePolicy: &compute.UpgradePolicy{
Mode: compute.UpgradeMode(updatePolicy),
Mode: compute.UpgradeMode(upgradePolicy),
AutomaticOSUpgrade: utils.Bool(automaticOsUpgrade),
RollingUpgradePolicy: expandAzureRmRollingUpgradePolicy(d),
},
VirtualMachineProfile: &compute.VirtualMachineScaleSetVMProfile{
NetworkProfile: expandAzureRmVirtualMachineScaleSetNetworkProfile(d),
Expand All @@ -737,6 +794,12 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
scaleSetProps.VirtualMachineProfile.DiagnosticsProfile = &diagnosticProfile
}

if v, ok := d.GetOk("health_probe_id"); ok {
scaleSetProps.VirtualMachineProfile.NetworkProfile.HealthProbe = &compute.APIEntityReference{
ID: utils.String(v.(string)),
}
}

properties := compute.VirtualMachineScaleSet{
Name: &name,
Location: &location,
Expand Down Expand Up @@ -823,9 +886,15 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
}

if properties := resp.VirtualMachineScaleSetProperties; properties != nil {

if upgradePolicy := properties.UpgradePolicy; upgradePolicy != nil {
d.Set("upgrade_policy_mode", upgradePolicy.Mode)
d.Set("automatic_os_upgrade", upgradePolicy.AutomaticOSUpgrade)

if rollingUpgradePolicy := upgradePolicy.RollingUpgradePolicy; rollingUpgradePolicy != nil {
if err := d.Set("rolling_upgrade_policy", flattenAzureRmVirtualMachineScaleSetRollingUpgradePolicy(rollingUpgradePolicy)); err != nil {
return fmt.Errorf("[DEBUG] Error setting Virtual Machine Scale Set Rolling Upgrade Policy error: %#v", err)
}
}
}
d.Set("overprovision", properties.Overprovision)
d.Set("single_placement_group", properties.SinglePlacementGroup)
Expand All @@ -852,7 +921,6 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
if err := d.Set("os_profile_secrets", flattenedSecrets); err != nil {
return fmt.Errorf("[DEBUG] Error setting `os_profile_secrets`: %#v", err)
}

}

if windowsConfiguration := osProfile.WindowsConfiguration; windowsConfiguration != nil {
Expand All @@ -874,6 +942,12 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
}

if networkProfile := profile.NetworkProfile; networkProfile != nil {
if hp := networkProfile.HealthProbe; hp != nil {
if id := hp.ID; id != nil {
d.Set("health_probe_id", id)
}
}

flattenedNetworkProfile := flattenAzureRmVirtualMachineScaleSetNetworkProfile(networkProfile)
if err := d.Set("network_profile", flattenedNetworkProfile); err != nil {
return fmt.Errorf("[DEBUG] Error setting `network_profile`: %#v", err)
Expand Down Expand Up @@ -913,6 +987,7 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
}
}
}

}

if plan := resp.Plan; plan != nil {
Expand Down Expand Up @@ -1088,6 +1163,25 @@ func flattenAzureRmVirtualMachineScaleSetBootDiagnostics(bootDiagnostic *compute
return []interface{}{b}
}

func flattenAzureRmVirtualMachineScaleSetRollingUpgradePolicy(rollingUpgradePolicy *compute.RollingUpgradePolicy) []interface{} {
b := make(map[string]interface{}, 0)

if v := rollingUpgradePolicy.MaxBatchInstancePercent; v != nil {
b["max_batch_instance_percent"] = *v
}
if v := rollingUpgradePolicy.MaxUnhealthyInstancePercent; v != nil {
b["max_unhealthy_instance_percent"] = *v
}
if v := rollingUpgradePolicy.MaxUnhealthyUpgradedInstancePercent; v != nil {
b["max_unhealthy_upgraded_instance_percent"] = *v
}
if v := rollingUpgradePolicy.PauseTimeBetweenBatches; v != nil {
b["pause_time_between_batches"] = *v
}

return []interface{}{b}
}

func flattenAzureRmVirtualMachineScaleSetNetworkProfile(profile *compute.VirtualMachineScaleSetNetworkProfile) []map[string]interface{} {
networkConfigurations := profile.NetworkInterfaceConfigurations
result := make([]map[string]interface{}, 0, len(*networkConfigurations))
Expand Down Expand Up @@ -1446,6 +1540,19 @@ func expandVirtualMachineScaleSetSku(d *schema.ResourceData) (*compute.Sku, erro
return sku, nil
}

func expandAzureRmRollingUpgradePolicy(d *schema.ResourceData) *compute.RollingUpgradePolicy {
if config, ok := d.GetOk("rolling_upgrade_policy.0"); ok {
policy := config.(map[string]interface{})
return &compute.RollingUpgradePolicy{
MaxBatchInstancePercent: utils.Int32(int32(policy["max_batch_instance_percent"].(int))),
MaxUnhealthyInstancePercent: utils.Int32(int32(policy["max_unhealthy_instance_percent"].(int))),
MaxUnhealthyUpgradedInstancePercent: utils.Int32(int32(policy["max_unhealthy_upgraded_instance_percent"].(int))),
PauseTimeBetweenBatches: utils.String(policy["pause_time_between_batches"].(string)),
}
}
return nil
}

func expandAzureRmVirtualMachineScaleSetNetworkProfile(d *schema.ResourceData) *compute.VirtualMachineScaleSetNetworkProfile {
scaleSetNetworkProfileConfigs := d.Get("network_profile").(*schema.Set).List()
networkProfileConfig := make([]compute.VirtualMachineScaleSetNetworkConfiguration, 0, len(scaleSetNetworkProfileConfigs))
Expand Down Expand Up @@ -2024,3 +2131,29 @@ func flattenAzureRmVirtualMachineScaleSetPlan(plan *compute.Plan) []interface{}

return []interface{}{result}
}

// When upgrade_policy_mode is not Rolling, we will just ignore rolling_upgrade_policy (returns true).
func azureRmVirtualMachineScaleSetSuppressRollingUpgradePolicyDiff(k, old, new string, d *schema.ResourceData) bool {
if k == "rolling_upgrade_policy.#" && new == "0" {
return strings.ToLower(d.Get("upgrade_policy_mode").(string)) != "rolling"
}
return false
}

// Make sure rolling_upgrade_policy is default value when upgrade_policy_mode is not Rolling.
func azureRmVirtualMachineScaleSetCustomizeDiff(d *schema.ResourceDiff, _ interface{}) error {
mode := d.Get("upgrade_policy_mode").(string)
if strings.ToLower(mode) != "rolling" {
if policyRaw, ok := d.GetOk("rolling_upgrade_policy.0"); ok {
policy := policyRaw.(map[string]interface{})
isDefault := (policy["max_batch_instance_percent"].(int) == 20) &&
(policy["max_unhealthy_instance_percent"].(int) == 20) &&
(policy["max_unhealthy_upgraded_instance_percent"].(int) == 20) &&
(policy["pause_time_between_batches"] == "PT0S")
if !isDefault {
return fmt.Errorf("If `upgrade_policy_mode` is `%s`, `rolling_upgrade_policy` must be removed or set to default values", mode)
}
}
}
return nil
}
Loading