Skip to content

Commit 63beb54

Browse files
author
jwcesign
authored
Merge pull request #51 from helen-frank/helen-frank/dev-general
chore: fix zoneData
2 parents 41daf4b + 595c777 commit 63beb54

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

pkg/providers/instancetype/instancetype.go

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,33 @@ type DefaultProvider struct {
6666

6767
instanceTypesInfo []*ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType
6868

69-
muInstanceTypeOfferings sync.RWMutex
70-
instanceTypeOfferings map[string]sets.Set[string]
69+
muInstanceTypesOfferings sync.RWMutex
70+
instanceTypesOfferings map[string]sets.Set[string]
7171

7272
instanceTypesCache *cache.Cache
7373

7474
unavailableOfferings *kcache.UnavailableOfferings
7575
cm *pretty.ChangeMonitor
7676
// instanceTypesSeqNum is a monotonically increasing change counter used to avoid the expensive hashing operation on instance types
7777
instanceTypesSeqNum uint64
78-
// instanceTypeOfferingsSeqNum is a monotonically increasing change counter used to avoid the expensive hashing operation on instance types
79-
instanceTypeOfferingsSeqNum uint64
78+
// instanceTypesOfferingsSeqNum is a monotonically increasing change counter used to avoid the expensive hashing operation on instance types
79+
instanceTypesOfferingsSeqNum uint64
8080
}
8181

8282
func NewDefaultProvider(region string, ecsClient *ecsclient.Client,
8383
instanceTypesCache *cache.Cache, unavailableOfferingsCache *kcache.UnavailableOfferings,
8484
pricingProvider pricing.Provider, vSwitchProvider vswitch.Provider) *DefaultProvider {
8585
return &DefaultProvider{
86-
ecsClient: ecsClient,
87-
region: region,
88-
vSwitchProvider: vSwitchProvider,
89-
pricingProvider: pricingProvider,
90-
instanceTypesInfo: []*ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType{},
91-
instanceTypeOfferings: map[string]sets.Set[string]{},
92-
instanceTypesCache: instanceTypesCache,
93-
unavailableOfferings: unavailableOfferingsCache,
94-
cm: pretty.NewChangeMonitor(),
95-
instanceTypesSeqNum: 0,
86+
ecsClient: ecsClient,
87+
region: region,
88+
vSwitchProvider: vSwitchProvider,
89+
pricingProvider: pricingProvider,
90+
instanceTypesInfo: []*ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType{},
91+
instanceTypesOfferings: map[string]sets.Set[string]{},
92+
instanceTypesCache: instanceTypesCache,
93+
unavailableOfferings: unavailableOfferingsCache,
94+
cm: pretty.NewChangeMonitor(),
95+
instanceTypesSeqNum: 0,
9696
}
9797
}
9898

@@ -105,35 +105,33 @@ func (p *DefaultProvider) LivenessProbe(req *http.Request) error {
105105

106106
func (p *DefaultProvider) List(ctx context.Context, kc *v1alpha1.KubeletConfiguration, nodeClass *v1alpha1.ECSNodeClass) ([]*cloudprovider.InstanceType, error) {
107107
p.muInstanceTypeInfo.RLock()
108-
p.muInstanceTypeOfferings.RLock()
108+
p.muInstanceTypesOfferings.RLock()
109109
defer p.muInstanceTypeInfo.RUnlock()
110-
defer p.muInstanceTypeOfferings.RUnlock()
110+
defer p.muInstanceTypesOfferings.RUnlock()
111111

112112
if kc == nil {
113113
kc = &v1alpha1.KubeletConfiguration{}
114114
}
115115
if len(p.instanceTypesInfo) == 0 {
116116
return nil, errors.New("no instance types found")
117117
}
118-
if len(p.instanceTypeOfferings) == 0 {
118+
if len(p.instanceTypesOfferings) == 0 {
119119
return nil, errors.New("no instance types offerings found")
120120
}
121-
// TODO: resolve the zones
122-
//if len(nodeClass.Status.VSwitches) == 0 {
123-
// return nil, errors.New("no vswitches found")
124-
//}
121+
if len(nodeClass.Status.VSwitches) == 0 {
122+
return nil, errors.New("no vswitches found")
123+
}
125124

126-
//vSwitchsZones := sets.New(lo.Map(nodeClass.Status.VSwitches, func(s v1alpha1.VSwitch, _ int) string {
127-
// return s.ZoneID
128-
//})...)
129-
vSwitchsZones := sets.New[string]("cn-shenzhen-a")
125+
vSwitchsZones := sets.New(lo.Map(nodeClass.Status.VSwitches, func(s v1alpha1.VSwitch, _ int) string {
126+
return s.ZoneID
127+
})...)
130128

131129
// Compute fully initialized instance types hash key
132130
vSwitchZonesHash, _ := hashstructure.Hash(vSwitchsZones, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
133131
kcHash, _ := hashstructure.Hash(kc, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
134132
key := fmt.Sprintf("%d-%d-%d-%016x-%016x",
135133
p.instanceTypesSeqNum,
136-
p.instanceTypeOfferingsSeqNum,
134+
p.instanceTypesOfferingsSeqNum,
137135
p.unavailableOfferings.SeqNum,
138136
vSwitchZonesHash,
139137
kcHash,
@@ -146,9 +144,9 @@ func (p *DefaultProvider) List(ctx context.Context, kc *v1alpha1.KubeletConfigur
146144
}
147145

148146
// Get all zones across all offerings
149-
// We don't use this in the cache key since this is produced from our instanceTypeOfferings which we do cache
147+
// We don't use this in the cache key since this is produced from our instanceTypesOfferings which we do cache
150148
allZones := sets.New[string]()
151-
for _, offeringZones := range p.instanceTypeOfferings {
149+
for _, offeringZones := range p.instanceTypesOfferings {
152150
for zone := range offeringZones {
153151
allZones.Insert(zone)
154152
}
@@ -159,13 +157,29 @@ func (p *DefaultProvider) List(ctx context.Context, kc *v1alpha1.KubeletConfigur
159157
}
160158

161159
result := lo.Map(p.instanceTypesInfo, func(i *ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType, _ int) *cloudprovider.InstanceType {
160+
zoneData := lo.Map(allZones.UnsortedList(), func(zoneID string, _ int) ZoneData {
161+
if !p.instanceTypesOfferings[lo.FromPtr(i.InstanceTypeId)].Has(zoneID) || !vSwitchsZones.Has(zoneID) {
162+
return ZoneData{
163+
ID: zoneID,
164+
Available: false,
165+
}
166+
}
167+
return ZoneData{
168+
ID: zoneID,
169+
Available: true,
170+
}
171+
})
172+
173+
// TODO: wait createOfferings ready
174+
_ = zoneData
175+
162176
// !!! Important !!!
163177
// Any changes to the values passed into the NewInstanceType method will require making updates to the cache key
164178
// so that Karpenter is able to cache the set of InstanceTypes based on values that alter the set of instance types
165179
// !!! Important !!!
166180
return NewInstanceType(ctx, i, kc, p.region,
167181
p.createOfferings(ctx, *i.InstanceTypeId, allZones,
168-
p.instanceTypeOfferings[*i.InstanceTypeId], nodeClass.Status.VSwitches))
182+
p.instanceTypesOfferings[*i.InstanceTypeId], nodeClass.Status.VSwitches))
169183
})
170184

171185
p.instanceTypesCache.SetDefault(key, result)
@@ -174,7 +188,7 @@ func (p *DefaultProvider) List(ctx context.Context, kc *v1alpha1.KubeletConfigur
174188

175189
func (p *DefaultProvider) UpdateInstanceTypes(ctx context.Context) error {
176190
// DO NOT REMOVE THIS LOCK ----------------------------------------------------------------------------
177-
// We lock here so that multiple callers to getInstanceTypeOfferings do not result in cache misses and multiple
191+
// We lock here so that multiple callers to getInstanceTypesOfferings do not result in cache misses and multiple
178192
// calls to ECS when we could have just made one call.
179193

180194
p.muInstanceTypeInfo.Lock()
@@ -200,14 +214,14 @@ func (p *DefaultProvider) UpdateInstanceTypes(ctx context.Context) error {
200214

201215
func (p *DefaultProvider) UpdateInstanceTypeOfferings(ctx context.Context) error {
202216
// DO NOT REMOVE THIS LOCK ----------------------------------------------------------------------------
203-
// We lock here so that multiple callers to getInstanceTypeOfferings do not result in cache misses and multiple
217+
// We lock here so that multiple callers to getInstanceTypesOfferings do not result in cache misses and multiple
204218
// calls to ECS when we could have just made one call.
205219

206-
p.muInstanceTypeOfferings.Lock()
207-
defer p.muInstanceTypeOfferings.Unlock()
220+
p.muInstanceTypesOfferings.Lock()
221+
defer p.muInstanceTypesOfferings.Unlock()
208222

209223
// Get offerings from ECS
210-
instanceTypeOfferings := map[string]sets.Set[string]{}
224+
instanceTypesOfferings := map[string]sets.Set[string]{}
211225
describeAvailableResourceRequest := &ecsclient.DescribeAvailableResourceRequest{
212226
RegionId: tea.String(p.region),
213227
DestinationResource: tea.String("InstanceType"),
@@ -228,21 +242,21 @@ func (p *DefaultProvider) UpdateInstanceTypeOfferings(ctx context.Context) error
228242
for _, az := range resp.Body.AvailableZones.AvailableZone {
229243
// TODO: Later, `ClosedWithStock` will be tested to determine if `ClosedWithStock` should be added.
230244
if *az.StatusCategory == "WithStock" { // WithStock, ClosedWithStock, WithoutStock, ClosedWithoutStock
231-
processAvailableResources(az, instanceTypeOfferings)
245+
processAvailableResources(az, instanceTypesOfferings)
232246
}
233247
}
234248

235-
if p.cm.HasChanged("instance-type-offering", instanceTypeOfferings) {
249+
if p.cm.HasChanged("instance-type-offering", instanceTypesOfferings) {
236250
// Only update instanceTypesSeqNun with the instance type offerings have been changed
237251
// This is to not create new keys with duplicate instance type offerings option
238-
atomic.AddUint64(&p.instanceTypeOfferingsSeqNum, 1)
239-
log.FromContext(ctx).WithValues("instance-type-count", len(instanceTypeOfferings)).V(1).Info("discovered offerings for instance types")
252+
atomic.AddUint64(&p.instanceTypesOfferingsSeqNum, 1)
253+
log.FromContext(ctx).WithValues("instance-type-count", len(instanceTypesOfferings)).V(1).Info("discovered offerings for instance types")
240254
}
241-
p.instanceTypeOfferings = instanceTypeOfferings
255+
p.instanceTypesOfferings = instanceTypesOfferings
242256
return nil
243257
}
244258

245-
func processAvailableResources(az *ecsclient.DescribeAvailableResourceResponseBodyAvailableZonesAvailableZone, instanceTypeOfferings map[string]sets.Set[string]) {
259+
func processAvailableResources(az *ecsclient.DescribeAvailableResourceResponseBodyAvailableZonesAvailableZone, instanceTypesOfferings map[string]sets.Set[string]) {
246260
if az.AvailableResources == nil || az.AvailableResources.AvailableResource == nil {
247261
return
248262
}
@@ -255,10 +269,10 @@ func processAvailableResources(az *ecsclient.DescribeAvailableResourceResponseBo
255269
for _, sr := range ar.SupportedResources.SupportedResource {
256270
// TODO: Later, `ClosedWithStock` will be tested to determine if `ClosedWithStock` should be added.
257271
if *sr.StatusCategory == "WithStock" { // WithStock, ClosedWithStock, WithoutStock, ClosedWithoutStock
258-
if _, ok := instanceTypeOfferings[*sr.Value]; !ok {
259-
instanceTypeOfferings[*sr.Value] = sets.New[string]()
272+
if _, ok := instanceTypesOfferings[*sr.Value]; !ok {
273+
instanceTypesOfferings[*sr.Value] = sets.New[string]()
260274
}
261-
instanceTypeOfferings[*sr.Value].Insert(*az.ZoneId)
275+
instanceTypesOfferings[*sr.Value].Insert(*az.ZoneId)
262276
}
263277
}
264278
}
@@ -357,6 +371,6 @@ func (p *DefaultProvider) createOffering(zone, capacityType string, vswitch *v1a
357371

358372
func (p *DefaultProvider) Reset() {
359373
p.instanceTypesInfo = []*ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType{}
360-
p.instanceTypeOfferings = map[string]sets.Set[string]{}
374+
p.instanceTypesOfferings = map[string]sets.Set[string]{}
361375
p.instanceTypesCache.Flush()
362376
}

pkg/providers/instancetype/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ const (
4949
GiBBytesRatio = 1024 * 1024 * 1024
5050
)
5151

52+
type ZoneData struct {
53+
ID string
54+
Available bool
55+
}
56+
5257
func NewInstanceType(ctx context.Context, info *ecsclient.DescribeInstanceTypesResponseBodyInstanceTypesInstanceType, kc *v1alpha1.KubeletConfiguration, region string, offerings cloudprovider.Offerings) *cloudprovider.InstanceType {
5358

5459
it := &cloudprovider.InstanceType{

0 commit comments

Comments
 (0)