From ef9cec15ffd88b66770c3c63c624d02c1f1a4e82 Mon Sep 17 00:00:00 2001 From: Amanuel Engeda Date: Wed, 13 Dec 2023 00:20:26 -0800 Subject: [PATCH] Fix increase --- pkg/providers/instancetype/instancetype.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/providers/instancetype/instancetype.go b/pkg/providers/instancetype/instancetype.go index e5c90866d6d0..717dac172b99 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 { @@ -115,11 +116,7 @@ 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) - - if item, ok := p.cache.Get(key); ok { - return item.([]*cloudprovider.InstanceType), nil - } + key := fmt.Sprintf("%d-%d-%d-%016x-%016x", p.instanceTypesSeqNum, p.instanceTypeOfferingsSeqNum, p.unavailableOfferings.SeqNum, subnetHash, kcHash) result := lo.Map(instanceTypes, func(i *ec2.InstanceTypeInfo, _ int) *cloudprovider.InstanceType { return NewInstanceType(ctx, i, kc, p.region, nodeClass, p.createOfferings(ctx, i, instanceTypeOfferings[aws.StringValue(i.InstanceType)], availabilityZones, subnetZones)) }) @@ -202,6 +199,7 @@ func (p *Provider) getAvailabilityZones(ctx context.Context) (sets.Set[string], logging.FromContext(ctx).With("zones", instanceTypeZones.UnsortedList()).Debugf("discovered availability zones") } p.cache.Set(AvailabilityZonesCacheKey, instanceTypeZones, 24*time.Hour) + p.cleanOldInstanceTypesFromCache() return instanceTypeZones, nil } @@ -235,6 +233,7 @@ func (p *Provider) getInstanceTypeOfferings(ctx context.Context) (map[string]set } atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1) p.cache.SetDefault(InstanceTypeOfferingsCacheKey, instanceTypeOfferings) + p.cleanOldInstanceTypesFromCache() return instanceTypeOfferings, nil } @@ -275,5 +274,16 @@ func (p *Provider) GetInstanceTypes(ctx context.Context) ([]*ec2.InstanceTypeInf } atomic.AddUint64(&p.instanceTypesSeqNum, 1) p.cache.SetDefault(InstanceTypesCacheKey, instanceTypes) + p.cleanOldInstanceTypesFromCache() return instanceTypes, nil } + +func (p *Provider) cleanOldInstanceTypesFromCache() { + cacheItemsKeys := lo.Keys(p.cache.Items()) + + for _, key := range cacheItemsKeys { + if !lo.Contains([]string{AvailabilityZonesCacheKey, InstanceTypeOfferingsCacheKey, InstanceTypesCacheKey}, key) { + p.cache.Delete(key) + } + } +}