diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 87036c0078de..148c7a8b2040 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -36,4 +36,6 @@ const ( const ( // DefaultCleanupInterval triggers cache cleanup (lazy eviction) at this interval. DefaultCleanupInterval = 10 * time.Minute + // InstanceTypesAndZonesCleanupInterval is the time before we refresh instance types and zones at EC2 + InstanceTypesAndZonesCleanupInterval = time.Minute ) diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 33d6d4528c84..4ff6b271bf98 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -160,7 +160,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont ) instanceTypeProvider := instancetype.NewProvider( *sess.Config.Region, - cache.New(awscache.InstanceTypesAndZonesTTL, awscache.DefaultCleanupInterval), + cache.New(awscache.InstanceTypesAndZonesTTL, awscache.InstanceTypesAndZonesCleanupInterval), ec2api, subnetProvider, unavailableOfferingsCache, diff --git a/pkg/providers/instancetype/instancetype.go b/pkg/providers/instancetype/instancetype.go index 05ce497b5ff0..90e59d734a3a 100644 --- a/pkg/providers/instancetype/instancetype.go +++ b/pkg/providers/instancetype/instancetype.go @@ -88,6 +88,7 @@ func NewProvider(region string, cache *cache.Cache, ec2api ec2iface.EC2API, subn } func (p *Provider) List(ctx context.Context, kc *corev1beta1.KubeletConfiguration, nodeClass *v1beta1.EC2NodeClass) ([]*cloudprovider.InstanceType, error) { + p.cache.DeleteExpired() // Get InstanceTypes from EC2 instanceTypes, err := p.GetInstanceTypes(ctx) if err != nil { @@ -112,8 +113,9 @@ func (p *Provider) List(ctx context.Context, kc *corev1beta1.KubeletConfiguratio // Compute fully initialized instance types hash key subnetHash, _ := hashstructure.Hash(subnets, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}) kcHash, _ := hashstructure.Hash(kc, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}) - key := fmt.Sprintf("%d-%d-%d-%s-%016x-%016x", p.instanceTypesSeqNum, p.instanceTypeOfferingsSeqNum, p.unavailableOfferings.SeqNum, nodeClass.UID, subnetHash, kcHash) - + amiFamilyHash, _ := hashstructure.Hash(nodeClass.Spec.AMIFamily, hashstructure.FormatV2, &hashstructure.HashOptions{}) + blockDeviceMappingsHash, _ := hashstructure.Hash(nodeClass.Spec.BlockDeviceMappings, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}) + key := fmt.Sprintf("%d-%d-%d-%016x-%016x-%016x-%016x", p.instanceTypesSeqNum, p.instanceTypeOfferingsSeqNum, p.unavailableOfferings.SeqNum, subnetHash, kcHash, amiFamilyHash, blockDeviceMappingsHash) if item, ok := p.cache.Get(key); ok { return item.([]*cloudprovider.InstanceType), nil } @@ -221,10 +223,12 @@ func (p *Provider) getInstanceTypeOfferings(ctx context.Context) (map[string]set }); err != nil { return nil, fmt.Errorf("describing instance type zone offerings, %w", err) } - if p.cm.HasChanged("instance-type-count", len(instanceTypeOfferings)) { + if p.cm.HasChanged("instance-type-offering", instanceTypeOfferings) { + // Only update instanceTypesSeqNun with the instance type offerings have been changed + // This is to not create new keys with duplicate instance type offerings option + atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1) logging.FromContext(ctx).With("instance-type-count", len(instanceTypeOfferings)).Debugf("discovered offerings for instance types") } - atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1) p.cache.SetDefault(InstanceTypeOfferingsCacheKey, instanceTypeOfferings) return instanceTypeOfferings, nil } @@ -261,10 +265,12 @@ func (p *Provider) GetInstanceTypes(ctx context.Context) ([]*ec2.InstanceTypeInf return nil, fmt.Errorf("fetching instance types using ec2.DescribeInstanceTypes, %w", err) } if p.cm.HasChanged("instance-types", instanceTypes) { + // Only update instanceTypesSeqNun with the instance types have been changed + // This is to not create new keys with duplicate instance types option + atomic.AddUint64(&p.instanceTypesSeqNum, 1) logging.FromContext(ctx).With( "count", len(instanceTypes)).Debugf("discovered instance types") } - atomic.AddUint64(&p.instanceTypesSeqNum, 1) p.cache.SetDefault(InstanceTypesCacheKey, instanceTypes) return instanceTypes, nil }