@@ -12,6 +12,7 @@ import (
1212 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1313 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1414 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
15+ "github.com/adrianriobo/qenvs/pkg/util"
1516 "github.com/adrianriobo/qenvs/pkg/util/logging"
1617 "golang.org/x/exp/maps"
1718)
@@ -195,16 +196,24 @@ func getBestSpotChoice(s []priceHistory, e []evictionRate, currentERT EvictionRa
195196 for _ , ev := range e {
196197 evm [fmt .Sprintf ("%s%s" , ev .Location , ev .VMType )] = ev .EvictionRate
197198 }
199+ var spotChoices []* BestSpotChoiceResponse
198200 for _ , sv := range s {
199201 er , ok := evm [fmt .Sprintf ("%s%s" , sv .Location , sv .VMType )]
202+ // If there are multiple choices we added them to a slice
203+ // and pick one randomly to improve distribution of instances
204+ // across locations
200205 if ok && er == getEvictionRateValue (currentERT ) {
201- return & BestSpotChoiceResponse {
202- VMType : sv .VMType ,
203- Location : sv .Location ,
204- Price : sv .Price ,
205- }, nil
206+ spotChoices = append (spotChoices ,
207+ & BestSpotChoiceResponse {
208+ VMType : sv .VMType ,
209+ Location : sv .Location ,
210+ Price : sv .Price ,
211+ })
206212 }
207213 }
214+ if len (spotChoices ) > 0 {
215+ return util .RandomItemFromArray (spotChoices ), nil
216+ }
208217 // If current is equal to max tolerance we can not give any spot
209218 if currentERT == maxERT {
210219 return nil , fmt .Errorf ("could not find any spot with minimum eviction rate" )
0 commit comments