Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 28 additions & 15 deletions azure/converters/vmss.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func SDKToVMSS(sdkvmss armcompute.VirtualMachineScaleSet, sdkinstances []armcomp
sdkvmss.Properties.VirtualMachineProfile.StorageProfile != nil &&
sdkvmss.Properties.VirtualMachineProfile.StorageProfile.ImageReference != nil {
imageRef := sdkvmss.Properties.VirtualMachineProfile.StorageProfile.ImageReference
vmss.Image = SDKImageToImage(imageRef, sdkvmss.Plan != nil)
vmss.Image = SDKImageToImage(imageRef, sdkvmss.Plan)
}

return vmss
Expand All @@ -95,7 +95,7 @@ func SDKVMToVMSSVM(sdkInstance armcompute.VirtualMachine, mode infrav1.Orchestra

if sdkInstance.Properties.StorageProfile != nil && sdkInstance.Properties.StorageProfile.ImageReference != nil {
imageRef := sdkInstance.Properties.StorageProfile.ImageReference
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan != nil)
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan)
}

if len(sdkInstance.Zones) > 0 {
Expand Down Expand Up @@ -147,7 +147,7 @@ func SDKToVMSSVM(sdkInstance armcompute.VirtualMachineScaleSetVM) *azure.VMSSVM

if sdkInstance.Properties.StorageProfile != nil && sdkInstance.Properties.StorageProfile.ImageReference != nil {
imageRef := sdkInstance.Properties.StorageProfile.ImageReference
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan != nil)
instance.Image = SDKImageToImage(imageRef, sdkInstance.Plan)
}

if len(sdkInstance.Zones) > 0 {
Expand All @@ -159,20 +159,33 @@ func SDKToVMSSVM(sdkInstance armcompute.VirtualMachineScaleSetVM) *azure.VMSSVM
}

// SDKImageToImage converts a SDK image reference to infrav1.Image.
func SDKImageToImage(sdkImageRef *armcompute.ImageReference, isThirdPartyImage bool) infrav1.Image {
if sdkImageRef.ID != nil {
return IDImageRefToImage(*sdkImageRef.ID)
func SDKImageToImage(sdkImageRef *armcompute.ImageReference, sdkPlan *armcompute.Plan) infrav1.Image {
var image infrav1.Image
switch {
case sdkImageRef.ID != nil:
image = IDImageRefToImage(*sdkImageRef.ID)
case sdkImageRef.CommunityGalleryImageID != nil:
image = cgImageRefToImage(*sdkImageRef.CommunityGalleryImageID)
case sdkImageRef.SharedGalleryImageID != nil:
image = sgImageRefToImage(*sdkImageRef.SharedGalleryImageID)
default:
image = mpImageRefToImage(sdkImageRef, sdkPlan != nil)
}
// community gallery image
if sdkImageRef.CommunityGalleryImageID != nil {
return cgImageRefToImage(*sdkImageRef.CommunityGalleryImageID)
}
// shared gallery image
if sdkImageRef.SharedGalleryImageID != nil {
return sgImageRefToImage(*sdkImageRef.SharedGalleryImageID)

if sdkPlan != nil {
if image.ComputeGallery != nil {
image.ComputeGallery.Plan = &infrav1.ImagePlan{
Publisher: ptr.Deref(sdkPlan.Publisher, ""),
Offer: ptr.Deref(sdkPlan.Product, ""),
SKU: ptr.Deref(sdkPlan.Name, ""),
}
} else if image.SharedGallery != nil {
image.SharedGallery.Publisher = sdkPlan.Publisher
image.SharedGallery.Offer = sdkPlan.Product
image.SharedGallery.SKU = sdkPlan.Name
}
}
// marketplace image
return mpImageRefToImage(sdkImageRef, isThirdPartyImage)
return image
}

// GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
Expand Down
186 changes: 179 additions & 7 deletions azure/converters/vmss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,53 @@ func Test_SDKToVMSS(t *testing.T) {
g.Expect(actual).To(gomega.Equal(expected))
},
},
{
Name: "ShouldPopulateImageWithPlan",
SubjectFactory: func(g *gomega.GomegaWithT) (armcompute.VirtualMachineScaleSet, []armcompute.VirtualMachineScaleSetVM) {
return armcompute.VirtualMachineScaleSet{
ID: ptr.To("vmssIDWithPlan"),
Name: ptr.To("vmssNameWithPlan"),
Properties: &armcompute.VirtualMachineScaleSetProperties{
ProvisioningState: ptr.To("Succeeded"),
VirtualMachineProfile: &armcompute.VirtualMachineScaleSetVMProfile{
StorageProfile: &armcompute.VirtualMachineScaleSetStorageProfile{
ImageReference: &armcompute.ImageReference{
ID: ptr.To("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
},
},
},
},
Plan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("product"),
Name: ptr.To("sku"),
},
}, nil
},
Expect: func(g *gomega.GomegaWithT, actual azure.VMSS) {
expected := azure.VMSS{
ID: "vmssIDWithPlan",
Name: "vmssNameWithPlan",
State: "Succeeded",
Image: infrav1.Image{
ComputeGallery: &infrav1.AzureComputeGalleryImage{
Gallery: "gallery",
Name: "image",
Version: "version",
SubscriptionID: ptr.To("subscription"),
ResourceGroup: ptr.To("rg"),
Plan: &infrav1.ImagePlan{
Publisher: "publisher",
Offer: "product",
SKU: "sku",
},
},
},
}

g.Expect(actual).To(gomega.Equal(expected))
},
},
}

for _, c := range cases {
Expand Down Expand Up @@ -202,6 +249,42 @@ func Test_SDKToVMSSVM(t *testing.T) {
State: "Creating",
},
},
{
Name: "VM with shared gallery storage and plan",
SDKInstance: armcompute.VirtualMachineScaleSetVM{
ID: ptr.To("/subscriptions/foo/resourceGroups/MY_RESOURCE_GROUP/providers/bar"),
Properties: &armcompute.VirtualMachineScaleSetVMProperties{
OSProfile: &armcompute.OSProfile{ComputerName: ptr.To("instance-000003")},
StorageProfile: &armcompute.StorageProfile{
ImageReference: &armcompute.ImageReference{
SharedGalleryImageID: ptr.To("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
},
},
},
Plan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("product"),
Name: ptr.To("sku"),
},
},
VMSSVM: &azure.VMSSVM{
ID: "/subscriptions/foo/resourceGroups/my_resource_group/providers/bar",
Name: "instance-000003",
Image: infrav1.Image{
SharedGallery: &infrav1.AzureSharedGalleryImage{
SubscriptionID: "subscription",
ResourceGroup: "rg",
Gallery: "gallery",
Name: "image",
Version: "version",
Publisher: ptr.To("publisher"),
Offer: ptr.To("product"),
SKU: ptr.To("sku"),
},
},
State: "Creating",
},
},
}

for _, c := range cases {
Expand All @@ -215,17 +298,16 @@ func Test_SDKToVMSSVM(t *testing.T) {

func Test_SDKImageToImage(t *testing.T) {
cases := []struct {
Name string
SDKImageRef *armcompute.ImageReference
IsThirdParty bool
Image infrav1.Image
Name string
SDKImageRef *armcompute.ImageReference
SDKPlan *armcompute.Plan
Image infrav1.Image
}{
{
Name: "id image",
SDKImageRef: &armcompute.ImageReference{
ID: ptr.To("imageID"),
},
IsThirdParty: false,
Image: infrav1.Image{
ID: ptr.To("imageID"),
},
Expand All @@ -238,7 +320,11 @@ func Test_SDKImageToImage(t *testing.T) {
SKU: ptr.To("sku"),
Version: ptr.To("version"),
},
IsThirdParty: true,
SDKPlan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("offer"),
Name: ptr.To("sku"),
},
Image: infrav1.Image{
Marketplace: &infrav1.AzureMarketplaceImage{
ImagePlan: infrav1.ImagePlan{
Expand Down Expand Up @@ -266,6 +352,29 @@ func Test_SDKImageToImage(t *testing.T) {
},
},
},
{
Name: "shared gallery image with plan",
SDKImageRef: &armcompute.ImageReference{
SharedGalleryImageID: ptr.To("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
},
SDKPlan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("product"),
Name: ptr.To("sku"),
},
Image: infrav1.Image{
SharedGallery: &infrav1.AzureSharedGalleryImage{
SubscriptionID: "subscription",
ResourceGroup: "rg",
Gallery: "gallery",
Name: "image",
Version: "version",
Publisher: ptr.To("publisher"),
Offer: ptr.To("product"),
SKU: ptr.To("sku"),
},
},
},
{
Name: "community gallery image",
SDKImageRef: &armcompute.ImageReference{
Expand Down Expand Up @@ -294,6 +403,31 @@ func Test_SDKImageToImage(t *testing.T) {
},
},
},
{
Name: "compute gallery image with plan",
SDKImageRef: &armcompute.ImageReference{
ID: ptr.To("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
},
SDKPlan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("product"),
Name: ptr.To("sku"),
},
Image: infrav1.Image{
ComputeGallery: &infrav1.AzureComputeGalleryImage{
Gallery: "gallery",
Name: "image",
Version: "version",
SubscriptionID: ptr.To("subscription"),
ResourceGroup: ptr.To("rg"),
Plan: &infrav1.ImagePlan{
Publisher: "publisher",
Offer: "product",
SKU: "sku",
},
},
},
},
{
Name: "compute gallery image not formatted as expected",
SDKImageRef: &armcompute.ImageReference{
Expand All @@ -316,7 +450,7 @@ func Test_SDKImageToImage(t *testing.T) {
t.Run(c.Name, func(t *testing.T) {
t.Parallel()
g := gomega.NewGomegaWithT(t)
g.Expect(converters.SDKImageToImage(c.SDKImageRef, c.IsThirdParty)).To(gomega.Equal(c.Image))
g.Expect(converters.SDKImageToImage(c.SDKImageRef, c.SDKPlan)).To(gomega.Equal(c.Image))
})
}
}
Expand Down Expand Up @@ -378,6 +512,44 @@ func Test_SDKVMToVMSSVM(t *testing.T) {
State: "Creating",
},
},
{
Name: "VM with shared gallery storage and plan",
Subject: armcompute.VirtualMachine{
ID: ptr.To("vmID5"),
Properties: &armcompute.VirtualMachineProperties{
OSProfile: &armcompute.OSProfile{
ComputerName: ptr.To("vmwithplan"),
},
StorageProfile: &armcompute.StorageProfile{
ImageReference: &armcompute.ImageReference{
SharedGalleryImageID: ptr.To("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
},
},
},
Plan: &armcompute.Plan{
Publisher: ptr.To("publisher"),
Product: ptr.To("product"),
Name: ptr.To("sku"),
},
},
Expected: &azure.VMSSVM{
ID: "vmID5",
Image: infrav1.Image{
SharedGallery: &infrav1.AzureSharedGalleryImage{
SubscriptionID: "subscription",
ResourceGroup: "rg",
Gallery: "gallery",
Name: "image",
Version: "version",
Publisher: ptr.To("publisher"),
Offer: ptr.To("product"),
SKU: ptr.To("sku"),
},
},
Name: "vmwithplan",
State: "Creating",
},
},
{
Name: "VM with provisioning state",
Subject: armcompute.VirtualMachine{
Expand Down
8 changes: 7 additions & 1 deletion azure/scope/machinepoolmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,13 @@ func (s *MachinePoolMachineScope) hasLatestModelApplied(ctx context.Context) (bo

// this means the ID was a compute gallery image ID
if newImage.ComputeGallery != nil {
return reflect.DeepEqual(s.instance.Image, newImage), nil
// The desired image from the spec has no plan metadata (the user
// cannot express it via image.ID), but the observed instance image
// may carry plan data populated by the SDK converter. Strip the
// plan from the observed copy so the comparison stays correct.
instanceImageCopy := s.instance.Image
instanceImageCopy.ComputeGallery.Plan = nil
return reflect.DeepEqual(instanceImageCopy, newImage), nil
}
}

Expand Down
Loading
Loading