@@ -65,7 +65,7 @@ func getSpotInfo(mCtx *mc.Context, args *spot.SpotRequestArgs) (*spot.SpotResult
6565
6666type 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+
179196func 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
339356type 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}
0 commit comments