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

New feature flag reimage_on_manual_upgrade for virtual_machine_scale_set #22975

Merged
merged 1 commit into from
Feb 20, 2024
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
1 change: 1 addition & 0 deletions internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Default() UserFeatures {
},
VirtualMachineScaleSet: VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
Expand Down
1 change: 1 addition & 0 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type VirtualMachineFeatures struct {

type VirtualMachineScaleSetFeatures struct {
ForceDelete bool
ReimageOnManualUpgrade bool
RollInstancesWhenRequired bool
ScaleToZeroOnDelete bool
}
Expand Down
8 changes: 8 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
Optional: true,
Default: false,
},
"reimage_on_manual_upgrade": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"roll_instances_when_required": {
Type: pluginsdk.TypeBool,
Required: true,
Expand Down Expand Up @@ -438,6 +443,9 @@ func expandFeatures(input []interface{}) features.UserFeatures {
items := raw.([]interface{})
if len(items) > 0 {
scaleSetRaw := items[0].(map[string]interface{})
if v, ok := scaleSetRaw["reimage_on_manual_upgrade"]; ok {
featuresMap.VirtualMachineScaleSet.ReimageOnManualUpgrade = v.(bool)
}
if v, ok := scaleSetRaw["roll_instances_when_required"]; ok {
featuresMap.VirtualMachineScaleSet.RollInstancesWhenRequired = v.(bool)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func TestExpandFeatures(t *testing.T) {
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
Expand Down Expand Up @@ -146,6 +147,7 @@ func TestExpandFeatures(t *testing.T) {
},
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"reimage_on_manual_upgrade": true,
"roll_instances_when_required": true,
"force_delete": true,
"scale_to_zero_before_deletion": true,
Expand Down Expand Up @@ -200,6 +202,7 @@ func TestExpandFeatures(t *testing.T) {
SkipShutdownAndForceDelete: true,
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ForceDelete: true,
ScaleToZeroOnDelete: true,
Expand Down Expand Up @@ -280,6 +283,7 @@ func TestExpandFeatures(t *testing.T) {
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"reimage_on_manual_upgrade": false,
"roll_instances_when_required": false,
"scale_to_zero_before_deletion": false,
},
Expand Down Expand Up @@ -334,6 +338,7 @@ func TestExpandFeatures(t *testing.T) {
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: false,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: false,
},
Expand Down Expand Up @@ -924,6 +929,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
},
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
Expand All @@ -944,6 +950,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: true,
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: true,
},
Expand All @@ -964,6 +971,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
Expand All @@ -985,6 +993,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: true,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: false,
},
Expand All @@ -997,6 +1006,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"reimage_on_manual_upgrade": false,
"roll_instances_when_required": false,
"scale_to_zero_before_deletion": false,
},
Expand All @@ -1006,6 +1016,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
ReimageOnManualUpgrade: false,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ func resourceLinuxVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta i

metaData := virtualMachineScaleSetUpdateMetaData{
AutomaticOSUpgradeIsEnabled: automaticOSUpgradeIsEnabled,
CanReimageOnManualUpgrade: meta.(*clients.Client).Features.VirtualMachineScaleSet.ReimageOnManualUpgrade,
CanRollInstancesWhenRequired: meta.(*clients.Client).Features.VirtualMachineScaleSet.RollInstancesWhenRequired,
UpdateInstances: updateInstances,
Client: meta.(*clients.Client).Compute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,28 @@ func TestAccLinuxVirtualMachineScaleSet_otherCancelRollingUpgrades(t *testing.T)
})
}

func TestAccLinuxVirtualMachineScaleSet_otherDisableReimageOnManualUpgrade(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine_scale_set", "test")
r := LinuxVirtualMachineScaleSetResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.otherDisableReimageOnManualUpgrade(data, "Standard_F2"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("admin_password"),
{
Config: r.otherDisableReimageOnManualUpgrade(data, "Standard_F4"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep("admin_password"),
})
}

func (r LinuxVirtualMachineScaleSetResource) otherBootDiagnostics(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down Expand Up @@ -3401,3 +3423,54 @@ resource "azurerm_gallery_application_version" "test" {

`, r.template(data), data.RandomString, data.RandomInteger)
}

func (r LinuxVirtualMachineScaleSetResource) otherDisableReimageOnManualUpgrade(data acceptance.TestData, sku string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
virtual_machine_scale_set {
reimage_on_manual_upgrade = false
roll_instances_when_required = true
}
}
}

%s

resource "azurerm_linux_virtual_machine_scale_set" "test" {
name = "acctestvmss-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "%s"
instances = 1
admin_username = "adminuser"
admin_password = "P@ssword1234!"
upgrade_mode = "Manual"

disable_password_authentication = false

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}

network_interface {
name = "example"
primary = true

ip_configuration {
name = "internal"
primary = true
subnet_id = azurerm_subnet.test.id
}
}
}
`, r.template(data), data.RandomInteger, sku)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,8 @@ func resourceOrchestratedVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData,

// AutomaticOSUpgradeIsEnabled currently is not supported in orchestrated VMSS flex
metaData := virtualMachineScaleSetUpdateMetaData{
AutomaticOSUpgradeIsEnabled: false,
// CanRollInstancesWhenRequired: meta.(*clients.Client).Features.VirtualMachineScaleSet.RollInstancesWhenRequired,
// UpdateInstances: updateInstances,
AutomaticOSUpgradeIsEnabled: false,
CanReimageOnManualUpgrade: false,
CanRollInstancesWhenRequired: false,
UpdateInstances: false,
Client: meta.(*clients.Client).Compute,
Expand Down
28 changes: 16 additions & 12 deletions internal/services/compute/virtual_machine_scale_set_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type virtualMachineScaleSetUpdateMetaData struct {
// is "automaticOSUpgrade" enable in the upgradeProfile block
AutomaticOSUpgradeIsEnabled bool

// can we reimage instances when `upgrade_mode` is set to `Manual`? this is a feature toggle
CanReimageOnManualUpgrade bool

// can we roll instances if we need too? this is a feature toggle
CanRollInstancesWhenRequired bool

Expand Down Expand Up @@ -170,20 +173,21 @@ func (metadata virtualMachineScaleSetUpdateMetaData) upgradeInstancesForManualUp
}
log.Printf("[DEBUG] Updated Instance %q to the Latest Configuration.", instanceId)

// TODO: does this want to be a separate, user-configurable toggle?
log.Printf("[DEBUG] Reimaging Instance %q..", instanceId)
reimageInput := &compute.VirtualMachineScaleSetReimageParameters{
InstanceIds: &instanceIds,
}
reimageFuture, err := client.Reimage(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, reimageInput)
if err != nil {
return fmt.Errorf("reimaging Instance %q (%s %s): %+v", instanceId, metadata.OSType, id, err)
}
if metadata.CanReimageOnManualUpgrade {
log.Printf("[DEBUG] Reimaging Instance %q..", instanceId)
reimageInput := &compute.VirtualMachineScaleSetReimageParameters{
InstanceIds: &instanceIds,
}
reimageFuture, err := client.Reimage(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, reimageInput)
if err != nil {
return fmt.Errorf("reimaging Instance %q (%s %s): %+v", instanceId, metadata.OSType, id, err)
}

if err = reimageFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for reimage of Instance %q (%s %s): %+v", instanceId, metadata.OSType, id, err)
if err = reimageFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for reimage of Instance %q (%s %s): %+v", instanceId, metadata.OSType, id, err)
}
log.Printf("[DEBUG] Reimaged Instance %q..", instanceId)
}
log.Printf("[DEBUG] Reimaged Instance %q..", instanceId)
}

log.Printf("[DEBUG] Rolled the VM Instances for %s %s.", metadata.OSType, id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ func resourceWindowsVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta

metaData := virtualMachineScaleSetUpdateMetaData{
AutomaticOSUpgradeIsEnabled: automaticOSUpgradeIsEnabled,
CanReimageOnManualUpgrade: meta.(*clients.Client).Features.VirtualMachineScaleSet.ReimageOnManualUpgrade,
CanRollInstancesWhenRequired: meta.(*clients.Client).Features.VirtualMachineScaleSet.RollInstancesWhenRequired,
UpdateInstances: updateInstances,
Client: meta.(*clients.Client).Compute,
Expand Down
2 changes: 2 additions & 0 deletions website/docs/guides/features-block.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ The `virtual_machine_scale_set` block supports the following:

~> **Note:** Support for Force Delete is in an opt-in Preview.

* `reimage_on_manual_upgrade` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically reimage during the update the instances in the Scale Set when `upgrade_mode` is `Manual`. Defaults to `true`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst this feature flag is fine, we should add a note calling out that doing so means that Terraform can't complete the full update in all cases:

Suggested change
* `reimage_on_manual_upgrade` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically reimage during the update the instances in the Scale Set when `upgrade_mode` is `Manual`. Defaults to `true`.
* `reimage_on_manual_upgrade` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically reimage during the update the instances in the Scale Set when `upgrade_mode` is `Manual`. Defaults to `true`.
-> **Note:** When `reimage_on_manual_upgrade` is set to `false` additional steps may be required in order to use the new configuration - typically these reuqire rolling the images within the Virtual Machine Scale Set.


* `roll_instances_when_required` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically roll the instances in the Scale Set when Required (for example when updating the Sku/Image). Defaults to `true`.

* `scale_to_zero_before_deletion` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources scale to 0 instances before deleting the resource. Defaults to `true`.
Loading