Skip to content

Commit f1ee47c

Browse files
committed
azure windows sku filter g2 sku
1 parent ab689ee commit f1ee47c

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

pkg/provider/azure/action/windows/windows.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import (
3131
"github.com/redhat-developer/mapt/pkg/util/logging"
3232
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
3333
"golang.org/x/exp/slices"
34+
35+
armcompute "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
36+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
37+
"context"
38+
"os"
3439
)
3540

3641
//go:embed rhqp-ci-setup.ps1
@@ -149,6 +154,10 @@ func (r *WindowsRequest) deployer(ctx *pulumi.Context) error {
149154
AdminPasswd: adminPasswd,
150155
SpotPrice: spotPrice,
151156
}
157+
err = SkuG2Support(&vmr, *location)
158+
if err != nil {
159+
return err
160+
}
152161
vm, err := vmr.Create(ctx)
153162
if err != nil {
154163
return err
@@ -371,3 +380,58 @@ func (r *WindowsRequest) profilesAsParams() string {
371380
})
372381
return strings.Join(pp, " ")
373382
}
383+
384+
func SkuG2Support(r *virtualmachine.VirtualMachineRequest, location string) error {
385+
cred, err := azidentity.NewDefaultAzureCredential(nil)
386+
if err != nil {
387+
return err
388+
}
389+
subscriptionId := os.Getenv("AZURE_SUBSCRIPTION_ID")
390+
391+
clientFactory, err := armcompute.NewClientFactory(subscriptionId, cred, nil)
392+
if err != nil {
393+
return err
394+
}
395+
imagesClient := clientFactory.NewVirtualMachineImagesClient()
396+
if ! verify_g2(imagesClient, location, r.Publisher, r.Offer, r.Sku) {
397+
finalSKU, err := get_g2_sku(imagesClient, location, r.Publisher, r.Offer, r.Sku)
398+
if err == nil && finalSKU != "" {
399+
if verify_g2(imagesClient, location, r.Publisher, r.Offer, finalSKU) {
400+
r.Sku = finalSKU
401+
fmt.Printf("%s is g1, Using SKU %s\n", r.Sku, finalSKU)
402+
return nil
403+
}
404+
}
405+
} else {
406+
return nil
407+
}
408+
return fmt.Errorf("the SKU %s is not support for G2", r.Sku)
409+
}
410+
411+
func verify_g2(imagesClient *armcompute.VirtualMachineImagesClient, location string, publisher string, offer string, sku string) bool {
412+
// List available image versions
413+
resp, err := imagesClient.List(context.Background(),location, publisher, offer, sku, nil)
414+
if err != nil {
415+
return false
416+
}
417+
418+
image := resp.VirtualMachineImageResourceArray[0]
419+
version := *image.Name
420+
resps, _ := imagesClient.Get(context.Background(),location, publisher, offer, sku, version, nil)
421+
info := resps.VirtualMachineImage
422+
generation := *info.Properties.HyperVGeneration
423+
return generation == "V2"
424+
}
425+
426+
func get_g2_sku(imagesClient *armcompute.VirtualMachineImagesClient, location string, publisher string, offer string, originSKU string) (string, error) {
427+
resp, err := imagesClient.ListSKUs(context.Background(),location, publisher, offer, nil)
428+
if err != nil {
429+
return "", err
430+
}
431+
for _, sku := range resp.VirtualMachineImageResourceArray {
432+
if strings.HasPrefix(*sku.Name, originSKU+"-") {
433+
return *sku.Name, nil
434+
}
435+
}
436+
return "", nil
437+
}

0 commit comments

Comments
 (0)