-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathinstance.go
517 lines (480 loc) · 22.2 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instance
import (
"context"
"errors"
"fmt"
"math"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/samber/lo"
"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/logging"
corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/utils/resources"
"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1"
"github.com/aws/karpenter-provider-aws/pkg/batcher"
"github.com/aws/karpenter-provider-aws/pkg/cache"
awserrors "github.com/aws/karpenter-provider-aws/pkg/errors"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/instancetype"
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"
"github.com/aws/karpenter-provider-aws/pkg/providers/subnet"
"github.com/aws/karpenter-provider-aws/pkg/utils"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/scheduling"
)
var (
// MaxInstanceTypes defines the number of instance type options to pass to CreateFleet
MaxInstanceTypes = 60
instanceTypeFlexibilityThreshold = 5 // falling back to on-demand without flexibility risks insufficient capacity errors
instanceStateFilter = &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: aws.StringSlice([]string{ec2.InstanceStateNamePending, ec2.InstanceStateNameRunning, ec2.InstanceStateNameStopping, ec2.InstanceStateNameStopped, ec2.InstanceStateNameShuttingDown}),
}
)
type Provider struct {
region string
ec2api ec2iface.EC2API
unavailableOfferings *cache.UnavailableOfferings
instanceTypeProvider *instancetype.Provider
subnetProvider *subnet.Provider
launchTemplateProvider *launchtemplate.Provider
ec2Batcher *batcher.EC2API
}
func NewProvider(ctx context.Context, region string, ec2api ec2iface.EC2API, unavailableOfferings *cache.UnavailableOfferings,
instanceTypeProvider *instancetype.Provider, subnetProvider *subnet.Provider, launchTemplateProvider *launchtemplate.Provider) *Provider {
return &Provider{
region: region,
ec2api: ec2api,
unavailableOfferings: unavailableOfferings,
instanceTypeProvider: instanceTypeProvider,
subnetProvider: subnetProvider,
launchTemplateProvider: launchTemplateProvider,
ec2Batcher: batcher.EC2(ctx, ec2api),
}
}
func (p *Provider) Create(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType) (*Instance, error) {
instanceTypes = p.filterInstanceTypes(nodeClaim, instanceTypes)
instanceTypes = orderInstanceTypesByPrice(instanceTypes, scheduling.NewNodeSelectorRequirements(nodeClaim.Spec.Requirements...))
if len(instanceTypes) > MaxInstanceTypes {
instanceTypes = instanceTypes[0:MaxInstanceTypes]
}
tags := getTags(ctx, nodeClass, nodeClaim)
fleetInstance, err := p.launchInstance(ctx, nodeClass, nodeClaim, instanceTypes, tags)
if awserrors.IsLaunchTemplateNotFound(err) {
// retry once if launch template is not found. This allows karpenter to generate a new LT if the
// cache was out-of-sync on the first try
fleetInstance, err = p.launchInstance(ctx, nodeClass, nodeClaim, instanceTypes, tags)
}
if err != nil {
return nil, err
}
efaEnabled := lo.Contains(lo.Keys(nodeClaim.Spec.Resources.Requests), v1beta1.ResourceEFA)
return NewInstanceFromFleet(fleetInstance, tags, efaEnabled), nil
}
func (p *Provider) Get(ctx context.Context, id string) (*Instance, error) {
out, err := p.ec2Batcher.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: aws.StringSlice([]string{id}),
Filters: []*ec2.Filter{instanceStateFilter},
})
if awserrors.IsNotFound(err) {
return nil, cloudprovider.NewNodeClaimNotFoundError(err)
}
if err != nil {
return nil, fmt.Errorf("failed to describe ec2 instances, %w", err)
}
instances, err := instancesFromOutput(out)
if err != nil {
return nil, fmt.Errorf("getting instances from output, %w", err)
}
if len(instances) != 1 {
return nil, fmt.Errorf("expected a single instance, %w", err)
}
return instances[0], nil
}
func (p *Provider) List(ctx context.Context) ([]*Instance, error) {
var out = &ec2.DescribeInstancesOutput{}
err := p.ec2api.DescribeInstancesPagesWithContext(ctx, &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("tag-key"),
Values: aws.StringSlice([]string{corev1beta1.NodePoolLabelKey}),
},
{
Name: aws.String("tag-key"),
Values: aws.StringSlice([]string{v1beta1.LabelNodeClass}),
},
{
Name: aws.String("tag-key"),
Values: aws.StringSlice([]string{fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)}),
},
instanceStateFilter,
},
}, func(page *ec2.DescribeInstancesOutput, _ bool) bool {
out.Reservations = append(out.Reservations, page.Reservations...)
return true
})
if err != nil {
return nil, fmt.Errorf("describing ec2 instances, %w", err)
}
instances, err := instancesFromOutput(out)
return instances, cloudprovider.IgnoreNodeClaimNotFoundError(err)
}
func (p *Provider) Delete(ctx context.Context, id string) error {
if _, err := p.ec2Batcher.TerminateInstances(ctx, &ec2.TerminateInstancesInput{
InstanceIds: []*string{aws.String(id)},
}); err != nil {
if awserrors.IsNotFound(err) {
return cloudprovider.NewNodeClaimNotFoundError(fmt.Errorf("instance already terminated"))
}
if _, e := p.Get(ctx, id); err != nil {
if cloudprovider.IsNodeClaimNotFoundError(e) {
return e
}
err = multierr.Append(err, e)
}
return fmt.Errorf("terminating instance, %w", err)
}
return nil
}
func (p *Provider) CreateTags(ctx context.Context, id string, tags map[string]string) error {
ec2Tags := lo.MapToSlice(tags, func(key, value string) *ec2.Tag {
return &ec2.Tag{Key: aws.String(key), Value: aws.String(value)}
})
if _, err := p.ec2api.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{
Resources: aws.StringSlice([]string{id}),
Tags: ec2Tags,
}); err != nil {
if awserrors.IsNotFound(err) {
return cloudprovider.NewNodeClaimNotFoundError(fmt.Errorf("tagging instance, %w", err))
}
return fmt.Errorf("tagging instance, %w", err)
}
return nil
}
func (p *Provider) launchInstance(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, tags map[string]string) (*ec2.CreateFleetInstance, error) {
capacityType := p.getCapacityType(nodeClaim, instanceTypes)
zonalSubnets, err := p.subnetProvider.ZonalSubnetsForLaunch(ctx, nodeClass, instanceTypes, capacityType)
if err != nil {
return nil, fmt.Errorf("getting subnets, %w", err)
}
// Get Launch Template Configs, which may differ due to GPU or Architecture requirements
launchTemplateConfigs, err := p.getLaunchTemplateConfigs(ctx, nodeClass, nodeClaim, instanceTypes, zonalSubnets, capacityType, tags)
if err != nil {
return nil, fmt.Errorf("getting launch template configs, %w", err)
}
if err := p.checkODFallback(nodeClaim, instanceTypes, launchTemplateConfigs); err != nil {
logging.FromContext(ctx).Warn(err.Error())
}
// Create fleet
createFleetInput := &ec2.CreateFleetInput{
Type: aws.String(ec2.FleetTypeInstant),
Context: nodeClass.Spec.Context,
LaunchTemplateConfigs: launchTemplateConfigs,
TargetCapacitySpecification: &ec2.TargetCapacitySpecificationRequest{
DefaultTargetCapacityType: aws.String(capacityType),
TotalTargetCapacity: aws.Int64(1),
},
TagSpecifications: []*ec2.TagSpecification{
{ResourceType: aws.String(ec2.ResourceTypeInstance), Tags: utils.MergeTags(tags)},
{ResourceType: aws.String(ec2.ResourceTypeVolume), Tags: utils.MergeTags(tags)},
{ResourceType: aws.String(ec2.ResourceTypeFleet), Tags: utils.MergeTags(tags)},
},
}
if capacityType == corev1beta1.CapacityTypeSpot {
createFleetInput.SpotOptions = &ec2.SpotOptionsRequest{AllocationStrategy: aws.String(ec2.SpotAllocationStrategyPriceCapacityOptimized)}
} else {
createFleetInput.OnDemandOptions = &ec2.OnDemandOptionsRequest{AllocationStrategy: aws.String(ec2.FleetOnDemandAllocationStrategyLowestPrice)}
}
createFleetOutput, err := p.ec2Batcher.CreateFleet(ctx, createFleetInput)
p.subnetProvider.UpdateInflightIPs(createFleetInput, createFleetOutput, instanceTypes, lo.Values(zonalSubnets), capacityType)
if err != nil {
if awserrors.IsLaunchTemplateNotFound(err) {
for _, lt := range launchTemplateConfigs {
p.launchTemplateProvider.Invalidate(ctx, aws.StringValue(lt.LaunchTemplateSpecification.LaunchTemplateName), aws.StringValue(lt.LaunchTemplateSpecification.LaunchTemplateId))
}
return nil, fmt.Errorf("creating fleet %w", err)
}
var reqFailure awserr.RequestFailure
if errors.As(err, &reqFailure) {
return nil, fmt.Errorf("creating fleet %w (%s)", err, reqFailure.RequestID())
}
return nil, fmt.Errorf("creating fleet %w", err)
}
p.updateUnavailableOfferingsCache(ctx, createFleetOutput.Errors, capacityType)
if len(createFleetOutput.Instances) == 0 || len(createFleetOutput.Instances[0].InstanceIds) == 0 {
return nil, combineFleetErrors(createFleetOutput.Errors)
}
return createFleetOutput.Instances[0], nil
}
func getTags(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, nodeClaim *corev1beta1.NodeClaim) map[string]string {
staticTags := map[string]string{
fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName): "owned",
corev1beta1.NodePoolLabelKey: nodeClaim.Labels[corev1beta1.NodePoolLabelKey],
corev1beta1.ManagedByAnnotationKey: options.FromContext(ctx).ClusterName,
v1beta1.LabelNodeClass: nodeClass.Name,
}
return lo.Assign(nodeClass.Spec.Tags, staticTags)
}
func (p *Provider) checkODFallback(nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType, launchTemplateConfigs []*ec2.FleetLaunchTemplateConfigRequest) error {
// only evaluate for on-demand fallback if the capacity type for the request is OD and both OD and spot are allowed in requirements
if p.getCapacityType(nodeClaim, instanceTypes) != corev1beta1.CapacityTypeOnDemand || !scheduling.NewNodeSelectorRequirements(nodeClaim.Spec.Requirements...).Get(corev1beta1.CapacityTypeLabelKey).Has(corev1beta1.CapacityTypeSpot) {
return nil
}
// loop through the LT configs for currently considered instance types to get the flexibility count
instanceTypeZones := map[string]struct{}{}
for _, ltc := range launchTemplateConfigs {
for _, override := range ltc.Overrides {
if override.InstanceType != nil {
instanceTypeZones[*override.InstanceType] = struct{}{}
}
}
}
if len(instanceTypes) < instanceTypeFlexibilityThreshold {
return fmt.Errorf("at least %d instance types are recommended when flexible to spot but requesting on-demand, "+
"the current provisioning request only has %d instance type options", instanceTypeFlexibilityThreshold, len(instanceTypes))
}
return nil
}
func (p *Provider) getLaunchTemplateConfigs(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, nodeClaim *corev1beta1.NodeClaim,
instanceTypes []*cloudprovider.InstanceType, zonalSubnets map[string]*ec2.Subnet, capacityType string, tags map[string]string) ([]*ec2.FleetLaunchTemplateConfigRequest, error) {
var launchTemplateConfigs []*ec2.FleetLaunchTemplateConfigRequest
launchTemplates, err := p.launchTemplateProvider.EnsureAll(ctx, nodeClass, nodeClaim, instanceTypes, capacityType, tags)
if err != nil {
return nil, fmt.Errorf("getting launch templates, %w", err)
}
for _, launchTemplate := range launchTemplates {
launchTemplateConfig := &ec2.FleetLaunchTemplateConfigRequest{
Overrides: p.getOverrides(launchTemplate.InstanceTypes, zonalSubnets, scheduling.NewNodeSelectorRequirements(nodeClaim.Spec.Requirements...).Get(v1.LabelTopologyZone), capacityType, launchTemplate.ImageID),
LaunchTemplateSpecification: &ec2.FleetLaunchTemplateSpecificationRequest{
LaunchTemplateName: aws.String(launchTemplate.Name),
Version: aws.String("$Latest"),
},
}
if len(launchTemplateConfig.Overrides) > 0 {
launchTemplateConfigs = append(launchTemplateConfigs, launchTemplateConfig)
}
}
if len(launchTemplateConfigs) == 0 {
return nil, fmt.Errorf("no capacity offerings are currently available given the constraints")
}
return launchTemplateConfigs, nil
}
// getOverrides creates and returns launch template overrides for the cross product of InstanceTypes and subnets (with subnets being constrained by
// zones and the offerings in InstanceTypes)
func (p *Provider) getOverrides(instanceTypes []*cloudprovider.InstanceType, zonalSubnets map[string]*ec2.Subnet, zones *scheduling.Requirement, capacityType string, image string) []*ec2.FleetLaunchTemplateOverridesRequest {
// Unwrap all the offerings to a flat slice that includes a pointer
// to the parent instance type name
type offeringWithParentName struct {
cloudprovider.Offering
parentInstanceTypeName string
}
var unwrappedOfferings []offeringWithParentName
for _, it := range instanceTypes {
ofs := lo.Map(it.Offerings.Available(), func(of cloudprovider.Offering, _ int) offeringWithParentName {
return offeringWithParentName{
Offering: of,
parentInstanceTypeName: it.Name,
}
})
unwrappedOfferings = append(unwrappedOfferings, ofs...)
}
var overrides []*ec2.FleetLaunchTemplateOverridesRequest
for _, offering := range unwrappedOfferings {
if capacityType != offering.CapacityType {
continue
}
if !zones.Has(offering.Zone) {
continue
}
subnet, ok := zonalSubnets[offering.Zone]
if !ok {
continue
}
overrides = append(overrides, &ec2.FleetLaunchTemplateOverridesRequest{
InstanceType: aws.String(offering.parentInstanceTypeName),
SubnetId: subnet.SubnetId,
ImageId: aws.String(image),
// This is technically redundant, but is useful if we have to parse insufficient capacity errors from
// CreateFleet so that we can figure out the zone rather than additional API calls to look up the subnet
AvailabilityZone: subnet.AvailabilityZone,
})
}
return overrides
}
func (p *Provider) updateUnavailableOfferingsCache(ctx context.Context, errors []*ec2.CreateFleetError, capacityType string) {
for _, err := range errors {
if awserrors.IsUnfulfillableCapacity(err) {
p.unavailableOfferings.MarkUnavailableForFleetErr(ctx, err, capacityType)
}
}
}
// getCapacityType selects spot if both constraints are flexible and there is an
// available offering. The AWS Cloud Provider defaults to [ on-demand ], so spot
// must be explicitly included in capacity type requirements.
func (p *Provider) getCapacityType(nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType) string {
requirements := scheduling.NewNodeSelectorRequirements(nodeClaim.
Spec.Requirements...)
if requirements.Get(corev1beta1.CapacityTypeLabelKey).Has(corev1beta1.CapacityTypeSpot) {
for _, instanceType := range instanceTypes {
for _, offering := range instanceType.Offerings.Available() {
if requirements.Get(v1.LabelTopologyZone).Has(offering.Zone) && offering.CapacityType == corev1beta1.CapacityTypeSpot {
return corev1beta1.CapacityTypeSpot
}
}
}
}
return corev1beta1.CapacityTypeOnDemand
}
func orderInstanceTypesByPrice(instanceTypes []*cloudprovider.InstanceType, requirements scheduling.Requirements) []*cloudprovider.InstanceType {
// Order instance types so that we get the cheapest instance types of the available offerings
sort.Slice(instanceTypes, func(i, j int) bool {
iPrice := math.MaxFloat64
jPrice := math.MaxFloat64
if len(instanceTypes[i].Offerings.Available().Compatible(requirements)) > 0 {
iPrice = instanceTypes[i].Offerings.Available().Compatible(requirements).Cheapest().Price
}
if len(instanceTypes[j].Offerings.Available().Compatible(requirements)) > 0 {
jPrice = instanceTypes[j].Offerings.Available().Compatible(requirements).Cheapest().Price
}
if iPrice == jPrice {
return instanceTypes[i].Name < instanceTypes[j].Name
}
return iPrice < jPrice
})
return instanceTypes
}
// filterInstanceTypes is used to provide filtering on the list of potential instance types to further limit it to those
// that make the most sense given our specific AWS cloudprovider.
func (p *Provider) filterInstanceTypes(nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType) []*cloudprovider.InstanceType {
instanceTypes = filterExoticInstanceTypes(instanceTypes)
// If we could potentially launch either a spot or on-demand node, we want to filter out the spot instance types that
// are more expensive than the cheapest on-demand type.
if p.isMixedCapacityLaunch(nodeClaim, instanceTypes) {
instanceTypes = filterUnwantedSpot(instanceTypes)
}
return instanceTypes
}
// isMixedCapacityLaunch returns true if nodepools and available offerings could potentially allow either a spot or
// and on-demand node to launch
func (p *Provider) isMixedCapacityLaunch(nodeClaim *corev1beta1.NodeClaim, instanceTypes []*cloudprovider.InstanceType) bool {
requirements := scheduling.NewNodeSelectorRequirements(nodeClaim.Spec.Requirements...)
// requirements must allow both
if !requirements.Get(corev1beta1.CapacityTypeLabelKey).Has(corev1beta1.CapacityTypeSpot) ||
!requirements.Get(corev1beta1.CapacityTypeLabelKey).Has(corev1beta1.CapacityTypeOnDemand) {
return false
}
hasSpotOfferings := false
hasODOffering := false
if requirements.Get(corev1beta1.CapacityTypeLabelKey).Has(corev1beta1.CapacityTypeSpot) {
for _, instanceType := range instanceTypes {
for _, offering := range instanceType.Offerings.Available() {
if requirements.Get(v1.LabelTopologyZone).Has(offering.Zone) {
if offering.CapacityType == corev1beta1.CapacityTypeSpot {
hasSpotOfferings = true
} else {
hasODOffering = true
}
}
}
}
}
return hasSpotOfferings && hasODOffering
}
// filterUnwantedSpot is used to filter out spot types that are more expensive than the cheapest on-demand type that we
// could launch during mixed capacity-type launches
func filterUnwantedSpot(instanceTypes []*cloudprovider.InstanceType) []*cloudprovider.InstanceType {
cheapestOnDemand := math.MaxFloat64
// first, find the price of our cheapest available on-demand instance type that could support this node
for _, it := range instanceTypes {
for _, o := range it.Offerings.Available() {
if o.CapacityType == corev1beta1.CapacityTypeOnDemand && o.Price < cheapestOnDemand {
cheapestOnDemand = o.Price
}
}
}
// Filter out any types where the cheapest offering, which should be spot, is more expensive than the cheapest
// on-demand instance type that would have worked. This prevents us from getting a larger more-expensive spot
// instance type compared to the cheapest sufficiently large on-demand instance type
instanceTypes = lo.Filter(instanceTypes, func(item *cloudprovider.InstanceType, index int) bool {
available := item.Offerings.Available()
if len(available) == 0 {
return false
}
return available.Cheapest().Price <= cheapestOnDemand
})
return instanceTypes
}
// filterExoticInstanceTypes is used to eliminate less desirable instance types (like GPUs) from the list of possible instance types when
// a set of more appropriate instance types would work. If a set of more desirable instance types is not found, then the original slice
// of instance types are returned.
func filterExoticInstanceTypes(instanceTypes []*cloudprovider.InstanceType) []*cloudprovider.InstanceType {
var genericInstanceTypes []*cloudprovider.InstanceType
for _, it := range instanceTypes {
// deprioritize metal even if our opinionated filter isn't applied due to something like an instance family
// requirement
if _, ok := lo.Find(it.Requirements.Get(v1beta1.LabelInstanceSize).Values(), func(size string) bool { return strings.Contains(size, "metal") }); ok {
continue
}
if !resources.IsZero(it.Capacity[v1beta1.ResourceAWSNeuron]) ||
!resources.IsZero(it.Capacity[v1beta1.ResourceAMDGPU]) ||
!resources.IsZero(it.Capacity[v1beta1.ResourceNVIDIAGPU]) ||
!resources.IsZero(it.Capacity[v1beta1.ResourceHabanaGaudi]) {
continue
}
genericInstanceTypes = append(genericInstanceTypes, it)
}
// if we got some subset of instance types, then prefer to use those
if len(genericInstanceTypes) != 0 {
return genericInstanceTypes
}
return instanceTypes
}
func instancesFromOutput(out *ec2.DescribeInstancesOutput) ([]*Instance, error) {
if len(out.Reservations) == 0 {
return nil, cloudprovider.NewNodeClaimNotFoundError(fmt.Errorf("instance not found"))
}
instances := lo.Flatten(lo.Map(out.Reservations, func(r *ec2.Reservation, _ int) []*ec2.Instance {
return r.Instances
}))
if len(instances) == 0 {
return nil, cloudprovider.NewNodeClaimNotFoundError(fmt.Errorf("instance not found"))
}
// Get a consistent ordering for instances
sort.Slice(instances, func(i, j int) bool {
return aws.StringValue(instances[i].InstanceId) < aws.StringValue(instances[j].InstanceId)
})
return lo.Map(instances, func(i *ec2.Instance, _ int) *Instance { return NewInstance(i) }), nil
}
func combineFleetErrors(errors []*ec2.CreateFleetError) (errs error) {
unique := sets.NewString()
for _, err := range errors {
unique.Insert(fmt.Sprintf("%s: %s", aws.StringValue(err.ErrorCode), aws.StringValue(err.ErrorMessage)))
}
for errorCode := range unique {
errs = multierr.Append(errs, fmt.Errorf(errorCode))
}
// If all the Fleet errors are ICE errors then we should wrap the combined error in the generic ICE error
iceErrorCount := lo.CountBy(errors, func(err *ec2.CreateFleetError) bool { return awserrors.IsUnfulfillableCapacity(err) })
if iceErrorCount == len(errors) {
return cloudprovider.NewInsufficientCapacityError(fmt.Errorf("with fleet error(s), %w", errs))
}
return fmt.Errorf("with fleet error(s), %w", errs)
}