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 - add feature for scale_in_on_delete #13635

Merged
merged 2 commits into from
Oct 26, 2021
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 @@ -33,6 +33,7 @@ func Default() UserFeatures {
VirtualMachineScaleSet: VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
}
}
1 change: 1 addition & 0 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type VirtualMachineFeatures struct {
type VirtualMachineScaleSetFeatures struct {
ForceDelete bool
RollInstancesWhenRequired bool
ScaleToZeroOnDelete bool
}

type KeyVaultFeatures struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
Type: pluginsdk.TypeBool,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

outside of the scope of this PR, but I'm surprised that this is required - this should be Optional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something else I noticed is that force_delete doesn't appear to actually be implemented

},
"scale_to_zero_before_deletion": {
Type: pluginsdk.TypeBool,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -277,6 +281,9 @@ func expandFeatures(input []interface{}) features.UserFeatures {
if v, ok := scaleSetRaw["force_delete"]; ok {
features.VirtualMachineScaleSet.ForceDelete = v.(bool)
}
if v, ok := scaleSetRaw["scale_to_zero_before_deletion"]; ok {
features.VirtualMachineScaleSet.ScaleToZeroOnDelete = v.(bool)
}
}
}

Expand Down
45 changes: 38 additions & 7 deletions internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestExpandFeatures(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
ResourceGroup: features.ResourceGroupFeatures{
PreventDeletionIfContainsResources: false,
Expand Down Expand Up @@ -100,8 +101,9 @@ func TestExpandFeatures(t *testing.T) {
},
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"roll_instances_when_required": true,
"force_delete": true,
"roll_instances_when_required": true,
"force_delete": true,
"scale_to_zero_before_deletion": true,
},
},
},
Expand Down Expand Up @@ -137,6 +139,7 @@ func TestExpandFeatures(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
RollInstancesWhenRequired: true,
ForceDelete: true,
ScaleToZeroOnDelete: true,
},
},
},
Expand Down Expand Up @@ -189,8 +192,9 @@ func TestExpandFeatures(t *testing.T) {
},
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"roll_instances_when_required": false,
"force_delete": false,
"roll_instances_when_required": false,
"scale_to_zero_before_deletion": false,
},
},
},
Expand Down Expand Up @@ -226,6 +230,7 @@ func TestExpandFeatures(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: false,
},
},
},
Expand Down Expand Up @@ -705,6 +710,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
},
},
Expand All @@ -724,6 +730,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: true,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: true,
},
},
},
Expand All @@ -743,6 +750,28 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: true,
},
},
},
{
Name: "Scale In On Delete Disabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"roll_instances_when_required": true,
"scale_to_zero_before_deletion": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
ScaleToZeroOnDelete: false,
},
},
},
Expand All @@ -752,8 +781,9 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
map[string]interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"roll_instances_when_required": false,
"force_delete": false,
"roll_instances_when_required": false,
"scale_to_zero_before_deletion": false,
},
},
},
Expand All @@ -762,6 +792,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: false,
ScaleToZeroOnDelete: false,
},
},
},
Expand All @@ -771,7 +802,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
t.Logf("[DEBUG] Test Case: %q", testCase.Name)
result := expandFeatures(testCase.Input)
if !reflect.DeepEqual(result.VirtualMachineScaleSet, testCase.Expected.VirtualMachineScaleSet) {
t.Fatalf("Expected %+v but got %+v", result.VirtualMachineScaleSet, testCase.Expected.VirtualMachineScaleSet)
t.Fatalf("Expected %+v but got %+v", testCase.Expected.VirtualMachineScaleSet, result.VirtualMachineScaleSet)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i
// Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet internal is in use by
// /{nicResourceID}/|providers|Microsoft.Compute|virtualMachineScaleSets|acctestvmss-190923101253410278|virtualMachines|0|networkInterfaces|example/ipConfigurations/internal and cannot be deleted.
// In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
if resp.Sku != nil {
scaleToZeroOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleToZeroOnDelete
if scaleToZeroOnDelete && resp.Sku != nil {
resp.Sku.Capacity = utils.Int64(int64(0))

log.Printf("[DEBUG] Scaling instances to 0 prior to deletion - this helps avoids networking issues within Azure")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,8 @@ func resourceWindowsVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta
// Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet internal is in use by
// /{nicResourceID}/|providers|Microsoft.Compute|virtualMachineScaleSets|acctestvmss-190923101253410278|virtualMachines|0|networkInterfaces|example/ipConfigurations/internal and cannot be deleted.
// In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
if resp.Sku != nil {
scaleToZeroOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleToZeroOnDelete
if scaleToZeroOnDelete && resp.Sku != nil {
resp.Sku.Capacity = utils.Int64(int64(0))

log.Printf("[DEBUG] Scaling instances to 0 prior to deletion - this helps avoids networking issues within Azure")
Expand Down
4 changes: 3 additions & 1 deletion website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ The `virtual_machine` block supports the following:

~> **Note:** When using a graceful shutdown, Azure gives the Virtual Machine a 5 minutes window in which to complete the shutdown process, at which point the machine will be force powered off - [more information can be found in this blog post](https://azure.microsoft.com/en-us/blog/linux-and-graceful-shutdowns-2/).

* `skip_shutdown_and_force_delete` - Should the `azurerm_linux_virtual_machine` and `azurerm_windows_virtual_machine` skip the shutdown command and `Force Delete`, this provides the ability to forcefully and immediately delete the VM and detach all sub-resources associated with the virtual machine. This allows those freed resources to be reattached to another VM instance or deleted. Defaults to `false`.
* `skip_shutdown_and_force_delete` - Should the `azurerm_linux_virtual_machine` and `azurerm_windows_virtual_machine` skip the shutdown command and `Force Delete`, this provides the ability to forcefully and immediately delete the VM and detach all sub-resources associated with the virtual machine. This allows those freed resources to be reattached to another VM instance or deleted. Defaults to `false`.

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

Expand All @@ -254,3 +254,5 @@ The `virtual_machine_scale_set` block supports the following:
~> **Note:** Support for Force Delete is in an opt-in Preview.

* `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`.