forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
736 lines (678 loc) · 25.5 KB
/
api_test.go
File metadata and controls
736 lines (678 loc) · 25.5 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
package eks_test
import (
"context"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/ssm"
ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
"github.com/stretchr/testify/mock"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/credentials"
"github.com/weaveworks/eksctl/pkg/eks"
. "github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/eks/fakes"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)
const (
genericError = "generic error"
cacheFilePath = "testdata/cached-credentials.yaml"
)
type newAWSProviderEntry struct {
updateFakes func(*fakes.FakeAWSConfigurationLoader)
updateEnv func() func()
err string
}
type newClusterProviderEntry struct {
updateMocks func(*mockprovider.MockProvider)
overwriteAWSProviderBuilderMock func(pc *api.ProviderConfig, acl AWSConfigurationLoader) (api.ClusterProvider, error)
err string
}
var _ = Describe("eksctl API", func() {
DescribeTable("creating the AWS provider", func(e newAWSProviderEntry) {
fakeConfigurationLoader := fakes.FakeAWSConfigurationLoader{}
fakeConfigurationLoader.LoadDefaultConfigReturns(aws.Config{
Region: api.DefaultRegion,
}, nil)
if e.updateFakes != nil {
e.updateFakes(&fakeConfigurationLoader)
}
if e.updateEnv != nil {
resetEnv := e.updateEnv()
defer resetEnv()
}
awsProvider, err := eks.NewAWSProvider(&api.ProviderConfig{}, &fakeConfigurationLoader)
if e.err != "" {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(e.err))
return
}
Expect(err).NotTo(HaveOccurred())
// check that all provider services were or can be (lazily) initialized properly
Expect(awsProvider.CloudFormation()).NotTo(BeNil())
Expect(awsProvider.ASG()).NotTo(BeNil())
Expect(awsProvider.EKS()).NotTo(BeNil())
Expect(awsProvider.SSM()).NotTo(BeNil())
Expect(awsProvider.CloudTrail()).NotTo(BeNil())
Expect(awsProvider.CloudWatchLogs()).NotTo(BeNil())
Expect(awsProvider.IAM()).NotTo(BeNil())
Expect(awsProvider.ELB()).NotTo(BeNil())
Expect(awsProvider.ELBV2()).NotTo(BeNil())
Expect(awsProvider.STS()).NotTo(BeNil())
Expect(awsProvider.STSPresigner()).NotTo(BeNil())
Expect(awsProvider.EC2()).NotTo(BeNil())
Expect(awsProvider.Outposts()).NotTo(BeNil())
// check that region was setup properly
Expect(awsProvider.Region()).To(Equal(api.DefaultRegion))
// check that load config options were setup properly
_, options := fakeConfigurationLoader.LoadDefaultConfigArgsForCall(0)
lo := &config.LoadOptions{}
for _, loadOptionsFunc := range options {
Expect(loadOptionsFunc(lo)).NotTo(HaveOccurred())
}
// check that credentials cache was setup properly
cco := &aws.CredentialsCacheOptions{}
lo.CredentialsCacheOptions(cco)
Expect(cco.ExpiryWindow).To(Equal(30 * time.Minute))
Expect(cco.ExpiryWindowJitterFrac).To(Equal(float64(0)))
},
Entry("fails to load default config", newAWSProviderEntry{
updateFakes: func(fal *fakes.FakeAWSConfigurationLoader) {
fal.LoadDefaultConfigReturns(*aws.NewConfig(), fmt.Errorf(genericError))
},
err: genericError,
}),
Entry("cached credentials file has wrong permissions", newAWSProviderEntry{
updateEnv: func() func() {
info, err := os.Stat(cacheFilePath)
Expect(err).NotTo(HaveOccurred())
Expect(os.Chmod(cacheFilePath, 0777)).NotTo(HaveOccurred())
Expect(os.Setenv(credentials.EksctlGlobalEnableCachingEnvName, "1")).NotTo(HaveOccurred())
Expect(os.Setenv(credentials.EksctlCacheFilenameEnvName, cacheFilePath)).NotTo(HaveOccurred())
return func() {
Expect(os.Chmod(cacheFilePath, info.Mode())).NotTo(HaveOccurred())
Expect(os.Unsetenv(credentials.EksctlGlobalEnableCachingEnvName)).NotTo(HaveOccurred())
Expect(os.Unsetenv(credentials.EksctlCacheFilenameEnvName)).NotTo(HaveOccurred())
}
},
err: fmt.Sprintf("cache file %s is not private", cacheFilePath),
}),
Entry("region code is not set", newAWSProviderEntry{
updateFakes: func(fal *fakes.FakeAWSConfigurationLoader) {
fal.LoadDefaultConfigReturns(aws.Config{
Region: "",
}, nil)
},
err: "AWS Region must be set, please set the AWS Region in AWS config file or as environment variable",
}),
Entry("creates the AWS provider successfully", newAWSProviderEntry{}),
)
DescribeTable("creating the EKS provider", func(e newClusterProviderEntry) {
clusterConfig := api.NewClusterConfig()
clusterConfig.Metadata = &api.ClusterMeta{}
mockProvider := mockprovider.NewMockProvider()
mockProvider.MockSTS().On("GetCallerIdentity", mock.Anything, mock.Anything).Return(
&sts.GetCallerIdentityOutput{
Account: aws.String("accountId"),
Arn: aws.String("arn"),
},
nil,
).Once()
if e.updateMocks != nil {
e.updateMocks(mockProvider)
}
awsProviderBuilderMock := func(pc *api.ProviderConfig, acl AWSConfigurationLoader) (api.ClusterProvider, error) {
return mockProvider, nil
}
if e.overwriteAWSProviderBuilderMock != nil {
awsProviderBuilderMock = e.overwriteAWSProviderBuilderMock
}
clusterProvider, err := eks.NewHelper(context.Background(), &api.ProviderConfig{}, clusterConfig, awsProviderBuilderMock)
if e.err != "" {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring(e.err))
return
}
Expect(err).NotTo(HaveOccurred())
// check that cluster config was setup properly
Expect(clusterConfig.Metadata.AccountID).To(Equal("accountId"))
Expect(clusterConfig.Metadata.Region).To(Equal(api.DefaultRegion))
// check that kube provider was setup properly
Expect(clusterProvider.KubeProvider).NotTo(BeNil())
},
Entry("fails to create the AWS provider", newClusterProviderEntry{
overwriteAWSProviderBuilderMock: func(pc *api.ProviderConfig, acl AWSConfigurationLoader) (api.ClusterProvider, error) {
return nil, fmt.Errorf(genericError)
},
err: genericError,
}),
Entry("fails to validate auth", newClusterProviderEntry{
updateMocks: func(mp *mockprovider.MockProvider) {
_, _ = mp.STS().GetCallerIdentity(context.Background(), nil)
mp.MockSTS().On("GetCallerIdentity", mock.Anything, mock.Anything).Return(
nil, fmt.Errorf(genericError),
).Once()
},
err: fmt.Sprintf("checking AWS STS access – cannot get role ARN for current session: %s", genericError),
}),
Entry("creates the EKS provider successfully", newClusterProviderEntry{}),
)
Context("loading config files", func() {
BeforeEach(func() {
err := api.Register()
Expect(err).NotTo(HaveOccurred())
})
It("should load a valid YAML config without error", func() {
cfg, err := LoadConfigFromFile("../../examples/01-simple-cluster.yaml")
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Metadata.Name).To(Equal("cluster-1"))
Expect(cfg.NodeGroups).To(HaveLen(1))
})
It("should load a valid JSON config without error", func() {
cfg, err := LoadConfigFromFile("testdata/example.json")
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Metadata.Name).To(Equal("cluster-1"))
Expect(cfg.NodeGroups).To(HaveLen(1))
})
It("should error when version is a float, not a string", func() {
_, err := LoadConfigFromFile("testdata/bad-type-1.yaml")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-type-1.yaml": json: cannot unmarshal number into Go struct field ClusterMeta.metadata.version of type string`))
})
It("should reject unknown field in a YAML config", func() {
_, err := LoadConfigFromFile("testdata/bad-field-1.yaml")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "zone"`))
})
It("should reject unknown field in a YAML config", func() {
_, err := LoadConfigFromFile("testdata/bad-field-2.yaml")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-2.yaml": error unmarshaling JSON: while decoding JSON: json: unknown field "bar"`))
})
It("should reject unknown field in a JSON config", func() {
_, err := LoadConfigFromFile("testdata/bad-field-1.json")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/bad-field-1.json": error unmarshaling JSON: while decoding JSON: json: unknown field "nodes"`))
})
It("should reject old API version", func() {
_, err := LoadConfigFromFile("testdata/old-version.json")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(HavePrefix(`loading config file "testdata/old-version.json": no kind "ClusterConfig" is registered for version "eksctl.io/v1alpha3" in scheme`))
})
It("should error when cannot read a file", func() {
_, err := LoadConfigFromFile("../../examples/nothing.xml")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal(`reading config file "../../examples/nothing.xml": open ../../examples/nothing.xml: no such file or directory`))
})
})
Context("Dynamic AMI Resolution", func() {
var (
ng *api.NodeGroup
provider *mockprovider.MockProvider
)
BeforeEach(func() {
ng = api.NewNodeGroup()
ng.AMIFamily = api.DefaultNodeImageFamily
provider = mockprovider.NewMockProvider()
mockDescribeImages(provider, "ami-123", func(input *ec2.DescribeImagesInput) bool {
return len(input.ImageIds) == 1
})
})
testEnsureAMI := func(matcher gomegatypes.GomegaMatcher, version string) {
err := ResolveAMI(context.Background(), provider, version, ng)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, ng.AMI).To(matcher)
}
It("should resolve AMI using SSM Parameter Store by default", func() {
provider.MockSSM().On("GetParameter", mock.Anything, &ssm.GetParameterInput{
Name: aws.String("/aws/service/eks/optimized-ami/1.14/amazon-linux-2/recommended/image_id"),
}).Return(&ssm.GetParameterOutput{
Parameter: &ssmtypes.Parameter{
Value: aws.String("ami-ssm"),
},
}, nil)
testEnsureAMI(Equal("ami-ssm"), "1.14")
})
It("should fall back to auto resolution for Ubuntu1804", func() {
ng.AMIFamily = api.NodeImageFamilyUbuntu1804
mockDescribeImages(provider, "ami-ubuntu", func(input *ec2.DescribeImagesInput) bool {
return input.Owners[0] == "099720109477"
})
testEnsureAMI(Equal("ami-ubuntu"), "1.14")
})
It("should fall back to auto resolution for Ubuntu2004 on 1.14", func() {
ng.AMIFamily = api.NodeImageFamilyUbuntu2004
mockDescribeImages(provider, "ami-ubuntu", func(input *ec2.DescribeImagesInput) bool {
return input.Owners[0] == "099720109477"
})
testEnsureAMI(Equal("ami-ubuntu"), "1.14")
})
It("should resolve AMI using SSM Parameter Store for Ubuntu2004 on 1.29", func() {
provider.MockSSM().On("GetParameter", mock.Anything, &ssm.GetParameterInput{
Name: aws.String("/aws/service/canonical/ubuntu/eks/20.04/1.29/stable/current/amd64/hvm/ebs-gp2/ami-id"),
}).Return(&ssm.GetParameterOutput{
Parameter: &ssmtypes.Parameter{
Value: aws.String("ami-ubuntu"),
},
}, nil)
ng.AMIFamily = api.NodeImageFamilyUbuntu2004
testEnsureAMI(Equal("ami-ubuntu"), "1.29")
})
It("should fall back to auto resolution for Ubuntu2204", func() {
ng.AMIFamily = api.NodeImageFamilyUbuntu2204
mockDescribeImages(provider, "ami-ubuntu", func(input *ec2.DescribeImagesInput) bool {
return input.Owners[0] == "099720109477"
})
testEnsureAMI(Equal("ami-ubuntu"), "1.14")
})
It("should fall back to auto resolution for UbuntuPro2204", func() {
ng.AMIFamily = api.NodeImageFamilyUbuntuPro2204
mockDescribeImages(provider, "ami-ubuntu", func(input *ec2.DescribeImagesInput) bool {
return input.Owners[0] == "099720109477"
})
testEnsureAMI(Equal("ami-ubuntu"), "1.14")
})
It("should retrieve the AMI from EC2 when AMI is auto", func() {
ng.AMI = "auto"
ng.InstanceType = "p2.xlarge"
mockDescribeImages(provider, "ami-auto", func(input *ec2.DescribeImagesInput) bool {
return len(input.ImageIds) == 0
})
testEnsureAMI(Equal("ami-auto"), "1.14")
})
})
})
func mockDescribeImages(p *mockprovider.MockProvider, amiID string, matcher func(*ec2.DescribeImagesInput) bool) {
p.MockEC2().On("DescribeImages", mock.Anything, mock.MatchedBy(matcher)).
Return(&ec2.DescribeImagesOutput{
Images: []ec2types.Image{
{
ImageId: aws.String(amiID),
State: ec2types.ImageStateAvailable,
OwnerId: aws.String("123"),
RootDeviceType: ec2types.DeviceTypeEbs,
RootDeviceName: aws.String("/dev/sda1"),
BlockDeviceMappings: []ec2types.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/sda1"),
Ebs: &ec2types.EbsBlockDevice{
Encrypted: aws.Bool(false),
},
},
},
},
},
}, nil)
}
var _ = Describe("Setting Availability Zones", func() {
var (
provider *mockprovider.MockProvider
cfg *api.ClusterConfig
)
BeforeEach(func() {
cfg = api.NewClusterConfig()
provider = mockprovider.NewMockProvider()
})
When("the AZs were set as CLI params", func() {
When("the given params contain enough AZs", func() {
It("sets them as the AZs to be used", func() {
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{"us-east-2a", "us-east-2b"}, provider.EC2(), "")
Expect(err).NotTo(HaveOccurred())
Expect(userProvider).To(BeTrue())
})
})
When("the given params contain too few AZs", func() {
It("returns an error", func() {
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{"us-east-2a"}, provider.EC2(), "")
Expect(err).To(MatchError("only 1 zone(s) specified [us-east-2a], 2 are required (can be non-unique)"))
Expect(userProvider).To(BeFalse())
})
})
})
When("the AZs were set in the config file", func() {
When("the config file contains enough AZs", func() {
It("sets them as the AZs to be used", func() {
cfg.AvailabilityZones = []string{"us-east-2a", "us-east-2b"}
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{}, provider.EC2(), "")
Expect(err).NotTo(HaveOccurred())
Expect(userProvider).To(BeTrue())
})
})
When("the config file contains too few AZs", func() {
It("returns an error", func() {
cfg.AvailabilityZones = []string{"us-east-2a"}
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{}, provider.EC2(), "")
Expect(err).To(MatchError("only 1 zone(s) specified [us-east-2a], 2 are required (can be non-unique)"))
Expect(userProvider).To(BeFalse())
})
})
})
When("no AZs were set", func() {
When("the call to fetch AZs fails", func() {
It("returns an error", func() {
region := "us-east-2"
provider.MockEC2().On("DescribeAvailabilityZones", mock.Anything, &ec2.DescribeAvailabilityZonesInput{
Filters: []ec2types.Filter{{
Name: aws.String("region-name"),
Values: []string{region},
}, {
Name: aws.String("state"),
Values: []string{string(ec2types.AvailabilityZoneStateAvailable)},
}, {
Name: aws.String("zone-type"),
Values: []string{string(ec2types.LocationTypeAvailabilityZone)},
}},
}).Return(&ec2.DescribeAvailabilityZonesOutput{}, fmt.Errorf("err"))
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{}, provider.EC2(), region)
Expect(err).To(MatchError("getting availability zones: error getting availability zones for region us-east-2: err"))
Expect(userProvider).To(BeFalse())
})
})
When("the call to fetch AZs succeeds", func() {
It("sets random AZs", func() {
region := "us-east-2"
provider.MockEC2().On("DescribeAvailabilityZones", mock.Anything, &ec2.DescribeAvailabilityZonesInput{
Filters: []ec2types.Filter{{
Name: aws.String("region-name"),
Values: []string{region},
}, {
Name: aws.String("state"),
Values: []string{string(ec2types.AvailabilityZoneStateAvailable)},
}, {
Name: aws.String("zone-type"),
Values: []string{string(ec2types.LocationTypeAvailabilityZone)},
}},
}).Return(&ec2.DescribeAvailabilityZonesOutput{
AvailabilityZones: []ec2types.AvailabilityZone{
{
GroupName: aws.String("name"),
ZoneName: aws.String(region),
ZoneId: aws.String("id"),
},
{
GroupName: aws.String("name"),
ZoneName: aws.String(region),
ZoneId: aws.String("id"),
}},
}, nil)
userProvider, err := eks.SetAvailabilityZones(context.Background(), cfg, []string{}, provider.EC2(), region)
Expect(err).NotTo(HaveOccurred())
Expect(userProvider).To(BeFalse())
})
})
})
})
var _ = Describe("CheckInstanceAvailability", func() {
var (
provider *mockprovider.MockProvider
cfg *api.ClusterConfig
)
BeforeEach(func() {
cfg = api.NewClusterConfig()
provider = mockprovider.NewMockProvider()
provider.MockEC2().On("DescribeInstanceTypeOfferings", mock.Anything, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: aws.String("instance-type"),
Values: []string{"t2.nano"},
},
},
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
}, mock.Anything).Return(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: []ec2types.InstanceTypeOffering{
{
InstanceType: "t2.nano",
Location: aws.String("dummy-zone-1a"),
LocationType: "availability-zone",
},
},
}, nil)
})
When("instance not available in nodegroup AZ", func() {
It("errors", func() {
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
InstanceType: "t2.nano",
AvailabilityZones: []string{"dummy-zone-1b"},
},
},
}
err := eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())
Expect(err).To(MatchError(`none of the provided AZs "dummy-zone-1b" support instance type t2.nano in nodegroup ng-1`))
})
})
When("uses instance distribution", func() {
When("azs aren't supported", func() {
It("errors", func() {
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1b"},
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano"},
},
},
}
err := eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())
Expect(err).To(MatchError(`none of the provided AZs "dummy-zone-1b" support instance type t2.nano in nodegroup ng-1`))
})
})
})
When("instance available in nodegroup AZ", func() {
It("allows the usage of the instance", func() {
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a"},
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano"},
},
},
}
Expect(eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())).To(Succeed())
})
})
When("mixed instances are used", func() {
It("allows the usage of the instance", func() {
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a"},
InstanceType: "mixed",
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano"},
},
},
}
Expect(eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())).To(Succeed())
})
})
When("instance available in nodegroup AZ", func() {
It("list is deduplicated", func() {
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a"},
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano", "t2.nano"},
},
},
}
Expect(eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())).To(Succeed())
})
})
When("instance not available in any of the global AZs", func() {
It("errors", func() {
cfg.AvailabilityZones = []string{"dummy-zone-1b"}
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano"},
},
},
}
err := eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())
Expect(err).To(MatchError(`none of the provided AZs "dummy-zone-1b" support instance type t2.nano in nodegroup ng-1`))
})
})
When("az is overridden by local nodegroup's AZ", func() {
It("uses the az defined in the nodegroup", func() {
cfg.AvailabilityZones = []string{"dummy-zone-1b"}
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a"},
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano"},
},
},
}
Expect(eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())).To(Succeed())
})
})
When("more than one AZ is available and more than one AZ is returned", func() {
When("one of the instances doesn't support any AZs", func() {
It("errors", func() {
provider.MockEC2().On("DescribeInstanceTypeOfferings", mock.Anything, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: aws.String("instance-type"),
Values: []string{"t2.large", "t2.micro", "t2.nano"},
},
},
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
}, mock.Anything).Return(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: []ec2types.InstanceTypeOffering{
{
InstanceType: "t2.nano",
Location: aws.String("dummy-zone-1a"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.micro",
Location: aws.String("dummy-zone-1b"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.large",
Location: aws.String("dummy-zone-1c"),
LocationType: "availability-zone",
},
},
}, nil)
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a", "dummy-zone-1b"},
InstanceType: "t2.large",
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano", "t2.micro"},
},
},
}
cfg.ManagedNodeGroups = []*api.ManagedNodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "mng-1",
AvailabilityZones: []string{"dummy-zone-1a", "dummy-zone-1b"},
InstanceType: "t2.large",
},
},
}
err := eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())
Expect(err).To(MatchError(`none of the provided AZs "dummy-zone-1a,dummy-zone-1b" support instance type t2.large in nodegroup mng-1`))
})
})
When("all instances are available in at least one of the provided AZs", func() {
It("allows the selection", func() {
provider.MockEC2().On("DescribeInstanceTypeOfferings", mock.Anything, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: aws.String("instance-type"),
Values: []string{"t2.large", "t2.micro", "t2.nano"},
},
},
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: aws.Int32(100),
}, mock.Anything).Return(&ec2.DescribeInstanceTypeOfferingsOutput{
InstanceTypeOfferings: []ec2types.InstanceTypeOffering{
{
InstanceType: "t2.nano",
Location: aws.String("dummy-zone-1a"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.nano",
Location: aws.String("dummy-zone-1b"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.micro",
Location: aws.String("dummy-zone-1b"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.large",
Location: aws.String("dummy-zone-1a"),
LocationType: "availability-zone",
},
{
InstanceType: "t2.large",
Location: aws.String("dummy-zone-1c"),
LocationType: "availability-zone",
},
},
}, nil)
cfg.NodeGroups = []*api.NodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "ng-1",
AvailabilityZones: []string{"dummy-zone-1a", "dummy-zone-1b"},
InstanceType: "t2.large",
},
InstancesDistribution: &api.NodeGroupInstancesDistribution{
InstanceTypes: []string{"t2.nano", "t2.micro"},
},
},
}
cfg.ManagedNodeGroups = []*api.ManagedNodeGroup{
{
NodeGroupBase: &api.NodeGroupBase{
Name: "mng-1",
AvailabilityZones: []string{"dummy-zone-1a", "dummy-zone-1b"},
InstanceType: "t2.large",
},
},
}
Expect(eks.CheckInstanceAvailability(context.Background(), cfg, provider.EC2())).To(Succeed())
})
})
})
})