@@ -17,6 +17,8 @@ limitations under the License.
1717package converters
1818
1919import (
20+ "regexp"
21+
2022 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
2123 "github.com/Azure/go-autorest/autorest/to"
2224 "k8s.io/utils/pointer"
@@ -25,6 +27,13 @@ import (
2527 "sigs.k8s.io/cluster-api-provider-azure/azure"
2628)
2729
30+ const (
31+ // RegExpStrCommunityGalleryID is a regexp string used for matching community gallery IDs and capturing specific values.
32+ RegExpStrCommunityGalleryID = `/CommunityGalleries/(?P<gallery>.*)/Images/(?P<name>.*)/Versions/(?P<version>.*)`
33+ // RegExpStrComputeGalleryID is a regexp string used for matching compute gallery IDs and capturing specific values.
34+ RegExpStrComputeGalleryID = `/subscriptions/(?P<subID>.*)/resourceGroups/(?P<rg>.*)/providers/Microsoft.Compute/galleries/(?P<gallery>.*)/images/(?P<name>.*)/versions/(?P<version>.*)`
35+ )
36+
2837// SDKToVMSS converts an Azure SDK VirtualMachineScaleSet to the AzureMachinePool type.
2938func SDKToVMSS (sdkvmss compute.VirtualMachineScaleSet , sdkinstances []compute.VirtualMachineScaleSetVM ) * azure.VMSS {
3039 vmss := & azure.VMSS {
@@ -150,8 +159,53 @@ func SDKToVMSSVM(sdkInstance compute.VirtualMachineScaleSetVM) *azure.VMSSVM {
150159
151160// SDKImageToImage converts a SDK image reference to infrav1.Image.
152161func SDKImageToImage (sdkImageRef * compute.ImageReference , isThirdPartyImage bool ) infrav1.Image {
162+ if sdkImageRef .ID != nil {
163+ return IDImageRefToImage (* sdkImageRef .ID )
164+ }
165+ // community gallery image
166+ if sdkImageRef .CommunityGalleryImageID != nil {
167+ return cgImageRefToImage (* sdkImageRef .CommunityGalleryImageID )
168+ }
169+ // shared gallery image
170+ if sdkImageRef .SharedGalleryImageID != nil {
171+ return sgImageRefToImage (* sdkImageRef .SharedGalleryImageID )
172+ }
173+ // marketplace image
174+ return mpImageRefToImage (sdkImageRef , isThirdPartyImage )
175+ }
176+
177+ // GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
178+ func GetOrchestrationMode (modeType infrav1.OrchestrationModeType ) compute.OrchestrationMode {
179+ if modeType == infrav1 .FlexibleOrchestrationMode {
180+ return compute .OrchestrationModeFlexible
181+ }
182+ return compute .OrchestrationModeUniform
183+ }
184+
185+ // IDImageRefToImage converts an ID to a infrav1.Image with ComputerGallery set or ID, depending on the structure of the ID.
186+ func IDImageRefToImage (id string ) infrav1.Image {
187+ // compute gallery image
188+ if ok , params := getParams (RegExpStrComputeGalleryID , id ); ok {
189+ return infrav1.Image {
190+ ComputeGallery : & infrav1.AzureComputeGalleryImage {
191+ Gallery : params ["gallery" ],
192+ Name : params ["name" ],
193+ Version : params ["version" ],
194+ SubscriptionID : pointer .String (params ["subID" ]),
195+ ResourceGroup : pointer .String (params ["rg" ]),
196+ },
197+ }
198+ }
199+
200+ // specific image
201+ return infrav1.Image {
202+ ID : & id ,
203+ }
204+ }
205+
206+ // mpImageRefToImage converts a marketplace gallery ImageReference to an infrav1.Image.
207+ func mpImageRefToImage (sdkImageRef * compute.ImageReference , isThirdPartyImage bool ) infrav1.Image {
153208 return infrav1.Image {
154- ID : sdkImageRef .ID ,
155209 Marketplace : & infrav1.AzureMarketplaceImage {
156210 ImagePlan : infrav1.ImagePlan {
157211 Publisher : to .String (sdkImageRef .Publisher ),
@@ -164,10 +218,49 @@ func SDKImageToImage(sdkImageRef *compute.ImageReference, isThirdPartyImage bool
164218 }
165219}
166220
167- // GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
168- func GetOrchestrationMode (modeType infrav1.OrchestrationModeType ) compute.OrchestrationMode {
169- if modeType == infrav1 .FlexibleOrchestrationMode {
170- return compute .OrchestrationModeFlexible
221+ // cgImageRefToImage converts a community gallery ImageReference to an infrav1.Image.
222+ func cgImageRefToImage (id string ) infrav1.Image {
223+ if ok , params := getParams (RegExpStrCommunityGalleryID , id ); ok {
224+ return infrav1.Image {
225+ ComputeGallery : & infrav1.AzureComputeGalleryImage {
226+ Gallery : params ["gallery" ],
227+ Name : params ["name" ],
228+ Version : params ["version" ],
229+ },
230+ }
171231 }
172- return compute .OrchestrationModeUniform
232+ return infrav1.Image {}
233+ }
234+
235+ // sgImageRefToImage converts a shared gallery ImageReference to an infrav1.Image.
236+ func sgImageRefToImage (id string ) infrav1.Image {
237+ if ok , params := getParams (RegExpStrComputeGalleryID , id ); ok {
238+ return infrav1.Image {
239+ SharedGallery : & infrav1.AzureSharedGalleryImage {
240+ SubscriptionID : params ["subID" ],
241+ ResourceGroup : params ["rg" ],
242+ Gallery : params ["gallery" ],
243+ Name : params ["name" ],
244+ Version : params ["version" ],
245+ },
246+ }
247+ }
248+ return infrav1.Image {}
249+ }
250+
251+ func getParams (regStr , str string ) (matched bool , params map [string ]string ) {
252+ re := regexp .MustCompile (regStr )
253+ match := re .FindAllStringSubmatch (str , - 1 )
254+
255+ if len (match ) == 1 {
256+ params = make (map [string ]string )
257+ for i , name := range re .SubexpNames () {
258+ if i > 0 && i <= len (match [0 ]) {
259+ params [name ] = match [0 ][i ]
260+ }
261+ }
262+ matched = true
263+ }
264+
265+ return matched , params
173266}
0 commit comments