@@ -17,13 +17,22 @@ 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 "k8s.io/utils/pointer"
2224 azprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
2325 infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
2426 "sigs.k8s.io/cluster-api-provider-azure/azure"
2527)
2628
29+ const (
30+ // RegExpStrCommunityGalleryID is a regexp string used for matching community gallery IDs and capturing specific values.
31+ RegExpStrCommunityGalleryID = `/CommunityGalleries/(?P<gallery>.*)/Images/(?P<name>.*)/Versions/(?P<version>.*)`
32+ // RegExpStrComputeGalleryID is a regexp string used for matching compute gallery IDs and capturing specific values.
33+ RegExpStrComputeGalleryID = `/subscriptions/(?P<subID>.*)/resourceGroups/(?P<rg>.*)/providers/Microsoft.Compute/galleries/(?P<gallery>.*)/images/(?P<name>.*)/versions/(?P<version>.*)`
34+ )
35+
2736// SDKToVMSS converts an Azure SDK VirtualMachineScaleSet to the AzureMachinePool type.
2837func SDKToVMSS (sdkvmss compute.VirtualMachineScaleSet , sdkinstances []compute.VirtualMachineScaleSetVM ) * azure.VMSS {
2938 vmss := & azure.VMSS {
@@ -149,8 +158,53 @@ func SDKToVMSSVM(sdkInstance compute.VirtualMachineScaleSetVM) *azure.VMSSVM {
149158
150159// SDKImageToImage converts a SDK image reference to infrav1.Image.
151160func SDKImageToImage (sdkImageRef * compute.ImageReference , isThirdPartyImage bool ) infrav1.Image {
161+ if sdkImageRef .ID != nil {
162+ return IDImageRefToImage (* sdkImageRef .ID )
163+ }
164+ // community gallery image
165+ if sdkImageRef .CommunityGalleryImageID != nil {
166+ return cgImageRefToImage (* sdkImageRef .CommunityGalleryImageID )
167+ }
168+ // shared gallery image
169+ if sdkImageRef .SharedGalleryImageID != nil {
170+ return sgImageRefToImage (* sdkImageRef .SharedGalleryImageID )
171+ }
172+ // marketplace image
173+ return mpImageRefToImage (sdkImageRef , isThirdPartyImage )
174+ }
175+
176+ // GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
177+ func GetOrchestrationMode (modeType infrav1.OrchestrationModeType ) compute.OrchestrationMode {
178+ if modeType == infrav1 .FlexibleOrchestrationMode {
179+ return compute .OrchestrationModeFlexible
180+ }
181+ return compute .OrchestrationModeUniform
182+ }
183+
184+ // IDImageRefToImage converts an ID to a infrav1.Image with ComputerGallery set or ID, depending on the structure of the ID.
185+ func IDImageRefToImage (id string ) infrav1.Image {
186+ // compute gallery image
187+ if ok , params := getParams (RegExpStrComputeGalleryID , id ); ok {
188+ return infrav1.Image {
189+ ComputeGallery : & infrav1.AzureComputeGalleryImage {
190+ Gallery : params ["gallery" ],
191+ Name : params ["name" ],
192+ Version : params ["version" ],
193+ SubscriptionID : pointer .String (params ["subID" ]),
194+ ResourceGroup : pointer .String (params ["rg" ]),
195+ },
196+ }
197+ }
198+
199+ // specific image
200+ return infrav1.Image {
201+ ID : & id ,
202+ }
203+ }
204+
205+ // mpImageRefToImage converts a marketplace gallery ImageReference to an infrav1.Image.
206+ func mpImageRefToImage (sdkImageRef * compute.ImageReference , isThirdPartyImage bool ) infrav1.Image {
152207 return infrav1.Image {
153- ID : sdkImageRef .ID ,
154208 Marketplace : & infrav1.AzureMarketplaceImage {
155209 ImagePlan : infrav1.ImagePlan {
156210 Publisher : pointer .StringDeref (sdkImageRef .Publisher , "" ),
@@ -163,10 +217,49 @@ func SDKImageToImage(sdkImageRef *compute.ImageReference, isThirdPartyImage bool
163217 }
164218}
165219
166- // GetOrchestrationMode returns the compute.OrchestrationMode for the given infrav1.OrchestrationModeType.
167- func GetOrchestrationMode (modeType infrav1.OrchestrationModeType ) compute.OrchestrationMode {
168- if modeType == infrav1 .FlexibleOrchestrationMode {
169- return compute .OrchestrationModeFlexible
220+ // cgImageRefToImage converts a community gallery ImageReference to an infrav1.Image.
221+ func cgImageRefToImage (id string ) infrav1.Image {
222+ if ok , params := getParams (RegExpStrCommunityGalleryID , id ); ok {
223+ return infrav1.Image {
224+ ComputeGallery : & infrav1.AzureComputeGalleryImage {
225+ Gallery : params ["gallery" ],
226+ Name : params ["name" ],
227+ Version : params ["version" ],
228+ },
229+ }
170230 }
171- return compute .OrchestrationModeUniform
231+ return infrav1.Image {}
232+ }
233+
234+ // sgImageRefToImage converts a shared gallery ImageReference to an infrav1.Image.
235+ func sgImageRefToImage (id string ) infrav1.Image {
236+ if ok , params := getParams (RegExpStrComputeGalleryID , id ); ok {
237+ return infrav1.Image {
238+ SharedGallery : & infrav1.AzureSharedGalleryImage {
239+ SubscriptionID : params ["subID" ],
240+ ResourceGroup : params ["rg" ],
241+ Gallery : params ["gallery" ],
242+ Name : params ["name" ],
243+ Version : params ["version" ],
244+ },
245+ }
246+ }
247+ return infrav1.Image {}
248+ }
249+
250+ func getParams (regStr , str string ) (matched bool , params map [string ]string ) {
251+ re := regexp .MustCompile (regStr )
252+ match := re .FindAllStringSubmatch (str , - 1 )
253+
254+ if len (match ) == 1 {
255+ params = make (map [string ]string )
256+ for i , name := range re .SubexpNames () {
257+ if i > 0 && i <= len (match [0 ]) {
258+ params [name ] = match [0 ][i ]
259+ }
260+ }
261+ matched = true
262+ }
263+
264+ return matched , params
172265}
0 commit comments