-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azure: check image availability in region for spot calculation
this adds a check to the spot calculation to only consider regions that supports the requested community gallary image
- Loading branch information
Showing
6 changed files
with
99 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6" | ||
maptAzIdentity "github.com/redhat-developer/mapt/pkg/provider/azure/module/identity" | ||
"github.com/redhat-developer/mapt/pkg/util/logging" | ||
) | ||
|
||
type ImageRequest struct { | ||
Region string | ||
ImageReference | ||
} | ||
|
||
func GetImage(req ImageRequest) (*armcompute.CommunityGalleryImagesClientGetResponse, error) { | ||
maptAzIdentity.SetAZIdentityEnvs() | ||
|
||
cred, err := azidentity.NewDefaultAzureCredential(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ctx := context.Background() | ||
subscriptionId := os.Getenv("AZURE_SUBSCRIPTION_ID") | ||
|
||
clientFactory, err := armcompute.NewClientFactory(subscriptionId, cred, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// for community gallary images | ||
if len(req.ID) > 0 { | ||
// extract gallary ID and image name from ID url which looks like: | ||
// /CommunityGalleries/Fedora-5e266ba4-2250-406d-adad-5d73860d958f/Images/Fedora-Cloud-41-x64 | ||
parts := strings.Split(req.ID, "/") | ||
if len(parts) != 4 { | ||
return nil, fmt.Errorf("invalida community gallary image ID: %s", req.ID) | ||
} | ||
res, err := clientFactory.NewCommunityGalleryImagesClient().Get(ctx, req.Region, parts[1], parts[3], nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &res, nil | ||
} | ||
// for azure offered VM images: https://learn.microsoft.com/en-us/rest/api/compute/virtual-machine-images/get | ||
// there's a different API to check but currently we only check availability of community images | ||
return nil, nil | ||
} | ||
|
||
func IsImageOffered(req ImageRequest) bool { | ||
if _, err := GetImage(req); err != nil { | ||
logging.Debugf("error while checking if image available at location: %v", err) | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters