Skip to content

Commit

Permalink
Add support for using images stored in Azure Shared Image Gallery
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <saugustus@vmware.com>
  • Loading branch information
justaugustus committed Oct 6, 2019
1 parent 61dd524 commit 98ca47b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
11 changes: 7 additions & 4 deletions api/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,22 @@ type AvailabilityZone struct {
Enabled *bool `json:"enabled,omitempty"`
}

// TODO: Add note on usage
type Image struct {
// TODO: Add note on usage
Publisher *string `json:"publisher,omitempty"`
Offer *string `json:"offer,omitempty"`
SKU *string `json:"sku,omitempty"`
Version *string `json:"version,omitempty"`

// TODO: Add note on how to use image ID

ID *string `json:"id,omitempty"`

SubscriptionID *string `json:"subscriptionID,omitempty"`
ResourceGroup *string `json:"resourceGroup,omitempty"`
Gallery *string `json:"gallery,omitempty"`
Name *string `json:"name,omitempty"`

Version *string `json:"version,omitempty"`
}

// APIEndpoint represents a reachable Kubernetes API endpoint.
Expand All @@ -434,9 +438,8 @@ type VMIdentity string

type OSDisk struct {
OSType string `json:"osType"`
Image *string `json:"image,omitempty"`
DiskSizeGB int32 `json:"diskSizeGB"`
ManagedDisk ManagedDisk `json:"managedDisk,omitempty"`
ManagedDisk ManagedDisk `json:"managedDisk"`
}

type ManagedDisk struct {
Expand Down
18 changes: 9 additions & 9 deletions api/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 28 additions & 27 deletions cloud/services/virtualmachines/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,39 +200,52 @@ func (s *Service) Delete(ctx context.Context, spec interface{}) error {
return err
}

// generateStorageProfile
// generateStorageProfile generates a pointer to a compute.StorageProfile which can utilized for VM creation.
func generateStorageProfile(vmSpec Spec) (*compute.StorageProfile, error) {
// TODO: Validate parameters before building storage profile
storageProfile := &compute.StorageProfile{
OsDisk: &compute.OSDisk{
Name: to.StringPtr(azure.GenerateOSDiskName(vmSpec.Name)),
OsType: compute.OperatingSystemTypes(vmSpec.OSDisk.OSType),
CreateOption: compute.DiskCreateOptionTypesFromImage,
DiskSizeGB: to.Int32Ptr(vmSpec.OSDisk.DiskSizeGB),
ManagedDisk: &compute.ManagedDiskParameters{
StorageAccountType: compute.StorageAccountTypes(vmSpec.OSDisk.ManagedDisk.StorageAccountType),
},
},
}

if vmSpec.OSDisk.Image != nil {
storageProfile.OsDisk.Image = &compute.VirtualHardDisk{}
storageProfile.OsDisk.Image.URI = vmSpec.OSDisk.Image
} else {
imageRef, err := generateImageReference(vmSpec.Image)
if err != nil {
return nil, err
}

storageProfile.ImageReference = imageRef
storageProfile.OsDisk.ManagedDisk = &compute.ManagedDiskParameters{
StorageAccountType: compute.StorageAccountTypes(vmSpec.OSDisk.ManagedDisk.StorageAccountType),
}
imageRef, err := generateImageReference(vmSpec.Image)
if err != nil {
return nil, err
}

storageProfile.ImageReference = imageRef

return storageProfile, nil
}

// generateImageReference generates a pointer to a compute.ImageReference which can utilized for VM creation.
func generateImageReference(image infrav1.Image) (*compute.ImageReference, error) {
imageRef := &compute.ImageReference{}

if image.ID != nil {
imageRef.ID = to.StringPtr(*image.ID)

// return early since we should only need the image ID
return imageRef, nil
} else if image.SubscriptionID != nil && image.ResourceGroup != nil && image.Gallery != nil && image.Name != nil && image.Version != nil {
imageID, err := generateImageID(image)
if err != nil {
return nil, err
}

imageRef.ID = to.StringPtr(imageID)

// return early since we're referencing an image that may not be published
return imageRef, nil
}

if image.Publisher != nil {
imageRef.Publisher = image.Publisher
}
Expand All @@ -245,22 +258,10 @@ func generateImageReference(image infrav1.Image) (*compute.ImageReference, error
if image.Version != nil {
imageRef.Version = image.Version

// return early since we don't need to generate an image ID for a published image
return imageRef, nil
}

// TODO: We may not need this logic if we validate Publisher, Offer, SKU, and Version is all that's
// required for published and marketplace images.
if image.SubscriptionID != nil && image.ResourceGroup != nil && image.Gallery != nil && image.Name != nil && image.Version != nil {
imageID, err := generateImageID(image)
if err != nil {
return nil, err
}

imageRef.ID = to.StringPtr(imageID)
}

return imageRef, nil
return nil, errors.Errorf("Image reference cannot be generated, as fields are missing: %+v", *imageRef)
}

// generateImageID generates the resource ID for an image stored in an Azure Shared Image Gallery.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,19 @@ spec:
properties:
gallery:
type: string
id:
type: string
name:
type: string
offer:
type: string
publisher:
description: 'TODO: Add note on usage'
type: string
resourceGroup:
type: string
sku:
type: string
subscriptionID:
description: 'TODO: Add note on how to use image ID'
type: string
version:
type: string
Expand All @@ -208,8 +208,6 @@ spec:
diskSizeGB:
format: int32
type: integer
image:
type: string
managedDisk:
properties:
storageAccountType:
Expand All @@ -221,6 +219,7 @@ spec:
type: string
required:
- diskSizeGB
- managedDisk
- osType
type: object
startupScript:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,23 @@ spec:
type: string
type: object
image:
description: 'TODO: Add note on usage'
properties:
gallery:
type: string
id:
type: string
name:
type: string
offer:
type: string
publisher:
description: 'TODO: Add note on usage'
type: string
resourceGroup:
type: string
sku:
type: string
subscriptionID:
description: 'TODO: Add note on how to use image ID'
type: string
version:
type: string
Expand All @@ -71,8 +72,6 @@ spec:
diskSizeGB:
format: int32
type: integer
image:
type: string
managedDisk:
properties:
storageAccountType:
Expand All @@ -84,6 +83,7 @@ spec:
type: string
required:
- diskSizeGB
- managedDisk
- osType
type: object
providerID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,23 @@ spec:
type: string
type: object
image:
description: 'TODO: Add note on usage'
properties:
gallery:
type: string
id:
type: string
name:
type: string
offer:
type: string
publisher:
description: 'TODO: Add note on usage'
type: string
resourceGroup:
type: string
sku:
type: string
subscriptionID:
description: 'TODO: Add note on how to use image ID'
type: string
version:
type: string
Expand All @@ -78,8 +79,6 @@ spec:
diskSizeGB:
format: int32
type: integer
image:
type: string
managedDisk:
properties:
storageAccountType:
Expand All @@ -91,6 +90,7 @@ spec:
type: string
required:
- diskSizeGB
- managedDisk
- osType
type: object
providerID:
Expand Down

0 comments on commit 98ca47b

Please sign in to comment.