Skip to content

Commit 1dd46a0

Browse files
author
James Goodhouse
committed
cherry-pick-3414-to-release-1.8 update SDKImageToImage func to handle different image types
1 parent b1160c3 commit 1dd46a0

File tree

3 files changed

+173
-15
lines changed

3 files changed

+173
-15
lines changed

azure/converters/vmss.go

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ limitations under the License.
1717
package converters
1818

1919
import (
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.
2837
func 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.
151160
func 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
}

azure/converters/vmss_test.go

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,7 @@ func Test_SDKToVMSSVM(t *testing.T) {
181181
ID: "/subscriptions/foo/resourceGroups/my_resource_group/providers/bar",
182182
Name: "instance-000001",
183183
Image: infrav1.Image{
184-
ID: pointer.String("imageID"),
185-
Marketplace: &infrav1.AzureMarketplaceImage{},
184+
ID: pointer.String("imageID"),
186185
},
187186
State: "Creating",
188187
},
@@ -223,28 +222,25 @@ func Test_SDKImageToImage(t *testing.T) {
223222
Image infrav1.Image
224223
}{
225224
{
226-
Name: "minimal image",
225+
Name: "id image",
227226
SDKImageRef: &compute.ImageReference{
228227
ID: pointer.String("imageID"),
229228
},
230229
IsThirdParty: false,
231230
Image: infrav1.Image{
232-
ID: pointer.String("imageID"),
233-
Marketplace: &infrav1.AzureMarketplaceImage{},
231+
ID: pointer.String("imageID"),
234232
},
235233
},
236234
{
237235
Name: "marketplace image",
238236
SDKImageRef: &compute.ImageReference{
239-
ID: pointer.String("imageID"),
240237
Publisher: pointer.String("publisher"),
241238
Offer: pointer.String("offer"),
242239
Sku: pointer.String("sku"),
243240
Version: pointer.String("version"),
244241
},
245242
IsThirdParty: true,
246243
Image: infrav1.Image{
247-
ID: pointer.String("imageID"),
248244
Marketplace: &infrav1.AzureMarketplaceImage{
249245
ImagePlan: infrav1.ImagePlan{
250246
Publisher: "publisher",
@@ -256,6 +252,65 @@ func Test_SDKImageToImage(t *testing.T) {
256252
},
257253
},
258254
},
255+
{
256+
Name: "shared gallery image",
257+
SDKImageRef: &compute.ImageReference{
258+
SharedGalleryImageID: pointer.String("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
259+
},
260+
Image: infrav1.Image{
261+
SharedGallery: &infrav1.AzureSharedGalleryImage{
262+
SubscriptionID: "subscription",
263+
ResourceGroup: "rg",
264+
Gallery: "gallery",
265+
Name: "image",
266+
Version: "version",
267+
},
268+
},
269+
},
270+
{
271+
Name: "community gallery image",
272+
SDKImageRef: &compute.ImageReference{
273+
CommunityGalleryImageID: pointer.String("/CommunityGalleries/gallery/Images/image/Versions/version"),
274+
},
275+
Image: infrav1.Image{
276+
ComputeGallery: &infrav1.AzureComputeGalleryImage{
277+
Gallery: "gallery",
278+
Name: "image",
279+
Version: "version",
280+
},
281+
},
282+
},
283+
{
284+
Name: "compute gallery image",
285+
SDKImageRef: &compute.ImageReference{
286+
ID: pointer.String("/subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/galleries/gallery/images/image/versions/version"),
287+
},
288+
Image: infrav1.Image{
289+
ComputeGallery: &infrav1.AzureComputeGalleryImage{
290+
Gallery: "gallery",
291+
Name: "image",
292+
Version: "version",
293+
SubscriptionID: pointer.String("subscription"),
294+
ResourceGroup: pointer.String("rg"),
295+
},
296+
},
297+
},
298+
{
299+
Name: "compute gallery image not formatted as expected",
300+
SDKImageRef: &compute.ImageReference{
301+
ID: pointer.String("/compute/gallery/not/formatted/as/expected"),
302+
},
303+
Image: infrav1.Image{
304+
ID: pointer.String("/compute/gallery/not/formatted/as/expected"),
305+
},
306+
},
307+
{
308+
Name: "community gallery image not formatted as expected",
309+
SDKImageRef: &compute.ImageReference{
310+
CommunityGalleryImageID: pointer.String("/community/gallery/not/formatted/as/expected"),
311+
},
312+
Image: infrav1.Image{},
313+
},
259314
}
260315

261316
for _, c := range cases {
@@ -319,8 +374,7 @@ func Test_SDKVMToVMSSVM(t *testing.T) {
319374
Expected: &azure.VMSSVM{
320375
ID: "vmID3",
321376
Image: infrav1.Image{
322-
ID: pointer.String("imageID"),
323-
Marketplace: &infrav1.AzureMarketplaceImage{},
377+
ID: pointer.String("imageID"),
324378
},
325379
Name: "vmwithstorage",
326380
State: "Creating",

azure/scope/machinepoolmachine.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/utils/pointer"
3232
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
3333
"sigs.k8s.io/cluster-api-provider-azure/azure"
34+
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
3435
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
3536
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
3637
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
@@ -542,6 +543,16 @@ func (s *MachinePoolMachineScope) hasLatestModelApplied(ctx context.Context) (bo
542543
return false, errors.New("machinepoolscope image must not be nil")
543544
}
544545

546+
// check if image.ID is actually a compute gallery image
547+
if s.instance.Image.ComputeGallery != nil && image.ID != nil {
548+
newImage := converters.IDImageRefToImage(*image.ID)
549+
550+
// this means the ID was a compute gallery image ID
551+
if newImage.ComputeGallery != nil {
552+
return reflect.DeepEqual(s.instance.Image, newImage), nil
553+
}
554+
}
555+
545556
// if the images match, then the VM is of the same model
546557
return reflect.DeepEqual(s.instance.Image, *image), nil
547558
}

0 commit comments

Comments
 (0)