/
test.cluster.ts
2861 lines (2402 loc) · 90.8 KB
/
test.cluster.ts
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as fs from 'fs';
import * as path from 'path';
import { countResources, expect, haveResource, haveResourceLike } from '@aws-cdk/assert-internal';
import * as asg from '@aws-cdk/aws-autoscaling';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import * as cdk8s from 'cdk8s';
import * as constructs from 'constructs';
import { Test } from 'nodeunit';
import * as YAML from 'yaml';
import * as eks from '../lib';
import { BottleRocketImage } from '../lib/private/bottlerocket';
import { testFixture, testFixtureNoVpc } from './util';
/* eslint-disable max-len */
const CLUSTER_VERSION = eks.KubernetesVersion.V1_19;
export = {
'can specify custom environment to cluster resource handler'(test: Test) {
const { stack } = testFixture();
new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
clusterHandlerEnvironment: {
foo: 'bar',
},
});
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.ClusterResourceProvider') as cdk.NestedStack;
test.deepEqual(expect(nested).value.Resources.OnEventHandler42BEBAE0.Properties.Environment, { Variables: { foo: 'bar' } });
test.done();
},
'throws when trying to place cluster handlers in a vpc with no private subnets'(test: Test) {
const { stack } = testFixture();
const vpc = new ec2.Vpc(stack, 'Vpc');
test.throws(() => {
new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
placeClusterHandlerInVpc: true,
vpc: vpc,
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }],
});
}, /Cannot place cluster handler in the VPC since no private subnets could be selected/);
test.done();
},
'imported Vpc from unparseable list tokens': (() => {
let stack: cdk.Stack;
let vpc: ec2.IVpc;
return {
'setUp'(cb: () => void) {
stack = new cdk.Stack();
const vpcId = cdk.Fn.importValue('myVpcId');
const availabilityZones = cdk.Fn.split(',', cdk.Fn.importValue('myAvailabilityZones'));
const publicSubnetIds = cdk.Fn.split(',', cdk.Fn.importValue('myPublicSubnetIds'));
const privateSubnetIds = cdk.Fn.split(',', cdk.Fn.importValue('myPrivateSubnetIds'));
const isolatedSubnetIds = cdk.Fn.split(',', cdk.Fn.importValue('myIsolatedSubnetIds'));
vpc = ec2.Vpc.fromVpcAttributes(stack, 'importedVpc', {
vpcId,
availabilityZones,
publicSubnetIds,
privateSubnetIds,
isolatedSubnetIds,
});
cb();
},
'throws if selecting more than one subnet group'(test: Test) {
test.throws(() => new eks.Cluster(stack, 'Cluster', {
vpc: vpc,
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }, { subnetType: ec2.SubnetType.PRIVATE }],
defaultCapacity: 0,
version: eks.KubernetesVersion.V1_19,
}), /cannot select multiple subnet groups/);
test.done();
},
'synthesis works if only one subnet group is selected'(test: Test) {
// WHEN
new eks.Cluster(stack, 'Cluster', {
vpc: vpc,
vpcSubnets: [{ subnetType: ec2.SubnetType.PUBLIC }],
defaultCapacity: 0,
version: eks.KubernetesVersion.V1_19,
});
// THEN
expect(stack).to(haveResourceLike('Custom::AWSCDK-EKS-Cluster', {
Config: {
resourcesVpcConfig: {
subnetIds: {
'Fn::Split': [
',',
{ 'Fn::ImportValue': 'myPublicSubnetIds' },
],
},
},
},
}));
test.done();
},
};
})(),
'throws when accessing cluster security group for imported cluster without cluster security group id'(test: Test) {
const { stack } = testFixture();
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
clusterName: 'cluster',
});
test.throws(() => cluster.clusterSecurityGroup, /"clusterSecurityGroup" is not defined for this imported cluster/);
test.done();
},
'can place cluster handlers in the cluster vpc'(test: Test) {
const { stack } = testFixture();
new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
placeClusterHandlerInVpc: true,
});
const nested = stack.node.tryFindChild('@aws-cdk/aws-eks.ClusterResourceProvider') as cdk.NestedStack;
function assertFunctionPlacedInVpc(id: string) {
test.deepEqual(expect(nested).value.Resources[id].Properties.VpcConfig.SubnetIds, [
{ Ref: 'referencetoStackClusterDefaultVpcPrivateSubnet1SubnetA64D1BF0Ref' },
{ Ref: 'referencetoStackClusterDefaultVpcPrivateSubnet2Subnet32D85AB8Ref' },
]);
}
assertFunctionPlacedInVpc('OnEventHandler42BEBAE0');
assertFunctionPlacedInVpc('IsCompleteHandler7073F4DA');
assertFunctionPlacedInVpc('ProviderframeworkonEvent83C1D0A7');
assertFunctionPlacedInVpc('ProviderframeworkisComplete26D7B0CB');
assertFunctionPlacedInVpc('ProviderframeworkonTimeout0B47CA38');
test.done();
},
'can access cluster security group for imported cluster with cluster security group id'(test: Test) {
const { stack } = testFixture();
const clusterSgId = 'cluster-sg-id';
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
clusterName: 'cluster',
clusterSecurityGroupId: clusterSgId,
});
const clusterSg = cluster.clusterSecurityGroup;
test.equal(clusterSg.securityGroupId, clusterSgId);
test.done();
},
'cluster security group is attached when adding self-managed nodes'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// WHEN
cluster.addAutoScalingGroupCapacity('self-managed', {
instanceType: new ec2.InstanceType('t2.medium'),
});
test.deepEqual(expect(stack).value.Resources.ClusterselfmanagedLaunchConfigA5B57EF6.Properties.SecurityGroups, [
{ 'Fn::GetAtt': ['ClusterselfmanagedInstanceSecurityGroup64468C3A', 'GroupId'] },
{ 'Fn::GetAtt': ['Cluster9EE0221C', 'ClusterSecurityGroupId'] },
]);
test.done();
},
'security group of self-managed asg is not tagged with owned'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
version: CLUSTER_VERSION,
});
// WHEN
cluster.addAutoScalingGroupCapacity('self-managed', {
instanceType: new ec2.InstanceType('t2.medium'),
});
// make sure the "kubernetes.io/cluster/<CLUSTER_NAME>: owned" tag isn't here.
test.deepEqual(expect(stack).value.Resources.ClusterselfmanagedInstanceSecurityGroup64468C3A.Properties.Tags, [
{ Key: 'Name', Value: 'Stack/Cluster/self-managed' },
]);
test.done();
},
'cluster security group is attached when connecting self-managed nodes'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
const selfManaged = new asg.AutoScalingGroup(stack, 'self-managed', {
instanceType: new ec2.InstanceType('t2.medium'),
vpc: vpc,
machineImage: new ec2.AmazonLinuxImage(),
});
// WHEN
cluster.connectAutoScalingGroupCapacity(selfManaged, {});
test.deepEqual(expect(stack).value.Resources.selfmanagedLaunchConfigD41289EB.Properties.SecurityGroups, [
{ 'Fn::GetAtt': ['selfmanagedInstanceSecurityGroupEA6D80C9', 'GroupId'] },
{ 'Fn::GetAtt': ['Cluster9EE0221C', 'ClusterSecurityGroupId'] },
]);
test.done();
},
'spot interrupt handler is not added if spotInterruptHandler is false when connecting self-managed nodes'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
const selfManaged = new asg.AutoScalingGroup(stack, 'self-managed', {
instanceType: new ec2.InstanceType('t2.medium'),
vpc: vpc,
machineImage: new ec2.AmazonLinuxImage(),
spotPrice: '0.1',
});
// WHEN
cluster.connectAutoScalingGroupCapacity(selfManaged, { spotInterruptHandler: false });
test.equal(cluster.node.findAll().filter(c => c.node.id === 'chart-spot-interrupt-handler').length, 0);
test.done();
},
'throws when a non cdk8s chart construct is added as cdk8s chart'(test: Test) {
const { stack } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
// create a plain construct, not a cdk8s chart
const someConstruct = new constructs.Construct(stack, 'SomeConstruct');
test.throws(() => cluster.addCdk8sChart('chart', someConstruct), /Invalid cdk8s chart. Must contain a \'toJson\' method, but found undefined/);
test.done();
},
'throws when a core construct is added as cdk8s chart'(test: Test) {
const { stack } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
// create a plain construct, not a cdk8s chart
const someConstruct = new cdk.Construct(stack, 'SomeConstruct');
test.throws(() => cluster.addCdk8sChart('chart', someConstruct), /Invalid cdk8s chart. Must contain a \'toJson\' method, but found undefined/);
test.done();
},
'cdk8s chart can be added to cluster'(test: Test) {
const { stack } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
const app = new cdk8s.App();
const chart = new cdk8s.Chart(app, 'Chart');
new cdk8s.ApiObject(chart, 'FakePod', {
apiVersion: 'v1',
kind: 'Pod',
metadata: {
name: 'fake-pod',
labels: {
// adding aws-cdk token to cdk8s chart
clusterName: cluster.clusterName,
},
},
});
cluster.addCdk8sChart('cdk8s-chart', chart);
expect(stack).to(haveResourceLike('Custom::AWSCDK-EKS-KubernetesResource', {
Manifest: {
'Fn::Join': [
'',
[
'[{"apiVersion":"v1","kind":"Pod","metadata":{"labels":{"clusterName":"',
{
Ref: 'Cluster9EE0221C',
},
'"},"name":"fake-pod"}}]',
],
],
},
}));
test.done();
},
'cluster connections include both control plane and cluster security group'(test: Test) {
const { stack } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
test.deepEqual(cluster.connections.securityGroups.map(sg => stack.resolve(sg.securityGroupId)), [
{ 'Fn::GetAtt': ['Cluster9EE0221C', 'ClusterSecurityGroupId'] },
{ 'Fn::GetAtt': ['ClusterControlPlaneSecurityGroupD274242C', 'GroupId'] },
]);
test.done();
},
'can declare a security group from a different stack'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props: { sg: ec2.ISecurityGroup, vpc: ec2.IVpc }) {
super(scope, id);
this.eksCluster = new eks.Cluster(this, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
securityGroup: props.sg,
vpc: props.vpc,
});
}
}
class NetworkStack extends cdk.Stack {
public readonly securityGroup: ec2.ISecurityGroup;
public readonly vpc: ec2.IVpc;
constructor(scope: constructs.Construct, id: string) {
super(scope, id);
this.vpc = new ec2.Vpc(this, 'Vpc');
this.securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { vpc: this.vpc });
}
}
const { app } = testFixture();
const networkStack = new NetworkStack(app, 'NetworkStack');
new ClusterStack(app, 'ClusterStack', { sg: networkStack.securityGroup, vpc: networkStack.vpc });
// make sure we can synth (no circular dependencies between the stacks)
app.synth();
test.done();
},
'can declare a manifest with a token from a different stack than the cluster that depends on the cluster stack'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.eksCluster = new eks.Cluster(this, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
}
}
class ManifestStack extends cdk.Stack {
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps & { cluster: eks.Cluster }) {
super(scope, id, props);
// this role creates a dependency between this stack and the cluster stack
const role = new iam.Role(this, 'CrossRole', {
assumedBy: new iam.ServicePrincipal('sqs.amazonaws.com'),
roleName: props.cluster.clusterArn,
});
// make sure this manifest doesn't create a dependency between the cluster stack
// and this stack
new eks.KubernetesManifest(this, 'cross-stack', {
manifest: [{
kind: 'ConfigMap',
apiVersion: 'v1',
metadata: {
name: 'config-map',
},
data: {
foo: role.roleArn,
},
}],
cluster: props.cluster,
});
}
}
const { app } = testFixture();
const clusterStack = new ClusterStack(app, 'ClusterStack');
new ManifestStack(app, 'ManifestStack', { cluster: clusterStack.eksCluster });
// make sure we can synth (no circular dependencies between the stacks)
app.synth();
test.done();
},
'can declare a chart with a token from a different stack than the cluster that depends on the cluster stack'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.eksCluster = new eks.Cluster(this, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
}
}
class ChartStack extends cdk.Stack {
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps & { cluster: eks.Cluster }) {
super(scope, id, props);
// this role creates a dependency between this stack and the cluster stack
const role = new iam.Role(this, 'CrossRole', {
assumedBy: new iam.ServicePrincipal('sqs.amazonaws.com'),
roleName: props.cluster.clusterArn,
});
// make sure this chart doesn't create a dependency between the cluster stack
// and this stack
new eks.HelmChart(this, 'cross-stack', {
chart: role.roleArn,
cluster: props.cluster,
});
}
}
const { app } = testFixture();
const clusterStack = new ClusterStack(app, 'ClusterStack');
new ChartStack(app, 'ChartStack', { cluster: clusterStack.eksCluster });
// make sure we can synth (no circular dependencies between the stacks)
app.synth();
test.done();
},
'can declare a HelmChart in a different stack than the cluster'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.eksCluster = new eks.Cluster(this, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
}
}
class ChartStack extends cdk.Stack {
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps & { cluster: eks.Cluster }) {
super(scope, id, props);
const resource = new cdk.CfnResource(this, 'resource', { type: 'MyType' });
new eks.HelmChart(this, `chart-${id}`, { cluster: props.cluster, chart: resource.ref });
}
}
const { app } = testFixture();
const clusterStack = new ClusterStack(app, 'ClusterStack');
new ChartStack(app, 'ChartStack', { cluster: clusterStack.eksCluster });
// make sure we can synth (no circular dependencies between the stacks)
app.synth();
test.done();
},
'throws when declaring an ASG role in a different stack than the cluster'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.eksCluster = new eks.Cluster(this, 'Cluster', {
version: CLUSTER_VERSION,
prune: false,
});
}
}
class CapacityStack extends cdk.Stack {
public group: asg.AutoScalingGroup;
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps & { cluster: eks.Cluster }) {
super(scope, id, props);
// the role is create in this stack implicitly by the ASG
this.group = new asg.AutoScalingGroup(this, 'autoScaling', {
instanceType: new ec2.InstanceType('t3.medium'),
vpc: props.cluster.vpc,
machineImage: new eks.EksOptimizedImage({
kubernetesVersion: CLUSTER_VERSION.version,
nodeType: eks.NodeType.STANDARD,
}),
});
}
}
const { app } = testFixture();
const clusterStack = new ClusterStack(app, 'ClusterStack');
const capacityStack = new CapacityStack(app, 'CapacityStack', { cluster: clusterStack.eksCluster });
try {
clusterStack.eksCluster.connectAutoScalingGroupCapacity(capacityStack.group, {});
test.ok(false, 'expected error');
} catch (err) {
test.equal(err.message, 'CapacityStack/autoScaling/InstanceRole should be defined in the scope of the ClusterStack stack to prevent circular dependencies');
}
test.done();
},
'can declare a ServiceAccount in a different stack than the cluster'(test: Test) {
class ClusterStack extends cdk.Stack {
public eksCluster: eks.Cluster;
constructor(scope: constructs.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.eksCluster = new eks.Cluster(this, 'EKSCluster', {
version: CLUSTER_VERSION,
prune: false,
});
}
}
class AppStack extends cdk.Stack {
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps & { cluster: eks.Cluster }) {
super(scope, id, props);
new eks.ServiceAccount(this, 'testAccount', { cluster: props.cluster, name: 'test-account', namespace: 'test' });
}
}
const { app } = testFixture();
const clusterStack = new ClusterStack(app, 'EKSCluster');
new AppStack(app, 'KubeApp', { cluster: clusterStack.eksCluster });
// make sure we can synth (no circular dependencies between the stacks)
app.synth();
test.done();
},
'a default cluster spans all subnets'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
// WHEN
new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0, version: CLUSTER_VERSION, prune: false });
// THEN
expect(stack).to(haveResourceLike('Custom::AWSCDK-EKS-Cluster', {
Config: {
roleArn: { 'Fn::GetAtt': ['ClusterRoleFA261979', 'Arn'] },
version: '1.19',
resourcesVpcConfig: {
securityGroupIds: [{ 'Fn::GetAtt': ['ClusterControlPlaneSecurityGroupD274242C', 'GroupId'] }],
subnetIds: [
{ Ref: 'VPCPublicSubnet1SubnetB4246D30' },
{ Ref: 'VPCPublicSubnet2Subnet74179F39' },
{ Ref: 'VPCPrivateSubnet1Subnet8BCA10E0' },
{ Ref: 'VPCPrivateSubnet2SubnetCFCDAA7A' },
],
},
},
}));
test.done();
},
'if "vpc" is not specified, vpc with default configuration will be created'(test: Test) {
// GIVEN
const { stack } = testFixtureNoVpc();
// WHEN
new eks.Cluster(stack, 'cluster', { version: CLUSTER_VERSION, prune: false }) ;
// THEN
expect(stack).to(haveResource('AWS::EC2::VPC'));
test.done();
},
'default capacity': {
'x2 m5.large by default'(test: Test) {
// GIVEN
const { stack } = testFixtureNoVpc();
// WHEN
const cluster = new eks.Cluster(stack, 'cluster', { version: CLUSTER_VERSION, prune: false });
// THEN
test.ok(cluster.defaultNodegroup);
expect(stack).to(haveResource('AWS::EKS::Nodegroup', {
InstanceTypes: [
'm5.large',
],
ScalingConfig: {
DesiredSize: 2,
MaxSize: 2,
MinSize: 2,
},
}));
test.done();
},
'quantity and type can be customized'(test: Test) {
// GIVEN
const { stack } = testFixtureNoVpc();
// WHEN
const cluster = new eks.Cluster(stack, 'cluster', {
defaultCapacity: 10,
defaultCapacityInstance: new ec2.InstanceType('m2.xlarge'),
version: CLUSTER_VERSION,
prune: false,
});
// THEN
test.ok(cluster.defaultNodegroup);
expect(stack).to(haveResource('AWS::EKS::Nodegroup', {
ScalingConfig: {
DesiredSize: 10,
MaxSize: 10,
MinSize: 10,
},
}));
// expect(stack).to(haveResource('AWS::AutoScaling::LaunchConfiguration', { InstanceType: 'm2.xlarge' }));
test.done();
},
'defaultCapacity=0 will not allocate at all'(test: Test) {
// GIVEN
const { stack } = testFixtureNoVpc();
// WHEN
const cluster = new eks.Cluster(stack, 'cluster', { defaultCapacity: 0, version: CLUSTER_VERSION, prune: false });
// THEN
test.ok(!cluster.defaultCapacity);
expect(stack).notTo(haveResource('AWS::AutoScaling::AutoScalingGroup'));
expect(stack).notTo(haveResource('AWS::AutoScaling::LaunchConfiguration'));
test.done();
},
},
'creating a cluster tags the private VPC subnets'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
// WHEN
new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0, version: CLUSTER_VERSION, prune: false });
// THEN
expect(stack).to(haveResource('AWS::EC2::Subnet', {
Tags: [
{ Key: 'aws-cdk:subnet-name', Value: 'Private' },
{ Key: 'aws-cdk:subnet-type', Value: 'Private' },
{ Key: 'kubernetes.io/role/internal-elb', Value: '1' },
{ Key: 'Name', Value: 'Stack/VPC/PrivateSubnet1' },
],
}));
test.done();
},
'creating a cluster tags the public VPC subnets'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
// WHEN
new eks.Cluster(stack, 'Cluster', { vpc, defaultCapacity: 0, version: CLUSTER_VERSION, prune: false });
// THEN
expect(stack).to(haveResource('AWS::EC2::Subnet', {
MapPublicIpOnLaunch: true,
Tags: [
{ Key: 'aws-cdk:subnet-name', Value: 'Public' },
{ Key: 'aws-cdk:subnet-type', Value: 'Public' },
{ Key: 'kubernetes.io/role/elb', Value: '1' },
{ Key: 'Name', Value: 'Stack/VPC/PublicSubnet1' },
],
}));
test.done();
},
'adding capacity creates an ASG without a rolling update policy'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// WHEN
cluster.addAutoScalingGroupCapacity('Default', {
instanceType: new ec2.InstanceType('t2.medium'),
});
test.deepEqual(expect(stack).value.Resources.ClusterASG0E4BA723.UpdatePolicy, { AutoScalingScheduledAction: { IgnoreUnmodifiedGroupSizeProperties: true } });
test.done();
},
'adding capacity creates an ASG with tags'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// WHEN
cluster.addAutoScalingGroupCapacity('Default', {
instanceType: new ec2.InstanceType('t2.medium'),
});
// THEN
expect(stack).to(haveResource('AWS::AutoScaling::AutoScalingGroup', {
Tags: [
{
Key: { 'Fn::Join': ['', ['kubernetes.io/cluster/', { Ref: 'Cluster9EE0221C' }]] },
PropagateAtLaunch: true,
Value: 'owned',
},
{
Key: 'Name',
PropagateAtLaunch: true,
Value: 'Stack/Cluster/Default',
},
],
}));
test.done();
},
'create nodegroup with existing role'(test: Test) {
// GIVEN
const { stack } = testFixtureNoVpc();
// WHEN
const cluster = new eks.Cluster(stack, 'cluster', {
defaultCapacity: 10,
defaultCapacityInstance: new ec2.InstanceType('m2.xlarge'),
version: CLUSTER_VERSION,
prune: false,
});
const existingRole = new iam.Role(stack, 'ExistingRole', {
assumedBy: new iam.AccountRootPrincipal(),
});
new eks.Nodegroup(stack, 'Nodegroup', {
cluster,
nodeRole: existingRole,
});
// THEN
test.ok(cluster.defaultNodegroup);
expect(stack).to(haveResource('AWS::EKS::Nodegroup', {
ScalingConfig: {
DesiredSize: 10,
MaxSize: 10,
MinSize: 10,
},
}));
test.done();
},
'adding bottlerocket capacity creates an ASG with tags'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// WHEN
cluster.addAutoScalingGroupCapacity('Bottlerocket', {
instanceType: new ec2.InstanceType('t2.medium'),
machineImageType: eks.MachineImageType.BOTTLEROCKET,
});
// THEN
expect(stack).to(haveResource('AWS::AutoScaling::AutoScalingGroup', {
Tags: [
{
Key: { 'Fn::Join': ['', ['kubernetes.io/cluster/', { Ref: 'Cluster9EE0221C' }]] },
PropagateAtLaunch: true,
Value: 'owned',
},
{
Key: 'Name',
PropagateAtLaunch: true,
Value: 'Stack/Cluster/Bottlerocket',
},
],
}));
test.done();
},
'adding bottlerocket capacity with bootstrapOptions throws error'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const cluster = new eks.Cluster(stack, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
test.throws(() => cluster.addAutoScalingGroupCapacity('Bottlerocket', {
instanceType: new ec2.InstanceType('t2.medium'),
machineImageType: eks.MachineImageType.BOTTLEROCKET,
bootstrapOptions: {},
}), /bootstrapOptions is not supported for Bottlerocket/);
test.done();
},
'import cluster with new kubectl private subnets'(test: Test) {
const { stack, vpc } = testFixture();
const cluster = eks.Cluster.fromClusterAttributes(stack, 'Cluster', {
clusterName: 'cluster',
kubectlPrivateSubnetIds: vpc.privateSubnets.map(s => s.subnetId),
});
test.deepEqual(cluster.kubectlPrivateSubnets?.map(s => stack.resolve(s.subnetId)), [
{ Ref: 'VPCPrivateSubnet1Subnet8BCA10E0' },
{ Ref: 'VPCPrivateSubnet2SubnetCFCDAA7A' },
]);
test.deepEqual(cluster.kubectlPrivateSubnets?.map(s => s.node.id), [
'KubectlSubnet0',
'KubectlSubnet1',
]);
test.done();
},
'exercise export/import'(test: Test) {
// GIVEN
const { stack: stack1, vpc, app } = testFixture();
const stack2 = new cdk.Stack(app, 'stack2', { env: { region: 'us-east-1' } });
const cluster = new eks.Cluster(stack1, 'Cluster', {
vpc,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// WHEN
const imported = eks.Cluster.fromClusterAttributes(stack2, 'Imported', {
vpc: cluster.vpc,
clusterEndpoint: cluster.clusterEndpoint,
clusterName: cluster.clusterName,
securityGroupIds: cluster.connections.securityGroups.map(x => x.securityGroupId),
clusterCertificateAuthorityData: cluster.clusterCertificateAuthorityData,
clusterSecurityGroupId: cluster.clusterSecurityGroupId,
clusterEncryptionConfigKeyArn: cluster.clusterEncryptionConfigKeyArn,
});
// this should cause an export/import
new cdk.CfnOutput(stack2, 'ClusterARN', { value: imported.clusterArn });
// THEN
expect(stack2).toMatch({
Outputs: {
ClusterARN: {
Value: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':eks:us-east-1:',
{
Ref: 'AWS::AccountId',
},
':cluster/',
{
'Fn::ImportValue': 'Stack:ExportsOutputRefCluster9EE0221C4853B4C3',
},
],
],
},
},
},
});
test.done();
},
'mastersRole can be used to map an IAM role to "system:masters"'(test: Test) {
// GIVEN
const { stack, vpc } = testFixture();
const role = new iam.Role(stack, 'role', { assumedBy: new iam.AnyPrincipal() });
// WHEN
new eks.Cluster(stack, 'Cluster', {
vpc,
mastersRole: role,
defaultCapacity: 0,
version: CLUSTER_VERSION,
prune: false,
});
// THEN
expect(stack).to(haveResource(eks.KubernetesManifest.RESOURCE_TYPE, {
Manifest: {
'Fn::Join': [
'',
[
'[{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"aws-auth","namespace":"kube-system"},"data":{"mapRoles":"[{\\"rolearn\\":\\"',