-
Notifications
You must be signed in to change notification settings - Fork 26
/
app.ts
425 lines (348 loc) · 12.8 KB
/
app.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
import * as fs from 'fs';
import * as path from 'path';
import { Construct, IConstruct, Node } from 'constructs';
import { ApiObject } from './api-object';
import { Chart } from './chart';
import { DependencyGraph } from './dependency';
import { Names } from './names';
import { IResolver, ImplicitTokenResolver, LazyResolver, NumberStringUnionResolver } from './resolve';
import { Yaml } from './yaml';
/** The method to divide YAML output into files */
export enum YamlOutputType {
/** All resources are output into a single YAML file */
FILE_PER_APP,
/** Resources are split into seperate files by chart */
FILE_PER_CHART,
/** Each resource is output to its own file */
FILE_PER_RESOURCE,
/** Each chart in its own folder and each resource in its own file */
FOLDER_PER_CHART_FILE_PER_RESOURCE,
}
export interface AppProps {
/**
* The directory to output Kubernetes manifests.
*
* If you synthesize your application using `cdk8s synth`, you must
* also pass this value to the CLI using the `--output` option or
* the `output` property in the `cdk8s.yaml` configuration file.
* Otherwise, the CLI will not know about the output directory,
* and synthesis will fail.
*
* This property is intended for internal and testing use.
*
* @default - CDK8S_OUTDIR if defined, otherwise "dist"
*/
readonly outdir?: string;
/**
* The file extension to use for rendered YAML files
* @default .k8s.yaml
*/
readonly outputFileExtension?: string;
/**
* How to divide the YAML output into files
* @default YamlOutputType.FILE_PER_CHART
*/
readonly yamlOutputType?: YamlOutputType;
/**
* When set to true, the output directory will contain a `construct-metadata.json` file
* that holds construct related metadata on every resource in the app.
*
* @default false
*/
readonly recordConstructMetadata?: boolean;
/**
* A list of resolvers that can be used to replace property values before
* they are written to the manifest file. When multiple resolvers are passed,
* they are invoked by order in the list, and only the first one that applies
* (e.g calls `context.replaceValue`) is invoked.
*
* @see https://cdk8s.io/docs/latest/basics/app/#resolvers
*
* @default - no resolvers.
*/
readonly resolvers?: IResolver[];
}
class SynthRequestCache {
public nodeChildrenCache: Map<Node, IConstruct[]> = new Map<Node, IConstruct[]>();
public findAll(node: Node): IConstruct[] {
if (this.nodeChildrenCache.has(node)) {
return this.nodeChildrenCache.get(node)!;
}
const children = node.findAll();
this.nodeChildrenCache.set(node, children);
return children;
}
}
/**
* Represents a cdk8s application.
*/
export class App extends Construct {
/**
* Synthesize a single chart.
*
* Each element returned in the resulting array represents a different ApiObject
* in the scope of the chart.
*
* Note that the returned array order is important. It is determined by the various dependencies between
* the constructs in the chart, where the first element is the one without dependencies, and so on...
*
* @returns An array of JSON objects.
* @param chart the chart to synthesize.
* @internal
*/
public static _synthChart(chart: Chart): any[] {
const app: App = App.of(chart);
const cache = new SynthRequestCache();
// we must prepare the entire app before synthesizing the chart
// because the dependency inference happens on the app level.
resolveDependencies(app, cache);
// validate the app since we want to call onValidate of the relevant constructs.
// note this will also call onValidate on constructs from possibly different charts,
// but thats ok too since we no longer treat constructs as a self-contained synthesis unit.
validate(app, cache);
return chartToKube(chart).map(obj => obj.toJson());
}
public static of(c: IConstruct): App {
const scope = c.node.scope;
if (!scope) {
// the app is the only construct without a scope.
return c as App;
}
return App.of(scope);
}
/**
* The output directory into which manifests will be synthesized.
*/
public readonly outdir: string;
/**
* The file extension to use for rendered YAML files
* @default .k8s.yaml
*/
public readonly outputFileExtension: string;
/** How to divide the YAML output into files
* @default YamlOutputType.FILE_PER_CHART
*/
public readonly yamlOutputType: YamlOutputType;
/**
* Resolvers used by this app. This includes both custom resolvers
* passed by the `resolvers` property, as well as built-in resolvers.
*/
public readonly resolvers: IResolver[];
private readonly recordConstructMetadata: boolean;
/**
* Returns all the charts in this app, sorted topologically.
*/
public get charts(): Chart[] {
const isChart = (x: IConstruct): x is Chart => x instanceof Chart;
return new DependencyGraph(this.node)
.topology()
.filter(isChart);
}
/**
* Defines an app
* @param props configuration options
*/
constructor(props: AppProps = {}) {
super(undefined as any, '');
this.outdir = props.outdir ?? process.env.CDK8S_OUTDIR ?? 'dist';
this.outputFileExtension = props.outputFileExtension ?? '.k8s.yaml';
this.yamlOutputType = props.yamlOutputType ?? YamlOutputType.FILE_PER_CHART;
this.resolvers = [...(props.resolvers ?? []), new LazyResolver(), new ImplicitTokenResolver(), new NumberStringUnionResolver()];
this.recordConstructMetadata = props.recordConstructMetadata ?? (process.env.CDK8S_RECORD_CONSTRUCT_METADATA === 'true' ? true : false);
}
/**
* Synthesizes all manifests to the output directory
*/
public synth(): void {
fs.mkdirSync(this.outdir, { recursive: true });
const cache = new SynthRequestCache();
// Since we plan on removing the distributed synth mechanism, we no longer call `Node.synthesize`, but rather simply implement
// the necessary operations. We do however want to preserve the distributed validation.
validate(this, cache);
// this is kind of sucky, eventually I would like the DependencyGraph
// to be able to answer this question.
const hasDependantCharts = resolveDependencies(this, cache);
const charts = this.charts;
switch (this.yamlOutputType) {
case YamlOutputType.FILE_PER_APP:
let apiObjectsList: ApiObject[] = [];
for (const chart of charts) {
apiObjectsList.push(...Object.values(chart.toJson()));
}
if (charts.length > 0) {
Yaml.save(
path.join(this.outdir, `app${this.outputFileExtension}`), // There is no "app name", so we just hardcode the file name
apiObjectsList);
}
break;
case YamlOutputType.FILE_PER_CHART:
const namer: ChartNamer = hasDependantCharts ? new IndexedChartNamer() : new SimpleChartNamer();
for (const chart of charts) {
const chartName = namer.name(chart);
const objects = Object.values(chart.toJson());
Yaml.save(path.join(this.outdir, chartName + this.outputFileExtension), objects);
}
break;
case YamlOutputType.FILE_PER_RESOURCE:
for (const chart of charts) {
const apiObjects = Object.values(chart.toJson());
apiObjects.forEach((apiObject) => {
if (!(apiObject === undefined)) {
const fileName = `${`${apiObject.kind}.${apiObject.metadata.name}`
.replace(/[^0-9a-zA-Z-_.]/g, '')}`;
Yaml.save(path.join(this.outdir, fileName + this.outputFileExtension), [apiObject]);
}
});
}
break;
case YamlOutputType.FOLDER_PER_CHART_FILE_PER_RESOURCE:
const folderNamer: ChartNamer = hasDependantCharts ? new IndexedChartFolderNamer() : new SimpleChartFolderNamer();
for (const chart of charts) {
const chartName = folderNamer.name(chart);
const apiObjects = chartToKube(chart);
const fullOutDir = path.join(this.outdir, chartName);
fs.mkdirSync(fullOutDir, { recursive: true });
apiObjects.forEach((apiObject) => {
if (!(apiObject === undefined)) {
const fileName = `${`${apiObject.kind}.${apiObject.metadata.name}`
.replace(/[^0-9a-zA-Z-_.]/g, '')}`;
Yaml.save(path.join(fullOutDir, fileName + this.outputFileExtension), [apiObject.toJson()]);
}
});
}
break;
default:
break;
}
if (this.recordConstructMetadata) {
const allObjects = this.charts.flatMap(chartToKube);
this.writeConstructMetadata(allObjects);
}
}
/**
* Synthesizes the app into a YAML string.
*
* @returns A string with all YAML objects across all charts in this app.
*/
public synthYaml(): string {
const cache = new SynthRequestCache();
resolveDependencies(this, cache);
validate(this, cache);
const charts = this.charts;
const docs: any[] = [];
for (const chart of charts) {
docs.push(...Object.values(chart.toJson()));
}
return Yaml.stringify(...docs);
}
private writeConstructMetadata(apiObjects: ApiObject[]) {
const resources: { [key: string]: any } = {};
for (const apiObject of apiObjects) {
resources[apiObject.name] = { path: apiObject.node.path };
}
fs.writeFileSync(path.join(this.outdir, 'construct-metadata.json'), JSON.stringify({
version: '1.0.0',
resources: resources,
}));
}
}
function validate(app: App, cache: SynthRequestCache) {
const errors = [];
for (const child of cache.findAll(app.node)) {
const childErrors = child.node.validate();
for (const error of childErrors) {
errors.push(`[${child.node.path}] ${error}`);
}
}
if (errors.length > 0) {
throw new Error(`Validation failed with the following errors:\n ${errors.join('\n ')}`);
}
}
function buildDependencies(app: App, cache: SynthRequestCache) {
const deps = [];
for (const child of cache.findAll(app.node)) {
for (const dep of child.node.dependencies) {
deps.push({ source: child, target: dep });
}
}
return deps;
}
function resolveDependencies(app: App, cache: SynthRequestCache) {
let hasDependantCharts = false;
// create an explicit chart dependency from nested chart relationships
for (const parentChart of cache.findAll(app.node).filter(x => x instanceof Chart)) {
for (const childChart of parentChart.node.children.filter(x => x instanceof Chart)) {
parentChart.node.addDependency(childChart);
hasDependantCharts = true;
}
}
// create an explicit chart dependency from implicit construct dependencies
for (const dep of buildDependencies(app, cache)) {
const sourceChart = Chart.of(dep.source);
const targetChart = Chart.of(dep.target);
if (sourceChart !== targetChart) {
sourceChart.node.addDependency(targetChart);
hasDependantCharts = true;
}
}
// create explicit api object dependencies from implicit construct dependencies
for (const dep of buildDependencies(app, cache)) {
const sourceChart = Chart.of(dep.source);
const targetChart = Chart.of(dep.target);
const targetApiObjects = cache.findAll(dep.target.node).filter(c => c instanceof ApiObject).filter(x => Chart.of(x) === targetChart);
const sourceApiObjects = cache.findAll(dep.source.node).filter(c => c instanceof ApiObject).filter(x => Chart.of(x) === sourceChart);
for (const target of targetApiObjects) {
for (const source of sourceApiObjects) {
if (target !== source) {
source.node.addDependency(target);
}
}
}
}
return hasDependantCharts;
}
function chartToKube(chart: Chart) {
return new DependencyGraph(chart.node).topology()
.filter(x => x instanceof ApiObject)
.filter(x => Chart.of(x) === chart) // include an object only in its closest parent chart
.map(x => (x as ApiObject));
}
interface ChartNamer {
name(chart: Chart): string;
}
class SimpleChartNamer implements ChartNamer {
constructor() {
}
public name(chart: Chart) {
return `${Names.toDnsLabel(chart)}`;
}
}
class IndexedChartNamer extends SimpleChartNamer implements ChartNamer {
private index: number = 0;
constructor() {
super();
}
public name(chart: Chart) {
const name = `${this.index.toString().padStart(4, '0')}-${super.name(chart)}`;
this.index++;
return name;
}
}
class SimpleChartFolderNamer implements ChartNamer {
constructor() {
}
public name(chart: Chart) {
return Names.toDnsLabel(chart);
}
}
class IndexedChartFolderNamer extends SimpleChartFolderNamer implements ChartNamer {
private index: number = 0;
constructor() {
super();
}
public name(chart: Chart) {
const name = `${this.index.toString().padStart(4, '0')}-${super.name(chart)}`;
this.index++;
return name;
}
}