Skip to content

Commit

Permalink
Fix increase
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Dec 13, 2023
1 parent 53e8963 commit ef9cec1
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions pkg/providers/instancetype/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit ef9cec1

Please sign in to comment.