-
Notifications
You must be signed in to change notification settings - Fork 32
feature: Add support for data disks on Azure Stack Hub #149
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
Closed
gcampbell12
wants to merge
2
commits into
openshift:main
from
gcampbell12:gc/support-for-data-disks-stack-hub
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ import ( | |
"crypto/rsa" | ||
"errors" | ||
"fmt" | ||
machinev1 "github.com/openshift/api/machine/v1beta1" | ||
apierrors "github.com/openshift/machine-api-operator/pkg/controller/machine" | ||
"regexp" | ||
|
||
"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/compute/mgmt/compute" | ||
"github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/network/mgmt/network" | ||
|
@@ -178,6 +181,11 @@ func (s *StackHubService) deriveVirtualMachineParametersStackHub(vmSpec *Spec, n | |
} | ||
} | ||
|
||
dataDisks, err := generateStackHubDataDisks(vmSpec) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate data disk spec: %w", err) | ||
} | ||
|
||
virtualMachine := &compute.VirtualMachine{ | ||
Location: to.StringPtr(s.Scope.MachineConfig.Location), | ||
Tags: vmSpec.Tags, | ||
|
@@ -196,6 +204,7 @@ func (s *StackHubService) deriveVirtualMachineParametersStackHub(vmSpec *Spec, n | |
StorageAccountType: compute.StorageAccountTypes(vmSpec.OSDisk.ManagedDisk.StorageAccountType), | ||
}, | ||
}, | ||
DataDisks: dataDisks, | ||
}, | ||
OsProfile: osProfile, | ||
NetworkProfile: &compute.NetworkProfile{ | ||
|
@@ -251,3 +260,86 @@ func (s *StackHubService) Delete(ctx context.Context, spec azure.Spec) error { | |
klog.V(2).Infof("successfully deleted vm %s ", vmSpec.Name) | ||
return err | ||
} | ||
|
||
func generateStackHubDataDisks(vmSpec *Spec) (*[]compute.DataDisk, error) { | ||
if len(vmSpec.DataDisks) == 0 { | ||
return nil, nil | ||
} | ||
|
||
seenDataDiskLuns := make(map[int32]struct{}) | ||
seenDataDiskNames := make(map[string]struct{}) | ||
// defines rules for matching. strings must start and finish with an alphanumeric character | ||
// and can only contain letters, numbers, underscores, periods or hyphens. | ||
reg := regexp.MustCompile(`^[a-zA-Z0-9](?:[\w\.-]*[a-zA-Z0-9])?$`) | ||
dataDisks := make([]compute.DataDisk, len(vmSpec.DataDisks)) | ||
|
||
for i, disk := range vmSpec.DataDisks { | ||
dataDiskName := azure.GenerateDataDiskName(vmSpec.Name, disk.NameSuffix) | ||
|
||
if len(dataDiskName) > 80 { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"The overall disk name name must not exceed 80 chars in length. Check your `nameSuffix`.", | ||
dataDiskName, vmSpec.Name) | ||
} | ||
|
||
if matched := reg.MatchString(disk.NameSuffix); !matched { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"The nameSuffix can only contain letters, numbers, "+ | ||
"underscores, periods or hyphens. Check your `nameSuffix`.", | ||
dataDiskName, vmSpec.Name) | ||
} | ||
|
||
if disk.DiskSizeGB < 4 { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"`diskSizeGB`: %d, is invalid, disk size must be greater or equal than 4.", | ||
dataDiskName, vmSpec.Name, disk.DiskSizeGB) | ||
} | ||
|
||
if _, exists := seenDataDiskNames[disk.NameSuffix]; exists { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"A Data Disk with `nameSuffix`: %s, already exists. `nameSuffix` must be unique.", | ||
dataDiskName, vmSpec.Name, disk.NameSuffix) | ||
} | ||
|
||
if disk.ManagedDisk.StorageAccountType == machinev1.StorageAccountUltraSSDLRS { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"`storageAccountType`: %s, is not supported for Data Disk in Azure Stack Hub.", | ||
dataDiskName, vmSpec.Name, machinev1.StorageAccountUltraSSDLRS, machinev1.CachingTypeNone) | ||
} | ||
|
||
if disk.ManagedDisk.DiskEncryptionSet != nil { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"Disk Encryption Set is not supported for Data Disk in Azure Stack Hub.", | ||
dataDiskName, vmSpec.Name) | ||
} | ||
|
||
if disk.Lun < 0 || disk.Lun > 63 { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"Invalid value `lun`: %d. `lun` cannot be lower than 0 or higher than 63.", | ||
dataDiskName, vmSpec.Name, disk.Lun) | ||
} | ||
|
||
if _, exists := seenDataDiskLuns[disk.Lun]; exists { | ||
return nil, apierrors.InvalidMachineConfiguration("failed to create Data Disk: %s for vm %s. "+ | ||
"A Data Disk with `lun`: %d, already exists. `lun` must be unique.", | ||
dataDiskName, vmSpec.Name, disk.Lun) | ||
} | ||
|
||
seenDataDiskNames[disk.NameSuffix] = struct{}{} | ||
seenDataDiskLuns[disk.Lun] = struct{}{} | ||
|
||
dataDisks[i] = compute.DataDisk{ | ||
CreateOption: compute.DiskCreateOptionTypesEmpty, | ||
DiskSizeGB: to.Int32Ptr(disk.DiskSizeGB), | ||
Lun: to.Int32Ptr(disk.Lun), | ||
Name: to.StringPtr(dataDiskName), | ||
Caching: compute.CachingTypes(disk.CachingType), | ||
} | ||
|
||
dataDisks[i].ManagedDisk = &compute.ManagedDiskParameters{ | ||
StorageAccountType: compute.StorageAccountTypes(disk.ManagedDisk.StorageAccountType), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was there a specific reason to omit support for Disk Encryption Set here? Guessing DES support is too new for Stack Hub's older API version? |
||
} | ||
|
||
return &dataDisks, nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a specific reason to omit support for DeletionPolicy here?
https://github.com/mdbooth/machine-api-provider-azure/blob/afd0f3f9fdf04a106ae0be1a05e19c54171e5c28/pkg/cloud/azure/services/virtualmachines/virtualmachines.go#L599-L607