@@ -19,14 +19,13 @@ package converters
1919import (
2020 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
2121 "github.com/Azure/go-autorest/autorest/to"
22-
2322 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha4"
24- infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1alpha4 "
23+ "sigs.k8s.io/cluster-api-provider-azure/azure "
2524)
2625
2726// SDKToVMSS converts an Azure SDK VirtualMachineScaleSet to the AzureMachinePool type.
28- func SDKToVMSS (sdkvmss compute.VirtualMachineScaleSet , sdkinstances []compute.VirtualMachineScaleSetVM ) * infrav1exp .VMSS {
29- vmss := & infrav1exp .VMSS {
27+ func SDKToVMSS (sdkvmss compute.VirtualMachineScaleSet , sdkinstances []compute.VirtualMachineScaleSetVM ) * azure .VMSS {
28+ vmss := & azure .VMSS {
3029 ID : to .String (sdkvmss .ID ),
3130 Name : to .String (sdkvmss .Name ),
3231 State : infrav1 .ProvisioningState (to .String (sdkvmss .ProvisioningState )),
@@ -46,25 +45,66 @@ func SDKToVMSS(sdkvmss compute.VirtualMachineScaleSet, sdkinstances []compute.Vi
4645 }
4746
4847 if len (sdkinstances ) > 0 {
49- vmss .Instances = make ([]infrav1exp .VMSSVM , len (sdkinstances ))
48+ vmss .Instances = make ([]azure .VMSSVM , len (sdkinstances ))
5049 for i , vm := range sdkinstances {
51- instance := infrav1exp.VMSSVM {
52- ID : to .String (vm .ID ),
53- InstanceID : to .String (vm .InstanceID ),
54- Name : to .String (vm .OsProfile .ComputerName ),
55- State : infrav1 .ProvisioningState (to .String (vm .ProvisioningState )),
56- }
57-
58- if vm .LatestModelApplied != nil {
59- instance .LatestModelApplied = * vm .LatestModelApplied
60- }
61-
62- if vm .Zones != nil && len (* vm .Zones ) > 0 {
63- instance .AvailabilityZone = to .StringSlice (vm .Zones )[0 ]
64- }
65- vmss .Instances [i ] = instance
50+ vmss .Instances [i ] = * SDKToVMSSVM (vm )
6651 }
6752 }
6853
54+ if sdkvmss .VirtualMachineProfile != nil &&
55+ sdkvmss .VirtualMachineProfile .StorageProfile != nil &&
56+ sdkvmss .VirtualMachineProfile .StorageProfile .ImageReference != nil {
57+
58+ imageRef := sdkvmss .VirtualMachineProfile .StorageProfile .ImageReference
59+ vmss .Image = SDKImageToImage (imageRef , sdkvmss .Plan != nil )
60+ }
61+
6962 return vmss
7063}
64+
65+ // SDKToVMSSVM converts an Azure SDK VirtualMachineScaleSetVM into an infrav1exp.VMSSVM
66+ func SDKToVMSSVM (sdkInstance compute.VirtualMachineScaleSetVM ) * azure.VMSSVM {
67+ instance := azure.VMSSVM {
68+ ID : to .String (sdkInstance .ID ),
69+ InstanceID : to .String (sdkInstance .InstanceID ),
70+ }
71+
72+ if sdkInstance .VirtualMachineScaleSetVMProperties == nil {
73+ return & instance
74+ }
75+
76+ instance .State = infrav1 .Creating
77+ if sdkInstance .ProvisioningState != nil {
78+ instance .State = infrav1 .ProvisioningState (to .String (sdkInstance .ProvisioningState ))
79+ }
80+
81+ if sdkInstance .OsProfile != nil && sdkInstance .OsProfile .ComputerName != nil {
82+ instance .Name = * sdkInstance .OsProfile .ComputerName
83+ }
84+
85+ if sdkInstance .StorageProfile != nil && sdkInstance .StorageProfile .ImageReference != nil {
86+ imageRef := sdkInstance .StorageProfile .ImageReference
87+ instance .Image = SDKImageToImage (imageRef , sdkInstance .Plan != nil )
88+ }
89+
90+ if sdkInstance .Zones != nil && len (* sdkInstance .Zones ) > 0 {
91+ // an instance should only have 1 zone, so we select the first item of the slice
92+ instance .AvailabilityZone = to .StringSlice (sdkInstance .Zones )[0 ]
93+ }
94+
95+ return & instance
96+ }
97+
98+ // SDKImageToImage converts a SDK image reference to infrav1.Image
99+ func SDKImageToImage (sdkImageRef * compute.ImageReference , isThirdPartyImage bool ) infrav1.Image {
100+ return infrav1.Image {
101+ ID : sdkImageRef .ID ,
102+ Marketplace : & infrav1.AzureMarketplaceImage {
103+ Publisher : to .String (sdkImageRef .Publisher ),
104+ Offer : to .String (sdkImageRef .Offer ),
105+ SKU : to .String (sdkImageRef .Sku ),
106+ Version : to .String (sdkImageRef .Version ),
107+ ThirdPartyImage : isThirdPartyImage ,
108+ },
109+ }
110+ }
0 commit comments