-
Notifications
You must be signed in to change notification settings - Fork 82
/
cluster.ts
956 lines (856 loc) · 37.2 KB
/
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
// Copyright 2016-2019, Pulumi Corporation.
//
// 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.
import * as aws from "@pulumi/aws";
import * as k8s from "@pulumi/kubernetes";
import * as pulumi from "@pulumi/pulumi";
import * as https from "https";
import * as jsyaml from "js-yaml";
import fetch from "node-fetch";
import * as which from "which";
import { VpcCni, VpcCniOptions } from "./cni";
import { createDashboard } from "./dashboard";
import { createNodeGroup, NodeGroup, NodeGroupBaseOptions, NodeGroupData } from "./nodegroup";
import { createNodeGroupSecurityGroup } from "./securitygroup";
import { ServiceRole } from "./servicerole";
import { createStorageClass, EBSVolumeType, StorageClass } from "./storageclass";
import { InputTags, UserStorageClasses } from "./utils";
/**
* RoleMapping describes a mapping from an AWS IAM role to a Kubernetes user and groups.
*/
export interface RoleMapping {
/**
* The ARN of the IAM role to add.
*/
roleArn: pulumi.Input<aws.ARN>;
/**
* The user name within Kubernetes to map to the IAM role. By default, the user name is the ARN of the IAM role.
*/
username: pulumi.Input<string>;
/**
* A list of groups within Kubernetes to which the role is mapped.
*/
groups: pulumi.Input<pulumi.Input<string>[]>;
}
/**
* UserMapping describes a mapping from an AWS IAM user to a Kubernetes user and groups.
*/
export interface UserMapping {
/**
* The ARN of the IAM user to add.
*/
userArn: pulumi.Input<aws.ARN>;
/**
* The user name within Kubernetes to map to the IAM user. By default, the user name is the ARN of the IAM user.
*/
username: pulumi.Input<string>;
/**
* A list of groups within Kubernetes to which the user is mapped to.
*/
groups: pulumi.Input<pulumi.Input<string>[]>;
}
/**
* CreationRoleProvider is a component containing the AWS Role and Provider necessary to override the `[system:master]`
* entity ARN. This is an optional argument used in `ClusterOptions`. Read more: https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html
*/
export interface CreationRoleProvider {
role: aws.iam.Role;
provider: pulumi.ProviderResource;
}
/**
* CoreData defines the core set of data associated with an EKS cluster, including the network in which it runs.
*/
export interface CoreData {
cluster: aws.eks.Cluster;
vpcId: pulumi.Output<string>;
subnetIds: pulumi.Output<string[]>;
clusterSecurityGroup: aws.ec2.SecurityGroup;
provider: k8s.Provider;
instanceRoles: pulumi.Output<aws.iam.Role[]>;
nodeGroupOptions: ClusterNodeGroupOptions;
publicSubnetIds?: pulumi.Output<string[]>;
privateSubnetIds?: pulumi.Output<string[]>;
eksNodeAccess?: k8s.core.v1.ConfigMap;
storageClasses?: UserStorageClasses;
kubeconfig?: pulumi.Output<any>;
vpcCni?: VpcCni;
tags?: InputTags;
nodeSecurityGroupTags?: InputTags;
}
function createOrGetInstanceProfile(name: string, parent: pulumi.ComponentResource, instanceRoleName?: pulumi.Input<aws.iam.Role>, instanceProfileName?: pulumi.Input<string>): aws.iam.InstanceProfile {
let instanceProfile: aws.iam.InstanceProfile;
if (instanceProfileName) {
instanceProfile = aws.iam.InstanceProfile.get(`${name}-instanceProfile`,
instanceProfileName, undefined, {parent: parent});
} else {
instanceProfile = new aws.iam.InstanceProfile(`${name}-instanceProfile`, {
role: instanceRoleName,
}, { parent: parent });
}
return instanceProfile;
}
function generateKubeconfig(clusterName: string, clusterEndpoint: string, certData: string, roleArn?: string) {
let args = ["token", "-i", clusterName];
if (roleArn) {
args = [...args, "-r", roleArn];
}
return {
apiVersion: "v1",
clusters: [{
cluster: {
server: clusterEndpoint,
"certificate-authority-data": certData,
},
name: "kubernetes",
}],
contexts: [{
context: {
cluster: "kubernetes",
user: "aws",
},
name: "aws",
}],
"current-context": "aws",
kind: "Config",
users: [{
name: "aws",
user: {
exec: {
apiVersion: "client.authentication.k8s.io/v1alpha1",
command: "aws-iam-authenticator",
args: args,
},
},
}],
};
}
export function getRoleProvider(name: string, region?: aws.Region, profile?: string): CreationRoleProvider {
const iamRole = new aws.iam.Role(`${name}-eksClusterCreatorRole`, {
assumeRolePolicy: aws.getCallerIdentity().then(id => `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${id.accountId}:root"
},
"Action": "sts:AssumeRole"
}
]
}`,
),
description: `Admin access to eks-${name}`,
});
// `eks:*` is needed to create/read/update/delete the EKS cluster, `iam:PassRole` is needed to pass the EKS service role to the cluster
// https://docs.aws.amazon.com/eks/latest/userguide/service_IAM_role.html
const rolePolicy = new aws.iam.RolePolicy(`${name}-eksClusterCreatorPolicy`, {
role: iamRole,
policy: {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: "eks:*",
Resource: "*",
},
{
Effect: "Allow",
Action: "iam:PassRole",
Resource: "*",
},
],
},
},
{ parent: iamRole },
);
const provider = new aws.Provider(`${name}-eksClusterCreatorEntity`, {
region: region,
profile: profile,
assumeRole: {
roleArn: iamRole.arn.apply(async(arn) => {
// wait 30 seconds to assume the IAM Role https://github.com/pulumi/pulumi-aws/issues/673
if (!pulumi.runtime.isDryRun()) {
await new Promise(resolve => setTimeout(resolve, 30 * 1000));
}
return arn;
}),
},
}, { parent: iamRole });
return {
role: iamRole,
provider: provider,
};
}
export function createCore(name: string, args: ClusterOptions, parent: pulumi.ComponentResource): CoreData {
// Check to ensure that aws-iam-authenticator is installed, as we'll need it in order to deploy k8s resources
// to the EKS cluster.
try {
which.sync("aws-iam-authenticator");
} catch (err) {
throw new Error("Could not find aws-iam-authenticator for EKS. See https://github.com/pulumi/eks#installing for installation instructions.");
}
if (args.instanceRole && args.instanceRoles) {
throw new Error("instanceRole and instanceRoles are mutually exclusive, and cannot both be set.");
}
if (args.subnetIds && (args.publicSubnetIds || args.privateSubnetIds)) {
throw new Error("subnetIds, and the use of publicSubnetIds and/or privateSubnetIds are mutually exclusive. Choose a single approach.");
}
if (args.nodeGroupOptions && (
args.nodeSubnetIds ||
args.nodeAssociatePublicIpAddress ||
args.instanceType ||
args.instanceProfileName ||
args.nodePublicKey ||
args.nodeRootVolumeSize ||
args.nodeUserData ||
args.minSize ||
args.maxSize ||
args.desiredCapacity ||
args.nodeAmiId)) {
throw new Error("Setting nodeGroupOptions, and any set of singular node group option(s) on the cluster, is mutually exclusive. Choose a single approach.");
}
// Configure the node group options.
const nodeGroupOptions: ClusterNodeGroupOptions = args.nodeGroupOptions || {
nodeSubnetIds: args.nodeSubnetIds,
nodeAssociatePublicIpAddress: args.nodeAssociatePublicIpAddress,
instanceType: args.instanceType,
nodePublicKey: args.nodePublicKey,
nodeRootVolumeSize: args.nodeRootVolumeSize,
nodeUserData: args.nodeUserData,
minSize: args.minSize,
maxSize: args.maxSize,
desiredCapacity: args.desiredCapacity,
amiId: args.nodeAmiId,
version: args.version,
};
// Configure default networking architecture.
let vpcId: pulumi.Input<string> = args.vpcId!;
let clusterSubnetIds: pulumi.Input<pulumi.Input<string>[]> = [];
// If no VPC is set, use the default VPC's subnets.
if (!args.vpcId) {
const invokeOpts = { parent: parent };
const vpc = aws.ec2.getVpc({ default: true }, invokeOpts);
vpcId = vpc.then(v => v.id);
clusterSubnetIds = vpc.then(v => aws.ec2.getSubnetIds({ vpcId: v.id }, invokeOpts)).then(subnets => subnets.ids);
}
// Form the subnetIds to use on the cluster from either:
// - subnetIds
// - A combination of privateSubnetIds and/or publicSubnetIds.
if (args.subnetIds !== undefined) {
clusterSubnetIds = args.subnetIds;
} else if (args.publicSubnetIds !== undefined || args.privateSubnetIds !== undefined) {
clusterSubnetIds = pulumi.all([
args.publicSubnetIds || [],
args.privateSubnetIds || [],
]).apply(([publicIds, privateIds]) => {
return [...publicIds, ...privateIds];
});
}
// Create the EKS service role
let eksRole: pulumi.Output<aws.iam.Role>;
if (args.serviceRole) {
eksRole = pulumi.output(args.serviceRole);
} else {
eksRole = (new ServiceRole(`${name}-eksRole`, {
service: "eks.amazonaws.com",
description: "Allows EKS to manage clusters on your behalf.",
managedPolicyArns: [
"arn:aws:iam::aws:policy/AmazonEKSClusterPolicy",
"arn:aws:iam::aws:policy/AmazonEKSServicePolicy",
],
}, { parent: parent })).role;
}
// Create the EKS cluster security group
let eksClusterSecurityGroup: aws.ec2.SecurityGroup;
if (args.clusterSecurityGroup) {
eksClusterSecurityGroup = args.clusterSecurityGroup;
} else {
eksClusterSecurityGroup = new aws.ec2.SecurityGroup(`${name}-eksClusterSecurityGroup`, {
vpcId: vpcId,
revokeRulesOnDelete: true,
tags: pulumi.all([
args.tags,
args.clusterSecurityGroupTags,
]).apply(([tags, clusterSecurityGroupTags]) => (<aws.Tags>{
"Name": `${name}-eksClusterSecurityGroup`,
...clusterSecurityGroupTags,
...tags,
})),
}, { parent: parent });
const eksClusterInternetEgressRule = new aws.ec2.SecurityGroupRule(`${name}-eksClusterInternetEgressRule`, {
description: "Allow internet access.",
type: "egress",
fromPort: 0,
toPort: 0,
protocol: "-1", // all
cidrBlocks: ["0.0.0.0/0"],
securityGroupId: eksClusterSecurityGroup.id,
}, { parent: parent });
}
// Create the EKS cluster
const eksCluster = new aws.eks.Cluster(`${name}-eksCluster`, {
roleArn: eksRole.apply(r => r.arn),
vpcConfig: {
securityGroupIds: [eksClusterSecurityGroup.id],
subnetIds: clusterSubnetIds,
endpointPrivateAccess: args.endpointPrivateAccess,
endpointPublicAccess: args.endpointPublicAccess,
},
version: args.version,
enabledClusterLogTypes: args.enabledClusterLogTypes,
}, {
parent: parent,
provider: args.creationRoleProvider ? args.creationRoleProvider.provider : undefined,
});
// Instead of using the kubeconfig directly, we also add a wait of up to 5 minutes or until we
// can reach the API server for the Output that provides access to the kubeconfig string so that
// there is time for the cluster API server to become completely available. Ideally we
// would rely on the EKS API only returning once this was available, but we have seen frequent
// cases where it is not yet available immediately after provisioning - possibly due to DNS
// propagation delay or other non-deterministic factors.
const endpoint = eksCluster.endpoint.apply(async (clusterEndpoint) => {
if (!pulumi.runtime.isDryRun()) {
// For up to 300 seconds, try to contact the API cluster endpoint and verify that it is reachable.
const agent = new https.Agent({ rejectUnauthorized: false });
for (let i = 0; i < 60; i++) {
try {
const resp = await fetch(clusterEndpoint, { agent });
await resp.json();
break;
} catch (e) {
pulumi.log.info(`Waiting for cluster endpoint (${i + 1})`, eksCluster, undefined, true);
}
await new Promise(resolve => setTimeout(resolve, 5 * 1000));
}
}
return clusterEndpoint;
});
// Compute the required kubeconfig. Note that we do not export this value: we want the exported config to
// depend on the autoscaling group we'll create later so that nothing attempts to use the EKS cluster before
// its worker nodes have come up.
const kubeconfig = pulumi.all([eksCluster.name, endpoint, eksCluster.certificateAuthority])
.apply(([clusterName, clusterEndpoint, clusterCertificateAuthority]) => {
let config = {};
if (args.creationRoleProvider) {
config = args.creationRoleProvider.role.arn.apply(arn => generateKubeconfig(clusterName, clusterEndpoint, clusterCertificateAuthority.data, arn));
} else {
config = generateKubeconfig(clusterName, clusterEndpoint, clusterCertificateAuthority.data);
}
return config;
});
const provider = new k8s.Provider(`${name}-eks-k8s`, {
kubeconfig: kubeconfig.apply(JSON.stringify),
}, { parent: parent });
// Add any requested StorageClasses.
const storageClasses = args.storageClasses || {};
const userStorageClasses: UserStorageClasses = pulumi.output({});
if (typeof storageClasses === "string") {
const storageClass = { type: storageClasses, default: true };
userStorageClasses[storageClasses] = pulumi.output(
createStorageClass(`${name.toLowerCase()}-${storageClasses}`, storageClass, { parent: parent, provider: provider }));
} else {
for (const key of Object.keys(storageClasses)) {
userStorageClasses[key] = pulumi.output(createStorageClass(`${name.toLowerCase()}-${key}`, storageClasses[key], { parent: parent, provider: provider }));
}
}
// Create the VPC CNI management resource.
const vpcCni = new VpcCni(`${name}-vpc-cni`, kubeconfig, args.vpcCniOptions, { parent: parent });
let instanceRoleMappings: pulumi.Output<RoleMapping[]>;
let instanceRoles: pulumi.Output<aws.iam.Role[]>;
// Create role mappings of the instance roles specified for aws-auth.
if (args.instanceRoles) {
instanceRoleMappings = pulumi.output(args.instanceRoles).apply(roles =>
roles.map(role => createInstanceRoleMapping(role.arn)),
);
instanceRoles = pulumi.output(args.instanceRoles);
} else if (args.instanceRole) {
// Create an instance profile if using a default node group
if (!args.skipDefaultNodeGroup) {
nodeGroupOptions.instanceProfile = createOrGetInstanceProfile(name, parent, args.instanceRole, args.instanceProfileName);
}
instanceRoleMappings = pulumi.output(args.instanceRole).apply(instanceRole =>
[createInstanceRoleMapping(instanceRole.arn)],
);
instanceRoles = pulumi.output([args.instanceRole]);
} else {
const instanceRole = (new ServiceRole(`${name}-instanceRole`, {
service: "ec2.amazonaws.com",
managedPolicyArns: [
"arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
"arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
],
}, { parent: parent })).role;
instanceRoles = pulumi.output([instanceRole]);
// Create a new policy for the role, if specified.
if (args.customInstanceRolePolicy) {
pulumi.log.warn("Option `customInstanceRolePolicy` has been deprecated. Please use `instanceRole` or `instanceRoles`. The role provided to either option should already include all required policies.", eksCluster);
const customRolePolicy = new aws.iam.RolePolicy(`${name}-EKSWorkerCustomPolicy`, {
role: instanceRole,
policy: args.customInstanceRolePolicy,
});
}
// Create an instance profile if using a default node group
if (!args.skipDefaultNodeGroup) {
nodeGroupOptions.instanceProfile = createOrGetInstanceProfile(name, parent, instanceRole, args.instanceProfileName);
}
instanceRoleMappings = pulumi.output(instanceRole).apply(role =>
[createInstanceRoleMapping(role.arn)],
);
}
const roleMappings = pulumi.all([pulumi.output(args.roleMappings || []), instanceRoleMappings])
.apply(([mappings, instanceMappings]) => {
let mappingYaml = "";
try {
mappingYaml = jsyaml.safeDump([...mappings, ...instanceMappings].map(m => ({
rolearn: m.roleArn,
username: m.username,
groups: m.groups,
})));
} catch (e) {
throw new Error(`The IAM role mappings provided could not be properly serialized to YAML for the aws-auth ConfigMap`);
}
return mappingYaml;
});
const nodeAccessData: any = {
mapRoles: roleMappings,
};
if (args.userMappings) {
nodeAccessData.mapUsers = pulumi.output(args.userMappings).apply(mappings => {
let mappingYaml = "";
try {
mappingYaml = jsyaml.safeDump(mappings.map(m => ({
userarn: m.userArn,
username: m.username,
groups: m.groups,
})));
} catch (e) {
throw new Error(`The IAM user mappings provided could not be properly serialized to YAML for the aws-auth ConfigMap`);
}
return mappingYaml;
});
}
const eksNodeAccess = new k8s.core.v1.ConfigMap(`${name}-nodeAccess`, {
apiVersion: "v1",
metadata: {
name: `aws-auth`,
namespace: "kube-system",
},
data: nodeAccessData,
}, { parent: parent, provider: provider });
return {
vpcId: pulumi.output(vpcId),
subnetIds: args.subnetIds ? pulumi.output(args.subnetIds): pulumi.output(clusterSubnetIds),
publicSubnetIds: args.publicSubnetIds ? pulumi.output(args.publicSubnetIds): undefined,
privateSubnetIds: args.privateSubnetIds ? pulumi.output(args.privateSubnetIds): undefined,
clusterSecurityGroup: eksClusterSecurityGroup,
cluster: eksCluster,
nodeGroupOptions: nodeGroupOptions,
kubeconfig: kubeconfig,
provider: provider,
vpcCni: vpcCni,
instanceRoles: instanceRoles,
eksNodeAccess: eksNodeAccess,
tags: args.tags,
nodeSecurityGroupTags: args.nodeSecurityGroupTags,
storageClasses: userStorageClasses,
};
}
/**
* Enable access to the EKS cluster for worker nodes, by creating an
* instance role mapping to the k8s username and groups of aws-auth.
*/
function createInstanceRoleMapping(arn: pulumi.Input<string>): RoleMapping {
return {
roleArn: arn,
username: "system:node:{{EC2PrivateDNSName}}",
groups: ["system:bootstrappers", "system:nodes"],
};
}
/**
* ClusterOptions describes the configuration options accepted by an EKSCluster component.
*/
export interface ClusterOptions {
/**
* The VPC in which to create the cluster and its worker nodes. If unset, the cluster will be created in the
* default VPC.
*/
vpcId?: pulumi.Input<string>;
/**
* The set of all subnets, public and private, to use for the worker node
* groups on the EKS cluster. These subnets are automatically tagged by EKS
* for Kubernetes purposes.
*
* If `vpcId` is not set, the cluster will use the AWS account's default VPC subnets.
*
* If the list of subnets includes both public and private subnets, the worker
* nodes will only be attached to the private subnets, and the public
* subnets will be used for internet-facing load balancers.
*
* See for more details: https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html.
*
* Note: The use of `subnetIds`, along with `publicSubnetIds`
* and/or `privateSubnetIds` is mutually exclusive. The use of
* `publicSubnetIds` and `privateSubnetIds` is encouraged.
*/
subnetIds?: pulumi.Input<pulumi.Input<string>[]>;
/**
* The set of public subnets to use for the worker node groups on the EKS cluster.
* These subnets are automatically tagged by EKS for Kubernetes purposes.
*
* If `vpcId` is not set, the cluster will use the AWS account's default VPC subnets.
*
* Worker network architecture options:
* - Private-only: Only set `privateSubnetIds`.
* - Default workers to run in a private subnet. In this setting, Kubernetes
* cannot create public, internet-facing load balancers for your pods.
* - Public-only: Only set `publicSubnetIds`.
* - Default workers to run in a public subnet.
* - Mixed (recommended): Set both `privateSubnetIds` and `publicSubnetIds`.
* - Default all worker nodes to run in private subnets, and use the public subnets
* for internet-facing load balancers.
*
* See for more details: https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html.
*
* Note: The use of `subnetIds`, along with `publicSubnetIds`
* and/or `privateSubnetIds` is mutually exclusive. The use of
* `publicSubnetIds` and `privateSubnetIds` is encouraged.
*/
publicSubnetIds?: pulumi.Input<pulumi.Input<string>[]>;
/*
* The set of private subnets to use for the worker node groups on the EKS cluster.
* These subnets are automatically tagged by EKS for Kubernetes purposes.
*
* If `vpcId` is not set, the cluster will use the AWS account's default VPC subnets.
*
* Worker network architecture options:
* - Private-only: Only set `privateSubnetIds`.
* - Default workers to run in a private subnet. In this setting, Kubernetes
* cannot create public, internet-facing load balancers for your pods.
* - Public-only: Only set `publicSubnetIds`.
* - Default workers to run in a public subnet.
* - Mixed (recommended): Set both `privateSubnetIds` and `publicSubnetIds`.
* - Default all worker nodes to run in private subnets, and use the public subnets
* for internet-facing load balancers.
*
* See for more details: https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html.
*
* Note: The use of `subnetIds`, along with `publicSubnetIds`
* and/or `privateSubnetIds` is mutually exclusive. The use of
* `publicSubnetIds` and `privateSubnetIds` is encouraged.
*
* Also consider setting `nodeAssociatePublicIpAddress: true` for
* fully private workers.
*/
privateSubnetIds?: pulumi.Input<pulumi.Input<string>[]>;
/**
* The common configuration settings for NodeGroups.
*/
nodeGroupOptions?: ClusterNodeGroupOptions;
/**
* Whether or not to auto-assign the EKS worker nodes public IP addresses.
* If this toggle is set to true, the EKS workers will be
* auto-assigned public IPs. If false, they will not be auto-assigned
* public IPs.
*/
nodeAssociatePublicIpAddress?: boolean;
/**
* Optional mappings from AWS IAM roles to Kubernetes users and groups.
*/
roleMappings?: pulumi.Input<pulumi.Input<RoleMapping>[]>;
/**
* Optional mappings from AWS IAM users to Kubernetes users and groups.
*/
userMappings?: pulumi.Input<pulumi.Input<UserMapping>[]>;
/**
* The configuration of the Amazon VPC CNI plugin for this instance. Defaults are described in the documentation
* for the VpcCniOptions type.
*/
vpcCniOptions?: VpcCniOptions;
/**
* The instance type to use for the cluster's nodes. Defaults to "t2.medium".
*/
instanceType?: pulumi.Input<aws.ec2.InstanceType>;
/**
* This enables the simple case of only registering a *single* IAM
* instance role with the cluster, that is required to be shared by
* *all* node groups in their instance profiles.
*
* Note: options `instanceRole` and `instanceRoles` are mutually exclusive.
*/
instanceRole?: pulumi.Input<aws.iam.Role>;
/**
* The default IAM InstanceProfile to use on the Worker NodeGroups, if one is not already set in the NodeGroup.
*/
instanceProfileName?: pulumi.Input<string>;
/**
* IAM Service Role for EKS to use to manage the cluster.
*/
serviceRole?: pulumi.Input<aws.iam.Role>;
/**
* The IAM Role Provider used to create & authenticate against the EKS cluster. This role is given `[system:masters]`
* permission in K8S, See: https://docs.aws.amazon.com/eks/latest/userguide/add-user-role.html
*/
creationRoleProvider?: CreationRoleProvider;
/**
* This enables the advanced case of registering *many* IAM instance roles
* with the cluster for per node group IAM, instead of the simpler, shared case of `instanceRole`.
*
* Note: options `instanceRole` and `instanceRoles` are mutually exclusive.
*/
instanceRoles?: pulumi.Input<pulumi.Input<aws.iam.Role>[]>;
/**
* @deprecated This option has been replaced with the use of
* `instanceRole` or `instanceRoles`. The role provided to either option
* should already include all required policies.
*
* Attach a custom role policy to worker node instance role
*/
customInstanceRolePolicy?: pulumi.Input<string>;
/**
* The AMI to use for worker nodes. Defaults to the value of Amazon EKS - Optimized AMI if no value is provided.
* More information about the AWS eks optimized ami is available at https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html.
* Use the information provided by AWS if you want to build your own AMI.
*/
nodeAmiId?: pulumi.Input<string>;
/**
* Public key material for SSH access to worker nodes. See allowed formats at:
* https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
* If not provided, no SSH access is enabled on VMs.
*/
nodePublicKey?: pulumi.Input<string>;
/**
* The subnets to use for worker nodes. Defaults to the value of subnetIds.
*/
nodeSubnetIds?: pulumi.Input<pulumi.Input<string>[]>;
/**
* The security group to use for the cluster API endpoint. If not provided, a new security group will be created
* with full internet egress and ingress from node groups.
*/
clusterSecurityGroup?: aws.ec2.SecurityGroup;
/**
* The tags to apply to the cluster security group.
*/
clusterSecurityGroupTags?: InputTags;
/**
* The tags to apply to the default `nodeSecurityGroup` created by the cluster.
*
* Note: The `nodeSecurityGroupTags` option and the node group option
* `nodeSecurityGroup` are mutually exclusive.
*/
nodeSecurityGroupTags?: InputTags;
/**
* The size in GiB of a cluster node's root volume. Defaults to 20.
*/
nodeRootVolumeSize?: pulumi.Input<number>;
/**
* Extra code to run on node startup. This code will run after the AWS EKS bootstrapping code and before the node
* signals its readiness to the managing CloudFormation stack. This code must be a typical user data script:
* critically it must begin with an interpreter directive (i.e. a `#!`).
*/
nodeUserData?: pulumi.Input<string>;
/**
* The number of worker nodes that should be running in the cluster. Defaults to 2.
*/
desiredCapacity?: pulumi.Input<number>;
/**
* The minimum number of worker nodes running in the cluster. Defaults to 1.
*/
minSize?: pulumi.Input<number>;
/**
* The maximum number of worker nodes running in the cluster. Defaults to 2.
*/
maxSize?: pulumi.Input<number>;
/**
* An optional set of StorageClasses to enable for the cluster. If this is a single volume type rather than a map,
* a single StorageClass will be created for that volume type.
*
* Note: As of Kubernetes v1.11+ on EKS, a default `gp2` storage class will
* always be created automatically for the cluster by the EKS service. See
* https://docs.aws.amazon.com/eks/latest/userguide/storage-classes.html
*/
storageClasses?: { [name: string]: StorageClass } | EBSVolumeType;
/**
* If this toggle is set to true, the EKS cluster will be created without node group attached.
*/
skipDefaultNodeGroup?: boolean;
/**
* @deprecated This option has been deprecated due to a lack of
* support for it on EKS, and the general community recommendation to avoid
* using it for security concerns. If you'd like alternatives to deploy the
* dashboard, consider writing it in Pulumi, or using the Helm chart.
*
* Whether or not to deploy the Kubernetes dashboard to the cluster. If the dashboard is deployed, it can be
* accessed as follows:
*
* 1. Retrieve an authentication token for the dashboard by running the following and copying the value of `token`
* from the output of the last command:
*
* $ kubectl -n kube-system get secret | grep eks-admin | awk '{print $1}'
* $ kubectl -n kube-system describe secret <output from previous command>
*
* 2. Start the kubectl proxy:
*
* $ kubectl proxy
*
* 3. Open `http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/` in a
* web browser.
* 4. Choose `Token` authentication, paste the token retrieved earlier into the `Token` field, and sign in.
*
* Defaults to `true`.
*/
deployDashboard?: boolean;
/**
* Key-value mapping of tags that are automatically applied to all AWS
* resources directly under management with this cluster, which support tagging.
*/
tags?: InputTags;
/**
* Desired Kubernetes master / control plane version. If you do not specify a value, the latest available version is used.
*/
version?: pulumi.Input<string>;
/**
* Enable EKS control plane logging. This sends logs to cloudwatch.
* Possible list of values are: ["api", "audit", "authenticator", "controllerManager", "scheduler"].
* By default it is off.
*/
enabledClusterLogTypes?: pulumi.Input<pulumi.Input<string>[]>;
/**
* Indicates whether or not the Amazon EKS public API server endpoint is enabled. Default is `true`.
*/
endpointPublicAccess?: boolean;
/**
* Indicates whether or not the Amazon EKS private API server endpoint is enabled. The default is `false`.
*/
endpointPrivateAccess?: boolean;
}
/**
* ClusterNodeGroupOptions describes the configuration options accepted by a cluster
* to create its own node groups. It's a subset of NodeGroupOptions.
*/
export interface ClusterNodeGroupOptions extends NodeGroupBaseOptions { }
/**
* Cluster is a component that wraps the AWS and Kubernetes resources necessary to run an EKS cluster, its worker
* nodes, its optional StorageClasses, and an optional deployment of the Kubernetes Dashboard.
*/
export class Cluster extends pulumi.ComponentResource {
/**
* A kubeconfig that can be used to connect to the EKS cluster. This must be serialized as a string before passing
* to the Kubernetes provider.
*/
public readonly kubeconfig: pulumi.Output<any>;
/**
* A Kubernetes resource provider that can be used to deploy into this cluster. For example, the code below will
* create a new Pod in the EKS cluster.
*
* let eks = new Cluster("eks");
* let pod = new kubernetes.core.v1.Pod("pod", { ... }, { provider: eks.provider });
*
*/
public readonly provider: k8s.Provider;
/**
* The security group for the EKS cluster.
*/
public readonly clusterSecurityGroup: aws.ec2.SecurityGroup;
/**
* The service roles used by the EKS cluster.
*/
public readonly instanceRoles: pulumi.Output<aws.iam.Role[]>;
/**
* The security group for the cluster's nodes.
*/
public readonly nodeSecurityGroup: aws.ec2.SecurityGroup;
/**
* The ingress rule that gives node group access to cluster API server
*/
public readonly eksClusterIngressRule: aws.ec2.SecurityGroupRule;
/**
* The default Node Group configuration, or undefined if `skipDefaultNodeGroup` was specified.
*/
public readonly defaultNodeGroup: NodeGroupData | undefined;
/**
* The EKS cluster.
*/
public readonly eksCluster: aws.eks.Cluster;
/**
* The EKS cluster and it's dependencies.
*/
public readonly core: CoreData;
/**
* Create a new EKS cluster with worker nodes, optional storage classes, and deploy the Kubernetes Dashboard if
* requested.
*
* @param name The _unique_ name of this component.
* @param args The arguments for this cluster.
* @param opts A bag of options that control this component's behavior.
*/
constructor(name: string, args?: ClusterOptions, opts?: pulumi.ComponentResourceOptions) {
super("eks:index:Cluster", name, args, opts);
args = args || {};
// Create the core resources required by the cluster.
const core = createCore(name, args, this);
this.core = core;
this.clusterSecurityGroup = core.clusterSecurityGroup;
this.eksCluster = core.cluster;
this.instanceRoles = core.instanceRoles;
// Create default node group security group and cluster ingress rule.
[this.nodeSecurityGroup, this.eksClusterIngressRule] = createNodeGroupSecurityGroup(name, {
vpcId: core.vpcId,
clusterSecurityGroup: core.clusterSecurityGroup,
eksCluster: core.cluster,
tags: pulumi.all([
args.tags,
args.nodeSecurityGroupTags,
]).apply(([tags, nodeSecurityGroupTags]) => (<aws.Tags>{
...nodeSecurityGroupTags,
...tags,
})),
}, this);
core.nodeGroupOptions.nodeSecurityGroup = this.nodeSecurityGroup;
core.nodeGroupOptions.clusterIngressRule = this.eksClusterIngressRule;
// Create the default worker node group and grant the workers access to the API server.
const configDeps = [core.kubeconfig];
if (!args.skipDefaultNodeGroup) {
this.defaultNodeGroup = createNodeGroup(name, {
cluster: core,
...core.nodeGroupOptions,
}, this);
configDeps.push(this.defaultNodeGroup.cfnStack.id);
}
// Export the cluster's kubeconfig with a dependency upon the cluster's autoscaling group. This will help
// ensure that the cluster's consumers do not attempt to use the cluster until its workers are attached.
this.kubeconfig = pulumi.all(configDeps).apply(([kubeconfig]) => kubeconfig);
// Export a k8s provider with the above kubeconfig. Note that we do not export the provider we created earlier
// in order to help ensure that worker nodes are available before the provider can be used.
this.provider = new k8s.Provider(`${name}-provider`, {
kubeconfig: this.kubeconfig.apply(JSON.stringify),
}, { parent: this });
// If we need to deploy the Kubernetes dashboard, do so now.
if (args.deployDashboard === undefined || args.deployDashboard) {
pulumi.log.warn("Option `deployDashboard` has been deprecated. Please consider using the Helm chart, or writing the dashboard directly in Pulumi.", this.eksCluster);
createDashboard(name, {}, this, this.provider);
}
this.registerOutputs({ kubeconfig: this.kubeconfig });
}
createNodeGroup(name: string, args: ClusterNodeGroupOptions): NodeGroup {
return new NodeGroup(name, {
...args,
cluster: this.core,
nodeSecurityGroup: this.core.nodeGroupOptions.nodeSecurityGroup,
clusterIngressRule: this.core.nodeGroupOptions.clusterIngressRule,
}, {
parent: this,
providers: { kubernetes: this.provider },
});
}
}