-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathcrd.ts
184 lines (151 loc) · 4.99 KB
/
crd.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
import { CodeMaker } from 'codemaker';
import { TypeGenerator } from 'json2jsii';
import * as yaml from 'yaml';
import { ImportSpec } from '../config';
import { download } from '../util';
import { GenerateOptions, ImportBase } from './base';
import { emitHeader, generateConstruct } from './codegen';
import { GroupVersionKind } from './k8s';
const CRD_KIND = 'CustomResourceDefinition';
export interface ManifestObjectDefinition {
apiVersion?: string;
kind?: string;
items?: ManifestObjectDefinition[]; // if `kind` is "List"
metadata?: {
name?: string;
};
spec?: {
group: string;
names: {
kind: string;
[key: string]: any;
};
versions?: Array<{
name: string;
schema?: { openAPIV3Schema?: any };
[key: string]: any;
}>;
version?: string;
validation?: { openAPIV3Schema?: any };
[key: string]: any;
};
}
// all these APIs are compatible from our perspective.
const SUPPORTED_API_VERSIONS = [
'apiextensions.k8s.io/v1beta1',
'apiextensions.k8s.io/v1',
];
export class CustomResourceDefinition {
private readonly schema?: any;
private readonly group: string;
private readonly version: string;
private readonly kind: string;
private readonly fqn: string;
constructor(manifest: ManifestObjectDefinition) {
const apiVersion = manifest?.apiVersion ?? 'undefined';
assert(SUPPORTED_API_VERSIONS.includes(apiVersion), `"apiVersion" is "${apiVersion}" but it should be one of: ${SUPPORTED_API_VERSIONS.map(x => `"${x}"`).join(', ')}`);
assert(manifest.kind === CRD_KIND, `"kind" must be "${CRD_KIND}"`);
const spec = manifest.spec;
if (!spec) {
throw new Error('manifest does not have a "spec" attribute');
}
const version = spec.version ?? (spec.versions ?? [])[0];
if (!version) {
throw new Error('unable to determine CRD version');
}
const schema = typeof version === 'string'
? spec.validation?.openAPIV3Schema
: version?.schema?.openAPIV3Schema ?? spec.validation?.openAPIV3Schema;
this.schema = schema;
this.group = spec.group;
this.version = typeof version === 'string' ? version : version.name;
this.kind = spec.names.kind;
this.fqn = this.kind;
}
public get key() {
return `${this.group}/${this.kind.toLocaleLowerCase()}`;
}
public get gvk(): GroupVersionKind {
return {
group: this.group,
version: this.version,
kind: this.kind,
};
}
public async generateTypeScript(code: CodeMaker, options: GenerateOptions) {
const types = new TypeGenerator();
generateConstruct(types, {
group: this.group,
version: this.version,
kind: this.kind,
fqn: this.fqn,
schema: this.schema,
custom: true,
prefix: options.classNamePrefix,
});
code.line(types.render());
}
}
export class ImportCustomResourceDefinition extends ImportBase {
public static async match(importSpec: ImportSpec): Promise<undefined | ManifestObjectDefinition[]> {
const { source } = importSpec;
const manifest = await download(source);
return yaml.parseAllDocuments(manifest).map((doc: yaml.Document) => doc.toJSON());
}
private readonly groups: Record<string, CustomResourceDefinition[]> = { };
constructor(manifest: ManifestObjectDefinition[]) {
super();
const crds: Record<string, CustomResourceDefinition> = { };
const groups: Record<string, CustomResourceDefinition[]> = { };
const extractCRDs = (objects: ManifestObjectDefinition[] = []) => {
for (const obj of objects) {
// filter empty docs in the manifest
if (!obj) {
continue;
}
// found a crd, yey!
if (obj.kind === CRD_KIND) {
const crd = new CustomResourceDefinition(obj);
const key = crd.key;
if (key in crds) {
throw new Error(`${key} already exists`);
}
crds[key] = crd;
continue;
}
// recurse into lists
if (obj.kind === 'List') {
extractCRDs(obj.items);
continue;
}
}
};
extractCRDs(manifest);
//sort to ensure consistent ordering for snapshot compare
const sortedCrds = Object.values(crds).sort((a: CustomResourceDefinition, b: CustomResourceDefinition) => a.key.localeCompare(b.key));
for (const crd of sortedCrds) {
const g = crd.gvk.group;
if ( !(g in groups) ) {
groups[g] = new Array<CustomResourceDefinition>();
}
groups[g].push(crd);
}
this.groups = groups;
}
public get moduleNames() {
return Object.keys(this.groups);
}
protected async generateTypeScript(code: CodeMaker, moduleName: string, options: GenerateOptions) {
const crds = this.groups[moduleName];
emitHeader(code);
for (const crd of crds) {
console.log(` ${crd.key}`);
await crd.generateTypeScript(code, options);
}
}
}
function assert(condition: boolean, message: string) {
if (!condition) {
throw new Error(`invalid CustomResourceDefinition manifest: ${message}`);
}
}