Skip to content

Commit

Permalink
check if best spot price location supports resource group
Browse files Browse the repository at this point in the history
the location suggested by the spot calculation sometimes do not
support the creation of Resource Groups  which is a requirement
for `mapt`, In those cases we just use a default region

we get the following error when a region is not usable:

```
Diagnostics:
  azure-native:resources:ResourceGroup (az-ghrunner-awd-rg):
    error: autorest/azure: Service returned an error. Status=400 Code="LocationNotAvailableForResourceGroup" [...]
```
  • Loading branch information
anjannath authored and adrianriobo committed Dec 17, 2024
1 parent b9f1dc9 commit 0515c52
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/provider/azure/action/aks/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ func (r *AKSRequest) deployer(ctx *pulumi.Context) error {
if err != nil {
return err
}
// Get location for creating Resouce Group
rgLocation := azure.GetSuitableLocationForResourceGroup(*location)
rg, err := resources.NewResourceGroup(ctx,
resourcesUtil.GetResourceName(r.Prefix, azureAKSID, "rg"),
&resources.ResourceGroupArgs{
Location: pulumi.String(*location),
Location: pulumi.String(rgLocation),
ResourceGroupName: pulumi.String(maptContext.RunID()),
Tags: maptContext.ResourceTags(),
})
Expand Down
5 changes: 4 additions & 1 deletion pkg/provider/azure/action/linux/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ func (r *LinuxRequest) deployer(ctx *pulumi.Context) error {
if err != nil {
return err
}

// Get location for creating the Resource Group
rgLocation := azure.GetSuitableLocationForResourceGroup(*location)
rg, err := resources.NewResourceGroup(ctx,
resourcesUtil.GetResourceName(r.Prefix, azureLinuxID, "rg"),
&resources.ResourceGroupArgs{
Location: pulumi.String(*location),
Location: pulumi.String(rgLocation),
ResourceGroupName: pulumi.String(maptContext.RunID()),
Tags: maptContext.ResourceTags(),
})
Expand Down
4 changes: 3 additions & 1 deletion pkg/provider/azure/action/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ func (r *WindowsRequest) deployer(ctx *pulumi.Context) error {
if err != nil {
return err
}
// Get location for creating Resource Group
rgLocation := azure.GetSuitableLocationForResourceGroup(*location)
rg, err := resources.NewResourceGroup(ctx,
resourcesUtil.GetResourceName(r.Prefix, azureWindowsDesktopID, "rg"),
&resources.ResourceGroupArgs{
Location: pulumi.String(*location),
Location: pulumi.String(rgLocation),
ResourceGroupName: pulumi.String(maptContext.RunID()),
Tags: maptContext.ResourceTags(),
})
Expand Down
64 changes: 63 additions & 1 deletion pkg/provider/azure/azure.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package azure

import (
"slices"

"github.com/redhat-developer/mapt/pkg/manager"
"github.com/redhat-developer/mapt/pkg/manager/credentials"
)
Expand All @@ -11,7 +13,55 @@ func GetClouProviderCredentials(fixedCredentials map[string]string) credentials.
FixedCredentials: fixedCredentials}
}

var DefaultCredentials = GetClouProviderCredentials(nil)
var (
DefaultCredentials = GetClouProviderCredentials(nil)
locationsSupportingResourceGroup = []string{
"eastasia",
"southeastasia",
"australiaeast",
"australiasoutheast",
"brazilsouth",
"canadacentral",
"canadaeast",
"switzerlandnorth",
"germanywestcentral",
"eastus2",
"eastus",
"centralus",
"northcentralus",
"francecentral",
"uksouth",
"ukwest",
"centralindia",
"southindia",
"jioindiawest",
"italynorth",
"japaneast",
"japanwest",
"koreacentral",
"koreasouth",
"mexicocentral",
"northeurope",
"norwayeast",
"polandcentral",
"qatarcentral",
"spaincentral",
"swedencentral",
"uaenorth",
"westcentralus",
"westeurope",
"westus2",
"westus",
"southcentralus",
"westus3",
"southafricanorth",
"australiacentral",
"australiacentral2",
"israelcentral",
"westindia",
"newzealandnorth",
}
)

func Destroy(projectName, backedURL, stackName string) error {
stack := manager.Stack{
Expand All @@ -21,3 +71,15 @@ func Destroy(projectName, backedURL, stackName string) error {
ProviderCredentials: DefaultCredentials}
return manager.DestroyStack(stack)
}

func locationSupportsResourceGroup(location string) bool {
return slices.Contains(locationsSupportingResourceGroup, location)
}

func GetSuitableLocationForResourceGroup(location string) string {
if locationSupportsResourceGroup(location) {
return location
}

return locationsSupportingResourceGroup[0]
}

0 comments on commit 0515c52

Please sign in to comment.