From e07b37d277e733fecb827dd69fba1fe2bb23a3e0 Mon Sep 17 00:00:00 2001 From: Nick Tran <10810510+njtran@users.noreply.github.com> Date: Wed, 2 Oct 2024 08:26:04 -0700 Subject: [PATCH] chore: don't panic for a nil instance type (#7144) --- pkg/providers/instancetype/instancetype.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/providers/instancetype/instancetype.go b/pkg/providers/instancetype/instancetype.go index dca16eb8bb29..ccb27b668c98 100644 --- a/pkg/providers/instancetype/instancetype.go +++ b/pkg/providers/instancetype/instancetype.go @@ -81,6 +81,7 @@ func NewDefaultProvider(instanceTypesCache *cache.Cache, ec2api ec2iface.EC2API, } } +// nolint:gocyclo func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) ([]*cloudprovider.InstanceType, error) { p.muInstanceTypesInfo.RLock() p.muInstanceTypesOfferings.RLock() @@ -130,7 +131,7 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) subnetZoneToID := lo.SliceToMap(nodeClass.Status.Subnets, func(s v1.Subnet) (string, string) { return s.Zone, s.ZoneID }) - result := lo.Map(p.instanceTypesInfo, func(i *ec2.InstanceTypeInfo, _ int) *cloudprovider.InstanceType { + result := lo.FilterMap(p.instanceTypesInfo, func(i *ec2.InstanceTypeInfo, _ int) (*cloudprovider.InstanceType, bool) { instanceTypeVCPU.With(prometheus.Labels{ instanceTypeLabel: *i.InstanceType, }).Set(float64(lo.FromPtr(i.VCpuInfo.DefaultVCpus))) @@ -152,7 +153,11 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) } }) + // If the resolver returned nil, it means that the instance type shouldn't be considered it := p.instanceTypesResolver.Resolve(ctx, i, zoneData, nodeClass) + if it == nil { + return nil, false + } for _, of := range it.Offerings { instanceTypeOfferingAvailable.With(prometheus.Labels{ instanceTypeLabel: it.Name, @@ -165,7 +170,7 @@ func (p *DefaultProvider) List(ctx context.Context, nodeClass *v1.EC2NodeClass) zoneLabel: of.Requirements.Get(corev1.LabelTopologyZone).Any(), }).Set(of.Price) } - return it + return it, true }) p.instanceTypesCache.SetDefault(key, result) return result, nil