Skip to content

Commit cc7c34b

Browse files
authored
Merge pull request #3469 from newrelic-forks/cherry-pick-3414-to-release-1.7
[release-1.7] update SDKImageToImage func to handle different image types
2 parents 955962e + 9c552b9 commit cc7c34b

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,6 +17,8 @@ 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
"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.
2938
func 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.
152161
func 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
}

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: to.StringPtr("imageID"),
185-
Marketplace: &infrav1.AzureMarketplaceImage{},
184+
ID: to.StringPtr("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: to.StringPtr("imageID"),
229228
},
230229
IsThirdParty: false,
231230
Image: infrav1.Image{
232-
ID: to.StringPtr("imageID"),
233-
Marketplace: &infrav1.AzureMarketplaceImage{},
231+
ID: to.StringPtr("imageID"),
234232
},
235233
},
236234
{
237235
Name: "marketplace image",
238236
SDKImageRef: &compute.ImageReference{
239-
ID: to.StringPtr("imageID"),
240237
Publisher: to.StringPtr("publisher"),
241238
Offer: to.StringPtr("offer"),
242239
Sku: to.StringPtr("sku"),
243240
Version: to.StringPtr("version"),
244241
},
245242
IsThirdParty: true,
246243
Image: infrav1.Image{
247-
ID: to.StringPtr("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: to.StringPtr("/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: to.StringPtr("/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: to.StringPtr("/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: to.StringPtr("subscription"),
294+
ResourceGroup: to.StringPtr("rg"),
295+
},
296+
},
297+
},
298+
{
299+
Name: "compute gallery image not formatted as expected",
300+
SDKImageRef: &compute.ImageReference{
301+
ID: to.StringPtr("/compute/gallery/not/formatted/as/expected"),
302+
},
303+
Image: infrav1.Image{
304+
ID: to.StringPtr("/compute/gallery/not/formatted/as/expected"),
305+
},
306+
},
307+
{
308+
Name: "community gallery image not formatted as expected",
309+
SDKImageRef: &compute.ImageReference{
310+
CommunityGalleryImageID: to.StringPtr("/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: to.StringPtr("imageID"),
323-
Marketplace: &infrav1.AzureMarketplaceImage{},
377+
ID: to.StringPtr("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)