From 8d5f49625cba6b17d6c280556fd9647399903067 Mon Sep 17 00:00:00 2001 From: Adrian Riobo Date: Wed, 16 Oct 2024 09:24:14 +0200 Subject: [PATCH] fix: After finding the vmtypes matching the expectations and checking spot prices we create the asg to spin the machine using the intial set of vmtypes but it may happen that any of them are not offered on the region of choice for spot price. After the spot price choice picks a region now the instace types are filtered ensuring they are offered Signed-off-by: Adrian Riobo --- pkg/provider/aws/action/fedora/fedora.go | 4 ++++ pkg/provider/aws/action/rhel/rhel.go | 4 ++++ pkg/provider/aws/data/instance-type.go | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/provider/aws/action/fedora/fedora.go b/pkg/provider/aws/action/fedora/fedora.go index 642077ce..c872c69c 100644 --- a/pkg/provider/aws/action/fedora/fedora.go +++ b/pkg/provider/aws/action/fedora/fedora.go @@ -91,6 +91,10 @@ func Create(r *Request) error { r.region = so.Region r.az = so.AvailabilityZone r.spotPrice = so.MaxPrice + r.VMType, err = data.FilterInstaceTypesOfferedByRegion(r.VMType, r.region) + if err != nil { + return err + } } else { r.region = os.Getenv("AWS_DEFAULT_REGION") az, err := data.GetRandomAvailabilityZone(r.region, nil) diff --git a/pkg/provider/aws/action/rhel/rhel.go b/pkg/provider/aws/action/rhel/rhel.go index bfff9085..c63d1e79 100644 --- a/pkg/provider/aws/action/rhel/rhel.go +++ b/pkg/provider/aws/action/rhel/rhel.go @@ -104,6 +104,10 @@ func Create(r *Request) error { r.region = so.Region r.az = so.AvailabilityZone r.spotPrice = so.MaxPrice + r.VMType, err = data.FilterInstaceTypesOfferedByRegion(r.VMType, r.region) + if err != nil { + return err + } } else { r.region = os.Getenv("AWS_DEFAULT_REGION") az, err := data.GetRandomAvailabilityZone(r.region, nil) diff --git a/pkg/provider/aws/data/instance-type.go b/pkg/provider/aws/data/instance-type.go index a69665ca..874244d4 100644 --- a/pkg/provider/aws/data/instance-type.go +++ b/pkg/provider/aws/data/instance-type.go @@ -16,15 +16,20 @@ var ( filternameInstanceType string = "instance-type" ) -// Get InstanceTypes offerings on current location func IsInstanceTypeOfferedByRegion(instanceType, region string) (bool, error) { + offerings, err := FilterInstaceTypesOfferedByRegion([]string{instanceType}, region) + return len(offerings) == 1, err +} + +// Get InstanceTypes offerings on current location +func FilterInstaceTypesOfferedByRegion(instanceTypes []string, region string) ([]string, error) { var cfgOpts config.LoadOptionsFunc if len(region) > 0 { cfgOpts = config.WithRegion(region) } cfg, err := config.LoadDefaultConfig(context.TODO(), cfgOpts) if err != nil { - return false, err + return nil, err } client := ec2.NewFromConfig(cfg) o, err := client.DescribeInstanceTypeOfferings( @@ -37,12 +42,16 @@ func IsInstanceTypeOfferedByRegion(instanceType, region string) (bool, error) { Values: []string{region}}, { Name: &filternameInstanceType, - Values: []string{instanceType}}, + Values: instanceTypes}, }}) if err != nil { - return false, err + return nil, err } - return len(o.InstanceTypeOfferings) == 1, nil + var offerings []string + for _, o := range o.InstanceTypeOfferings { + offerings = append(offerings, string(o.InstanceType)) + } + return offerings, nil } func IsInstanceTypeOfferedByAZ(region, instanceType, az string) (bool, error) {