-
Notifications
You must be signed in to change notification settings - Fork 4k
/
cluster.ts
295 lines (242 loc) · 10.8 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
// tslint:disable:no-console
import { IsCompleteResponse, OnEventResponse } from '@aws-cdk/custom-resources/lib/provider-framework/types';
// eslint-disable-next-line import/no-extraneous-dependencies
import * as aws from 'aws-sdk';
import { EksClient, ResourceEvent, ResourceHandler } from './common';
const MAX_CLUSTER_NAME_LEN = 100;
export class ClusterResourceHandler extends ResourceHandler {
public get clusterName() {
if (!this.physicalResourceId) {
throw new Error('Cannot determine cluster name without physical resource ID');
}
return this.physicalResourceId;
}
private readonly newProps: aws.EKS.CreateClusterRequest;
private readonly oldProps: Partial<aws.EKS.CreateClusterRequest>;
constructor(eks: EksClient, event: ResourceEvent) {
super(eks, event);
this.newProps = parseProps(this.event.ResourceProperties);
this.oldProps = event.RequestType === 'Update' ? parseProps(event.OldResourceProperties) : { };
}
// ------
// CREATE
// ------
protected async onCreate(): Promise<OnEventResponse> {
console.log('onCreate: creating cluster with options:', JSON.stringify(this.newProps, undefined, 2));
if (!this.newProps.roleArn) {
throw new Error('"roleArn" is required');
}
const clusterName = this.newProps.name || this.generateClusterName();
const resp = await this.eks.createCluster({
...this.newProps,
name: clusterName,
});
if (!resp.cluster) {
throw new Error(`Error when trying to create cluster ${clusterName}: CreateCluster returned without cluster information`);
}
return {
PhysicalResourceId: resp.cluster.name,
};
}
protected async isCreateComplete() {
return this.isActive();
}
// ------
// DELETE
// ------
protected async onDelete(): Promise<OnEventResponse> {
console.log(`onDelete: deleting cluster ${this.clusterName}`);
try {
await this.eks.deleteCluster({ name: this.clusterName });
} catch (e) {
if (e.code !== 'ResourceNotFoundException') {
throw e;
} else {
console.log(`cluster ${this.clusterName} not found, idempotently succeeded`);
}
}
return {
PhysicalResourceId: this.clusterName,
};
}
protected async isDeleteComplete(): Promise<IsCompleteResponse> {
console.log(`isDeleteComplete: waiting for cluster ${this.clusterName} to be deleted`);
try {
const resp = await this.eks.describeCluster({ name: this.clusterName });
console.log('describeCluster returned:', JSON.stringify(resp, undefined, 2));
} catch (e) {
if (e.code === 'ResourceNotFoundException') {
console.log('received ResourceNotFoundException, this means the cluster has been deleted (or never existed)');
return { IsComplete: true };
}
console.log('describeCluster error:', e);
throw e;
}
return {
IsComplete: false,
};
}
// ------
// UPDATE
// ------
protected async onUpdate() {
const updates = analyzeUpdate(this.oldProps, this.newProps);
console.log('onUpdate:', JSON.stringify({ updates }, undefined, 2));
// if there is an update that requires replacement, go ahead and just create
// a new cluster with the new config. The old cluster will automatically be
// deleted by cloudformation upon success.
if (updates.replaceName || updates.replaceRole || updates.replaceVpc) {
// if we are replacing this cluster and the cluster has an explicit
// physical name, the creation of the new cluster will fail with "there is
// already a cluster with that name". this is a common behavior for
// CloudFormation resources that support specifying a physical name.
if (this.oldProps.name === this.newProps.name && this.oldProps.name) {
throw new Error(`Cannot replace cluster "${this.oldProps.name}" since it has an explicit physical name. Either rename the cluster or remove the "name" configuration`);
}
return await this.onCreate();
}
// if a version update is required, issue the version update
if (updates.updateVersion) {
if (!this.newProps.version) {
throw new Error(`Cannot remove cluster version configuration. Current version is ${this.oldProps.version}`);
}
return await this.updateClusterVersion(this.newProps.version);
}
if (updates.updateLogging || updates.updateAccess) {
const config: aws.EKS.UpdateClusterConfigRequest = {
name: this.clusterName,
logging: this.newProps.logging,
};
if (updates.updateAccess) {
// Updating the cluster with securityGroupIds and subnetIds (as specified in the warning here:
// https://awscli.amazonaws.com/v2/documentation/api/latest/reference/eks/update-cluster-config.html)
// will fail, therefore we take only the access fields explicitly
config.resourcesVpcConfig = {
endpointPrivateAccess: this.newProps.resourcesVpcConfig.endpointPrivateAccess,
endpointPublicAccess: this.newProps.resourcesVpcConfig.endpointPublicAccess,
publicAccessCidrs: this.newProps.resourcesVpcConfig.publicAccessCidrs,
};
}
const updateResponse = await this.eks.updateClusterConfig(config);
return { EksUpdateId: updateResponse.update?.id };
}
// no updates
return;
}
protected async isUpdateComplete() {
console.log('isUpdateComplete');
// if this is an EKS update, we will monitor the update event itself
if (this.event.EksUpdateId) {
const complete = await this.isEksUpdateComplete(this.event.EksUpdateId);
if (!complete) {
return { IsComplete: false };
}
// fall through: if the update is done, we simply delegate to isActive()
// in order to extract attributes and state from the cluster itself, which
// is supposed to be in an ACTIVE state after the update is complete.
}
return this.isActive();
}
private async updateClusterVersion(newVersion: string) {
console.log(`updating cluster version to ${newVersion}`);
// update-cluster-version will fail if we try to update to the same version,
// so skip in this case.
const cluster = (await this.eks.describeCluster({ name: this.clusterName })).cluster;
if (cluster?.version === newVersion) {
console.log(`cluster already at version ${cluster.version}, skipping version update`);
return;
}
const updateResponse = await this.eks.updateClusterVersion({ name: this.clusterName, version: newVersion });
return { EksUpdateId: updateResponse.update?.id };
}
private async isActive(): Promise<IsCompleteResponse> {
console.log('waiting for cluster to become ACTIVE');
const resp = await this.eks.describeCluster({ name: this.clusterName });
console.log('describeCluster result:', JSON.stringify(resp, undefined, 2));
const cluster = resp.cluster;
// if cluster is undefined (shouldnt happen) or status is not ACTIVE, we are
// not complete. note that the custom resource provider framework forbids
// returning attributes (Data) if isComplete is false.
if (cluster?.status !== 'ACTIVE') {
return {
IsComplete: false,
};
} else {
return {
IsComplete: true,
Data: {
Name: cluster.name,
Endpoint: cluster.endpoint,
Arn: cluster.arn,
// IMPORTANT: CFN expects that attributes will *always* have values,
// so return an empty string in case the value is not defined.
// Otherwise, CFN will throw with `Vendor response doesn't contain
// XXXX key`.
CertificateAuthorityData: cluster.certificateAuthority?.data ?? '',
ClusterSecurityGroupId: cluster.resourcesVpcConfig?.clusterSecurityGroupId ?? '',
OpenIdConnectIssuerUrl: cluster.identity?.oidc?.issuer ?? '',
OpenIdConnectIssuer: cluster.identity?.oidc?.issuer?.substring(8) ?? '', // Strips off https:// from the issuer url
// We can safely return the first item from encryption configuration array, because it has a limit of 1 item
// https://docs.aws.amazon.com/eks/latest/APIReference/API_CreateCluster.html#AmazonEKS-CreateCluster-request-encryptionConfig
EncryptionConfigKeyArn: cluster.encryptionConfig?.shift()?.provider?.keyArn ?? '',
},
};
}
}
private async isEksUpdateComplete(eksUpdateId: string) {
this.log({ isEksUpdateComplete: eksUpdateId });
const describeUpdateResponse = await this.eks.describeUpdate({
name: this.clusterName,
updateId: eksUpdateId,
});
this.log({ describeUpdateResponse });
if (!describeUpdateResponse.update) {
throw new Error(`unable to describe update with id "${eksUpdateId}"`);
}
switch (describeUpdateResponse.update.status) {
case 'InProgress':
return false;
case 'Successful':
return true;
case 'Failed':
case 'Cancelled':
throw new Error(`cluster update id "${eksUpdateId}" failed with errors: ${JSON.stringify(describeUpdateResponse.update.errors)}`);
default:
throw new Error(`unknown status "${describeUpdateResponse.update.status}" for update id "${eksUpdateId}"`);
}
}
private generateClusterName() {
const suffix = this.requestId.replace(/-/g, ''); // 32 chars
const prefix = this.logicalResourceId.substr(0, MAX_CLUSTER_NAME_LEN - suffix.length - 1);
return `${prefix}-${suffix}`;
}
}
function parseProps(props: any): aws.EKS.CreateClusterRequest {
return props?.Config ?? { };
}
interface UpdateMap {
replaceName: boolean; // name
replaceVpc: boolean; // resourcesVpcConfig.subnetIds and securityGroupIds
replaceRole: boolean; // roleArn
updateVersion: boolean; // version
updateLogging: boolean; // logging
updateAccess: boolean; // resourcesVpcConfig.endpointPrivateAccess and endpointPublicAccess
}
function analyzeUpdate(oldProps: Partial<aws.EKS.CreateClusterRequest>, newProps: aws.EKS.CreateClusterRequest): UpdateMap {
console.log('old props: ', JSON.stringify(oldProps));
console.log('new props: ', JSON.stringify(newProps));
const newVpcProps = newProps.resourcesVpcConfig || { };
const oldVpcProps = oldProps.resourcesVpcConfig || { };
return {
replaceName: newProps.name !== oldProps.name,
replaceVpc:
JSON.stringify(newVpcProps.subnetIds) !== JSON.stringify(oldVpcProps.subnetIds) ||
JSON.stringify(newVpcProps.securityGroupIds) !== JSON.stringify(oldVpcProps.securityGroupIds),
updateAccess:
newVpcProps.endpointPrivateAccess !== oldVpcProps.endpointPrivateAccess ||
newVpcProps.endpointPublicAccess !== oldVpcProps.endpointPublicAccess,
replaceRole: newProps.roleArn !== oldProps.roleArn,
updateVersion: newProps.version !== oldProps.version,
updateLogging: JSON.stringify(newProps.logging) !== JSON.stringify(oldProps.logging),
};
}