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

Add support for no-downtime disk resizes. #17245

Merged
merged 21 commits into from
Nov 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func Default() UserFeatures {
LogAnalyticsWorkspace: LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: true,
},
ManagedDisk: ManagedDiskFeatures{
ExpandWithoutDowntime: true,
},
ResourceGroup: ResourceGroupFeatures{
PreventDeletionIfContainsResources: true,
},
Expand Down
5 changes: 5 additions & 0 deletions internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type UserFeatures struct {
TemplateDeployment TemplateDeploymentFeatures
LogAnalyticsWorkspace LogAnalyticsWorkspaceFeatures
ResourceGroup ResourceGroupFeatures
ManagedDisk ManagedDiskFeatures
}

type CognitiveAccountFeatures struct {
Expand Down Expand Up @@ -62,6 +63,10 @@ type ApplicationInsightFeatures struct {
DisableGeneratedRule bool
}

type ManagedDiskFeatures struct {
ExpandWithoutDowntime bool
}

type AppConfigurationFeatures struct {
PurgeSoftDeleteOnDestroy bool
RecoverSoftDeleted bool
Expand Down
25 changes: 25 additions & 0 deletions internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
},
},
},

"managed_disk": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"expand_without_downtime": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
},
},
},
}

// this is a temporary hack to enable us to gradually add provider blocks to test configurations
Expand Down Expand Up @@ -441,5 +456,15 @@ func expandFeatures(input []interface{}) features.UserFeatures {
}
}

if raw, ok := val["managed_disk"]; ok {
items := raw.([]interface{})
if len(items) > 0 {
managedDiskRaw := items[0].(map[string]interface{})
if v, ok := managedDiskRaw["expand_without_downtime"]; ok {
featuresMap.ManagedDisk.ExpandWithoutDowntime = v.(bool)
}
}
}

return featuresMap
}
84 changes: 84 additions & 0 deletions internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestExpandFeatures(t *testing.T) {
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: true,
},
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: true,
},
TemplateDeployment: features.TemplateDeploymentFeatures{
DeleteNestedItemsDuringDeletion: true,
},
Expand Down Expand Up @@ -108,6 +111,11 @@ func TestExpandFeatures(t *testing.T) {
"permanently_delete_on_destroy": true,
},
},
"managed_disk": []interface{}{
map[string]interface{}{
"expand_without_downtime": true,
},
},
"network": []interface{}{
map[string]interface{}{
"relaxed_locking": true,
Expand Down Expand Up @@ -168,6 +176,9 @@ func TestExpandFeatures(t *testing.T) {
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: true,
},
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: true,
},
ResourceGroup: features.ResourceGroupFeatures{
PreventDeletionIfContainsResources: true,
},
Expand Down Expand Up @@ -230,6 +241,11 @@ func TestExpandFeatures(t *testing.T) {
"permanently_delete_on_destroy": false,
},
},
"managed_disk": []interface{}{
map[string]interface{}{
"expand_without_downtime": false,
},
},
"network_locking": []interface{}{
map[string]interface{}{
"relaxed_locking": false,
Expand Down Expand Up @@ -290,6 +306,9 @@ func TestExpandFeatures(t *testing.T) {
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: false,
},
ResourceGroup: features.ResourceGroupFeatures{
PreventDeletionIfContainsResources: false,
},
Expand Down Expand Up @@ -1119,3 +1138,68 @@ func TestExpandFeaturesResourceGroup(t *testing.T) {
}
}
}

func TestExpandFeaturesManagedDisk(t *testing.T) {
testData := []struct {
Name string
Input []interface{}
EnvVars map[string]interface{}
Expected features.UserFeatures
}{
{
Name: "Empty Block",
Input: []interface{}{
map[string]interface{}{
"managed_disk": []interface{}{},
},
},
Expected: features.UserFeatures{
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: true,
},
},
},
{
Name: "No Downtime Resize Enabled",
Input: []interface{}{
map[string]interface{}{
"managed_disk": []interface{}{
map[string]interface{}{
"expand_without_downtime": true,
},
},
},
},
Expected: features.UserFeatures{
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: true,
},
},
},
{
Name: "No Downtime Resize Disabled",
Input: []interface{}{
map[string]interface{}{
"managed_disk": []interface{}{
map[string]interface{}{
"expand_without_downtime": false,
},
},
},
},
Expected: features.UserFeatures{
ManagedDisk: features.ManagedDiskFeatures{
ExpandWithoutDowntime: false,
},
},
},
}

for _, testCase := range testData {
t.Logf("[DEBUG] Test Case: %q", testCase.Name)
result := expandFeatures(testCase.Input)
if !reflect.DeepEqual(result.ManagedDisk, testCase.Expected.ManagedDisk) {
t.Fatalf("Expected %+v but got %+v", result.ManagedDisk, testCase.Expected.ManagedDisk)
}
}
}
12 changes: 12 additions & 0 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/marketplaceordering/mgmt/2015-06-01/marketplaceordering"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-07-01/skus"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhostgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhosts"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/proximityplacementgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/sshpublickeys"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/virtualmachines"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/diskencryptionsets"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/snapshots"
Expand All @@ -31,9 +33,11 @@ type Client struct {
ImagesClient *compute.ImagesClient
MarketplaceAgreementsClient *marketplaceordering.MarketplaceAgreementsClient
ProximityPlacementGroupsClient *proximityplacementgroups.ProximityPlacementGroupsClient
SkusClient *skus.SkusClient
SSHPublicKeysClient *sshpublickeys.SshPublicKeysClient
SnapshotsClient *snapshots.SnapshotsClient
UsageClient *compute.UsageClient
VirtualMachinesClient *virtualmachines.VirtualMachinesClient
VMExtensionImageClient *compute.VirtualMachineExtensionImagesClient
VMExtensionClient *compute.VirtualMachineExtensionsClient
VMScaleSetClient *compute.VirtualMachineScaleSetsClient
Expand Down Expand Up @@ -93,6 +97,9 @@ func NewClient(o *common.ClientOptions) *Client {
proximityPlacementGroupsClient := proximityplacementgroups.NewProximityPlacementGroupsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&proximityPlacementGroupsClient.Client, o.ResourceManagerAuthorizer)

skusClient := skus.NewSkusClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&skusClient.Client, o.ResourceManagerAuthorizer)

snapshotsClient := snapshots.NewSnapshotsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&snapshotsClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -102,6 +109,9 @@ func NewClient(o *common.ClientOptions) *Client {
usageClient := compute.NewUsageClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&usageClient.Client, o.ResourceManagerAuthorizer)

virtualMachinesClient := virtualmachines.NewVirtualMachinesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&virtualMachinesClient.Client, o.ResourceManagerAuthorizer)

vmExtensionImageClient := compute.NewVirtualMachineExtensionImagesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&vmExtensionImageClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -143,9 +153,11 @@ func NewClient(o *common.ClientOptions) *Client {
ImagesClient: &imagesClient,
MarketplaceAgreementsClient: &marketplaceAgreementsClient,
ProximityPlacementGroupsClient: &proximityPlacementGroupsClient,
SkusClient: &skusClient,
SSHPublicKeysClient: &sshPublicKeysClient,
SnapshotsClient: &snapshotsClient,
UsageClient: &usageClient,
VirtualMachinesClient: &virtualMachinesClient,
VMExtensionImageClient: &vmExtensionImageClient,
VMExtensionClient: &vmExtensionClient,
VMScaleSetClient: &vmScaleSetClient,
Expand Down
25 changes: 22 additions & 3 deletions internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,8 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro

func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.DisksClient
virtualMachinesClient := meta.(*clients.Client).Compute.VirtualMachinesClient
skusClient := meta.(*clients.Client).Compute.SkusClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand Down Expand Up @@ -678,9 +680,26 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
}

if d.HasChange("disk_size_gb") {
if old, new := d.GetChange("disk_size_gb"); new.(int) > old.(int) {
shouldShutDown = true
diskUpdate.Properties.DiskSizeGB = utils.Int64(int64(new.(int)))
if oldSize, newSize := d.GetChange("disk_size_gb"); newSize.(int) > oldSize.(int) {
canBeResizedWithoutDowntime := false
if meta.(*clients.Client).Features.ManagedDisk.ExpandWithoutDowntime {
diskSupportsNoDowntimeResize, err := determineIfDataDiskSupportsNoDowntimeResize(disk.Model, oldSize.(int), newSize.(int))
if err != nil {
return fmt.Errorf("determining if the Disk supports no-downtime-resize: %+v", err)
}

vmSkuSupportsNoDowntimeResize, err := determineIfVirtualMachineSkuSupportsNoDowntimeResize(ctx, disk.Model.ManagedBy, virtualMachinesClient, skusClient)
if err != nil {
return fmt.Errorf("determining if the Virtual Machine the Disk is attached to supports no-downtime-resize: %+v", err)
}

canBeResizedWithoutDowntime = *vmSkuSupportsNoDowntimeResize && *diskSupportsNoDowntimeResize
}
if !canBeResizedWithoutDowntime {
log.Printf("[INFO] The %s, or the Virtual Machine that it's attached to, doesn't support no-downtime-resizing - requiring that the VM should be shutdown", *id)
shouldShutDown = true
}
diskUpdate.Properties.DiskSizeGB = utils.Int64(int64(newSize.(int)))
} else {
return fmt.Errorf("- New size must be greater than original size. Shrinking disks is not supported on Azure")
}
Expand Down