Use separate caching/sku for data disks

This commit is contained in:
Paul Meyer 2020-04-29 20:28:10 +00:00
parent f601f54d0b
commit ac3d19ee23
5 changed files with 77 additions and 33 deletions

View File

@ -88,6 +88,14 @@ type Config struct {
// The [cache type](https://docs.microsoft.com/en-us/rest/api/compute/images/createorupdate#cachingtypes)
// specified in the resulting image and for attaching it to the Packer VM. Defaults to `ReadOnly`
OSDiskCacheType string `mapstructure:"os_disk_cache_type"`
// The [storage SKU](https://docs.microsoft.com/en-us/rest/api/compute/disks/createorupdate#diskstorageaccounttypes)
// to use for datadisks. Defaults to `Standard_LRS`.
DataDiskStorageAccountType string `mapstructure:"data_disk_storage_account_type"`
// The [cache type](https://docs.microsoft.com/en-us/rest/api/compute/images/createorupdate#cachingtypes)
// specified in the resulting image and for attaching it to the Packer VM. Defaults to `ReadOnly`
DataDiskCacheType string `mapstructure:"data_disk_cache_type"`
// The [Hyper-V generation type](https://docs.microsoft.com/en-us/rest/api/compute/images/createorupdate#hypervgenerationtypes) for Managed Image output.
// Defaults to `V1`.
ImageHyperVGeneration string `mapstructure:"image_hyperv_generation"`
@ -253,6 +261,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
b.config.OSDiskCacheType = string(compute.CachingTypesReadOnly)
}
if b.config.DataDiskStorageAccountType == "" {
b.config.DataDiskStorageAccountType = string(compute.PremiumLRS)
}
if b.config.DataDiskCacheType == "" {
b.config.DataDiskCacheType = string(compute.CachingTypesReadOnly)
}
if b.config.ImageHyperVGeneration == "" {
b.config.ImageHyperVGeneration = string(compute.V1)
}
@ -300,6 +316,14 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("os_disk_storage_account_type: %v", err))
}
if err := checkDiskCacheType(b.config.DataDiskCacheType); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("data_disk_cache_type: %v", err))
}
if err := checkStorageAccountType(b.config.DataDiskStorageAccountType); err != nil {
errs = packer.MultiErrorAppend(errs, fmt.Errorf("data_disk_storage_account_type: %v", err))
}
if b.config.ImageResourceID != "" {
r, err := azure.ParseResourceID(b.config.ImageResourceID)
if err != nil ||
@ -527,6 +551,8 @@ func buildsteps(config Config, info *client.ComputeInfo) []multistep.Step {
&StepCreateNewDiskset{
OSDiskID: config.TemporaryOSDiskID,
OSDiskSizeGB: config.OSDiskSizeGB,
OSDiskStorageAccountType: config.OSDiskStorageAccountType,
DataDiskStorageAccountType: config.DataDiskStorageAccountType,
SourceImageResourceID: config.Source,
Location: info.Location,

View File

@ -21,6 +21,7 @@ type StepCreateNewDiskset struct {
OSDiskID string // Disk ID
OSDiskSizeGB int32 // optional, ignored if 0
OSDiskStorageAccountType string // from compute.DiskStorageAccountTypes
DataDiskStorageAccountType string // from compute.DiskStorageAccountTypes
DataDiskIDPrefix string
@ -205,9 +206,9 @@ func (s StepCreateNewDiskset) getDatadiskDefinitionFromImage(lun int32) compute.
Lun: to.Int32Ptr(lun),
}
if s.OSDiskStorageAccountType != "" {
if s.DataDiskStorageAccountType != "" {
disk.Sku = &compute.DiskSku{
Name: compute.DiskStorageAccountTypes(s.OSDiskStorageAccountType),
Name: compute.DiskStorageAccountTypes(s.DataDiskStorageAccountType),
}
}
return disk

View File

@ -93,6 +93,7 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
fields: StepCreateNewDiskset{
OSDiskID: "/subscriptions/SubscriptionID/resourcegroups/ResourceGroupName/providers/Microsoft.Compute/disks/TemporaryOSDiskName",
OSDiskStorageAccountType: string(compute.StandardLRS),
DataDiskStorageAccountType: string(compute.PremiumLRS),
DataDiskIDPrefix: "/subscriptions/SubscriptionID/resourcegroups/ResourceGroupName/providers/Microsoft.Compute/disks/TemporaryDataDisk-",
HyperVGeneration: string(compute.V1),
Location: "westus",
@ -128,7 +129,7 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
}
},
"sku": {
"name": "Standard_LRS"
"name": "Premium_LRS"
}
}`, `
{
@ -143,7 +144,7 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
}
},
"sku": {
"name": "Standard_LRS"
"name": "Premium_LRS"
}
}`, `
{
@ -158,7 +159,7 @@ func TestStepCreateNewDisk_Run(t *testing.T) {
}
},
"sku": {
"name": "Standard_LRS"
"name": "Premium_LRS"
}
}`},
want: multistep.ActionContinue,

View File

@ -17,6 +17,7 @@ import (
type StepCreateSharedImageVersion struct {
Destination SharedImageGalleryDestination
OSDiskCacheType string
DataDiskCacheType string
Location string
}
@ -62,6 +63,7 @@ func (s *StepCreateSharedImageVersion) Run(ctx context.Context, state multistep.
datadisks = append(datadisks, compute.GalleryDataDiskImage{
Lun: to.Int32Ptr(lun),
Source: &compute.GalleryArtifactVersionSource{ID: to.StringPtr(resource.String())},
HostCaching: compute.HostCaching(s.DataDiskCacheType),
})
}
}

View File

@ -20,6 +20,7 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
type fields struct {
Destination SharedImageGalleryDestination
OSDiskCacheType string
DataDiskCacheType string
Location string
}
tests := []struct {
@ -46,6 +47,8 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
},
ExcludeFromLatest: true,
},
OSDiskCacheType: "ReadWrite",
DataDiskCacheType: "None",
Location: "region2",
},
snapshotset: diskset(
@ -68,6 +71,7 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
},
"storageProfile": {
"osDiskImage": {
"hostCaching": "ReadWrite",
"source": {
"id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/osdisksnapshot"
}
@ -75,15 +79,24 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
"dataDiskImages": [
{
"lun": 0,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot0" }
"hostCaching": "None",
"source": {
"id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot0"
}
},
{
"lun": 1,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot1" }
"hostCaching": "None",
"source": {
"id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot1"
}
},
{
"lun": 2,
"source": { "id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot2" }
"hostCaching": "None",
"source": {
"id": "/subscriptions/12345/resourceGroups/group1/providers/Microsoft.Compute/snapshots/datadisksnapshot2"
}
}
]
}
@ -120,6 +133,7 @@ func TestStepCreateSharedImageVersion_Run(t *testing.T) {
s := &StepCreateSharedImageVersion{
Destination: tt.fields.Destination,
OSDiskCacheType: tt.fields.OSDiskCacheType,
DataDiskCacheType: tt.fields.DataDiskCacheType,
Location: tt.fields.Location,
}
if got := s.Run(context.TODO(), state); !reflect.DeepEqual(got, tt.want) {