-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
315 lines (285 loc) · 9.31 KB
/
index.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
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";
import * as k8s from "@pulumi/kubernetes";
// Set a variable name to be used for all resources
const my_name = `eks-ng-issue`;
// **************************************************************
// ******* SETUP ENV **********
// **************************************************************
// Create VPC.
const myvpc = new awsx.ec2.Vpc(`${my_name}-vpc`, {
cidrBlock: "10.0.0.0/22",
numberOfAvailabilityZones: 3,
natGateways: { strategy: "Single" },
tags: { Name: `${my_name}-vpc` },
enableDnsHostnames: true,
enableDnsSupport: true,
});
export const myvpc_id = myvpc.vpcId;
export const myvpc_public_subnets = myvpc.publicSubnetIds;
export const myvpc_private_subnets = myvpc.privateSubnetIds;
// Create a security group for the eks cluster.
const eksclustersecuritygroup = new aws.ec2.SecurityGroup(
`${my_name}-eks-cluster-sg`,
{
vpcId: myvpc.vpcId,
revokeRulesOnDelete: true,
description: "EKS created security group created by code.",
egress: [
{
description: "Allow outbound internet access",
protocol: "-1",
fromPort: 0,
toPort: 0,
cidrBlocks: ["0.0.0.0/0"],
},
],
ingress: [
{
description: "Ingress to self cluster. ",
protocol: "-1",
fromPort: 0,
toPort: 0,
self: true, // This allows us to call the securitygroup itself as a source
},
],
tags: { Name: `${my_name}-eks-cluster-sg` },
},
{ dependsOn: myvpc }
);
export const eksnodegroupsecuritygroup_name = eksclustersecuritygroup.id;
export const securitygroup_eksnode_id = eksclustersecuritygroup.id;
export const securitygroup_eksnode_name = eksclustersecuritygroup.name;
export const securitygroup_eksnode_vpcid = eksclustersecuritygroup.vpcId;
export const securitygroup_eksnode_tags = eksclustersecuritygroup.tags;
// Create a role for the eks cluster.
const eksRole = new aws.iam.Role(`${my_name}-eksRole`, {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: { Service: "eks.amazonaws.com" },
},
],
}),
});
// Add the managed policy amazon eks cluster policy to the eks role.
const eksPolicyAttachment = new aws.iam.RolePolicyAttachment(
`${my_name}-eksPolicyAttachment`,
{
policyArn: aws.iam.ManagedPolicy.AmazonEKSClusterPolicy,
role: eksRole.name,
}
);
// Add the managed policy amazon eks vpc resource controller policy to the eks role.
const AmazonEKSVPCResourceControllerPolicyAttachment =
new aws.iam.RolePolicyAttachment("example-AmazonEKSVPCResourceController", {
policyArn: "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController",
role: eksRole.name,
});
// Create an IAM role for the node group.
const nodeRole = new aws.iam.Role(`${my_name}-nodeRole`, {
assumeRolePolicy: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Action: "sts:AssumeRole",
Effect: "Allow",
Principal: { Service: "ec2.amazonaws.com" },
},
],
}),
});
// Add the managed policy amazon eks worker node policy to the node role.
const example_AmazonEKSWorkerNodePolicy = new aws.iam.RolePolicyAttachment(
"example-AmazonEKSWorkerNodePolicy",
{
policyArn: "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy",
role: nodeRole.name,
}
);
// Add the managed policy amazon eks cni policy to the node role.
const example_AmazonEKSCNIPolicy = new aws.iam.RolePolicyAttachment(
"example-AmazonEKSCNIPolicy",
{
policyArn: "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
role: nodeRole.name,
}
);
// Add the managed policy amazon ec2 container registry read only to the node role.
const example_AmazonEC2ContainerRegistryReadOnly =
new aws.iam.RolePolicyAttachment(
"example-AmazonEC2ContainerRegistryReadOnly",
{
policyArn: "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
role: nodeRole.name,
}
);
// ########## CREATE EKS CLUSTER ##########
const mycluster = new aws.eks.Cluster(`${my_name}-eks`, {
roleArn: eksRole.arn,
version: "1.25",
enabledClusterLogTypes: [
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler",
],
vpcConfig: {
securityGroupIds: [eksclustersecuritygroup.id],
subnetIds: myvpc.publicSubnetIds,
},
});
// Generate a kubeconfig for the EKS cluster.
const mykubeconfig = pulumi
.all([
mycluster.endpoint,
mycluster.certificateAuthority.data,
mycluster.name,
])
.apply(([endpoint, certData, clusterName]) => {
return {
apiVersion: "v1",
clusters: [
{
cluster: {
server: endpoint,
"certificate-authority-data": certData,
},
name: clusterName,
},
],
contexts: [
{
context: { cluster: clusterName, user: "aws" },
name: "aws",
},
],
"current-context": "aws",
kind: "Config",
users: [
{
name: "aws",
user: {
exec: {
apiVersion: "client.authentication.k8s.io/v1beta1",
command: "aws",
args: ["eks", "get-token", "--cluster-name", clusterName],
},
},
},
],
};
});
export const kubeconfig = pulumi.secret(mykubeconfig);
// ##############################################################
// ########## On-Cluster Setup ##################
// ##############################################################
// Create the k8s provider
const k8sProvider = new k8s.Provider(`${my_name}-k8sprovider`, {
kubeconfig: kubeconfig.apply(JSON.stringify),
});
// create the namespace for poddisruption budget
const mynamespace = new k8s.core.v1.Namespace(
`${my_name}-namespace`,
{},
{ provider: k8sProvider, dependsOn: [mycluster] } // Use this for ~/.kube/config
);
// export the namespace name
export const mynamespace_name = mynamespace.metadata.name;
// Create a deployment for the poddisruption budget to target.
const myapp = new k8s.apps.v1.Deployment(
`${my_name}-app`,
{
metadata: { namespace: mynamespace.metadata.name },
spec: {
replicas: 1,
selector: {
matchLabels: {
app: "myapp",
},
},
template: {
metadata: {
labels: {
app: "myapp",
},
},
spec: {
containers: [
{
name: "myapp",
image: "alpine:latest",
command: [ "/bin/sh", "-c", "--" ],
args: [ "while true; do sleep 30; done;" ],
ports: [{ containerPort: 80 }],
},
],
},
},
},
},
{ provider: k8sProvider, dependsOn: [mynamespace, mycluster] }
);
// ##############################################################
// ########## Repro Steps #################
// ##############################################################
// Create the poddisruption budget. This will prevent the node group from updating if the poddisruption budget is not met.
// Delete this resource after the first run when you are trying to re-create the issue.
// const pdb = new k8s.policy.v1.PodDisruptionBudget(
// `${my_name}-pdb`,
// {
// metadata: { namespace: mynamespace.metadata.name },
// spec: {
// // minAvailable: "100%",
// maxUnavailable: "0%",
// selector: {
// matchLabels: {
// app: "myapp",
// },
// },
// },
// },
// { dependsOn: [mynamespace, mycluster], provider: k8sProvider }
// );
// // export the poddisruption budget name
// export const pdb_name = pdb.metadata.name;
// Create launch template for the eks node group.
// This is where a lot of the magic happens. The launch template is what is used to create the instances in the node group.
const mylaunchTemplate = new aws.ec2.LaunchTemplate(
`${my_name}-launchtemplate`,
{
tags: { Name: `${my_name}-launchtemplate` },
instanceType: "t3a.small", // Toggle this instance type with the one below so that the launch template changes versions.
// instanceType: "t3a.nano", // Toggle this instance type with the one above so that the launch template changes versions.
description:
"This is the example launch template for the EKS cluster managed node group by User A",
updateDefaultVersion: true,
vpcSecurityGroupIds: [eksclustersecuritygroup.id],
}
);
// export the launch template id and version
export const mylaunchTemplate_id = mylaunchTemplate.id;
export const mylaunchTemplate_version = mylaunchTemplate.latestVersion;
// create the eks node group with the launch template
const eksnodegroup = new aws.eks.NodeGroup(`${my_name}-eksNodeGroup`, {
clusterName: mycluster.name,
//subnetIds: myvpc.publicSubnetIds, // Provide a list of subnet IDs associate with the node group
subnetIds: myvpc.privateSubnetIds, // Provide a list of subnet IDs associate with the node group
nodeRoleArn: nodeRole.arn,
launchTemplate: {
id: mylaunchTemplate.id,
version: pulumi.interpolate`${mylaunchTemplate.latestVersion}`,
},
scalingConfig: {
desiredSize: 2,
maxSize: 8,
minSize: 2,
},
});
// export the nodegroup name
export const eksnodegroup_name = eksnodegroup.nodeGroupName;