Skip to content

Commit e46a2ce

Browse files
lilyLuLiuadrianriobo
authored andcommitted
azure windows sku filter g2 sku
1 parent 2a831cb commit e46a2ce

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (r *WindowsRequest) deployer(ctx *pulumi.Context) error {
148148
AdminUsername: r.AdminUsername,
149149
AdminPasswd: adminPasswd,
150150
SpotPrice: spotPrice,
151+
Location: rgLocation,
151152
}
152153
vm, err := vmr.Create(ctx)
153154
if err != nil {

pkg/provider/azure/data/images.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,57 @@ func IsImageOffered(req ImageRequest) bool {
5454
}
5555
return true
5656
}
57+
58+
func SkuG2Support(location string, publisher string, offer string, sku string) (string, error) {
59+
cred, err := azidentity.NewDefaultAzureCredential(nil)
60+
if err != nil {
61+
return "", err
62+
}
63+
subscriptionId := os.Getenv("AZURE_SUBSCRIPTION_ID")
64+
65+
clientFactory, err := armcompute.NewClientFactory(subscriptionId, cred, nil)
66+
if err != nil {
67+
return "", err
68+
}
69+
imagesClient := clientFactory.NewVirtualMachineImagesClient()
70+
if ! verify_g2(imagesClient, location, publisher, offer, sku) {
71+
finalSKU, err := get_g2_sku(imagesClient, location, publisher, offer, sku)
72+
if err == nil && finalSKU != "" {
73+
if verify_g2(imagesClient, location, publisher, offer, finalSKU) {
74+
fmt.Printf("%s is g1, Using SKU %s\n", sku, finalSKU)
75+
return finalSKU, nil
76+
}
77+
}
78+
} else {
79+
return sku, nil
80+
}
81+
return "", fmt.Errorf("the SKU %s is not support for G2", sku)
82+
}
83+
84+
func verify_g2(imagesClient *armcompute.VirtualMachineImagesClient, location string, publisher string, offer string, sku string) bool {
85+
// List available image versions
86+
resp, err := imagesClient.List(context.Background(),location, publisher, offer, sku, nil)
87+
if err != nil {
88+
return false
89+
}
90+
91+
image := resp.VirtualMachineImageResourceArray[0]
92+
version := *image.Name
93+
resps, _ := imagesClient.Get(context.Background(),location, publisher, offer, sku, version, nil)
94+
info := resps.VirtualMachineImage
95+
generation := *info.Properties.HyperVGeneration
96+
return generation == "V2"
97+
}
98+
99+
func get_g2_sku(imagesClient *armcompute.VirtualMachineImagesClient, location string, publisher string, offer string, originSKU string) (string, error) {
100+
resp, err := imagesClient.ListSKUs(context.Background(),location, publisher, offer, nil)
101+
if err != nil {
102+
return "", err
103+
}
104+
for _, sku := range resp.VirtualMachineImageResourceArray {
105+
if strings.HasPrefix(*sku.Name, originSKU+"-") {
106+
return *sku.Name, nil
107+
}
108+
}
109+
return "", nil
110+
}

pkg/provider/azure/module/virtual-machine/virtual-machine.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pulumi/pulumi-tls/sdk/v5/go/tls"
1111
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
1212
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
13+
"github.com/redhat-developer/mapt/pkg/provider/azure/data"
1314
"github.com/redhat-developer/mapt/pkg/util/logging"
1415
resourcesUtil "github.com/redhat-developer/mapt/pkg/util/resources"
1516
)
@@ -38,6 +39,7 @@ type VirtualMachineRequest struct {
3839
AdminPasswd *random.RandomPassword
3940
// Linux optional
4041
Userdata string
42+
Location string
4143
}
4244

4345
// Create virtual machine based on request + export to context
@@ -48,10 +50,14 @@ func (r *VirtualMachineRequest) Create(ctx *pulumi.Context) (*compute.VirtualMac
4850
imageReferenceArgs = compute.ImageReferenceArgs{
4951
CommunityGalleryImageId: pulumi.String(r.ImageID)}
5052
} else {
53+
finalSku, err := data.SkuG2Support(r.Location, r.Publisher, r.Offer, r.Sku)
54+
if err != nil {
55+
return nil, err
56+
}
5157
imageReferenceArgs = compute.ImageReferenceArgs{
5258
Publisher: pulumi.String(r.Publisher),
5359
Offer: pulumi.String(r.Offer),
54-
Sku: pulumi.String(r.Sku),
60+
Sku: pulumi.String(finalSku),
5561
Version: pulumi.String("latest"),
5662
}
5763
}

0 commit comments

Comments
 (0)