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

Cancel RollingUpgrades before deleting VMSS resources #15634

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
268cff5
New datasource azurerm_managed_disk_export for managed disks export
Feb 22, 2022
2c87c9e
Formatting tf and website files to meet lint needs
Feb 22, 2022
77d0084
Adding azurerm_managed_disk_export_revoke data source to revoke expor…
harshavmb Feb 23, 2022
c7c011d
Adding err return value for diskRevokeFuture.WaitForCompletionRef func
harshavmb Feb 23, 2022
0234202
Merge branch 'hashicorp:main' into main
harshavmb Feb 23, 2022
da3b6f3
New resource azurerm_disk_export to manage disk exports instead of da…
harshavmb Feb 25, 2022
5cbf828
Merge branch 'hashicorp:main' into vmss-rollingupgrades-cancel-destroy
harshavmb Feb 28, 2022
0e7be9b
Merge branch 'hashicorp:main' into main
harshavmb Feb 28, 2022
e63865c
Merge branch 'vmss-rollingupgrades-cancel-destroy' of https://github.…
harshavmb Feb 28, 2022
db1caa8
New datasource azurerm_managed_disk_export for managed disks export
Feb 22, 2022
0b6e446
Formatting tf and website files to meet lint needs
Feb 22, 2022
84e0fb9
Adding azurerm_managed_disk_export_revoke data source to revoke expor…
harshavmb Feb 23, 2022
06d000f
Adding err return value for diskRevokeFuture.WaitForCompletionRef func
harshavmb Feb 23, 2022
bb449bf
New resource azurerm_disk_export to manage disk exports instead of da…
harshavmb Feb 25, 2022
470f6d5
Merge branch 'vmss-rollingupgrades-cancel-destroy' of https://github.…
harshavmb Feb 28, 2022
832296c
Cancel Rolling Upgrades of VM scalesets before deleting them during d…
harshavmb Feb 28, 2022
f493fe3
Catching error return values as per golangci-lint
harshavmb Mar 1, 2022
03653e5
Merge branch 'hashicorp:main' into main
harshavmb Mar 2, 2022
a7cd2be
Merge branch 'hashicorp:main' into main
harshavmb Apr 22, 2022
948a3f8
Merge branch 'main' of https://github.com/hashicorp/terraform-provide…
harshavmb Apr 22, 2022
5bce212
Merge branch 'main' of https://github.com/harshavmb/terraform-provide…
harshavmb Apr 22, 2022
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
30 changes: 30 additions & 0 deletions internal/services/compute/client/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package client

import (
"context"
"fmt"
"log"
"net/http"
"strings"
)

func (c *Client) CancelRollingUpgradesBeforeDeletion(ctx context.Context, resourceGroupName string, vmScaleSetName string) error {
future, err := c.VMScaleSetRollingUpgradesClient.Cancel(ctx, resourceGroupName, vmScaleSetName)

// If rolling upgrades haven't been run (when VMSS are just provisioned with rolling upgrades but no extensions, auto-scaling are run )
// we can not cancel rolling upgrades
// API call :: GET https://management.azure.com/subscriptions/{subId}/resourceGroups/{rgName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmSSName}/rollingUpgrades/latest?api-version=2021-07-01
// Azure API throws 409 conflict error saying "The entity was not found in this Azure location."
// If the above error message matches, we identify and move forward to delete the VMSS
// in all other cases, it just cancels the rolling upgrades and move ahead to delete the VMSS
if err != nil && !(future.Response().StatusCode == http.StatusConflict && strings.Contains(err.Error(), "There is no ongoing Rolling Upgrade to cancel.")) {
return fmt.Errorf("error cancelling rolling upgrades of Virtual Machine Scale Set %q (Resource Group %q): %+v", vmScaleSetName, resourceGroupName, err)
}

log.Printf("[DEBUG] Waiting for deletion of Virtual Machine Scale Set %q (Resource Group %q)..", vmScaleSetName, resourceGroupName)
if err := future.WaitForCompletionRef(ctx, c.VMScaleSetExtensionsClient.Client); err != nil && !(future.Response().StatusCode == http.StatusConflict && strings.Contains(err.Error(), "There is no ongoing Rolling Upgrade to cancel.")) {
return fmt.Errorf("waiting for cancelling rolling upgrades of Virtual Machine Scale Set %q (Resource Group %q): %+v", vmScaleSetName, resourceGroupName, err)
}
log.Printf("[DEBUG] cancelled Virtual Machine Scale Set Rolling Upgrades %q (Resource Group %q).", vmScaleSetName, resourceGroupName)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func resourceLinuxVirtualMachineScaleSet() *pluginsdk.Resource {

"boot_diagnostics": bootDiagnosticsSchema(),

"cancel_rolling_upgrades_before_deletion": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"computer_name_prefix": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -1153,6 +1159,14 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i
return err
}

cancelRollingUpgradesBeforeDeletion := d.Get("cancel_rolling_upgrades_before_deletion").(bool)
if cancelRollingUpgradesBeforeDeletion {
err := meta.(*clients.Client).Compute.CancelRollingUpgradesBeforeDeletion(ctx, id.ResourceGroup, id.Name)
if err != nil {
return err
}
}

// Upgrading to the 2021-07-01 exposed a new expand parameter to the GET method
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ func resourceVirtualMachineScaleSetExtension() *pluginsdk.Resource {
ValidateFunc: validation.StringIsJSON,
DiffSuppressFunc: pluginsdk.SuppressJsonDiff,
},

"cancel_rolling_upgrades_before_deletion": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
},
}
}
Expand Down Expand Up @@ -341,6 +347,14 @@ func resourceVirtualMachineScaleSetExtensionDelete(d *pluginsdk.ResourceData, me
return err
}

cancelRollingUpgradesBeforeDeletion := d.Get("cancel_rolling_upgrades_before_deletion").(bool)
if cancelRollingUpgradesBeforeDeletion {
err := meta.(*clients.Client).Compute.CancelRollingUpgradesBeforeDeletion(ctx, id.ResourceGroup, id.VirtualMachineScaleSetName)
if err != nil {
return err
}
}

future, err := client.Delete(ctx, id.ResourceGroup, id.VirtualMachineScaleSetName, id.ExtensionName)
if err != nil {
return fmt.Errorf("deleting Extension %q (Virtual Machine Scale Set %q / Resource Group %q): %+v", id.ExtensionName, id.VirtualMachineScaleSetName, id.ResourceGroup, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ func resourceWindowsVirtualMachineScaleSet() *pluginsdk.Resource {

"boot_diagnostics": bootDiagnosticsSchema(),

"cancel_rolling_upgrades_before_deletion": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"computer_name_prefix": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -1219,6 +1225,14 @@ func resourceWindowsVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta
return err
}

cancelRollingUpgradesBeforeDeletion := d.Get("cancel_rolling_upgrades_before_deletion").(bool)
if cancelRollingUpgradesBeforeDeletion {
err := meta.(*clients.Client).Compute.CancelRollingUpgradesBeforeDeletion(ctx, id.ResourceGroup, id.Name)
if err != nil {
return err
}
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ The following arguments are supported:

* `boot_diagnostics` - (Optional) A `boot_diagnostics` block as defined below.

* `cancel_rolling_upgrades_before_deletion` - (Optional) Cancel Rolling Upgrades before deleting the VM ScaleSets during destroy phase. Defaults to false.

* `computer_name_prefix` - (Optional) The prefix which should be used for the name of the Virtual Machines in this Scale Set. If unspecified this defaults to the value for the `name` field. If the value of the `name` field is not a valid `computer_name_prefix`, then you must specify `computer_name_prefix`.

* `custom_data` - (Optional) The Base64-Encoded Custom Data which should be used for this Virtual Machine Scale Set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The following arguments are supported:

* `type_handler_version` - (Required) Specifies the version of the extension to use, available versions can be found using the Azure CLI.

* `cancel_rolling_upgrades_before_deletion` - (Optional) Cancel Rolling Upgrades before deleting the VMSS extensions during destroy phase. Defaults to false.

~> **Note:** The `Publisher` and `Type` of Virtual Machine Scale Set Extensions can be found using the Azure CLI, via:

```shell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ The following arguments are supported:

* `boot_diagnostics` - (Optional) A `boot_diagnostics` block as defined below.

* `cancel_rolling_upgrades_before_deletion` - (Optional) Cancel Rolling Upgrades before deleting the VM ScaleSets during destroy phase. Defaults to false.

* `computer_name_prefix` - (Optional) The prefix which should be used for the name of the Virtual Machines in this Scale Set. If unspecified this defaults to the value for the `name` field. If the value of the `name` field is not a valid `computer_name_prefix`, then you must specify `computer_name_prefix`.

* `custom_data` - (Optional) The Base64-Encoded Custom Data which should be used for this Virtual Machine Scale Set.
Expand Down