Skip to content

Commit d0edcd0

Browse files
committed
chore: refactor image check for azure during spot calculations
previously spot calculations check evictions and prices and then filter if image is available... this commit filter locations at very beguinning excluding those not having the image, then the set of data, and rquired api invocations are reduced. Signed-off-by: Adrian Riobo <ariobolo@redhat.com>
1 parent 159fd57 commit d0edcd0

3 files changed

Lines changed: 43 additions & 41 deletions

File tree

pkg/provider/azure/data/images.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1010
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v7"
11+
mc "github.com/redhat-developer/mapt/pkg/manager/context"
1112
"github.com/redhat-developer/mapt/pkg/util/logging"
1213
)
1314

@@ -47,9 +48,11 @@ func GetImage(req ImageRequest) (*armcompute.CommunityGalleryImagesClientGetResp
4748
return nil, nil
4849
}
4950

50-
func IsImageOffered(req ImageRequest) bool {
51+
func IsImageOffered(mCtx *mc.Context, req ImageRequest) bool {
5152
if _, err := GetImage(req); err != nil {
52-
logging.Debugf("error while checking if image available at location: %v", err)
53+
if mCtx.Debug() {
54+
logging.Debugf("error while checking if image available at location: %v", err)
55+
}
5356
return false
5457
}
5558
return true

pkg/provider/azure/data/spot.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func getSpotInfo(mCtx *mc.Context, args *spot.SpotRequestArgs) (*spot.SpotResult
6565

6666
type SpotInfoArgs struct {
6767
ComputeSizes []string
68-
ImageRef ImageReference
68+
ImageRef *ImageReference
6969
OSType string
7070
ExcludedLocations []string
7171
SpotTolerance *spot.Tolerance
@@ -86,17 +86,10 @@ func SpotInfo(mCtx *mc.Context, args *SpotInfoArgs) (*spot.SpotResults, error) {
8686
if args.SpotTolerance == nil {
8787
args.SpotTolerance = &spot.DefaultTolerance
8888
}
89-
// Get all available locations for subscription allowing PublicIPs
90-
locations, err := LocationsBySupportedResourceType(RTPublicIPAddresses)
89+
locations, err := filterLocations(mCtx, args)
9190
if err != nil {
9291
return nil, err
9392
}
94-
if len(args.ExcludedLocations) > 0 {
95-
locations = util.ArrayFilter(locations,
96-
func(item string) bool {
97-
return !slices.Contains(args.ExcludedLocations, item)
98-
})
99-
}
10093
clientFactory, err := getGraphClientFactory()
10194
if err != nil {
10295
return nil, err
@@ -131,8 +124,6 @@ func SpotInfo(mCtx *mc.Context, args *SpotInfoArgs) (*spot.SpotResults, error) {
131124
&spotChoiceArgs{
132125
evictionRates: evictionRates,
133126
spotPricings: spotPricings,
134-
// TODO CHECK this
135-
imageRef: &args.ImageRef,
136127
})
137128
if err != nil {
138129
return nil, err
@@ -176,6 +167,32 @@ var evictionRatesToInt = map[string]int{
176167
"20+": 4,
177168
}
178169

170+
// filter locations suitable for running mapt targets on spot instances
171+
func filterLocations(mCtx *mc.Context, args *SpotInfoArgs) ([]string, error) {
172+
// Get all available locations for subscription allowing PublicIPs
173+
locations, err := LocationsBySupportedResourceType(RTPublicIPAddresses)
174+
if err != nil {
175+
return nil, err
176+
}
177+
if len(args.ExcludedLocations) > 0 {
178+
locations = util.ArrayFilter(locations,
179+
func(location string) bool {
180+
return !slices.Contains(args.ExcludedLocations, location)
181+
})
182+
}
183+
if args.ImageRef != nil {
184+
locations = util.ArrayFilter(locations,
185+
func(location string) bool {
186+
return IsImageOffered(mCtx,
187+
ImageRequest{
188+
Region: location,
189+
ImageReference: *args.ImageRef,
190+
})
191+
})
192+
}
193+
return locations, err
194+
}
195+
179196
func allowedER(spotTolerance spot.Tolerance) []string {
180197
idx := slices.IndexFunc(evictionRates,
181198
func(e evictionRateSpec) bool {
@@ -339,7 +356,6 @@ func spotPricingAsync(location string, args spotPricingArgs, c chan hostingPlace
339356
type spotChoiceArgs struct {
340357
evictionRates map[string][]evictionRateResult
341358
spotPricings map[string][]spotPricingResult
342-
imageRef *ImageReference
343359
}
344360

345361
// checkBestOption will cross data from prices (starting at lower prices)
@@ -353,22 +369,14 @@ func selectSpotChoice(args *spotChoiceArgs) (*SpotInfoResult, error) {
353369
result := make(map[string]*SpotInfoResult)
354370
// Fix random error with graphql query not giving information for eviction rates
355371
if len(args.evictionRates) == 0 {
356-
return spotOnlyByPrices(args.spotPricings, args.imageRef.ID)
372+
return spotOnlyByPrices(args.spotPricings)
357373
}
358374
// This can bexecuted async
359375
for l, pss := range args.spotPricings {
360-
validImage := true
361-
if args.imageRef != nil {
362-
validImage = IsImageOffered(
363-
ImageRequest{
364-
Region: l,
365-
ImageReference: *args.imageRef,
366-
})
367-
}
368376
for _, ps := range pss {
369377
idx := slices.IndexFunc(args.evictionRates[l],
370378
func(evr evictionRateResult) bool {
371-
return evr.Location == ps.Location && validImage
379+
return evr.Location == ps.Location
372380
})
373381
if idx != -1 {
374382
result[l] = &SpotInfoResult{
@@ -394,24 +402,15 @@ func selectSpotChoice(args *spotChoiceArgs) (*SpotInfoResult, error) {
394402
// // This is a fallback function in case we need to get an option only based in price
395403
// // In order to add some type of distribution across the information we will 1/3 at beguining
396404
// // 1/3 at the end and then randomly we will pick one of the remaining
397-
func spotOnlyByPrices(s map[string][]spotPricingResult, imageID string) (*SpotInfoResult, error) {
405+
func spotOnlyByPrices(s map[string][]spotPricingResult) (*SpotInfoResult, error) {
398406
var bsp []*SpotInfoResult
399407
for location, prices := range s {
400-
ir := ImageRequest{
401-
Region: location,
402-
ImageReference: ImageReference{
403-
ID: imageID,
404-
},
405-
}
406-
if IsImageOffered(ir) {
407-
bsp = append(bsp,
408-
&SpotInfoResult{
409-
ComputeSize: prices[0].ComputeSize,
410-
Location: location,
411-
Price: prices[0].Price,
412-
})
413-
414-
}
408+
bsp = append(bsp,
409+
&SpotInfoResult{
410+
ComputeSize: prices[0].ComputeSize,
411+
Location: location,
412+
Price: prices[0].Price,
413+
})
415414
}
416415
return util.RandomItemFromArray(bsp), nil
417416
}

pkg/provider/azure/modules/allocation/allocation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func Allocation(mCtx *mc.Context, args *AllocationArgs) (*AllocationResult, erro
4949
OSType: args.OSType,
5050
}
5151
if args.ImageRef != nil {
52-
sArgs.ImageRef = *args.ImageRef
52+
sArgs.ImageRef = args.ImageRef
5353
}
5454
bsc, err := data.SpotInfo(mCtx, sArgs)
5555
if err != nil {

0 commit comments

Comments
 (0)