Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: check image availability in region for spot calculation #378

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cmd/mapt/cmd/azure/hosts/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
params "github.com/redhat-developer/mapt/cmd/mapt/cmd/constants"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
azureLinux "github.com/redhat-developer/mapt/pkg/provider/azure/action/linux"
"github.com/redhat-developer/mapt/pkg/provider/azure/data"
"github.com/redhat-developer/mapt/pkg/provider/util/instancetypes"
"github.com/redhat-developer/mapt/pkg/util"

Expand All @@ -25,14 +26,14 @@ const (
)

func GetUbuntuCmd() *cobra.Command {
return getLinuxCmd(cmdUbuntu, cmdUbuntuDesc, azureLinux.Ubuntu, defaultUbuntuVersion)
return getLinuxCmd(cmdUbuntu, cmdUbuntuDesc, data.Ubuntu, defaultUbuntuVersion)
}

func GetFedoraCmd() *cobra.Command {
return getLinuxCmd(cmdFedora, cmdFedoraDesc, azureLinux.Fedora, defaultFedoraVersion)
return getLinuxCmd(cmdFedora, cmdFedoraDesc, data.Fedora, defaultFedoraVersion)
}

func getLinuxCmd(cmd, cmdDesc string, ostype azureLinux.OSType, defaultOSVersion string) *cobra.Command {
func getLinuxCmd(cmd, cmdDesc string, ostype data.OSType, defaultOSVersion string) *cobra.Command {
c := &cobra.Command{
Use: cmd,
Short: cmdDesc,
Expand All @@ -47,7 +48,7 @@ func getLinuxCmd(cmd, cmdDesc string, ostype azureLinux.OSType, defaultOSVersion
return c
}

func getCreateLinux(ostype azureLinux.OSType, defaultOSVersion string) *cobra.Command {
func getCreateLinux(ostype data.OSType, defaultOSVersion string) *cobra.Command {
c := &cobra.Command{
Use: params.CreateCmdName,
Short: params.CreateCmdName,
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/azure/action/aks/aks.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ func (r *AKSRequest) valuesCheckingSpot() (*string, *float64, error) {
if r.Spot {
bsc, err :=
spotAzure.GetBestSpotChoice(spotAzure.BestSpotChoiceRequest{
VMTypes: []string{r.VMSize},
OSType: "linux",
EvictioRateTolerance: r.SpotTolerance,
VMTypes: []string{r.VMSize},
OSType: "linux",
EvictionRateTolerance: r.SpotTolerance,
})
logging.Debugf("Best spot price option found: %v", bsc)
if err != nil {
Expand Down
82 changes: 0 additions & 82 deletions pkg/provider/azure/action/linux/imageref.go

This file was deleted.

24 changes: 15 additions & 9 deletions pkg/provider/azure/action/linux/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/redhat-developer/mapt/pkg/manager"
maptContext "github.com/redhat-developer/mapt/pkg/manager/context"
"github.com/redhat-developer/mapt/pkg/provider/azure"
"github.com/redhat-developer/mapt/pkg/provider/azure/data"
"github.com/redhat-developer/mapt/pkg/provider/azure/module/network"
virtualmachine "github.com/redhat-developer/mapt/pkg/provider/azure/module/virtual-machine"
"github.com/redhat-developer/mapt/pkg/provider/util/command"
Expand Down Expand Up @@ -39,7 +40,7 @@ type LinuxRequest struct {
VMSizes []string
Arch string
InstanceRequest instancetypes.InstanceRequest
OSType OSType
OSType data.OSType
Version string
Username string
Spot bool
Expand Down Expand Up @@ -121,7 +122,7 @@ func (r *LinuxRequest) deployer(ctx *pulumi.Context) error {
}
ctx.Export(fmt.Sprintf("%s-%s", r.Prefix, outputUserPrivateKey), privateKey.PrivateKeyPem)
// Image refence info
ir, err := getImageRef(r.OSType, r.Arch, r.Version)
ir, err := data.GetImageRef(r.OSType, r.Arch, r.Version)
if err != nil {
return err
}
Expand All @@ -132,10 +133,10 @@ func (r *LinuxRequest) deployer(ctx *pulumi.Context) error {
ResourceGroup: rg,
NetworkInteface: n.NetworkInterface,
VMSize: vmType,
Publisher: ir.publisher,
Offer: ir.offer,
Sku: ir.sku,
ImageID: ir.id,
Publisher: ir.Publisher,
Offer: ir.Offer,
Sku: ir.Sku,
ImageID: ir.ID,
AdminUsername: r.Username,
PrivateKey: privateKey,
SpotPrice: spotPrice,
Expand Down Expand Up @@ -174,11 +175,16 @@ func (r *LinuxRequest) deployer(ctx *pulumi.Context) error {

func (r *LinuxRequest) valuesCheckingSpot() (*string, string, *float64, error) {
if r.Spot {
ir, err := data.GetImageRef(r.OSType, r.Arch, r.Version)
if err != nil {
return nil, "", nil, err
}
bsc, err :=
spotAzure.GetBestSpotChoice(spotAzure.BestSpotChoiceRequest{
VMTypes: util.If(len(r.VMSizes) > 0, r.VMSizes, []string{defaultVMSize}),
OSType: "linux",
EvictioRateTolerance: r.SpotTolerance,
VMTypes: util.If(len(r.VMSizes) > 0, r.VMSizes, []string{defaultVMSize}),
OSType: "linux",
EvictionRateTolerance: r.SpotTolerance,
ImageRef: *ir,
})
logging.Debugf("Best spot price option found: %v", bsc)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/provider/azure/action/rhel/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

azureLinux "github.com/redhat-developer/mapt/pkg/provider/azure/action/linux"
"github.com/redhat-developer/mapt/pkg/provider/azure/data"
"github.com/redhat-developer/mapt/pkg/provider/util/command"
"github.com/redhat-developer/mapt/pkg/provider/util/instancetypes"
spotAzure "github.com/redhat-developer/mapt/pkg/spot/azure"
Expand Down Expand Up @@ -53,7 +54,7 @@ func Create(r *Request) (err error) {
InstanceRequest: r.InstanceRequest,
Version: r.Version,
Arch: r.Arch,
OSType: azureLinux.RHEL,
OSType: data.RHEL,
Username: r.Username,
Spot: r.Spot,
SpotTolerance: r.SpotTolerance,
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/azure/action/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ func (r *WindowsRequest) valuesCheckingSpot() (*string, string, *float64, error)
if r.Spot {
bsc, err :=
spotAzure.GetBestSpotChoice(spotAzure.BestSpotChoiceRequest{
VMTypes: util.If(len(r.VMSizes) > 0, r.VMSizes, []string{defaultVMSize}),
OSType: "windows",
EvictioRateTolerance: r.SpotTolerance,
VMTypes: util.If(len(r.VMSizes) > 0, r.VMSizes, []string{defaultVMSize}),
OSType: "windows",
EvictionRateTolerance: r.SpotTolerance,
})
logging.Debugf("Best spot price option found: %v", bsc)
if err != nil {
Expand Down
81 changes: 81 additions & 0 deletions pkg/provider/azure/data/imageref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package data

import (
"fmt"
"strings"
)

type OSType int

const (
Ubuntu OSType = iota + 1
RHEL
Fedora
)

const fedoraImageGalleryBase = "/CommunityGalleries/Fedora-5e266ba4-2250-406d-adad-5d73860d958f/Images/"

type ImageReference struct {
Publisher string
Offer string
Sku string
// community gallery image ID
ID string
}

var (
defaultImageRefs = map[OSType]map[string]ImageReference{
RHEL: {
"x86_64": {
Publisher: "RedHat",
Offer: "RHEL",
Sku: "%s_%s",
},
"arm64": {
Publisher: "RedHat",
Offer: "rhel-arm64",
Sku: "%s_%s-arm64",
},
},
Ubuntu: {
"x86_64": {
Publisher: "Canonical",
Offer: "ubuntu-%s_%s-lts-daily",
Sku: "server",
},
},
Fedora: {
"x86_64": {
ID: fedoraImageGalleryBase + "Fedora-Cloud-%s-x64/Versions/latest",
},
"arm64": {
ID: fedoraImageGalleryBase + "Fedora-Cloud-%s-Arm64/Versions/latest",
},
},
}
)

// version should came in format X.Y (major.minor)
func GetImageRef(osTarget OSType, arch string, version string) (*ImageReference, error) {
ir := defaultImageRefs[osTarget][arch]
versions := strings.Split(version, ".")
switch osTarget {
case Ubuntu:
return &ImageReference{
Publisher: ir.Publisher,
Offer: fmt.Sprintf(ir.Offer, versions[0], versions[1]),
Sku: ir.Sku,
}, nil
case RHEL:
return &ImageReference{
Publisher: ir.Publisher,
Offer: ir.Offer,
Sku: fmt.Sprintf(ir.Sku, versions[0], versions[1]),
}, nil
case Fedora:
return &ImageReference{
ID: fmt.Sprintf(ir.ID, versions[0]),
}, nil
}
return nil, fmt.Errorf("os type not supported")
}
59 changes: 59 additions & 0 deletions pkg/provider/azure/data/images.go
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
}
Loading
Loading